Sha256: 8be393a331b653777962f7e0228e47ae5a7a1aa5acc16e335b8ccd2f36822dff

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for nested percent literals.
      #
      # @example
      #
      #   # bad
      #
      #   # The percent literal for nested_attributes is parsed as four tokens,
      #   # yielding the array [:name, :content, :"%i[incorrectly", :"nested]"].
      #   attributes = {
      #     valid_attributes: %i[name content],
      #     nested_attributes: %i[name content %i[incorrectly nested]]
      #   }
      class NestedPercentLiteral < Cop
        include PercentLiteral

        MSG = 'Within percent literals, nested percent literals do not ' \
          'function and may be unwanted in the result.'.freeze

        # The array of regular expressions representing percent literals that,
        # if found within a percent literal expression, will cause a
        # NestedPercentLiteral violation to be emitted.
        REGEXES = PercentLiteral::PERCENT_LITERAL_TYPES.map do |percent_literal|
          /\A#{percent_literal}\W/
        end.freeze

        def on_array(node)
          process(node, *PercentLiteral::PERCENT_LITERAL_TYPES)
        end

        def on_percent_literal(node)
          add_offense(node) if contains_percent_literals?(node)
        end

        private

        def contains_percent_literals?(node)
          node.each_child_node.any? do |child|
            literal = child.children.first.to_s.scrub
            REGEXES.any? { |regex| literal.match(regex) }
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rubocop-0.59.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.59.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.58.2 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.58.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.58.0 lib/rubocop/cop/lint/nested_percent_literal.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.57.2 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.57.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.57.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-0.56.0 lib/rubocop/cop/lint/nested_percent_literal.rb