From e438697d4b4c4fb33715647c984b657a0d5161db Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 8 Dec 2021 13:53:32 -0500 Subject: [PATCH 1/3] Better handling for formatting files with errors --- CHANGELOG.md | 4 ++++ lib/syntax_tree/cli.rb | 50 +++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7229020e..832b945f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] +### Added + +- Better handling for formatting files with errors. + ## [1.0.0] ### Added diff --git a/lib/syntax_tree/cli.rb b/lib/syntax_tree/cli.rb index b57b84c4..7e1572d7 100644 --- a/lib/syntax_tree/cli.rb +++ b/lib/syntax_tree/cli.rb @@ -126,6 +126,9 @@ def run(filepath, source) delta = ((Time.now - start) * 1000).round puts "\r#{color} #{delta}ms" + rescue + puts "\r#{filepath}" + raise end end @@ -177,25 +180,12 @@ def run(argv) action.run(filepath, source) rescue ParseError => error warn("Error: #{error.message}") - lines = source.lines - - maximum = [error.lineno + 3, lines.length].min - digits = Math.log10(maximum).ceil - - ([error.lineno - 3, 0].max...maximum).each do |line_index| - line_number = line_index + 1 - if line_number == error.lineno - part1 = Color.red(">") - part2 = Color.gray("%#{digits}d |" % line_number) - warn("#{part1} #{part2} #{lines[line_index]}") - - part3 = Color.gray(" %#{digits}s |" % " ") - warn("#{part3} #{" " * error.column}#{Color.red("^")}") - else - prefix = Color.gray(" %#{digits}d |" % line_number) - warn("#{prefix} #{lines[line_index]}") - end + if error.lineno + highlight_error(error, source) + else + warn(error.message) + warn(error.backtrace) end errored = true @@ -232,6 +222,30 @@ def source_for(filepath) File.read(filepath, encoding: encoding) end + + # Highlights a snippet from a source and parse error. + def highlight_error(error, source) + lines = source.lines + + maximum = [error.lineno + 3, lines.length].min + digits = Math.log10(maximum).ceil + + ([error.lineno - 3, 0].max...maximum).each do |line_index| + line_number = line_index + 1 + + if line_number == error.lineno + part1 = Color.red(">") + part2 = Color.gray("%#{digits}d |" % line_number) + warn("#{part1} #{part2} #{lines[line_index]}") + + part3 = Color.gray(" %#{digits}s |" % " ") + warn("#{part3} #{" " * error.column}#{Color.red("^")}") + else + prefix = Color.gray(" %#{digits}d |" % line_number) + warn("#{prefix} #{lines[line_index]}") + end + end + end end end end From 30ebbf5c3f6e73a13ca38ff7eaf2d9f561f9cdc3 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 8 Dec 2021 14:06:25 -0500 Subject: [PATCH 2/3] Colorize the output snippet using IRB --- CHANGELOG.md | 1 + lib/syntax_tree/cli.rb | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 832b945f..ac2c4da3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ### Added - Better handling for formatting files with errors. +- Colorize the output snippet using IRB. ## [1.0.0] diff --git a/lib/syntax_tree/cli.rb b/lib/syntax_tree/cli.rb index 7e1572d7..eb68214d 100644 --- a/lib/syntax_tree/cli.rb +++ b/lib/syntax_tree/cli.rb @@ -236,16 +236,22 @@ def highlight_error(error, source) if line_number == error.lineno part1 = Color.red(">") part2 = Color.gray("%#{digits}d |" % line_number) - warn("#{part1} #{part2} #{lines[line_index]}") + warn("#{part1} #{part2} #{colorize_line(lines[line_index])}") part3 = Color.gray(" %#{digits}s |" % " ") warn("#{part3} #{" " * error.column}#{Color.red("^")}") else prefix = Color.gray(" %#{digits}d |" % line_number) - warn("#{prefix} #{lines[line_index]}") + warn("#{prefix} #{colorize_line(lines[line_index])}") end end end + + # Take a line of Ruby source and colorize the output. + def colorize_line(line) + require "irb" + IRB::Color.colorize_code(line, complete: false, ignore_error: true) + end end end end From cb72445831f8362cc0e5c1c65e4e599c3274697e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 8 Dec 2021 14:10:27 -0500 Subject: [PATCH 3/3] Bump to v1.1.0 --- CHANGELOG.md | 4 +++- Gemfile.lock | 2 +- lib/syntax_tree/version.rb | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac2c4da3..33ab95d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] +## [1.1.0] - 2021-12-08 + ### Added - Better handling for formatting files with errors. - Colorize the output snippet using IRB. -## [1.0.0] +## [1.0.0] - 2021-12-08 ### Added diff --git a/Gemfile.lock b/Gemfile.lock index 4094a6c8..0059156b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - syntax_tree (1.0.0) + syntax_tree (1.1.0) GEM remote: https://rubygems.org/ diff --git a/lib/syntax_tree/version.rb b/lib/syntax_tree/version.rb index 166791b6..a0428314 100644 --- a/lib/syntax_tree/version.rb +++ b/lib/syntax_tree/version.rb @@ -3,5 +3,5 @@ require "ripper" class SyntaxTree < Ripper - VERSION = "1.0.0" + VERSION = "1.1.0" end