Sha256: 91a4091fb2b5de08cae4ae06c4633ff4756d583f7b203a3ae129f2bd7a2d5588

Contents?: true

Size: 1.97 KB

Versions: 31

Compression:

Stored size: 1.97 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]]
      #   }
      #
      #   # 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

31 entries across 31 versions & 5 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/nested_percent_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.29.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.29.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.28.2 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.28.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.28.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.27.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.26.1 lib/rubocop/cop/lint/nested_percent_literal.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.26.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.25.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.25.0 lib/rubocop/cop/lint/nested_percent_literal.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.24.1 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.24.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.23.0 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.22.3 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.22.2 lib/rubocop/cop/lint/nested_percent_literal.rb
rubocop-1.22.1 lib/rubocop/cop/lint/nested_percent_literal.rb