-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix an incompatible behavior for Prism::Translation::Parser
#2552
Conversation
@koic I just added that to the |
Oops, I forgot to recompile. But, after compiling, the following issues occur. ExpectedIt returns the range of the semicolon even if there is a space before the semicolon: $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
#<Parser::Source::Range (string) 15...16>
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
";"
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
#<Parser::Source::Range (string) 16...17>
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
";" ActualIt returns $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
#<Parser::Source::Range (string) 15...16>
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
";"
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
nil
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
-e:1:in `<main>': undefined method `source' for nil (NoMethodError)
p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source
^^^^^^^ This PR also fixes the above issues. I've updated the PR description as well. |
1f7e2eb
to
1aa9c8c
Compare
Prism::Translation::Parser
Prism::Translation::Parser
1aa9c8c
to
098b9f5
Compare
else | ||
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || (node.conditions.last.location.end_offset + 1), [";"]) | ||
end, | ||
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || node.statements&.then_keyword_loc, [";", "then"]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This is the same solution as the following.
https://github.com/ruby/prism/blob/v0.24.0/lib/prism/translation/parser/compiler.rb#L828
098b9f5
to
a181fa9
Compare
Prism::Translation::Parser
Prism::Translation::Parser
This PR fixes the following incompatible behavior for `Prism::Translation::Parser` when using `case`/`when`/`end` with `;` expression. ## Expected It returns the range of the semicolon even if there is a space before the semicolon: ```console $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] #<Parser::Source::Range (string) 15...16> $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] ";" $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] #<Parser::Source::Range (string) 16...17> $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] ";" ``` ## Actual It returns `nil` if there is a space before the semicolon: ```console $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] #<Parser::Source::Range (string) 15...16> $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] ";" $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] nil $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] -e:1:in `<main>': undefined method `source' for nil (NoMethodError) p Prism::Translation::Parser33.parse("case foo when x ; end").children.to_a[1].loc.begin.source ^^^^^^^ ```
a181fa9
to
9625805
Compare
This PR fixes the following incompatible behavior for
Prism::Translation::Parser
when usingcase
/when
/end
with;
expression.Expected
It returns the range of the semicolon even if there is a space before the semicolon:
Actual
It returns
nil
if there is a space before the semicolon: