Skip to content

Commit

Permalink
Merge pull request #474 from crystal-ameba/473-mt-breaks-linttypos-rule
Browse files Browse the repository at this point in the history
Guard calls to the `typos` with a mutex
  • Loading branch information
Sija authored Oct 8, 2024
2 parents f72f0b1 + 9aeb25b commit 476a956
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/ameba/rule/lint/typos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +68,6 @@ module Ameba::Rule::Lint
end

private record Typo,
path : String,
typo : String,
corrections : Array(String),
location : {Int32, Int32},
Expand All @@ -63,34 +82,22 @@ 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
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
if fail_on_error?
raise RuntimeError.new "Could not find `typos` executable"
end
end
end
Expand Down

0 comments on commit 476a956

Please sign in to comment.