Merge pull request #157 from crystal-ameba/feat/yaml-serializable

Move to YAML::Serializable
This commit is contained in:
Vitalii Elenhaupt 2020-06-18 16:02:59 +03:00 committed by GitHub
commit 318fbdc4bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 35 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)