Skip to content

Commit 9b4b99d

Browse files
authored
Ruby: Print Parse Errors at the end of the parse output (#784)
<img width="2304" height="745" alt="CleanShot 2025-11-05 at 21 37 47@2x" src="https://github.com/user-attachments/assets/26e8f94c-2ad4-4063-a056-5e4c673dacf2" />
1 parent 5a4ffda commit 9b4b99d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/herb/cli.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require "optparse"
77

88
class Herb::CLI
9+
include Herb::Colors
10+
911
attr_accessor :json, :silent, :no_interactive, :no_log_file, :no_timing, :local, :escape, :no_escape, :freeze, :debug
1012

1113
def initialize(args)
@@ -27,6 +29,8 @@ def call
2729
puts result.value.to_json
2830
else
2931
puts result.value.inspect
32+
33+
print_error_summary(result.errors) if @command == "parse" && result.respond_to?(:errors) && result.errors.any?
3034
end
3135
end
3236

@@ -223,6 +227,33 @@ def options
223227

224228
private
225229

230+
def print_error_summary(errors)
231+
puts
232+
puts white("#{bold(red("Errors"))} #{dimmed("(#{errors.size} total)")}")
233+
puts
234+
235+
errors.each_with_index do |error, index|
236+
error_type = error.error_name
237+
error_location = format_location_for_copy(error.location)
238+
error_message = error.message
239+
240+
puts white(" #{bold("#{index + 1}.")} #{bold(red(error_type))} #{dimmed("at #{error_location}")}")
241+
puts white(" #{error_message}")
242+
puts unless index == errors.size - 1
243+
end
244+
end
245+
246+
def format_location_for_copy(location)
247+
line = location.start.line
248+
column = location.start.column
249+
250+
if @file
251+
"#{@file}:#{line}:#{column}"
252+
else
253+
"#{line}:#{column}"
254+
end
255+
end
256+
226257
def compile_template
227258
require_relative "engine"
228259

0 commit comments

Comments
 (0)