mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Set default severities
This commit is contained in:
		
							parent
							
								
									0be42f94db
								
							
						
					
					
						commit
						f6a57f9272
					
				
					 5 changed files with 35 additions and 19 deletions
				
			
		| 
						 | 
				
			
			@ -33,5 +33,9 @@ module Ameba::Rule::Lint
 | 
			
		|||
      issue.location.to_s.should eq "source.cr:1:11"
 | 
			
		||||
      issue.message.should eq "unexpected token: end (expected ';' or newline)"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "has highest severity" do
 | 
			
		||||
      subject.severity.should eq Severity::Error
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,12 +2,6 @@ require "../spec_helper"
 | 
			
		|||
 | 
			
		||||
module Ameba
 | 
			
		||||
  describe Severity do
 | 
			
		||||
    describe ".default" do
 | 
			
		||||
      it "returns default severity" do
 | 
			
		||||
        Severity.default.should eq Severity::Refactoring
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    describe ".from_name" do
 | 
			
		||||
      it "creates error severity by name" do
 | 
			
		||||
        Severity.from_name("Error").should eq Severity::Error
 | 
			
		||||
| 
						 | 
				
			
			@ -31,46 +25,60 @@ module Ameba
 | 
			
		|||
 | 
			
		||||
  struct SeverityConvertable
 | 
			
		||||
    YAML.mapping(
 | 
			
		||||
      severity: { type: Severity, converter: SeverityYamlConverter }
 | 
			
		||||
      severity: {type: Severity, converter: SeverityYamlConverter}
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe SeverityYamlConverter do
 | 
			
		||||
    describe ".from_yaml" do
 | 
			
		||||
      it "converts from yaml to Severity::Error" do
 | 
			
		||||
        yaml = { severity: "error" }.to_yaml
 | 
			
		||||
        yaml = {severity: "error"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml)
 | 
			
		||||
        converted.severity.should eq Severity::Error
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "converts from yaml to Severity::Warning" do
 | 
			
		||||
        yaml = { severity: "warning" }.to_yaml
 | 
			
		||||
        yaml = {severity: "warning"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml)
 | 
			
		||||
        converted.severity.should eq Severity::Warning
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "converts from yaml to Severity::Refactoring" do
 | 
			
		||||
        yaml = { severity: "refactoring" }.to_yaml
 | 
			
		||||
        yaml = {severity: "refactoring"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml)
 | 
			
		||||
        converted.severity.should eq Severity::Refactoring
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "raises if severity is not a scalar" do
 | 
			
		||||
        yaml = {severity: {refactoring: true}}.to_yaml
 | 
			
		||||
        expect_raises(Exception, "Severity must be a scalar") do
 | 
			
		||||
          SeverityConvertable.from_yaml(yaml)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "raises if severity has a wrong type" do
 | 
			
		||||
        yaml = {severity: [1, 2, 3]}.to_yaml
 | 
			
		||||
        expect_raises(Exception, "Severity must be a scalar") do
 | 
			
		||||
          SeverityConvertable.from_yaml(yaml)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    describe ".to_yaml" do
 | 
			
		||||
      it "converts Severity::Error to yaml" do
 | 
			
		||||
        yaml = { severity: "error" }.to_yaml
 | 
			
		||||
        yaml = {severity: "error"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml).to_yaml
 | 
			
		||||
        converted.should eq "---\nseverity: Error\n"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "converts Severity::Warning to yaml" do
 | 
			
		||||
        yaml = { severity: "warning" }.to_yaml
 | 
			
		||||
        yaml = {severity: "warning"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml).to_yaml
 | 
			
		||||
        converted.should eq "---\nseverity: Warning\n"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "converts Severity::Refactoring to yaml" do
 | 
			
		||||
        yaml = { severity: "refactoring" }.to_yaml
 | 
			
		||||
        yaml = {severity: "refactoring"}.to_yaml
 | 
			
		||||
        converted = SeverityConvertable.from_yaml(yaml).to_yaml
 | 
			
		||||
        converted.should eq "---\nseverity: Refactoring\n"
 | 
			
		||||
      end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -227,6 +227,11 @@ class Ameba::Config
 | 
			
		|||
        {% properties["enabled".id] = {key: "Enabled", default: true, type: Bool} %}
 | 
			
		||||
      {% end %}
 | 
			
		||||
 | 
			
		||||
      {% if properties["severity".id] == nil %}
 | 
			
		||||
        {% default = @type.name.starts_with?("Ameba::Rule::Lint") ? "Severity::Warning".id : "Severity::Refactoring".id %}
 | 
			
		||||
        {% properties["severity".id] = {key: "Severity", default: default, type: Severity, converter: SeverityYamlConverter} %}
 | 
			
		||||
      {% end %}
 | 
			
		||||
 | 
			
		||||
      {% if properties["excluded".id] == nil %}
 | 
			
		||||
        {% properties["excluded".id] = {key: "Excluded", type: "Array(String)?".id} %}
 | 
			
		||||
      {% end %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,10 @@ module Ameba::Rule::Lint
 | 
			
		|||
  # ```
 | 
			
		||||
  #
 | 
			
		||||
  struct Syntax < Base
 | 
			
		||||
    getter description = "Reports invalid Crystal syntax."
 | 
			
		||||
    properties do
 | 
			
		||||
      description "Reports invalid Crystal syntax"
 | 
			
		||||
      severity Severity::Error
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def test(source)
 | 
			
		||||
      source.ast
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,10 +4,6 @@ module Ameba
 | 
			
		|||
    Warning
 | 
			
		||||
    Refactoring
 | 
			
		||||
 | 
			
		||||
    def self.default
 | 
			
		||||
      Refactoring
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def self.from_name(name : String)
 | 
			
		||||
      case name.downcase
 | 
			
		||||
      when "error"
 | 
			
		||||
| 
						 | 
				
			
			@ -37,7 +33,7 @@ module Ameba
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    def self.to_yaml(value : Severity, yaml : YAML::Nodes::Builder)
 | 
			
		||||
      yaml.scalar value.to_s
 | 
			
		||||
      yaml.scalar value
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue