Skip to content

Commit

Permalink
Add rubocop linter for Truncate (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerJDev authored Aug 14, 2023
1 parent 279b11b commit 1b770f9
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/light-rockets-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/view-components': minor
---

Add rubocop linter for `Truncate` component

<!-- Changed components: _none_ -->
2 changes: 2 additions & 0 deletions .erb-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ linters:
Enabled: false
Exclude:
- app/components/primer/layout_component.html.erb
Primer/Migrations/TruncateComponent:
Enabled: true
1 change: 1 addition & 0 deletions lib/primer/view_components/linters.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

Dir[File.join(__dir__, "linters", "*.rb")].sort.each { |file| require file }
Dir[File.join(__dir__, "linters", "migrations", "*.rb")].sort.each { |file| require file }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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
57 changes: 57 additions & 0 deletions test/lib/rubocop/truncate_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require "lib/cop_test_case"

class ButtonMigrationTest < CopTestCase
def cop_class
::RuboCop::Cop::Migrations::TruncateComponent
end

def test_no_warnings_on_beta_truncate
investigate(cop, <<~RUBY)
Primer::Beta::Truncate.new()
RUBY

assert_empty cop.offenses
end

def test_warn_on_truncate_no_args
investigate(cop, <<~RUBY)
Primer::Truncate.new()
RUBY

assert_equal 1, cop.offenses.count
assert_equal "Primer::Beta::Truncate.new(tag: :div)", cop.offenses.first.corrector.rewrite.strip
end

def test_warn_on_truncate_simple_arg
investigate(cop, <<~RUBY)
Primer::Truncate.new(0)
RUBY

assert_equal 1, cop.offenses.count
assert cop.offenses.first.corrector.present?

assert_equal "Primer::Beta::Truncate.new(0, tag: :div)", cop.offenses.first.corrector.rewrite.strip
end

def test_warn_no_fix_truncate_inline
investigate(cop, <<~RUBY)
Primer::Truncate.new(inline: true)
RUBY

assert_equal 1, cop.offenses.count
refute cop.offenses.first.corrector.present?
end

def test_do_not_add_tag_if_tag_is_present
investigate(cop, <<~RUBY)
Primer::Truncate.new(tag: :span)
RUBY

assert_equal 1, cop.offenses.count
assert cop.offenses.first.corrector.present?

assert_equal "Primer::Beta::Truncate.new(tag: :span)", cop.offenses.first.corrector.rewrite.strip
end
end

0 comments on commit 1b770f9

Please sign in to comment.