mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: debugger statement
This commit is contained in:
parent
64ccc7448c
commit
348496ab9d
2 changed files with 64 additions and 0 deletions
44
spec/ameba/rules/debugger_statement_spec.cr
Normal file
44
spec/ameba/rules/debugger_statement_spec.cr
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
require "../../spec_helper"
|
||||||
|
|
||||||
|
module Ameba::Rules
|
||||||
|
subject = DebuggerStatement.new
|
||||||
|
|
||||||
|
describe DebuggerStatement do
|
||||||
|
it "passes if there is no debugger statement" do
|
||||||
|
s = Source.new %(
|
||||||
|
"this is not a debugger statement"
|
||||||
|
s = "debugger"
|
||||||
|
|
||||||
|
def debugger(program)
|
||||||
|
end
|
||||||
|
debugger ""
|
||||||
|
|
||||||
|
class A
|
||||||
|
def debugger
|
||||||
|
end
|
||||||
|
end
|
||||||
|
A.new.debugger
|
||||||
|
)
|
||||||
|
subject.catch(s).valid?.should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if there is a debugger statement" do
|
||||||
|
s = Source.new %(
|
||||||
|
a = 2
|
||||||
|
debugger
|
||||||
|
a = a + 1
|
||||||
|
)
|
||||||
|
subject.catch(s).valid?.should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reports rule, pos and message" do
|
||||||
|
s = Source.new "debugger"
|
||||||
|
subject.catch(s).valid?.should be_false
|
||||||
|
|
||||||
|
error = s.errors.first
|
||||||
|
error.rule.should_not be_nil
|
||||||
|
error.pos.should eq 1
|
||||||
|
error.message.should eq "Possible forgotten debugger statement detected"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
20
src/ameba/rules/debugger_statement.cr
Normal file
20
src/ameba/rules/debugger_statement.cr
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
module Ameba::Rules
|
||||||
|
# A rule that disallows calls to debugger.
|
||||||
|
#
|
||||||
|
# This is because we don't want debugger breakpoints accidentally being
|
||||||
|
# committed into our codebase.
|
||||||
|
struct DebuggerStatement < Rule
|
||||||
|
def test(source)
|
||||||
|
AST::CallVisitor.new self, source
|
||||||
|
end
|
||||||
|
|
||||||
|
def test(source, node : Crystal::Call)
|
||||||
|
return unless node.name == "debugger" &&
|
||||||
|
node.args.empty? &&
|
||||||
|
node.obj.nil?
|
||||||
|
|
||||||
|
source.error self, node.location.try &.line_number,
|
||||||
|
"Possible forgotten debugger statement detected"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue