🕌

[Feature #21573] よりシンプルなエラーメッセージを出力できるようにしたいというチケット

に公開

[Feature #21573] Simpler syntax errors

  • 現状はシンタックスエラーが発生した場合に発生原因などを特定できるようにするためにエラーメッセージにコンテキスト情報が追加されている
1 +x 2 do
do
__END__
output:
test.rb: --> test.rb

Unmatched keyword, missing `end' ?

> 1  1 +x 2 do
> 2  do

test.rb:1: syntax errors found (SyntaxError)
> 1 | 1 +x 2 do
    |      ^ unexpected integer, expecting end-of-input
    |        ^~ unexpected 'do', ignoring it
    |        ^~ unexpected 'do', expecting end-of-input
> 2 | do
    | ^~ unexpected 'do', ignoring it
  • これを以下のようなシンプルなエラーメッセージで出力できるようにしたいという内容
    • 環境変数 SIMPLE_ERRORS=1 になっていると以下のようなメッセージに変わる想定
1 +x 2 do
do
__END__
output:
./miniruby: -:1: syntax errors found (SyntaxError)
-:1: unexpected integer, expecting end-of-input
-:1: unexpected 'do', expecting end-of-input
-:1: unexpected 'do', ignoring it
-:2: unexpected 'do', ignoring it
source = <<~EOS
1 +x 2 do
do
EOS

require "prism"

pp Prism.parse(source).errors
GitHubで編集を提案

Discussion