Sha256: b9f7f487b59a0d473084ad1fd28698c03b723b73bbc25d7ca22a91a46402ec74

Contents?: true

Size: 1.75 KB

Versions: 37

Compression:

Stored size: 1.75 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 < Base
        include PercentLiteral
        extend AutoCorrector

        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) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          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

        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)
          !/[[:alnum:]]/.match?(literal)
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.12.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.12.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.11.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.10.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.9.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.9.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.8.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.8.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.7.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.6.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.6.0 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.5.2 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.5.1 lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.5.0 lib/rubocop/cop/lint/percent_symbol_array.rb