From 4e3caf2986456f7a74d7bdc6062a88c24bd491cb Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Tue, 20 Dec 2022 00:34:11 +0100 Subject: [PATCH] Few tweaks and readability refactors --- spec/ameba/rule/style/constant_names_spec.cr | 2 +- spec/spec_helper.cr | 4 ++++ src/ameba/rule/style/parentheses_around_condition.cr | 3 +-- src/ameba/source.cr | 1 + src/ameba/source/rewriter.cr | 4 +++- src/ameba/spec/annotated_source.cr | 12 +++++++++--- src/ameba/spec/be_valid.cr | 4 ++-- src/ameba/spec/support.cr | 4 ---- 8 files changed, 21 insertions(+), 13 deletions(-) diff --git a/spec/ameba/rule/style/constant_names_spec.cr b/spec/ameba/rule/style/constant_names_spec.cr index 0053ecf5..4629c0bc 100644 --- a/spec/ameba/rule/style/constant_names_spec.cr +++ b/spec/ameba/rule/style/constant_names_spec.cr @@ -17,7 +17,7 @@ module Ameba it "passes if type names are screaming-cased" do expect_no_issues subject, <<-CRYSTAL LUCKY_NUMBERS = [3, 7, 11] - DOCUMENTATION_URL = "http://crystal-lang.org/docs" + DOCUMENTATION_URL = "https://crystal-lang.org/docs" Int32 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 60df2ed0..4a18746b 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -286,3 +286,7 @@ end def as_nodes(source) Ameba::TestNodeVisitor.new(as_node source) end + +def trailing_whitespace + ' ' +end diff --git a/src/ameba/rule/style/parentheses_around_condition.cr b/src/ameba/rule/style/parentheses_around_condition.cr index 5ddc15f4..a19dbabf 100644 --- a/src/ameba/rule/style/parentheses_around_condition.cr +++ b/src/ameba/rule/style/parentheses_around_condition.cr @@ -57,8 +57,7 @@ module Ameba::Rule::Style if cond.is_a?(Crystal::Assign) && allow_safe_assignment? issue_for cond, MSG_MISSING do |corrector| - corrector.insert_before(cond, '(') - corrector.insert_after(cond, ')') + corrector.wrap(cond, '(', ')') end return end diff --git a/src/ameba/source.cr b/src/ameba/source.cr index 38ce9b82..9811589e 100644 --- a/src/ameba/source.cr +++ b/src/ameba/source.cr @@ -27,6 +27,7 @@ module Ameba def correct corrector = Corrector.new(code) issues.each(&.correct(corrector)) + corrected_code = corrector.process return false if code == corrected_code diff --git a/src/ameba/source/rewriter.cr b/src/ameba/source/rewriter.cr index fc07c071..1deda8ec 100644 --- a/src/ameba/source/rewriter.cr +++ b/src/ameba/source/rewriter.cr @@ -125,7 +125,9 @@ class Ameba::Source private def check_range_validity(begin_pos, end_pos) 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 diff --git a/src/ameba/spec/annotated_source.cr b/src/ameba/spec/annotated_source.cr index 874fd936..4f77e3d6 100644 --- a/src/ameba/spec/annotated_source.cr +++ b/src/ameba/spec/annotated_source.cr @@ -2,7 +2,8 @@ class Ameba::Spec::AnnotatedSource ANNOTATION_PATTERN_1 = /\A\s*(# )?(\^+|\^{})( error:)? / ANNOTATION_PATTERN_2 = " # error: " - ABBREV = "[...]" + + ABBREV = "[...]" getter lines : Array(String) @@ -15,6 +16,7 @@ class Ameba::Spec::AnnotatedSource def self.parse(annotated_code) lines = [] of String annotations = [] of {Int32, String, String} + code_lines = annotated_code.split('\n') # must preserve trailing newline code_lines.each do |code_line| case @@ -39,7 +41,9 @@ class Ameba::Spec::AnnotatedSource # NOTE: Annotations are sorted so that reconstructing the annotation # text via `#to_s` is deterministic. 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 # 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 # text via `#to_s` is deterministic. 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 def ==(other) diff --git a/src/ameba/spec/be_valid.cr b/src/ameba/spec/be_valid.cr index 6247c697..f4eb3063 100644 --- a/src/ameba/spec/be_valid.cr +++ b/src/ameba/spec/be_valid.cr @@ -13,8 +13,8 @@ module Ameba::Spec def failure_message(source) String.build do |str| str << "Source expected to be valid, but there are issues: \n\n" - source.issues.reject(&.disabled?).each do |e| - str << " * #{e.rule.name}: #{e.message}\n" + source.issues.reject(&.disabled?).each do |issue| + str << " * #{issue.rule.name}: #{issue.message}\n" end end end diff --git a/src/ameba/spec/support.cr b/src/ameba/spec/support.cr index 2813ce15..851401f6 100644 --- a/src/ameba/spec/support.cr +++ b/src/ameba/spec/support.cr @@ -14,9 +14,5 @@ module Ameba end end -def trailing_whitespace - ' ' -end - include Ameba::Spec::BeValid include Ameba::Spec::ExpectIssue