From 522933e6155d1fd6b7fe80a422eaba0926c2f5fa Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Tue, 8 Oct 2024 03:29:18 +0200 Subject: [PATCH 1/2] Guard calls to the `typos` with a mutex --- src/ameba/rule/lint/typos.cr | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/ameba/rule/lint/typos.cr b/src/ameba/rule/lint/typos.cr index 8a2506e95..9050ddce3 100644 --- a/src/ameba/rule/lint/typos.cr +++ b/src/ameba/rule/lint/typos.cr @@ -24,6 +24,26 @@ module Ameba::Rule::Lint BIN_PATH = Process.find_executable("typos") rescue nil + @@mutex = Mutex.new + + protected def self.typos_from(bin_path : String, source : Source) : Array(Typo)? + result = @@mutex.synchronize do + status = Process.run(bin_path, args: %w[--format json -], + input: IO::Memory.new(source.code), + output: output = IO::Memory.new, + ) + output.to_s unless status.success? + end + return unless result + + ([] of Typo).tap do |typos| + # NOTE: `--format json` is actually JSON Lines (`jsonl`) + result.each_line do |line| + Typo.parse(line).try { |typo| typos << typo } + end + end + end + def bin_path : String? @bin_path || BIN_PATH end @@ -80,18 +100,7 @@ module Ameba::Rule::Lint end return end - status = Process.run(bin_path, args: %w[--format json -], - input: IO::Memory.new(source.code), - output: output = IO::Memory.new, - ) - return if status.success? - - ([] of Typo).tap do |typos| - # NOTE: `--format json` is actually JSON Lines (`jsonl`) - output.to_s.each_line do |line| - Typo.parse(line).try { |typo| typos << typo } - end - end + Typos.typos_from(bin_path, source) end end end From 9aeb25b3bbc77db6fc7732be8bdcb150053b918a Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Tue, 8 Oct 2024 06:35:45 +0200 Subject: [PATCH 2/2] Minor refactors --- src/ameba/rule/lint/typos.cr | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/ameba/rule/lint/typos.cr b/src/ameba/rule/lint/typos.cr index 9050ddce3..9f19f690a 100644 --- a/src/ameba/rule/lint/typos.cr +++ b/src/ameba/rule/lint/typos.cr @@ -68,7 +68,6 @@ module Ameba::Rule::Lint end private record Typo, - path : String, typo : String, corrections : Array(String), location : {Int32, Int32}, @@ -83,24 +82,23 @@ module Ameba::Rule::Lint return if typo.empty? || corrections.empty? - path = issue["path"].as_s line_no = issue["line_num"].as_i col_no = issue["byte_offset"].as_i + 1 end_col_no = col_no + typo.size - 1 - new(path, typo, corrections, - {line_no, col_no}, {line_no, end_col_no}) + new(typo, corrections, + {line_no, col_no}, + {line_no, end_col_no}) end end protected def typos_from(source : Source) : Array(Typo)? - unless bin_path = self.bin_path - if fail_on_error? - raise RuntimeError.new "Could not find `typos` executable" - end - return + if bin_path = self.bin_path + return Typos.typos_from(bin_path, source) + end + if fail_on_error? + raise RuntimeError.new "Could not find `typos` executable" end - Typos.typos_from(bin_path, source) end end end