Skip to content

Commit

Permalink
Fix an unexpected behavior for Prism::Translation::Parser
Browse files Browse the repository at this point in the history
This PR fixes an unexpected 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
                                                                                        ^^^^^^^
```
  • Loading branch information
koic committed Mar 4, 2024
1 parent 919d682 commit 1aa9c8c
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1607,11 +1607,7 @@ def visit_when_node(node)
builder.when(
token(node.keyword_loc),
visit_all(node.conditions),
if node.then_keyword_loc
token(node.then_keyword_loc)
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"]),
visit(node.statements)
)
end
Expand Down

0 comments on commit 1aa9c8c

Please sign in to comment.