mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Merge pull request #305 from crystal-ameba/Sija/followup-to-pr-300
Few more readability refactors
This commit is contained in:
commit
f9b6b17657
10 changed files with 72 additions and 54 deletions
|
@ -6,10 +6,10 @@ module Ameba::Formatter
|
||||||
|
|
||||||
sources.each do |source|
|
sources.each do |source|
|
||||||
source.issues.select(&.disabled?).each do |e|
|
source.issues.select(&.disabled?).each do |e|
|
||||||
if loc = e.location
|
next unless loc = e.location
|
||||||
output << "#{source.path}:#{loc.line_number}".colorize(:cyan)
|
|
||||||
output << " #{e.rule.name}\n"
|
output << "#{source.path}:#{loc.line_number}".colorize(:cyan)
|
||||||
end
|
output << " #{e.rule.name}\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,6 +72,7 @@ module Ameba::Formatter
|
||||||
|
|
||||||
private def finished_in_message(started, finished)
|
private def finished_in_message(started, finished)
|
||||||
return unless started && finished
|
return unless started && finished
|
||||||
|
|
||||||
"Finished in #{to_human(finished - started)}".colorize(:default)
|
"Finished in #{to_human(finished - started)}".colorize(:default)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -92,6 +93,7 @@ module Ameba::Formatter
|
||||||
|
|
||||||
minutes = span.minutes
|
minutes = span.minutes
|
||||||
seconds = span.seconds
|
seconds = span.seconds
|
||||||
|
|
||||||
"#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes"
|
"#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,19 +12,22 @@ module Ameba::Formatter
|
||||||
getter output : IO::FileDescriptor | IO::Memory
|
getter output : IO::FileDescriptor | IO::Memory
|
||||||
getter location : Crystal::Location
|
getter location : Crystal::Location
|
||||||
|
|
||||||
# Creates a new instance of ExplainFormatter.
|
# Creates a new instance of `ExplainFormatter`.
|
||||||
# Accepts *output* which indicates the io where the explanation will be wrtitten to.
|
#
|
||||||
|
# Accepts *output* which indicates the io where the explanation will be written to.
|
||||||
# Second argument is *location* which indicates the location to explain.
|
# Second argument is *location* which indicates the location to explain.
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# ExplainFormatter.new output,
|
# ExplainFormatter.new output,
|
||||||
# {file: path, line: line_number, column: column_number}
|
# file: path,
|
||||||
|
# line: line_number,
|
||||||
|
# column: column_number
|
||||||
# ```
|
# ```
|
||||||
def initialize(@output, location)
|
def initialize(@output, location)
|
||||||
@location = Crystal::Location.new(location[:file], location[:line], location[:column])
|
@location = Crystal::Location.new(location[:file], location[:line], location[:column])
|
||||||
end
|
end
|
||||||
|
|
||||||
# Reports the explainations at the *@location*.
|
# Reports the explanations at the *@location*.
|
||||||
def finished(sources)
|
def finished(sources)
|
||||||
source = sources.find(&.path.==(@location.filename))
|
source = sources.find(&.path.==(@location.filename))
|
||||||
return unless source
|
return unless source
|
||||||
|
|
|
@ -6,12 +6,13 @@ module Ameba::Formatter
|
||||||
source.issues.each do |e|
|
source.issues.each do |e|
|
||||||
next if e.disabled?
|
next if e.disabled?
|
||||||
next if e.correctable? && config[:autocorrect]?
|
next if e.correctable? && config[:autocorrect]?
|
||||||
if loc = e.location
|
|
||||||
@mutex.synchronize do
|
next unless loc = e.location
|
||||||
output.printf "%s:%d:%d: %s: [%s] %s\n",
|
|
||||||
source.path, loc.line_number, loc.column_number, e.rule.severity.symbol,
|
@mutex.synchronize do
|
||||||
e.rule.name, e.message.gsub('\n', " ")
|
output.printf "%s:%d:%d: %s: [%s] %s\n",
|
||||||
end
|
source.path, loc.line_number, loc.column_number, e.rule.severity.symbol,
|
||||||
|
e.rule.name, e.message.gsub('\n', " ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,7 +62,6 @@ module Ameba::Formatter
|
||||||
# },
|
# },
|
||||||
# }
|
# }
|
||||||
# ```
|
# ```
|
||||||
#
|
|
||||||
class JSONFormatter < BaseFormatter
|
class JSONFormatter < BaseFormatter
|
||||||
def initialize(@output = STDOUT)
|
def initialize(@output = STDOUT)
|
||||||
@result = AsJSON::Result.new
|
@result = AsJSON::Result.new
|
||||||
|
@ -75,10 +74,17 @@ module Ameba::Formatter
|
||||||
def source_finished(source : Source)
|
def source_finished(source : Source)
|
||||||
json_source = AsJSON::Source.new source.path
|
json_source = AsJSON::Source.new source.path
|
||||||
|
|
||||||
source.issues.each do |e|
|
source.issues.each do |issue|
|
||||||
next if e.disabled?
|
next if issue.disabled?
|
||||||
next if e.correctable? && config[:autocorrect]?
|
next if issue.correctable? && config[:autocorrect]?
|
||||||
json_source.issues << AsJSON::Issue.new(e.rule.name, e.rule.severity.to_s, e.location, e.end_location, e.message)
|
|
||||||
|
json_source.issues << AsJSON::Issue.new(
|
||||||
|
issue.rule.name,
|
||||||
|
issue.rule.severity.to_s,
|
||||||
|
issue.location,
|
||||||
|
issue.end_location,
|
||||||
|
issue.message
|
||||||
|
)
|
||||||
@result.summary.issues_count += 1
|
@result.summary.issues_count += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,7 +102,11 @@ module Ameba::Formatter
|
||||||
metadata = Metadata.new,
|
metadata = Metadata.new,
|
||||||
summary = Summary.new do
|
summary = Summary.new do
|
||||||
def to_json(json)
|
def to_json(json)
|
||||||
{sources: sources, metadata: metadata, summary: summary}.to_json(json)
|
{
|
||||||
|
sources: sources,
|
||||||
|
metadata: metadata,
|
||||||
|
summary: summary,
|
||||||
|
}.to_json(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -104,7 +114,10 @@ module Ameba::Formatter
|
||||||
path : String,
|
path : String,
|
||||||
issues = [] of Issue do
|
issues = [] of Issue do
|
||||||
def to_json(json)
|
def to_json(json)
|
||||||
{path: path, issues: issues}.to_json(json)
|
{
|
||||||
|
path: path,
|
||||||
|
issues: issues,
|
||||||
|
}.to_json(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -115,15 +128,19 @@ module Ameba::Formatter
|
||||||
end_location : Crystal::Location?,
|
end_location : Crystal::Location?,
|
||||||
message : String do
|
message : String do
|
||||||
def to_json(json)
|
def to_json(json)
|
||||||
json.object do
|
{
|
||||||
json.field :rule_name, rule_name
|
rule_name: rule_name,
|
||||||
json.field :severity, severity
|
severity: severity,
|
||||||
json.field :message, message
|
message: message,
|
||||||
json.field :location,
|
location: {
|
||||||
{line: location.try &.line_number, column: location.try &.column_number}
|
line: location.try &.line_number,
|
||||||
json.field :end_location,
|
column: location.try &.column_number,
|
||||||
{line: end_location.try &.line_number, column: end_location.try &.column_number}
|
},
|
||||||
end
|
end_location: {
|
||||||
|
line: end_location.try &.line_number,
|
||||||
|
column: end_location.try &.column_number,
|
||||||
|
},
|
||||||
|
}.to_json(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -131,10 +148,10 @@ module Ameba::Formatter
|
||||||
ameba_version : String = Ameba::VERSION,
|
ameba_version : String = Ameba::VERSION,
|
||||||
crystal_version : String = Crystal::VERSION do
|
crystal_version : String = Crystal::VERSION do
|
||||||
def to_json(json)
|
def to_json(json)
|
||||||
json.object do
|
{
|
||||||
json.field :ameba_version, ameba_version
|
ameba_version: ameba_version,
|
||||||
json.field :crystal_version, crystal_version
|
crystal_version: crystal_version,
|
||||||
end
|
}.to_json(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -143,10 +160,10 @@ module Ameba::Formatter
|
||||||
property issues_count = 0
|
property issues_count = 0
|
||||||
|
|
||||||
def to_json(json)
|
def to_json(json)
|
||||||
json.object do
|
{
|
||||||
json.field :target_sources_count, target_sources_count
|
target_sources_count: target_sources_count,
|
||||||
json.field :issues_count, issues_count
|
issues_count: issues_count,
|
||||||
end
|
}.to_json(json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,9 +20,9 @@ module Ameba::Formatter
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
file = generate_todo_config issues
|
generate_todo_config(issues).tap do |file|
|
||||||
@output.puts "Created #{file.path}"
|
@output.puts "Created #{file.path}"
|
||||||
file
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def generate_todo_config(issues)
|
private def generate_todo_config(issues)
|
||||||
|
@ -43,6 +43,7 @@ module Ameba::Formatter
|
||||||
issues.each do |issue|
|
issues.each do |issue|
|
||||||
next if issue.disabled? || issue.rule.is_a?(Rule::Lint::Syntax)
|
next if issue.disabled? || issue.rule.is_a?(Rule::Lint::Syntax)
|
||||||
next if issue.correctable? && config[:autocorrect]?
|
next if issue.correctable? && config[:autocorrect]?
|
||||||
|
|
||||||
(h[issue.rule] ||= Array(Issue).new) << issue
|
(h[issue.rule] ||= Array(Issue).new) << issue
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -160,7 +160,7 @@ module Ameba::Rule::Style
|
||||||
end
|
end
|
||||||
|
|
||||||
private def guard_clause(node)
|
private def guard_clause(node)
|
||||||
node = node.right if node.is_a?(Crystal::And) || node.is_a?(Crystal::Or)
|
node = node.right if node.is_a?(Crystal::BinaryOp)
|
||||||
|
|
||||||
return unless location = node.location
|
return unless location = node.location
|
||||||
return unless end_location = node.end_location
|
return unless end_location = node.end_location
|
||||||
|
@ -175,11 +175,9 @@ module Ameba::Rule::Style
|
||||||
end
|
end
|
||||||
|
|
||||||
def guard_clause_source(source, guard_clause, parent)
|
def guard_clause_source(source, guard_clause, parent)
|
||||||
if parent.is_a?(Crystal::And) || parent.is_a?(Crystal::Or)
|
node = parent.is_a?(Crystal::BinaryOp) ? parent : guard_clause
|
||||||
node_source(parent, source.lines)
|
|
||||||
else
|
node_source(node, source.lines)
|
||||||
node_source(guard_clause, source.lines)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,8 +80,8 @@ module Ameba::Rule::Style
|
||||||
|
|
||||||
begin_loc, end_loc = begin_range
|
begin_loc, end_loc = begin_range
|
||||||
begin_loc, end_loc = def_loc.seek(begin_loc), def_loc.seek(end_loc)
|
begin_loc, end_loc = def_loc.seek(begin_loc), def_loc.seek(end_loc)
|
||||||
begin_end_loc = begin_loc.adjust(column_number: {{"begin".size - 1}})
|
begin_end_loc = begin_loc.adjust(column_number: {{ "begin".size - 1 }})
|
||||||
end_end_loc = end_loc.adjust(column_number: {{"end".size - 1}})
|
end_end_loc = end_loc.adjust(column_number: {{ "end".size - 1 }})
|
||||||
|
|
||||||
issue_for begin_loc, begin_end_loc, MSG do |corrector|
|
issue_for begin_loc, begin_end_loc, MSG do |corrector|
|
||||||
corrector.remove(begin_loc, begin_end_loc)
|
corrector.remove(begin_loc, begin_end_loc)
|
||||||
|
|
|
@ -62,10 +62,7 @@ module Ameba::Rule::Style
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def operator?(name)
|
protected def operator?(name)
|
||||||
name.each_char do |char|
|
!name.empty? && name[0].in?(OPERATOR_CHARS)
|
||||||
return false unless char.in?(OPERATOR_CHARS)
|
|
||||||
end
|
|
||||||
!name.empty?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def setter?(name)
|
protected def setter?(name)
|
||||||
|
|
|
@ -9,7 +9,6 @@ module Ameba
|
||||||
# runner = Ameba::Runner.new config
|
# runner = Ameba::Runner.new config
|
||||||
# runner.run.success? # => true or false
|
# runner.run.success? # => true or false
|
||||||
# ```
|
# ```
|
||||||
#
|
|
||||||
class Runner
|
class Runner
|
||||||
# An error indicating that the inspection loop got stuck correcting
|
# An error indicating that the inspection loop got stuck correcting
|
||||||
# issues back and forth.
|
# issues back and forth.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue