Sha256: bf07e97ef31b1d6c7d60fd31ecf7fc46ec7209b7d86138c51ac55e1753365a91

Contents?: true

Size: 1019 Bytes

Versions: 13

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for usage of the %W() syntax when %w() would do.
      class UnneededCapitalW < Cop
        include PercentLiteral

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

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

        private

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

          add_offense(node, :expression)
        end

        def requires_interpolation?(node)
          node.child_nodes.any? do |string|
            string.dstr_type? ||
              double_quotes_acceptable?(string.str_content) ||
              string.source == '\s'
          end
        end

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

Version data entries

13 entries across 13 versions & 2 rubygems

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