Sha256: d8a63a554b35d87d5fe9ba21554456c94bdd231ee606bf88619f1c8cead3e3e1

Contents?: true

Size: 1.38 KB

Versions: 120

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Migrations
      # Lint & autocorrect Truncate components
      class TruncateComponent < RuboCop::Cop::Cop
        INVALID_MESSAGE = <<~STR
          `Primer::Truncate` is deprecated. Please use `Primer::Beta::Truncate` instead!
        STR

        def_node_matcher :truncate_component, <<~PATTERN
          (send (const (const nil? :Primer) :Truncate) :new ...)
        PATTERN

        def_node_matcher :hash_with_inline_value?, <<~PATTERN
          (hash ... (pair (sym :inline) (...)) ... )
        PATTERN

        def_node_matcher :truncate_with_tag?, <<~PATTERN
          (hash ... (pair (sym :tag) (...)) ... )
        PATTERN

        def on_send(node)
          return unless truncate_component(node)

          add_offense(node, message: INVALID_MESSAGE)
        end

        def autocorrect(node)
          return if hash_with_inline_value?(node.arguments.first)

          lambda do |corrector|
            if node.arguments.first.nil? == false
              corrector.replace(node.children.first, "Primer::Beta::Truncate")
              corrector.insert_after(node.arguments.first, ", tag: :div") unless truncate_with_tag?(node.arguments.first)
            else
              corrector.replace(node.loc.expression, "Primer::Beta::Truncate.new(tag: :div)")
            end
          end
        end
      end
    end
  end
end

Version data entries

120 entries across 120 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.33.0 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.25.1 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.32.1 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.32.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.29.1 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.29.0 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.23.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.28.1 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.28.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.27.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.26.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.25.1 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.22.0 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.21.1 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.25.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.24.0 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.21.0 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.20.1 lib/primer/view_components/linters/migrations/truncate_component.rb
primer_view_components-0.20.0 lib/primer/view_components/linters/migrations/truncate_component.rb
openproject-primer_view_components-0.23.0 lib/primer/view_components/linters/migrations/truncate_component.rb