diff --git a/spec/ameba/base_spec.cr b/spec/ameba/base_spec.cr index 6beae408..21970320 100644 --- a/spec/ameba/base_spec.cr +++ b/spec/ameba/base_spec.cr @@ -1,18 +1,12 @@ require "../spec_helper" module Ameba::Rule - struct NoProperties < Rule::Base - def test(source) - end - end - 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 end @@ -32,12 +26,6 @@ module Ameba::Rule end end - describe "when a rule does not have defined properties" do - it "is enabled by default" do - NoProperties.new.enabled.should be_true - end - end - describe "#excluded?" do it "returns false if a rule does no have a list of excluded source" do DummyRule.new.excluded?(Source.new "", "source.cr").should_not be_true @@ -80,7 +68,7 @@ module Ameba::Rule end it "returns false if rule has a different name" do - DummyRule.new.should_not eq(NoProperties.new) + DummyRule.new.should_not eq(ScopeRule.new) end end end diff --git a/spec/ameba/formatter/todo_formatter_spec.cr b/spec/ameba/formatter/todo_formatter_spec.cr index 1abc9904..050162af 100644 --- a/spec/ameba/formatter/todo_formatter_spec.cr +++ b/spec/ameba/formatter/todo_formatter_spec.cr @@ -93,11 +93,11 @@ module Ameba # Run `ameba --only Ameba/DummyRule` for details Ameba/DummyRule: Description: Dummy rule that does nothing. - Enabled: true - Severity: Convention Excluded: - source1.cr - source2.cr + Enabled: true + Severity: Convention CONTENT end diff --git a/spec/ameba/severity_spec.cr b/spec/ameba/severity_spec.cr index fe8c5cc5..94f3b730 100644 --- a/spec/ameba/severity_spec.cr +++ b/spec/ameba/severity_spec.cr @@ -44,9 +44,10 @@ module Ameba end struct SeverityConvertable - YAML.mapping( - severity: {type: Severity, converter: SeverityYamlConverter} - ) + include YAML::Serializable + + @[YAML::Field(converter: Ameba::SeverityYamlConverter)] + property severity : Severity end describe SeverityYamlConverter do diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 005b207e..4df5496c 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -37,10 +37,7 @@ module Ameba struct NamedRule < Rule::Base properties do - description : String = "A rule with a custom name." - end - - def test(source) + description "A rule with a custom name." end def self.name @@ -50,7 +47,7 @@ module Ameba struct ErrorRule < Rule::Base properties do - description : String = "Always adds an error at 1:1" + description "Always adds an error at 1:1" end def test(source) @@ -59,9 +56,11 @@ module Ameba end struct ScopeRule < Rule::Base + @[YAML::Field(ignore: true)] getter scopes = [] of AST::Scope - def test(source) + properties do + description "Internal rule to test scopes" end def test(source, node : Crystal::ASTNode, scope : AST::Scope) @@ -70,9 +69,11 @@ module Ameba end struct FlowExpressionRule < Rule::Base + @[YAML::Field(ignore: true)] getter expressions = [] of AST::FlowExpression - def test(source) + properties do + description "Internal rule to test flow expressions" end def test(source, node, flow_expression : AST::FlowExpression) @@ -81,9 +82,11 @@ module Ameba end struct RedundantControlExpressionRule < Rule::Base + @[YAML::Field(ignore: true)] getter nodes = [] of Crystal::ASTNode - def test(source) + properties do + description "Internal rule to test redundant control expressions" end def test(source, node, visitor : AST::RedundantControlExpressionVisitor) @@ -95,6 +98,10 @@ module Ameba struct RaiseRule < Rule::Base property should_raise = false + properties do + description "Internal rule that always raises" + end + def test(source) should_raise && raise "something went wrong" end diff --git a/src/ameba/config.cr b/src/ameba/config.cr index dc94bc7b..1ecbd3a8 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -241,28 +241,32 @@ class Ameba::Config {% end %} {% properties[name] = {key: key, default: value, type: type, converter: converter} %} + + @[YAML::Field(key: {{key}}, converter: {{converter}}, type: {{type}})] + property {{name}} : {{type}} = {{value}} {% end %} {% if properties["enabled".id] == nil %} - {% properties["enabled".id] = {key: "Enabled", default: true, type: Bool} %} + @[YAML::Field(key: "Enabled")] + property enabled = true {% end %} {% if properties["severity".id] == nil %} - {% default = @type.name.starts_with?("Ameba::Rule::Lint") ? "Severity::Warning".id : "Severity::Convention".id %} - {% properties["severity".id] = {key: "Severity", default: default, type: Severity, converter: SeverityYamlConverter} %} + {% default = @type.name.starts_with?("Ameba::Rule::Lint") ? "Ameba::Severity::Warning".id : "Ameba::Severity::Convention".id %} + @[YAML::Field(key: "Severity", converter: Ameba::SeverityYamlConverter)] + property severity = {{default}} {% end %} {% if properties["excluded".id] == nil %} - {% properties["excluded".id] = {key: "Excluded", type: "Array(String)?".id} %} + @[YAML::Field(key: "Excluded")] + property excluded : Array(String)? {% end %} - - YAML.mapping({{properties}}) end macro included macro inherited - # allow creating rules without properties - properties {} + include YAML::Serializable + include YAML::Serializable::Strict def self.new(config = nil) if (raw = config.try &.raw).is_a? Hash diff --git a/src/ameba/rule/lint/syntax.cr b/src/ameba/rule/lint/syntax.cr index f5c2addc..8d135401 100644 --- a/src/ameba/rule/lint/syntax.cr +++ b/src/ameba/rule/lint/syntax.cr @@ -22,7 +22,7 @@ module Ameba::Rule::Lint struct Syntax < Base properties do description "Reports invalid Crystal syntax" - severity Severity::Error + severity Ameba::Severity::Error end def test(source)