diff options
author | Koichi ITO <koic.ito@gmail.com> | 2024-02-29 22:31:00 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-02-29 16:06:40 +0000 |
commit | f8dd2342bf99738e2a8d7a4fa9e31bff186e0d5a (patch) | |
tree | 19447b83ab570ddc62cf11ad4a7404e3a91567bc /lib/prism/translation/parser | |
parent | 8a918b456c6fe7449dbffc8bfdc321a2969aea58 (diff) |
[ruby/prism] Fix an incorrect parsing for `Prism::Translation::Parser`
This PR fixes an incorrect parsing for `Prism::Translation::Parser`
when one-line pattern mathing with Ruby 2.7 runtime.
## Expected
Parsing should be done based on the specified Ruby parsing version,
independent of the Ruby runtime version. When parsing for Ruby 3.3,
it should return `:match_pattern_p` node:
```console
$ ruby -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("foo in bar")'
ruby 3.0.6p216 (2023-03-30 revision https://github.com/ruby/prism/commit/23a532679b) [x86_64-darwin19]
s(:match_pattern_p,
s(:send, nil, :foo),
s(:match_var, :bar))
```
## Actual
When parsing with Ruby 2.7 runtime, `match_pattern` node is returned,
even though it is expected to parse for Ruby 3.3:
```console
$ ruby -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("foo in bar")'
ruby 2.7.8p225 (2023-03-30 revision https://github.com/ruby/prism/commit/1f4d455848) [x86_64-darwin19]
s(:match_pattern,
s(:send, nil, :foo),
s(:match_var, :bar))
```
The cause was the use of `RUBY_VERSION` for condition logic,
which made it dependent on runtime Ruby version.
`Prism::Translation::Parser` supports parsing for Ruby 3.3+.
Therefore, the condition for parsing Ruby 2.7, which is not supported, is being removed.
## Background
Found due to incompatibility with RuboCop's `Layout/SpaceAroundKeyword` and `Style/TernaryParentheses` cops.
https://github.com/ruby/prism/commit/e752e251d2
Diffstat (limited to 'lib/prism/translation/parser')
-rw-r--r-- | lib/prism/translation/parser/compiler.rb | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index ab6cea9484..0025e4697d 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1070,22 +1070,12 @@ module Prism # foo in bar # ^^^^^^^^^^ - if RUBY_VERSION >= "3.0" - def visit_match_predicate_node(node) - builder.match_pattern_p( - visit(node.value), - token(node.operator_loc), - within_pattern { |compiler| node.pattern.accept(compiler) } - ) - end - else - def visit_match_predicate_node(node) - builder.match_pattern( - visit(node.value), - token(node.operator_loc), - within_pattern { |compiler| node.pattern.accept(compiler) } - ) - end + def visit_match_predicate_node(node) + builder.match_pattern_p( + visit(node.value), + token(node.operator_loc), + within_pattern { |compiler| node.pattern.accept(compiler) } + ) end # foo => bar |