Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lint for methods with many arguments
- Loading branch information
1 parent
ab0394d
commit 7efa5a8
Showing
3 changed files
with
91 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
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,42 @@ | ||
module Pelusa | ||
module Lint | ||
class ManyArguments | ||
def initialize | ||
@violations = Set.new | ||
end | ||
|
||
def check(klass) | ||
initialize | ||
iterate_lines!(klass) | ||
return SuccessfulAnalysis.new(name) if @violations.empty? | ||
|
||
FailedAnalysis.new(name, formatted_violations) do |violations| | ||
"Methods with more than #{limit} arguments: #{violations.join(', ')}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def name | ||
"Methods have short argument lists" | ||
end | ||
|
||
def limit | ||
Pelusa.configuration['ManyArguments'].fetch('limit', 3) | ||
end | ||
|
||
def iterate_lines!(klass) | ||
iterator = Iterator.new do |node| | ||
if node.is_a?(Rubinius::AST::Define) && node.arguments.total_args > limit | ||
@violations << node.name | ||
end | ||
end | ||
Array(klass).each(&iterator) | ||
end | ||
|
||
def formatted_violations | ||
@violations.to_a | ||
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,47 @@ | ||
require 'test_helper' | ||
|
||
module Pelusa | ||
module Lint | ||
describe ManyArguments do | ||
before do | ||
@lint = ManyArguments.new | ||
end | ||
|
||
describe '#check' do | ||
describe 'when the class contains only method definitions with a small number of arguments' do | ||
it 'returns a SuccessAnalysis' do | ||
klass = """ | ||
class Foo | ||
def bar(dog) | ||
return dog | ||
end | ||
def baz(dog, cat) | ||
return dog + cat | ||
end | ||
def bam(dog, cat, fish) | ||
return dog + cat + fish | ||
end | ||
end""".to_ast | ||
|
||
analysis = @lint.check(klass) | ||
analysis.successful?.must_equal true | ||
end | ||
end | ||
|
||
describe 'when the class contains a method definition with many arguments' do | ||
it 'returns a FailureAnalysis' do | ||
klass = """ | ||
class Foo | ||
def bar(dog, cat, fish, lobster) | ||
x = 2 | ||
end | ||
end""".to_ast | ||
|
||
analysis = @lint.check(klass) | ||
analysis.failed?.must_equal true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |