Few tweaks and readability refactors

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-20 00:34:11 +01:00
parent ab059616b5
commit 4e3caf2986
8 changed files with 21 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -2,7 +2,8 @@
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)

View file

@ -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

View file

@ -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