summaryrefslogtreecommitdiff
path: root/rubocop/cop/style/regexp_literal_mixed_preserve.rb
blob: 4dc38d82f456cb24d1cc7b0d7c718ede8d026009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop is based on `Style/RegexpLiteral` but adds a new
      # `EnforcedStyle` option `mixed_preserve`.
      #
      # This cop will be removed once the upstream PR is merged and RuboCop upgraded.
      #
      # See https://github.com/rubocop/rubocop/pull/9688
      class RegexpLiteralMixedPreserve < RuboCop::Cop::Style::RegexpLiteral
        module Patch
          private

          def allowed_slash_literal?(node)
            super || allowed_mixed_preserve?(node)
          end

          def allowed_percent_r_literal?(node)
            super || allowed_mixed_preserve?(node)
          end

          def allowed_mixed_preserve?(node)
            style == :mixed_preserve && !contains_disallowed_slash?(node)
          end
        end

        prepend Patch
      end
    end
  end
end