Sha256: 6db071e7fd4776d20c5185399943a7d8c684fdf479c75f39b06a6f0e38adaab8

Contents?: true

Size: 1.14 KB

Versions: 29

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for usage of the %W() syntax when %w() would do.
      #
      # @example
      #   # bad
      #   %W(cat dog pig)
      #   %W[door wall floor]
      #
      #   # good
      #   %w/swim run bike/
      #   %w[shirt pants shoes]
      #   %W(apple #{fruit} grape)
      class RedundantCapitalW < Cop
        include PercentLiteral

        MSG = 'Do not use `%W` unless interpolation is needed. ' \
              'If not, use `%w`.'

        def on_array(node)
          process(node, '%W')
        end

        def autocorrect(node)
          lambda do |corrector|
            src = node.loc.begin.source
            corrector.replace(node.loc.begin, src.tr('W', 'w'))
          end
        end

        private

        def on_percent_literal(node)
          return if requires_interpolation?(node)

          add_offense(node)
        end

        def requires_interpolation?(node)
          node.child_nodes.any? do |string|
            string.dstr_type? ||
              double_quotes_required?(string.source)
          end
        end
      end
    end
  end
end

Version data entries

29 entries across 27 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.89.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.89.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.88.0 lib/rubocop/cop/style/redundant_capital_w.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.87.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.87.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.86.0 lib/rubocop/cop/style/redundant_capital_w.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/redundant_capital_w.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/redundant_capital_w.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.85.1 lib/rubocop/cop/style/redundant_capital_w.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.85.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.84.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.83.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.82.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.81.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.80.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-0.80.0 lib/rubocop/cop/style/redundant_capital_w.rb