-
Notifications
You must be signed in to change notification settings - Fork 39
Add Lint/DuplicateMethodSignature rule
#697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
spec/ameba/rule/lint/duplicate_method_signature_spec.cr
This file contains hidden or 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,119 @@ | ||
| require "../../../spec_helper" | ||
|
|
||
| module Ameba::Rule::Lint | ||
| describe DuplicateMethodSignature do | ||
| subject = DuplicateMethodSignature.new | ||
|
|
||
| it "passes if there are no duplicate methods" do | ||
| expect_no_issues subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo; end | ||
| def foo(&); end | ||
| def foo?; end | ||
| def foo!; end | ||
| def foo( | ||
| bar : Bar, | ||
| ) | ||
| end | ||
| def foo( | ||
| baz : Baz, | ||
| ) | ||
| end | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "passes if there are duplicate methods with `previous_def`" do | ||
| expect_no_issues subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo | ||
| 42 | ||
| end | ||
|
|
||
| def foo | ||
| previous_def if rand >= 0.42 | ||
| 24 | ||
| end | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are duplicate methods without `previous_def`" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo | ||
| 42 | ||
| end | ||
|
|
||
| def foo | ||
| # ^^^^^^^ error: Duplicate method signature detected | ||
| previous_definition if rand >= 0.42 | ||
| 24 | ||
| end | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are multiple duplicate methods" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo; end | ||
| def bar; end | ||
| def foo; end | ||
| # ^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| def foo; end | ||
| # ^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are duplicate methods (with block)" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo(&); end | ||
| def bar(&); end | ||
| def foo(&); end | ||
| # ^^^^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are duplicate methods (with visibility modifier)" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo; end | ||
| private def foo; end | ||
| # ^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| protected def foo; end | ||
| # ^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are duplicate methods (with arguments)" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo(a, b, c = 3, &); end | ||
| def foo(a, b, c = 3); end | ||
| def foo(a, b, c = 3); end | ||
| # ^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| end | ||
| CRYSTAL | ||
| end | ||
|
|
||
| it "reports if there are duplicate methods with different bodies" do | ||
| expect_issue subject, <<-CRYSTAL | ||
| class Foo | ||
| def foo(a, b, c = 3) | ||
| puts :foo | ||
| end | ||
|
|
||
| def foo(a, b, c = 3) | ||
| # ^^^^^^^^^^^^^^^^^^^^ error: Duplicate method signature detected | ||
| puts :bar | ||
| end | ||
| end | ||
| CRYSTAL | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,66 @@ | ||
| module Ameba::Rule::Lint | ||
| # Reports repeated class or module method signatures. | ||
| # | ||
| # Only methods of the same signature are considered duplicates, | ||
| # regardless of their bodies, except for ones including `previous_def`. | ||
| # | ||
| # ``` | ||
| # class Foo | ||
| # def greet(name) | ||
| # puts "Hello #{name}!" | ||
| # end | ||
| # | ||
| # def greet(name) # duplicated method signature | ||
| # puts "¡Hola! #{name}" | ||
| # end | ||
| # end | ||
| # ``` | ||
| # | ||
| # YAML configuration example: | ||
| # | ||
| # ``` | ||
| # Lint/DuplicateMethodSignature: | ||
| # Enabled: true | ||
| # ``` | ||
| class DuplicateMethodSignature < Base | ||
| properties do | ||
| since_version "1.7.0" | ||
| description "Reports repeated method signatures" | ||
| end | ||
|
|
||
| MSG = "Duplicate method signature detected" | ||
|
|
||
| def test(source) | ||
| AST::ScopeVisitor.new self, source | ||
| end | ||
|
|
||
| def test(source, node : Crystal::ClassDef | Crystal::ModuleDef, scope : AST::Scope) | ||
| found_defs = Set(String).new | ||
|
|
||
| each_def_node(node) do |def_node| | ||
| def_node_to_s = def_node.to_s | ||
|
|
||
| next if def_node_to_s.matches?(/\Wprevious_def\W/) | ||
| next if found_defs.add?(def_node_to_s.lines.first) | ||
|
|
||
| issue_for def_node, MSG | ||
| end | ||
| end | ||
|
|
||
| private def each_def_node(node, &) | ||
| case body = node.body | ||
| when Crystal::Def | ||
| yield body | ||
| when Crystal::VisibilityModifier | ||
| yield body.exp | ||
| when Crystal::Expressions | ||
| body.expressions.each do |exp| | ||
| case exp | ||
| when Crystal::Def then yield exp | ||
| when Crystal::VisibilityModifier then yield exp.exp | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.