Use NodeVisitor by default and remove the boilerplace code

This commit is contained in:
Vitalii Elenhaupt 2020-03-25 18:21:07 +02:00
parent 1d827b4969
commit 1a0468c653
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
26 changed files with 6 additions and 101 deletions

View file

@ -32,7 +32,12 @@ module Ameba::Rule
# This method is designed to test the source passed in. If source has issues
# that are tested by this rule, it should add an issue.
abstract def test(source : Source)
#
# Be default it uses a node visitor to traverse all the nodes in the source.
# Must be overriten for other type of rules.
def test(source : Source)
AST::NodeVisitor.new self, source
end
def test(source : Source, node : Crystal::ASTNode, *opts)
# can't be abstract

View file

@ -28,10 +28,6 @@ module Ameba::Rule::Lint
MSG = "Comparison to a boolean is pointless"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
comparison = %w(== != ===).includes?(node.name)
to_boolean = node.args.first?.try &.is_a?(Crystal::BoolLiteral) ||

View file

@ -18,10 +18,6 @@ module Ameba::Rule::Lint
MSG = "Possible forgotten debugger statement detected"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
return unless node.name == "debugger" &&
node.args.empty? &&

View file

@ -46,10 +46,6 @@ module Ameba::Rule::Lint
MSG = "Empty `ensure` block detected"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::ExceptionHandler)
node_ensure = node.ensure
return if node_ensure.nil? || !node_ensure.nop?

View file

@ -38,10 +38,6 @@ module Ameba::Rule::Lint
MSG = "Avoid empty expression %s"
MSG_EXRS = "Avoid empty expressions"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::NilLiteral)
exp = node_source(node, source.lines).try &.join

View file

@ -27,10 +27,6 @@ module Ameba::Rule::Lint
MSG = "Duplicated keys in hash literal: %s"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::HashLiteral)
return unless (keys = duplicated_keys(node.entries)).any?

View file

@ -30,10 +30,6 @@ module Ameba::Rule::Lint
MSG = "Literal value found in conditional"
def test(source)
AST::NodeVisitor.new self, source
end
def check_node(source, node)
return unless literal?(node.cond)
issue_for node, MSG

View file

@ -25,10 +25,6 @@ module Ameba::Rule::Lint
MSG = "Literal value found in interpolation"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::StringInterpolation)
found = node.expressions.any? { |e| !e.is_a?(Crystal::StringLiteral) && literal?(e) }
return unless found

View file

@ -30,10 +30,6 @@ module Ameba::Rule::Lint
MSG = "%s always returns 0"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
return unless node.name == "rand" &&
node.args.size == 1 &&

View file

@ -32,10 +32,6 @@ module Ameba::Rule::Lint
description "Disallows redundant `with_index` calls"
end
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
args, block = node.args, node.block

View file

@ -33,10 +33,6 @@ module Ameba::Rule::Lint
description "Disallows redundant `with_object` calls"
end
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
return if node.name != "each_with_object" ||
node.args.size != 1 ||

View file

@ -41,10 +41,6 @@ module Ameba::Rule::Lint
MSG = "Exception handler has shadowed exceptions: %s"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::ExceptionHandler)
return unless excs = node.rescues

View file

@ -51,10 +51,6 @@ module Ameba::Rule::Lint
issue_for cond, MSG
end
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::When)
ConditionInWhenVisitor.new self, source, node
end

View file

@ -17,10 +17,6 @@ module Ameba::Rule::Metrics
MSG = "Cyclomatic complexity too high [%d/%d]"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Def)
complexity = AST::CountingVisitor.new(node).count

View file

@ -34,10 +34,6 @@ module Ameba::Rule::Performance
description "Identifies usage of `any?` calls that follow filters."
end
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Call)
return unless node.name == ANY_NAME && (obj = node.obj)

View file

@ -29,10 +29,6 @@ module Ameba::Rule::Style
MSG = "Constant name should be screaming-cased: %s, not %s"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Assign)
if (target = node.target).is_a? Crystal::Path
name = target.names.first

View file

@ -45,10 +45,6 @@ module Ameba::Rule::Style
MSG = "Method name should be underscore-cased: %s, not %s"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Def)
return if (expected = node.name.underscore) == node.name

View file

@ -34,10 +34,6 @@ module Ameba::Rule::Style
MSG = "Avoid negated conditions in unless blocks"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Unless)
return unless negated_condition? node.cond
issue_for node, MSG

View file

@ -37,10 +37,6 @@ module Ameba::Rule::Style
MSG = "Favour method name '%s?' over '%s'"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Def)
if node.name =~ /^(is|has)_(\w+)\?/
alternative = $2

View file

@ -64,10 +64,6 @@ module Ameba::Rule::Style
MSG = "Redundant `begin` block detected"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Def)
return unless redundant_begin?(source, node)

View file

@ -105,10 +105,6 @@ module Ameba::Rule::Style
MSG = "Redundant `next` detected"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Block)
AST::RedundantControlExpressionVisitor.new(self, source, node.body)
end

View file

@ -102,10 +102,6 @@ module Ameba::Rule::Style
MSG = "Redundant `return` detected"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Def)
AST::RedundantControlExpressionVisitor.new(self, source, node.body)
end

View file

@ -59,10 +59,6 @@ module Ameba::Rule::Style
MSG = "Type name should be camelcased: %s, but it was %s"
def test(source)
AST::NodeVisitor.new self, source
end
private def check_node(source, node)
name = node.name.to_s
expected = name.camelcase

View file

@ -50,10 +50,6 @@ module Ameba::Rule::Style
MSG = "Favour if over unless with else"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::Unless)
return if node.else.nop?
issue_for node, MSG

View file

@ -30,10 +30,6 @@ module Ameba::Rule::Style
MSG = "Var name should be underscore-cased: %s, not %s"
def test(source)
AST::NodeVisitor.new self, source
end
private def check_node(source, node)
return if (expected = node.name.underscore) == node.name

View file

@ -33,10 +33,6 @@ module Ameba::Rule::Style
MSG = "While statement using true literal as condition"
def test(source)
AST::NodeVisitor.new self, source
end
def test(source, node : Crystal::While)
return unless node.cond.true_literal?
issue_for node, MSG