Sha256: 62aa051efa6711004ab656816b9d171fb6cd538e6231eae006da9c960272d0b6

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8
# 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.
      class PercentStringArray < Cop
        include PercentLiteral

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

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

        def on_percent_literal(node)
          if contains_quotes_or_commas?(node)
            add_offense(node, :expression, MSG)
          end
        end

        private

        def contains_quotes_or_commas?(node)
          patterns = [/,$/, /^'.*'$/, /^".*"$/]

          node.children.any? do |child|
            literal = child.children.first

            # To avoid likely false positives (e.g. a single ' or ")
            next if literal.to_s.gsub(/[^\p{Alnum}]/, '').empty?

            patterns.any? { |pat| literal =~ pat }
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            node.children.each do |child|
              range = child.loc.expression

              match = /['"]?,?$/.match(range.source)
              corrector.remove_trailing(range, match[0].length) if match

              corrector.remove_leading(range, 1) if /^['"]/ =~ range.source
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/percent_string_array.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/percent_string_array.rb
rubocop-0.42.0 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-0.41.2 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-0.41.1 lib/rubocop/cop/lint/percent_string_array.rb
rubocop-0.41.0 lib/rubocop/cop/lint/percent_string_array.rb