Sha256: 1b6abe3c3b2ed15358304cf3b86585c94c6b612ec67ae4d8d1d9807be10ac91e

Contents?: true

Size: 1.11 KB

Versions: 35

Compression:

Stored size: 1.11 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 < Base
        include PercentLiteral
        extend AutoCorrector

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

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

        private

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

          add_offense(node) do |corrector|
            src = node.loc.begin.source
            corrector.replace(node.loc.begin, src.tr('W', 'w'))
          end
        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

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.12.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.12.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.11.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.10.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.9.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.9.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.8.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.8.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.7.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.6.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.6.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.5.2 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.5.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.5.0 lib/rubocop/cop/style/redundant_capital_w.rb