Sha256: d6cb60e0240a80b5e744eb4647fe3440a72cae5b7c7705c280e8cb1c6ae696b9

Contents?: true

Size: 1.55 KB

Versions: 6821

Compression:

Stored size: 1.55 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.
        PERCENT_LITERAL_TYPES = PreferredDelimiters::PERCENT_LITERAL_TYPES
        REGEXES = PERCENT_LITERAL_TYPES.map do |percent_literal|
          /\A#{percent_literal}\W/
        end.freeze

        def on_array(node)
          process(node, *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

6,821 entries across 6,815 versions & 25 rubygems

Version Path
ory-client-0.0.1.alpha147 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-kratos-client-0.9.0.alpha3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha146 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha145 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-kratos-client-0.9.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha144 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha143 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha142 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha141 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha140 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha139 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha138 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha134 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha133 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha132 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
primary_connect_proto-0.7.3 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
primary_connect_proto-0.7.2 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha131 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
ory-client-0.0.1.alpha130 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb
primary_connect_proto-0.7.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/lint/nested_percent_literal.rb