mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge pull request #157 from crystal-ameba/feat/yaml-serializable
Move to YAML::Serializable
This commit is contained in:
commit
318fbdc4bf
6 changed files with 35 additions and 35 deletions
|
@ -1,18 +1,12 @@
|
||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
module Ameba::Rule
|
module Ameba::Rule
|
||||||
struct NoProperties < Rule::Base
|
|
||||||
def test(source)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe Base do
|
describe Base do
|
||||||
context ".rules" do
|
context ".rules" do
|
||||||
it "returns a list of all rules" do
|
it "returns a list of all rules" do
|
||||||
rules = Rule.rules
|
rules = Rule.rules
|
||||||
rules.should_not be_nil
|
rules.should_not be_nil
|
||||||
rules.should contain DummyRule
|
rules.should contain DummyRule
|
||||||
rules.should contain NoProperties
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -32,12 +26,6 @@ module Ameba::Rule
|
||||||
end
|
end
|
||||||
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
|
describe "#excluded?" do
|
||||||
it "returns false if a rule does no have a list of excluded source" 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
|
DummyRule.new.excluded?(Source.new "", "source.cr").should_not be_true
|
||||||
|
@ -80,7 +68,7 @@ module Ameba::Rule
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns false if rule has a different name" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -93,11 +93,11 @@ module Ameba
|
||||||
# Run `ameba --only Ameba/DummyRule` for details
|
# Run `ameba --only Ameba/DummyRule` for details
|
||||||
Ameba/DummyRule:
|
Ameba/DummyRule:
|
||||||
Description: Dummy rule that does nothing.
|
Description: Dummy rule that does nothing.
|
||||||
Enabled: true
|
|
||||||
Severity: Convention
|
|
||||||
Excluded:
|
Excluded:
|
||||||
- source1.cr
|
- source1.cr
|
||||||
- source2.cr
|
- source2.cr
|
||||||
|
Enabled: true
|
||||||
|
Severity: Convention
|
||||||
CONTENT
|
CONTENT
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -44,9 +44,10 @@ module Ameba
|
||||||
end
|
end
|
||||||
|
|
||||||
struct SeverityConvertable
|
struct SeverityConvertable
|
||||||
YAML.mapping(
|
include YAML::Serializable
|
||||||
severity: {type: Severity, converter: SeverityYamlConverter}
|
|
||||||
)
|
@[YAML::Field(converter: Ameba::SeverityYamlConverter)]
|
||||||
|
property severity : Severity
|
||||||
end
|
end
|
||||||
|
|
||||||
describe SeverityYamlConverter do
|
describe SeverityYamlConverter do
|
||||||
|
|
|
@ -37,10 +37,7 @@ module Ameba
|
||||||
|
|
||||||
struct NamedRule < Rule::Base
|
struct NamedRule < Rule::Base
|
||||||
properties do
|
properties do
|
||||||
description : String = "A rule with a custom name."
|
description "A rule with a custom name."
|
||||||
end
|
|
||||||
|
|
||||||
def test(source)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.name
|
def self.name
|
||||||
|
@ -50,7 +47,7 @@ module Ameba
|
||||||
|
|
||||||
struct ErrorRule < Rule::Base
|
struct ErrorRule < Rule::Base
|
||||||
properties do
|
properties do
|
||||||
description : String = "Always adds an error at 1:1"
|
description "Always adds an error at 1:1"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source)
|
def test(source)
|
||||||
|
@ -59,9 +56,11 @@ module Ameba
|
||||||
end
|
end
|
||||||
|
|
||||||
struct ScopeRule < Rule::Base
|
struct ScopeRule < Rule::Base
|
||||||
|
@[YAML::Field(ignore: true)]
|
||||||
getter scopes = [] of AST::Scope
|
getter scopes = [] of AST::Scope
|
||||||
|
|
||||||
def test(source)
|
properties do
|
||||||
|
description "Internal rule to test scopes"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source, node : Crystal::ASTNode, scope : AST::Scope)
|
def test(source, node : Crystal::ASTNode, scope : AST::Scope)
|
||||||
|
@ -70,9 +69,11 @@ module Ameba
|
||||||
end
|
end
|
||||||
|
|
||||||
struct FlowExpressionRule < Rule::Base
|
struct FlowExpressionRule < Rule::Base
|
||||||
|
@[YAML::Field(ignore: true)]
|
||||||
getter expressions = [] of AST::FlowExpression
|
getter expressions = [] of AST::FlowExpression
|
||||||
|
|
||||||
def test(source)
|
properties do
|
||||||
|
description "Internal rule to test flow expressions"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source, node, flow_expression : AST::FlowExpression)
|
def test(source, node, flow_expression : AST::FlowExpression)
|
||||||
|
@ -81,9 +82,11 @@ module Ameba
|
||||||
end
|
end
|
||||||
|
|
||||||
struct RedundantControlExpressionRule < Rule::Base
|
struct RedundantControlExpressionRule < Rule::Base
|
||||||
|
@[YAML::Field(ignore: true)]
|
||||||
getter nodes = [] of Crystal::ASTNode
|
getter nodes = [] of Crystal::ASTNode
|
||||||
|
|
||||||
def test(source)
|
properties do
|
||||||
|
description "Internal rule to test redundant control expressions"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source, node, visitor : AST::RedundantControlExpressionVisitor)
|
def test(source, node, visitor : AST::RedundantControlExpressionVisitor)
|
||||||
|
@ -95,6 +98,10 @@ module Ameba
|
||||||
struct RaiseRule < Rule::Base
|
struct RaiseRule < Rule::Base
|
||||||
property should_raise = false
|
property should_raise = false
|
||||||
|
|
||||||
|
properties do
|
||||||
|
description "Internal rule that always raises"
|
||||||
|
end
|
||||||
|
|
||||||
def test(source)
|
def test(source)
|
||||||
should_raise && raise "something went wrong"
|
should_raise && raise "something went wrong"
|
||||||
end
|
end
|
||||||
|
|
|
@ -241,28 +241,32 @@ class Ameba::Config
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
{% properties[name] = {key: key, default: value, type: type, converter: converter} %}
|
{% properties[name] = {key: key, default: value, type: type, converter: converter} %}
|
||||||
|
|
||||||
|
@[YAML::Field(key: {{key}}, converter: {{converter}}, type: {{type}})]
|
||||||
|
property {{name}} : {{type}} = {{value}}
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
{% if properties["enabled".id] == nil %}
|
{% if properties["enabled".id] == nil %}
|
||||||
{% properties["enabled".id] = {key: "Enabled", default: true, type: Bool} %}
|
@[YAML::Field(key: "Enabled")]
|
||||||
|
property enabled = true
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
{% if properties["severity".id] == nil %}
|
{% if properties["severity".id] == nil %}
|
||||||
{% default = @type.name.starts_with?("Ameba::Rule::Lint") ? "Severity::Warning".id : "Severity::Convention".id %}
|
{% default = @type.name.starts_with?("Ameba::Rule::Lint") ? "Ameba::Severity::Warning".id : "Ameba::Severity::Convention".id %}
|
||||||
{% properties["severity".id] = {key: "Severity", default: default, type: Severity, converter: SeverityYamlConverter} %}
|
@[YAML::Field(key: "Severity", converter: Ameba::SeverityYamlConverter)]
|
||||||
|
property severity = {{default}}
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
{% if properties["excluded".id] == nil %}
|
{% if properties["excluded".id] == nil %}
|
||||||
{% properties["excluded".id] = {key: "Excluded", type: "Array(String)?".id} %}
|
@[YAML::Field(key: "Excluded")]
|
||||||
|
property excluded : Array(String)?
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
YAML.mapping({{properties}})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
macro included
|
macro included
|
||||||
macro inherited
|
macro inherited
|
||||||
# allow creating rules without properties
|
include YAML::Serializable
|
||||||
properties {}
|
include YAML::Serializable::Strict
|
||||||
|
|
||||||
def self.new(config = nil)
|
def self.new(config = nil)
|
||||||
if (raw = config.try &.raw).is_a? Hash
|
if (raw = config.try &.raw).is_a? Hash
|
||||||
|
|
|
@ -22,7 +22,7 @@ module Ameba::Rule::Lint
|
||||||
struct Syntax < Base
|
struct Syntax < Base
|
||||||
properties do
|
properties do
|
||||||
description "Reports invalid Crystal syntax"
|
description "Reports invalid Crystal syntax"
|
||||||
severity Severity::Error
|
severity Ameba::Severity::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
def test(source)
|
def test(source)
|
||||||
|
|
Loading…
Reference in a new issue