Sha256: 5d86224ce652969d495d8c5ac9b31f622e0b9d4c1d42c390b9ef3b92ad7e0ef4

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8

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)
          src = node.loc.expression.source
          # 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 !=
            ProcessedSource.new(corrected(src)).ast.children

          add_offense(node, :begin, msg)
        end

        def autocorrect(node)
          src = node.loc.expression.source

          @corrections << lambda do |corrector|
            corrector.replace(node.loc.expression, corrected(src))
          end
        end

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

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/percent_q_literals.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.26.1 lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.26.0 lib/rubocop/cop/style/percent_q_literals.rb
rubocop-0.25.0 lib/rubocop/cop/style/percent_q_literals.rb