Sha256: 7e59a41121d63c225e762a7df6476ac9fe3f7b3cc5082ab0b02a852578683f44

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for usage of the %Q() syntax when %q() would do.
      class PercentQLiterals < Cop
        include PercentLiteral
        include ConfigurableEnforcedStyle

        def on_str(node)
          process(node, '%Q', '%q')
        end

        private

        def on_percent_literal(node)
          if style == :lower_case_q
            if type(node) == '%Q'
              check(node,
                    'Do not use `%Q` unless interpolation is needed.  ' \
                    'Use `%q`.')
            end
          elsif type(node) == '%q'
            check(node, 'Use `%Q` instead of `%q`.')
          end
        end

        def check(node, msg)
          # Report offense only if changing case doesn't change semantics,
          # i.e., if the string would become dynamic or has special characters.
          return if node.children != parse(corrected(node.source)).ast.children

          add_offense(node, :begin, msg)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.source_range, corrected(node.source))
          end
        end

        def corrected(src)
          src.sub(src[1], src[1].swapcase)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.46.0 lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.45.0 lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.44.1 lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.44.0 lib/rubocop/cop/style/percent_q_literals.rb