diff --git a/src/ameba/runner.cr b/src/ameba/runner.cr index dd2ca2f7..87e398c6 100644 --- a/src/ameba/runner.cr +++ b/src/ameba/runner.cr @@ -19,8 +19,7 @@ module Ameba def initialize(path, issues_by_iteration, loop_start = -1) root_cause = issues_by_iteration[loop_start..-1] - .map(&.map(&.rule.name).uniq!.join(", ")) - .join(" -> ") + .join(" -> ", &.map(&.rule.name).uniq!.join(", ")) message = String.build do |io| io << "Infinite loop" @@ -210,7 +209,7 @@ module Ameba private def check_for_infinite_loop(source, corrected_issues, processed_sources) checksum = Digest::SHA1.hexdigest(source.code) - if (loop_start = processed_sources.index(checksum)) + if loop_start = processed_sources.index(checksum) raise InfiniteCorrectionLoopError.new( source.path, corrected_issues, diff --git a/src/ameba/source.cr b/src/ameba/source.cr index 8e27c242..d6e0b97d 100644 --- a/src/ameba/source.cr +++ b/src/ameba/source.cr @@ -11,9 +11,6 @@ module Ameba # Crystal code (content of a source file). getter code : String - @lines : Array(String)? - @ast : Crystal::ASTNode? - # Creates a new source by `code` and `path`. # # For example: @@ -50,9 +47,7 @@ module Ameba # source.lines # => ["a = 1", "b = 2"] # ``` # - def lines : Array(String) - @lines ||= code.split('\n') - end + getter lines : Array(String) { code.split('\n') } # Returns AST nodes constructed by `Crystal::Parser`. # @@ -61,12 +56,11 @@ module Ameba # source.ast # ``` # - def ast : Crystal::ASTNode - @ast ||= - Crystal::Parser.new(code) - .tap(&.wants_doc = true) - .tap(&.filename = path) - .parse + getter ast : Crystal::ASTNode do + Crystal::Parser.new(code) + .tap(&.wants_doc = true) + .tap(&.filename = path) + .parse end getter fullpath : String do diff --git a/src/ameba/source/corrector.cr b/src/ameba/source/corrector.cr index 43b3d348..94124786 100644 --- a/src/ameba/source/corrector.cr +++ b/src/ameba/source/corrector.cr @@ -8,7 +8,7 @@ class Ameba::Source def initialize(code : String) @rewriter = Rewriter.new(code) - @line_sizes = code.lines(chomp: false).map(&.size) + @line_sizes = code.each_line(chomp: false).map(&.size).to_a end # Replaces the code of the given range with *content*. diff --git a/src/ameba/source/rewriter.cr b/src/ameba/source/rewriter.cr index 6825ea87..5eda2d6a 100644 --- a/src/ameba/source/rewriter.cr +++ b/src/ameba/source/rewriter.cr @@ -59,9 +59,9 @@ class Ameba::Source # The updates are organized in a tree, according to the ranges they act on # (where children are strictly contained by their parent). class Rewriter - getter code + getter code : String - def initialize(@code : String) + def initialize(@code) @action_root = Rewriter::Action.new(0, code.size) end diff --git a/src/ameba/source/rewriter/action.cr b/src/ameba/source/rewriter/action.cr index df4d1270..456ad33a 100644 --- a/src/ameba/source/rewriter/action.cr +++ b/src/ameba/source/rewriter/action.cr @@ -73,21 +73,23 @@ class Ameba::Source::Rewriter family = analyse_hierarchy(action) sibling_left, sibling_right = family[:sibling_left], family[:sibling_right] - if (fusible = family[:fusible]) + if fusible = family[:fusible] child = family[:child] child ||= [] of Action fuse_deletions(action, fusible, sibling_left + child + sibling_right) else - extra_sibling = if (parent = family[:parent]) - # action should be a descendant of one of the children - parent.do_combine(action) - elsif (child = family[:child]) - # or it should become the parent of some of the children, - action.with(children: child).combine_children(action.children) - else - # or else it should become an additional child - action - end + extra_sibling = + case + when parent = family[:parent] + # action should be a descendant of one of the children + parent.do_combine(action) + when child = family[:child] + # or it should become the parent of some of the children, + action.with(children: child).combine_children(action.children) + else + # or else it should become an additional child + action + end self.with(children: sibling_left + [extra_sibling] + sibling_right) end end @@ -102,8 +104,8 @@ class Ameba::Source::Rewriter protected def fuse_deletions(action, fusible, other_siblings) without_fusible = self.with(children: other_siblings) fusible = [action] + fusible - fused_begin_pos = fusible.map(&.begin_pos).min - fused_end_pos = fusible.map(&.end_pos).max + fused_begin_pos = fusible.min_of(&.begin_pos) + fused_end_pos = fusible.max_of(&.end_pos) fused_deletion = action.with(begin_pos: fused_begin_pos, end_pos: fused_end_pos) without_fusible.do_combine(fused_deletion) end