Skip to content

Instantly share code, notes, and snippets.

@rzane
Created March 27, 2025 12:06
Show Gist options
  • Save rzane/5c11d14f7d1cbc9787c2df28e77dbc65 to your computer and use it in GitHub Desktop.
Save rzane/5c11d14f7d1cbc9787c2df28e77dbc65 to your computer and use it in GitHub Desktop.
Capture and ignore all typescript errors
# Usage:
# $ npm install -g @aivenio/tsc-output-parser
# $ ruby ts-expect-error.rb
require 'json'
def fix_errors
puts "Running the TypeScript compiler to find errors..."
errors = JSON.parse(`$(yarn bin tsc) --noEmit | tsc-output-parser`)
changes = Hash.new { |h, k| h[k] = Set.new }
puts "Correcting the errors..."
errors.each do |error|
path = error.dig('value', 'path', 'value')
line = error.dig('value', 'cursor', 'value', 'line')
next if changes[path].include?(line)
yield path, line, changes[path]
changes[path].add(line)
end
end
fix_errors do |path, line, changes|
lines = File.readlines(path)
lines.insert(line + changes.length - 1, "// @ts-expect-error FIXME\n")
File.write(path, lines.join)
end
# Convert JS comments to JSX comments
fix_errors do |path, line, _changes|
lines = File.readlines(path)
comment = lines[line - 2]
if comment && comment.start_with?("// @ts-expect-error")
lines[line - 2] = "{/* @ts-expect-error FIXME */}\n"
File.write(path, lines.join)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment