mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Few tweaks and readability refactors
This commit is contained in:
parent
ab059616b5
commit
4e3caf2986
8 changed files with 21 additions and 13 deletions
|
@ -17,7 +17,7 @@ module Ameba
|
||||||
it "passes if type names are screaming-cased" do
|
it "passes if type names are screaming-cased" do
|
||||||
expect_no_issues subject, <<-CRYSTAL
|
expect_no_issues subject, <<-CRYSTAL
|
||||||
LUCKY_NUMBERS = [3, 7, 11]
|
LUCKY_NUMBERS = [3, 7, 11]
|
||||||
DOCUMENTATION_URL = "http://crystal-lang.org/docs"
|
DOCUMENTATION_URL = "https://crystal-lang.org/docs"
|
||||||
|
|
||||||
Int32
|
Int32
|
||||||
|
|
||||||
|
|
|
@ -286,3 +286,7 @@ end
|
||||||
def as_nodes(source)
|
def as_nodes(source)
|
||||||
Ameba::TestNodeVisitor.new(as_node source)
|
Ameba::TestNodeVisitor.new(as_node source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trailing_whitespace
|
||||||
|
' '
|
||||||
|
end
|
||||||
|
|
|
@ -57,8 +57,7 @@ module Ameba::Rule::Style
|
||||||
|
|
||||||
if cond.is_a?(Crystal::Assign) && allow_safe_assignment?
|
if cond.is_a?(Crystal::Assign) && allow_safe_assignment?
|
||||||
issue_for cond, MSG_MISSING do |corrector|
|
issue_for cond, MSG_MISSING do |corrector|
|
||||||
corrector.insert_before(cond, '(')
|
corrector.wrap(cond, '(', ')')
|
||||||
corrector.insert_after(cond, ')')
|
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,7 @@ module Ameba
|
||||||
def correct
|
def correct
|
||||||
corrector = Corrector.new(code)
|
corrector = Corrector.new(code)
|
||||||
issues.each(&.correct(corrector))
|
issues.each(&.correct(corrector))
|
||||||
|
|
||||||
corrected_code = corrector.process
|
corrected_code = corrector.process
|
||||||
return false if code == corrected_code
|
return false if code == corrected_code
|
||||||
|
|
||||||
|
|
|
@ -125,7 +125,9 @@ class Ameba::Source
|
||||||
|
|
||||||
private def check_range_validity(begin_pos, end_pos)
|
private def check_range_validity(begin_pos, end_pos)
|
||||||
return unless begin_pos < 0 || end_pos > code.size
|
return unless begin_pos < 0 || end_pos > code.size
|
||||||
raise IndexError.new("The range #{begin_pos}...#{end_pos} is outside the bounds of the source")
|
raise IndexError.new(
|
||||||
|
"The range #{begin_pos}...#{end_pos} is outside the bounds of the source"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
class Ameba::Spec::AnnotatedSource
|
class Ameba::Spec::AnnotatedSource
|
||||||
ANNOTATION_PATTERN_1 = /\A\s*(# )?(\^+|\^{})( error:)? /
|
ANNOTATION_PATTERN_1 = /\A\s*(# )?(\^+|\^{})( error:)? /
|
||||||
ANNOTATION_PATTERN_2 = " # error: "
|
ANNOTATION_PATTERN_2 = " # error: "
|
||||||
|
|
||||||
ABBREV = "[...]"
|
ABBREV = "[...]"
|
||||||
|
|
||||||
getter lines : Array(String)
|
getter lines : Array(String)
|
||||||
|
@ -15,6 +16,7 @@ class Ameba::Spec::AnnotatedSource
|
||||||
def self.parse(annotated_code)
|
def self.parse(annotated_code)
|
||||||
lines = [] of String
|
lines = [] of String
|
||||||
annotations = [] of {Int32, String, String}
|
annotations = [] of {Int32, String, String}
|
||||||
|
|
||||||
code_lines = annotated_code.split('\n') # must preserve trailing newline
|
code_lines = annotated_code.split('\n') # must preserve trailing newline
|
||||||
code_lines.each do |code_line|
|
code_lines.each do |code_line|
|
||||||
case
|
case
|
||||||
|
@ -39,7 +41,9 @@ class Ameba::Spec::AnnotatedSource
|
||||||
# NOTE: Annotations are sorted so that reconstructing the annotation
|
# NOTE: Annotations are sorted so that reconstructing the annotation
|
||||||
# text via `#to_s` is deterministic.
|
# text via `#to_s` is deterministic.
|
||||||
def initialize(@lines, annotations : Enumerable({Int32, String, String}))
|
def initialize(@lines, annotations : Enumerable({Int32, String, String}))
|
||||||
@annotations = annotations.to_a.sort_by { |line, _, message| {line, message} }
|
@annotations = annotations.to_a.sort_by do |line, _, message|
|
||||||
|
{line, message}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Annotates the source code with the Ameba issues provided.
|
# Annotates the source code with the Ameba issues provided.
|
||||||
|
@ -47,7 +51,9 @@ class Ameba::Spec::AnnotatedSource
|
||||||
# NOTE: Annotations are sorted so that reconstructing the annotation
|
# NOTE: Annotations are sorted so that reconstructing the annotation
|
||||||
# text via `#to_s` is deterministic.
|
# text via `#to_s` is deterministic.
|
||||||
def initialize(@lines, issues : Enumerable(Issue))
|
def initialize(@lines, issues : Enumerable(Issue))
|
||||||
@annotations = issues_to_annotations(issues).sort_by { |line, _, message| {line, message} }
|
@annotations = issues_to_annotations(issues).sort_by do |line, _, message|
|
||||||
|
{line, message}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
|
|
|
@ -13,8 +13,8 @@ module Ameba::Spec
|
||||||
def failure_message(source)
|
def failure_message(source)
|
||||||
String.build do |str|
|
String.build do |str|
|
||||||
str << "Source expected to be valid, but there are issues: \n\n"
|
str << "Source expected to be valid, but there are issues: \n\n"
|
||||||
source.issues.reject(&.disabled?).each do |e|
|
source.issues.reject(&.disabled?).each do |issue|
|
||||||
str << " * #{e.rule.name}: #{e.message}\n"
|
str << " * #{issue.rule.name}: #{issue.message}\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,9 +14,5 @@ module Ameba
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def trailing_whitespace
|
|
||||||
' '
|
|
||||||
end
|
|
||||||
|
|
||||||
include Ameba::Spec::BeValid
|
include Ameba::Spec::BeValid
|
||||||
include Ameba::Spec::ExpectIssue
|
include Ameba::Spec::ExpectIssue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue