Sha256: a86e4865190cb4bcc42cae441403ea1e599e9ebdeaace7e91cbec70b09ea0dc0

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

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

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-1.29.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.29.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.28.2 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.28.1 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.28.0 lib/rubocop/cop/style/redundant_capital_w.rb
rubocop-1.27.0 lib/rubocop/cop/style/redundant_capital_w.rb