Sha256: 93a2185c4049f129c6333b9da545177cf8ec38c1b43a0bda65c8d1769287c3f9

Contents?: true

Size: 1.38 KB

Versions: 80

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "rubocop"
require "json"
require "parser/current"
require_relative "./../../../primer/view_components/linters/helpers/deprecated_components_helpers"

module RuboCop
  module Cop
    module Primer
      # This cop ensures that components marked as "deprecated" in `static/statuses.json` are discouraged from use.
      #
      # bad
      # Primer::BlankslateComponent.new(:foo)
      #
      # good
      # Primer::Beta::Blankslate.new(:foo)
      #
      # bad
      # Primer::Tooltip.new(:foo)
      #
      # good
      # Primer::Alpha::Tooltip.new(:foo)
      class DeprecatedComponents < BaseCop
        include ERBLint::Linters::Helpers::DeprecatedComponentsHelpers

        def on_send(node)
          return unless node.source.include?("Primer::")

          deprecated_components.each do |component|
            pattern = NodePattern.new("(send #{pattern(component)} :new ...)")
            add_offense(node, message: message(component)) if pattern.match(node)
          end
        end

        private

        # Converts a string to acceptable rubocop-ast pattern syntax
        def pattern(component)
          Parser::CurrentRuby.parse(component)
                             .to_s
                             .gsub("nil", "nil?")
                             .delete("\n")
                             .gsub("  ", " ")
        end
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 2 rubygems

Version Path
primer_view_components-0.0.102 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.101 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.100 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.99 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.98 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.97 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.96 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.95 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.94 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.93 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.92 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.91 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.90 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.89 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.88 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.87 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.86 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.85 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.84 lib/rubocop/cop/primer/deprecated_components.rb
primer_view_components-0.0.83 lib/rubocop/cop/primer/deprecated_components.rb