mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Add severity to formatters
This commit is contained in:
		
							parent
							
								
									575fe07879
								
							
						
					
					
						commit
						117e100855
					
				
					 11 changed files with 73 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -64,6 +64,19 @@ module Ameba::Formatter
 | 
			
		|||
          log.should contain "      \e[33m^\e[0m"
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it "writes severity" do
 | 
			
		||||
          output.clear
 | 
			
		||||
          s = Source.new(%(
 | 
			
		||||
            a = 22
 | 
			
		||||
            puts a
 | 
			
		||||
          )).tap do |source|
 | 
			
		||||
            source.add_issue(DummyRule.new, {1, 5}, "DummyRuleError")
 | 
			
		||||
          end
 | 
			
		||||
          subject.finished [s]
 | 
			
		||||
          log = output.to_s
 | 
			
		||||
          log.should contain "[R]"
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it "doesn't write affected code if it is disabled" do
 | 
			
		||||
          output.clear
 | 
			
		||||
          s = Source.new(%(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -50,6 +50,7 @@ module Ameba
 | 
			
		|||
        source = Source.new "a = 42", "source.cr"
 | 
			
		||||
        output = explanation(source)
 | 
			
		||||
        output.should contain "RULE INFO"
 | 
			
		||||
        output.should contain "Refactoring"
 | 
			
		||||
        output.should contain "Ameba/ErrorRule"
 | 
			
		||||
        output.should contain "Always adds an error at 1:1"
 | 
			
		||||
      end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,7 +22,7 @@ module Ameba::Formatter
 | 
			
		|||
        subject = flycheck
 | 
			
		||||
        subject.source_finished s
 | 
			
		||||
        subject.output.to_s.should eq(
 | 
			
		||||
          "source.cr:1:2: E: [#{DummyRule.rule_name}] message\n"
 | 
			
		||||
          "source.cr:1:2: R: [#{DummyRule.rule_name}] message\n"
 | 
			
		||||
        )
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +32,7 @@ module Ameba::Formatter
 | 
			
		|||
        subject = flycheck
 | 
			
		||||
        subject.source_finished s
 | 
			
		||||
        subject.output.to_s.should eq(
 | 
			
		||||
          "source.cr:1:2: E: [#{DummyRule.rule_name}] multi line\n"
 | 
			
		||||
          "source.cr:1:2: R: [#{DummyRule.rule_name}] multi line\n"
 | 
			
		||||
        )
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,6 +37,14 @@ module Ameba
 | 
			
		|||
        result["sources"][0]["issues"][0]["rule_name"].should eq DummyRule.rule_name
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "shows severity" do
 | 
			
		||||
        s = Source.new ""
 | 
			
		||||
        s.add_issue DummyRule.new, {1, 2}, "message"
 | 
			
		||||
 | 
			
		||||
        result = get_result [s]
 | 
			
		||||
        result["sources"][0]["issues"][0]["severity"].should eq "Refactoring"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "shows a message" do
 | 
			
		||||
        s = Source.new ""
 | 
			
		||||
        s.add_issue DummyRule.new, {1, 2}, "message"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,10 @@ module Ameba
 | 
			
		|||
        create_todo.should contain "DummyRule"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "creates a todo with severity" do
 | 
			
		||||
        create_todo.should contain "Refactoring"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "creates a todo with problems count" do
 | 
			
		||||
        create_todo.should contain "Problems found: 1"
 | 
			
		||||
      end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,26 @@ require "../spec_helper"
 | 
			
		|||
 | 
			
		||||
module Ameba
 | 
			
		||||
  describe Severity do
 | 
			
		||||
    describe "#symbol" do
 | 
			
		||||
      it "returns the symbol for each severity in the enum" do
 | 
			
		||||
        Severity.values.each do |severity|
 | 
			
		||||
          severity.symbol.should_not be_nil
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "returns symbol for Error" do
 | 
			
		||||
        Severity::Error.symbol.should eq 'E'
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "returns symbol for Warning" do
 | 
			
		||||
        Severity::Warning.symbol.should eq 'W'
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "returns symbol for Refactoring" do
 | 
			
		||||
        Severity::Refactoring.symbol.should eq 'R'
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    describe ".from_name" do
 | 
			
		||||
      it "creates error severity by name" do
 | 
			
		||||
        Severity.from_name("Error").should eq Severity::Error
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,7 @@ module Ameba::Formatter
 | 
			
		|||
          next if (location = issue.location).nil?
 | 
			
		||||
 | 
			
		||||
          output << "#{location}\n".colorize(:cyan)
 | 
			
		||||
          output << "#{issue.rule.name}: #{issue.message}\n".colorize(:red)
 | 
			
		||||
          output << "[#{issue.rule.severity.symbol}] #{issue.rule.name}: #{issue.message}\n".colorize(:red)
 | 
			
		||||
 | 
			
		||||
          if show_affected_code && (code = affected_code(source, location))
 | 
			
		||||
            output << code
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -57,7 +57,7 @@ module Ameba::Formatter
 | 
			
		|||
 | 
			
		||||
      if rule.responds_to?(:description)
 | 
			
		||||
        output_title "RULE INFO"
 | 
			
		||||
        output_paragraph [rule.name, rule.description]
 | 
			
		||||
        output_paragraph [rule.severity.to_s, rule.name, rule.description]
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      output_title "DETAILED DESCRIPTION"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@ module Ameba::Formatter
 | 
			
		|||
        next if e.disabled?
 | 
			
		||||
        if loc = e.location
 | 
			
		||||
          output.printf "%s:%d:%d: %s: [%s] %s\n",
 | 
			
		||||
            source.path, loc.line_number, loc.column_number, "E",
 | 
			
		||||
            source.path, loc.line_number, loc.column_number, e.rule.severity.symbol,
 | 
			
		||||
            e.rule.name, e.message.gsub("\n", " ")
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,6 +25,7 @@ module Ameba::Formatter
 | 
			
		|||
  #           },
 | 
			
		||||
  #           "message":   "Useless assignment to variable `a`",
 | 
			
		||||
  #           "rule_name": "UselessAssign",
 | 
			
		||||
  #           "severity":  "Refactoring",
 | 
			
		||||
  #         },
 | 
			
		||||
  #         {
 | 
			
		||||
  #           "location": {
 | 
			
		||||
| 
						 | 
				
			
			@ -49,6 +50,7 @@ module Ameba::Formatter
 | 
			
		|||
  #           },
 | 
			
		||||
  #           "message":   "Useless assignment to variable `a`",
 | 
			
		||||
  #           "rule_name": "UselessAssign",
 | 
			
		||||
  #           "severity":  "Refactoring",
 | 
			
		||||
  #         },
 | 
			
		||||
  #       ],
 | 
			
		||||
  #       "path": "src/ameba/formatter/json_formatter.cr",
 | 
			
		||||
| 
						 | 
				
			
			@ -75,7 +77,7 @@ module Ameba::Formatter
 | 
			
		|||
 | 
			
		||||
      source.issues.each do |e|
 | 
			
		||||
        next if e.disabled?
 | 
			
		||||
        json_source.issues << AsJSON::Issue.new(e.rule.name, e.location, e.end_location, e.message)
 | 
			
		||||
        json_source.issues << AsJSON::Issue.new(e.rule.name, e.rule.severity.to_s, e.location, e.end_location, e.message)
 | 
			
		||||
        @result.summary.issues_count += 1
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -107,12 +109,14 @@ module Ameba::Formatter
 | 
			
		|||
 | 
			
		||||
    record Issue,
 | 
			
		||||
      rule_name : String,
 | 
			
		||||
      severity : String,
 | 
			
		||||
      location : Crystal::Location?,
 | 
			
		||||
      end_location : Crystal::Location?,
 | 
			
		||||
      message : String do
 | 
			
		||||
      def to_json(json)
 | 
			
		||||
        json.object do
 | 
			
		||||
          json.field :rule_name, rule_name
 | 
			
		||||
          json.field :severity, severity
 | 
			
		||||
          json.field :message, message
 | 
			
		||||
          json.field :location,
 | 
			
		||||
            {line: location.try &.line_number, column: location.try &.column_number}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,22 @@ module Ameba
 | 
			
		|||
    Warning
 | 
			
		||||
    Refactoring
 | 
			
		||||
 | 
			
		||||
    # Returns a symbol uniquely indicating severity.
 | 
			
		||||
    #
 | 
			
		||||
    # ```
 | 
			
		||||
    # Severity::Warning.symbol # => 'W'
 | 
			
		||||
    # ```
 | 
			
		||||
    def symbol
 | 
			
		||||
      to_s[0]
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # Creates Severity by the name.
 | 
			
		||||
    #
 | 
			
		||||
    # ```
 | 
			
		||||
    # Severity.from_name('refactoring') # => Severity::Refactoring
 | 
			
		||||
    # Severity.from_name('foo-bar')     # => Exception: Incorrect severity name..
 | 
			
		||||
    # ```
 | 
			
		||||
    #
 | 
			
		||||
    def self.from_name(name : String)
 | 
			
		||||
      case name.downcase
 | 
			
		||||
      when "error"
 | 
			
		||||
| 
						 | 
				
			
			@ -18,6 +34,7 @@ module Ameba
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Converter for `YAML.mapping` which converts severity enum to and from YAML.
 | 
			
		||||
  class SeverityYamlConverter
 | 
			
		||||
    def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
 | 
			
		||||
      unless node.is_a?(YAML::Nodes::Scalar)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue