Sha256: b03cbba44f9cec537bff2e24721b61cb32df8a586829fde1c534cd9d86c09208

Contents?: true

Size: 1.72 KB

Versions: 145

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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) { |corrector| autocorrect(corrector, node) }
        end

        private

        def autocorrect(corrector, node)
          node.children.each do |child|
            range = child.source_range

            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

145 entries across 144 versions & 15 rubygems

Version Path
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-1.59.0/lib/rubocop/cop/lint/percent_symbol_array.rb
rubocop-1.59.0 lib/rubocop/cop/lint/percent_symbol_array.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/percent_symbol_array.rb