Sha256: 3c0a5fce337073397de8e598e7ce64b783e53139c310b2e7204a6b670154a0ed

Contents?: true

Size: 1.96 KB

Versions: 186

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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]]
      #   }
      #
      #   # good
      #
      #   # Neither is incompatible with the bad case, but probably the intended code.
      #   attributes = {
      #     valid_attributes: %i[name content],
      #     nested_attributes: [:name, :content, %i[incorrectly nested]]
      #   }
      #
      #   attributes = {
      #     valid_attributes: %i[name content],
      #     nested_attributes: [:name, :content, [:incorrectly, :nested]]
      #   }
      #
      class NestedPercentLiteral < Base
        include PercentLiteral

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

        # 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 { |percent_literal| /\A#{percent_literal}\W/ }.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

186 entries across 179 versions & 17 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.67.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.66.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.66.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.65.1 lib/rubocop/cop/lint/nested_percent_literal.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.65.0 lib/rubocop/cop/lint/nested_percent_literal.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/nested_percent_literal.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/nested_percent_literal.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/nested_percent_literal.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.64.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.63.4 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.63.3 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.63.2 lib/rubocop/cop/lint/nested_percent_literal.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.63.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.63.0 lib/rubocop/cop/lint/nested_percent_literal.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.62.1 lib/rubocop/cop/lint/nested_percent_literal.rb