Syntax rule running priorities
This commit is contained in:
parent
c9db63bf34
commit
4c85ad7c75
|
@ -7,6 +7,19 @@ module Ameba::Rule
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Base do
|
describe Base do
|
||||||
|
context ".rules" do
|
||||||
|
it "returns a list of all rules" do
|
||||||
|
rules = Rule.rules
|
||||||
|
rules.should_not be_nil
|
||||||
|
rules.should contain DummyRule
|
||||||
|
rules.should contain NoProperties
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not include syntax rule" do
|
||||||
|
Rule.rules.should_not contain Rule::Syntax
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "properties" do
|
context "properties" do
|
||||||
subject = DummyRule.new
|
subject = DummyRule.new
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,21 @@ module Ameba
|
||||||
it "excludes source from this rule" do
|
it "excludes source from this rule" do
|
||||||
create_todo.should contain "Excluded:\n - source.cr"
|
create_todo.should contain "Excluded:\n - source.cr"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when invalid syntax" do
|
||||||
|
it "does not exclude Syntax rule" do
|
||||||
|
file = IO::Memory.new
|
||||||
|
formatter = Formatter::TODOFormatter.new IO::Memory.new, file
|
||||||
|
|
||||||
|
s = Source.new "def invalid_syntax"
|
||||||
|
s.error Rule::Syntax.new, s.location(1, 2), "message"
|
||||||
|
|
||||||
|
formatter.finished [s]
|
||||||
|
content = file.to_s
|
||||||
|
|
||||||
|
content.should_not contain "Syntax"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,6 @@ module Ameba::Rule
|
||||||
def hello
|
def hello
|
||||||
puts "totally valid"
|
puts "totally valid"
|
||||||
rescue e: Exception
|
rescue e: Exception
|
||||||
#
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
subject.catch(s).should be_valid
|
subject.catch(s).should be_valid
|
||||||
|
@ -20,7 +19,6 @@ module Ameba::Rule
|
||||||
def hello
|
def hello
|
||||||
puts "invalid"
|
puts "invalid"
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
#
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
subject.catch(s).should_not be_valid
|
subject.catch(s).should_not be_valid
|
||||||
|
|
|
@ -51,6 +51,30 @@ module Ameba
|
||||||
|
|
||||||
Runner.new(rules, [source], formatter).run.success?.should be_true
|
Runner.new(rules, [source], formatter).run.success?.should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "invalid syntax" do
|
||||||
|
it "reports a syntax error" do
|
||||||
|
rules = [Rule::Syntax.new] of Rule::Base
|
||||||
|
source = Source.new "def bad_syntax"
|
||||||
|
|
||||||
|
Runner.new(rules, [source], formatter).run
|
||||||
|
source.should_not be_valid
|
||||||
|
source.errors.first.rule.name.should eq "Syntax"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not run other rules" do
|
||||||
|
rules = [Rule::Syntax.new, Rule::ConstantNames.new] of Rule::Base
|
||||||
|
source = Source.new %q(
|
||||||
|
MyBadConstant = 1
|
||||||
|
|
||||||
|
when my_bad_syntax
|
||||||
|
)
|
||||||
|
|
||||||
|
Runner.new(rules, [source], formatter).run
|
||||||
|
source.should_not be_valid
|
||||||
|
source.errors.size.should eq 1
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#success?" do
|
describe "#success?" do
|
||||||
|
|
|
@ -31,6 +31,7 @@ module Ameba::Formatter
|
||||||
private def rule_errors_map(errors)
|
private def rule_errors_map(errors)
|
||||||
Hash(Rule::Base, Array(Source::Error)).new.tap do |h|
|
Hash(Rule::Base, Array(Source::Error)).new.tap do |h|
|
||||||
errors.each do |error|
|
errors.each do |error|
|
||||||
|
next if error.rule.is_a? Rule::Syntax
|
||||||
h[error.rule] ||= Array(Source::Error).new
|
h[error.rule] ||= Array(Source::Error).new
|
||||||
h[error.rule] << error
|
h[error.rule] << error
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,13 +82,14 @@ module Ameba::Rule
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a list of all available rules.
|
# Returns a list of all available rules
|
||||||
|
# (except a `Rule::Syntax` which is a special rule).
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# Ameba::Rule.rules # => [LineLength, ConstantNames, ....]
|
# Ameba::Rule.rules # => [LineLength, ConstantNames, ....]
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
def self.rules
|
def self.rules
|
||||||
Base.subclasses
|
Base.subclasses.reject! &.== Rule::Syntax
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,11 +20,6 @@ module Ameba::Rule
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
struct Syntax < Base
|
struct Syntax < Base
|
||||||
properties do
|
|
||||||
enabled = true
|
|
||||||
description = "Reports invalid Crystal syntax"
|
|
||||||
end
|
|
||||||
|
|
||||||
def test(source)
|
def test(source)
|
||||||
source.ast
|
source.ast
|
||||||
rescue e : Crystal::SyntaxException
|
rescue e : Crystal::SyntaxException
|
||||||
|
|
|
@ -20,6 +20,9 @@ module Ameba
|
||||||
# A formatter to prepare report.
|
# A formatter to prepare report.
|
||||||
@formatter : Formatter::BaseFormatter
|
@formatter : Formatter::BaseFormatter
|
||||||
|
|
||||||
|
# A syntax rule which always inspects a source first
|
||||||
|
@syntax_rule = Rule::Syntax.new
|
||||||
|
|
||||||
# Instantiates a runner using a `config`.
|
# Instantiates a runner using a `config`.
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
|
@ -57,9 +60,11 @@ module Ameba
|
||||||
@sources.each do |source|
|
@sources.each do |source|
|
||||||
@formatter.source_started source
|
@formatter.source_started source
|
||||||
|
|
||||||
@rules.each do |rule|
|
if @syntax_rule.catch(source).valid?
|
||||||
next if rule.excluded?(source)
|
@rules.each do |rule|
|
||||||
rule.test(source)
|
next if rule.excluded?(source)
|
||||||
|
rule.test(source)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@formatter.source_finished source
|
@formatter.source_finished source
|
||||||
|
|
Loading…
Reference in New Issue