Sha256: 12b0ff128be093994e916dbe6d0756aca0144b01a5d1f6854088301d93bd3b42

Contents?: true

Size: 1.84 KB

Versions: 42

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for quotes and commas in %w, e.g. `%w('foo', "bar")`
      #
      # It is more likely that the additional characters are unintended (for
      # example, mistranslating an array of literals to percent string notation)
      # rather than meant to be part of the resulting strings.
      #
      # @example
      #
      #   # bad
      #
      #   %w('foo', "bar")
      #
      # @example
      #
      #   # good
      #
      #   %w(foo bar)
      class PercentStringArray < Base
        include PercentLiteral
        extend AutoCorrector

        QUOTES_AND_COMMAS = [/,$/, /^'.*'$/, /^".*"$/].freeze
        LEADING_QUOTE = /^['"]/.freeze
        TRAILING_QUOTE = /['"]?,?$/.freeze

        MSG = "Within `%w`/`%W`, quotes and ',' are unnecessary and may be " \
          'unwanted in the resulting strings.'

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

        def on_percent_literal(node)
          return unless contains_quotes_or_commas?(node)

          add_offense(node) do |corrector|
            node.each_value do |value|
              range = value.loc.expression

              match = range.source.match(TRAILING_QUOTE)
              corrector.remove_trailing(range, match[0].length) if match

              corrector.remove_leading(range, 1) if LEADING_QUOTE.match?(range.source)
            end
          end
        end

        private

        def contains_quotes_or_commas?(node)
          node.values.any? do |value|
            literal = value.children.first.to_s.scrub

            # To avoid likely false positives (e.g. a single ' or ")
            next if literal.gsub(/[^[[:alnum:]]]/, '').empty?

            QUOTES_AND_COMMAS.any? { |pat| literal.match?(pat) }
          end
        end
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 4 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.17.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.16.1 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.16.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.15.0 lib/rubocop/cop/lint/percent_string_array.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.14.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.13.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.12.1 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.12.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.11.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.10.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.9.1 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.9.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-1.8.1 lib/rubocop/cop/lint/percent_string_array.rb