From b812f7a717167955ed419f5cd3e7acb763fa56e4 Mon Sep 17 00:00:00 2001 From: ksss Date: Sat, 13 May 2023 23:46:45 +0900 Subject: [PATCH 1/3] Print detailed_message on parse command --- lib/rbs/cli.rb | 2 +- test/rbs/cli_test.rb | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/rbs/cli.rb b/lib/rbs/cli.rb index bd00f033a..d1892a860 100644 --- a/lib/rbs/cli.rb +++ b/lib/rbs/cli.rb @@ -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 diff --git a/test/rbs/cli_test.rb b/test/rbs/cli_test.rb index 756668d3e..c80f6cafd 100644 --- a/test/rbs/cli_test.rb +++ b/test/rbs/cli_test.rb @@ -481,9 +481,16 @@ 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 @@ -495,8 +502,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 @@ -507,8 +517,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 @@ -519,8 +532,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 From c7345d719b9166a266bf003b41be5d2eefc5a8a1 Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 14 May 2023 16:14:30 +0900 Subject: [PATCH 2/3] Support Ruby 3.1 or earlier --- lib/rbs/errors.rb | 7 ++++++- test/rbs/cli_test.rb | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/rbs/errors.rb b/lib/rbs/errors.rb index 66203178e..9876c5fef 100644 --- a/lib/rbs/errors.rb +++ b/lib/rbs/errors.rb @@ -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 + end # Support only one line return msg unless location.start_line == location.end_line diff --git a/test/rbs/cli_test.rb b/test/rbs/cli_test.rb index c80f6cafd..2c28a4f52 100644 --- a/test/rbs/cli_test.rb +++ b/test/rbs/cli_test.rb @@ -485,7 +485,6 @@ def foo: () -> void "", " 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", From 5f06e44aad7839923d73aeb8df4e5f54e6e382f1 Mon Sep 17 00:00:00 2001 From: ksss Date: Sun, 14 May 2023 21:42:17 +0900 Subject: [PATCH 3/3] Imitate original behavior --- lib/rbs/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rbs/errors.rb b/lib/rbs/errors.rb index 9876c5fef..466f6a7d4 100644 --- a/lib/rbs/errors.rb +++ b/lib/rbs/errors.rb @@ -26,7 +26,7 @@ def detailed_message(highlight: false, **) super else # Failback to `#message` in Ruby 3.1 or earlier - message + "#{message} (#{self.class.name})" end # Support only one line