Skip to content

Commit

Permalink
Merge pull request #1329 from ksss/e-detailed_message
Browse files Browse the repository at this point in the history
Print detailed_message on parse command
  • Loading branch information
soutaro committed May 16, 2023
2 parents a7a19e8 + 5f06e44 commit ebb5257
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def run_parse(args, options)
Parser.public_send(parse_method, buf, require_eof: true)
end
rescue RBS::ParsingError => ex
stdout.puts ex.message
stdout.print ex.detailed_message(highlight: true)
syntax_error = true
end

Expand Down
7 changes: 6 additions & 1 deletion lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class DefinitionError < BaseError; end

module DetailedMessageable
def detailed_message(highlight: false, **)
msg = super
msg = if Exception.method_defined?(:detailed_message)
super
else
# Failback to `#message` in Ruby 3.1 or earlier
"#{message} (#{self.class.name})"
end

# Support only one line
return msg unless location.start_line == location.end_line
Expand Down
33 changes: 24 additions & 9 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,15 @@ def foo: () -> void
assert_raises(SystemExit) { cli.run(%W(parse #{dir})) }

assert_equal [
"#{dir}/semantics_error.rbs:2:10...2:11: Syntax error: expected a token `pCOLON`, token=`.` (pDOT)",
"#{dir}/syntax_error.rbs:3:0...3:3: Syntax error: unexpected token for simple type, token=`end` (kEND)",
], stdout.string.split("\n").sort
"#{dir}/semantics_error.rbs:2:10...2:11: Syntax error: expected a token `pCOLON`, token=`.` (pDOT) (RBS::ParsingError)",
"",
" def self.foo: () -> void",
" ^",
"#{dir}/syntax_error.rbs:3:0...3:3: Syntax error: unexpected token for simple type, token=`end` (kEND) (RBS::ParsingError)",
"",
" end",
" ^^^"
], stdout.string.gsub(/\e\[.*?m/, '').split("\n")
end
end
end
Expand All @@ -495,8 +501,11 @@ def test_parse_e

assert_raises(SystemExit) { cli.run(['parse', '-e', 'class C en']) }
assert_equal [
"-e:1:8...1:10: Syntax error: unexpected token for class/module declaration member, token=`en` (tLIDENT)"
], stdout.string.split("\n").sort
"-e:1:8...1:10: Syntax error: unexpected token for class/module declaration member, token=`en` (tLIDENT) (RBS::ParsingError)",
"",
" class C en",
" ^^"
], stdout.string.gsub(/\e\[.*?m/, '').split("\n")
end
end

Expand All @@ -507,8 +516,11 @@ def test_parse_type

assert_raises(SystemExit) { cli.run(['parse', '--type', '-e', '?']) }
assert_equal [
"-e:1:0...1:1: Syntax error: unexpected token for simple type, token=`?` (pQUESTION)",
], stdout.string.split("\n").sort
"-e:1:0...1:1: Syntax error: unexpected token for simple type, token=`?` (pQUESTION) (RBS::ParsingError)",
"",
" ?",
" ^"
], stdout.string.gsub(/\e\[.*?m/, '').split("\n")
end
end

Expand All @@ -519,8 +531,11 @@ def test_parse_method_type

assert_raises(SystemExit) { cli.run(['parse', '--method-type', '-e', '()']) }
assert_equal [
"-e:1:2...1:3: Syntax error: expected a token `pARROW`, token=`` (pEOF)",
], stdout.string.split("\n").sort
"-e:1:2...1:3: Syntax error: expected a token `pARROW`, token=`` (pEOF) (RBS::ParsingError)",
"",
" ()",
" ^"
], stdout.string.gsub(/\e\[.*?m/, '').split("\n")
end
end

Expand Down

0 comments on commit ebb5257

Please sign in to comment.