Commit graph

10 commits

Author SHA1 Message Date
Kevin Newton
5856ea3fd1 [ruby/prism] Fix up some minor parser incompatibilities
c6c771d1fa
2024-03-04 14:39:52 +00:00
Noah Gibbs
551f64745f [ruby/prism] Allow skipping warnings as needed, and pass a reason through to Parser::Diagnostic
6a84a3c9fb
2024-02-21 22:38:48 +00:00
Noah Gibbs
45ae69e37e [ruby/prism] Update lib/prism/translation/parser.rb
c3cc282343

Co-authored-by: Kevin Newton <kddnewton@gmail.com>
2024-02-21 22:38:48 +00:00
Noah Gibbs
e3b9eec349 [ruby/prism] Translation::Parser should process warnings, not just errors
ea7e400f85
2024-02-21 22:38:47 +00:00
Koichi ITO
be82755d4a [ruby/prism] Support multi-versioning for Prism::Translation::Parser
## Summary

Fixes https://github.com/ruby/prism/pull/2356.

I'm working on integrating Prism into RuboCop.

This PR introduces `Prism::Translation::Parser33` and `Prism::Translation::Parser34`, named
in accordance with the following comments.
https://github.com/rubocop/rubocop/issues/12600#issuecomment-1932707748

Currently, `Prism::Translation::Parser` always operates in Ruby 3.4 mode.
This means it will not parse as Ruby 3.3 even if `TargetRubyVersion: 80_82_73_83_77.33` is specified.

Therefore, the `it` introduced in Ruby 3.4 is parsed incompatibly with Ruby 3.3. In Ruby 3.3,
the expected name for an `lvar` is `:it`, not `:"0it"`.

### Expected AST

The following is an expected AST when parsing Ruby 3.3 code:

```console
$ bundle exec ruby -rprism -rprism/translation/parser33 -ve "p Prism::Translation::Parser33.parse('items.map { it.do_something }')"
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:block,
  s(:send,
    s(:send, nil, :items), :map),
  s(:args),
  s(:send,
    s(:send, nil, :it), :do_something))
```

### Actual AST

The following is an actual AST when parsing Ruby 3.3 code:

```console
$ ruby -rprism -ve "p Prism::Translation::Parser.parse('items.map { it.do_something }')"
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:block,
  s(:send,
    s(:send, nil, :items), :map),
  s(:args),
  s(:send,
    s(:lvar, :"0it"), :do_something))
```

`Prism::Translation::Parser33` and `Prism::Translation::Parser34` aim to correspond to Ruby 3.3 and Ruby 3.4, respectively.

And, The hack of specifying `TargetRubyVersion: 80_82_73_83_77.33` is expected to become unnecessary in the future,
but the behavior will be maintained until RuboCop's support is finalized:
https://github.com/rubocop/rubocop/issues/12600#issuecomment-1933657732

## Additional Information

A private method named `convert_for_prism` is prepared to convert the `version` from Parser to the `version` expected by Prism.
For example, a Parser-compatible value is `3.3`, whereas Prism expects `"3.3.0"`.

`Parser#version` is not used in RuboCop, but it's unclear how it is utilized in other libraries that rely on the Parser gem.

Therefore, logic to maintain compatibility between Parser and Prism is implemented.

62d3991e22
2024-02-15 20:21:13 +00:00
Kevin Newton
cf1cd215c0 [ruby/prism] Significantly faster offset cache for parser
8cd92eef79
2024-02-09 16:27:05 +00:00
Kevin Newton
164c18af7b [ruby/prism] Correct handle recover parameters on tokenize for parser translation
63979de21d
2024-02-07 14:44:36 +00:00
Kevin Newton
420a6349ec [ruby/prism] Small fixes for the parser translator
4327051c86
2024-02-02 13:36:23 -05:00
Kevin Newton
e050097beb [ruby/prism] Raise diagnostics for parser
102b4a16f5
2024-01-29 16:09:47 +00:00
Kevin Newton
f12ebe1188 [ruby/prism] Add parser translation
8cdec8070c
2024-01-27 19:59:42 +00:00