mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
parent
2ddf26b8c3
commit
94e1d4567a
6 changed files with 51 additions and 2 deletions
|
@ -48,6 +48,15 @@ module Ameba::Rule::Performance
|
|||
end
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "reports in macro scope" do
|
||||
source = Source.new %(
|
||||
{{ [1, 2, 3].reject { |e| e > 2 }.any? }}
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
[1, 2, 3].reject { |e| e > 2 }.any?
|
||||
|
|
|
@ -107,6 +107,15 @@ module Ameba::Rule::Performance
|
|||
issue.message.should eq "Use `reverse_each.find {...}` instead of `select {...}.last`"
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "doesn't report in macro scope" do
|
||||
source = Source.new %(
|
||||
{{[1, 2, 3].select { |e| e > 2 }.last }}
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports a correct message for last?" do
|
||||
s = Source.new %(
|
||||
[1, 2, 3].select { |e| e > 2 }.last?
|
||||
|
|
|
@ -49,6 +49,15 @@ module Ameba::Rule::Performance
|
|||
end
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "doesn't report in macro scope" do
|
||||
source = Source.new %(
|
||||
{{[1, 2, 3].select { |v| v > 1 }.size}}
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
end
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
lines.split("\n").reject(&.empty?).size
|
||||
|
|
|
@ -34,6 +34,13 @@ module Ameba::AST
|
|||
# ```
|
||||
#
|
||||
class NodeVisitor < BaseVisitor
|
||||
@skip : Array(Crystal::ASTNode.class)?
|
||||
|
||||
def initialize(@rule, @source, skip = nil)
|
||||
@skip = skip.try &.map { |el| el.as(Crystal::ASTNode.class) }
|
||||
super @rule, @source
|
||||
end
|
||||
|
||||
{% for name in NODES %}
|
||||
# A visit callback for `Crystal::{{name}}` node.
|
||||
# Returns true meaning that child nodes will be traversed as well.
|
||||
|
@ -42,5 +49,10 @@ module Ameba::AST
|
|||
true
|
||||
end
|
||||
{% end %}
|
||||
|
||||
def visit(node)
|
||||
return true unless (skip = @skip)
|
||||
!skip.includes?(node.class)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,7 +35,12 @@ module Ameba::Rule::Performance
|
|||
end
|
||||
|
||||
def test(source)
|
||||
AST::NodeVisitor.new self, source
|
||||
AST::NodeVisitor.new self, source, skip: [
|
||||
Crystal::Macro,
|
||||
Crystal::MacroExpression,
|
||||
Crystal::MacroIf,
|
||||
Crystal::MacroFor,
|
||||
]
|
||||
end
|
||||
|
||||
def test(source, node : Crystal::Call)
|
||||
|
|
|
@ -41,7 +41,12 @@ module Ameba::Rule::Performance
|
|||
end
|
||||
|
||||
def test(source)
|
||||
AST::NodeVisitor.new self, source
|
||||
AST::NodeVisitor.new self, source, skip: [
|
||||
Crystal::Macro,
|
||||
Crystal::MacroExpression,
|
||||
Crystal::MacroIf,
|
||||
Crystal::MacroFor,
|
||||
]
|
||||
end
|
||||
|
||||
def test(source, node : Crystal::Call)
|
||||
|
|
Loading…
Reference in a new issue