Skip to content

Commit

Permalink
Fix an error for Prism::Translation::Parser
Browse files Browse the repository at this point in the history
This PR fixes an error for `Prism::Translation::Parser` when using `case`/`when`/`end` without `then` expression.

```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.source'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
/Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1610:in `visit_when_node':
undefined method `then_keyword_loc' for an instance of Prism::WhenNode (NoMethodError)

            if node.then_keyword_loc
                   ^^^^^^^^^^^^^^^^^
        from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:18060:in `accept'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `block in visit_all'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `map'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `visit_all'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:361:in `visit_case_node'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:3434:in `accept'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `block in visit_all'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `map'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `visit_all'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1476:in `visit_statements_node'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:16964:in `accept'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:30:in `visit'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1300:in `visit_program_node'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:14988:in `accept'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:159:in `build_ast'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:48:in `parse'
        from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse'
        from -e:1:in `<main>'
```
  • Loading branch information
koic committed Mar 4, 2024
1 parent 919d682 commit 1f7e2eb
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 1f7e2eb

Please sign in to comment.