Sha256: eaec8cce5ebb480ffae01988f6c014cf74d38bb701d2a775368121b8d1215f8d

Contents?: true

Size: 1.1 KB

Versions: 32

Compression:

Stored size: 1.1 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

32 entries across 32 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_capital_w.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.26.1 lib/rubocop/cop/style/redundant_capital_w.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.26.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.25.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.25.0 lib/rubocop/cop/style/redundant_capital_w.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.24.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.24.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.23.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.22.3 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.22.2 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.22.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.22.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.21.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.20.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.19.1 lib/rubocop/cop/style/redundant_capital_w.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.19.0 lib/rubocop/cop/style/redundant_capital_w.rb