Sha256: 7d188373f474c7d2304783af1b40a2f900e34f2e268dddb793c7bc610ba2960a

Contents?: true

Size: 1.69 KB

Versions: 54

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for colons and commas in %i, e.g. `%i(: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 symbols.
      #
      # @example
      #
      #   # bad
      #
      #   %i(:foo, :bar)
      #
      # @example
      #
      #   # good
      #
      #   %i(foo bar)
      class PercentSymbolArray < Cop
        include PercentLiteral

        MSG = "Within `%i`/`%I`, ':' and ',' are unnecessary and may be " \
          'unwanted in the resulting symbols.'

        def on_array(node)
          process(node, '%i', '%I')
        end

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

          add_offense(node)
        end

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

              corrector.remove_trailing(range, 1) if range.source.end_with?(',')
              corrector.remove_leading(range, 1) if
                range.source.start_with?(':')
            end
          end
        end

        private

        def contains_colons_or_commas?(node)
          node.children.any? do |child|
            literal = child.children.first.to_s

            next if non_alphanumeric_literal?(literal)

            literal.start_with?(':') || literal.end_with?(',')
          end
        end

        def non_alphanumeric_literal?(literal)
          literal !~ /[[:alnum:]]/
        end
      end
    end
  end
end

Version data entries

54 entries across 35 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/percent_symbol_array.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/percent_symbol_array.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.85.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.85.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.84.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.83.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.82.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.81.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.80.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.80.0 lib/rubocop/cop/lint/percent_symbol_array.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/percent_symbol_array.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.79.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-0.78.0 lib/rubocop/cop/lint/percent_symbol_array.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/lint/percent_symbol_array.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/lint/percent_symbol_array.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/lint/percent_symbol_array.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/lint/percent_symbol_array.rb