-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rubocop linter for
Truncate
(#2200)
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
45 changes: 45 additions & 0 deletions
45
lib/primer/view_components/linters/migrations/truncate_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |