Sha256: e47961bc8ff36b431ae4530407538a8875c65190ef974b0629aa2de75a9660a0

Contents?: true

Size: 1.07 KB

Versions: 203

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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

203 entries across 196 versions & 20 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.73.2 lib/rubocop/cop/style/redundant_capital_w.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.73.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.73.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.72.2 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.72.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.72.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.71.2 lib/rubocop/cop/style/redundant_capital_w.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.71.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.71.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.70.0 lib/rubocop/cop/style/redundant_capital_w.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.69.2 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.69.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.69.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.68.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.67.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.66.1 lib/rubocop/cop/style/redundant_capital_w.rb