Commit graph

17 commits

Author SHA1 Message Date
Kevin Newton
03a73fdc3d [ruby/prism] Add then keyword loc to when nodes
e1e613df16
2024-03-04 16:40:37 +00:00
Kevin Newton
85fe8b6b9c [ruby/prism] Update lib/prism/translation/parser/compiler.rb
dccfd83bc4
2024-03-04 16:26:47 +00:00
Koichi ITO
a03f92923b [ruby/prism] Fix incompatibility AST for regexp match in Prism::Translation::Parser
This PR fixes the following incompatibility AST for regexp match between Parser gem and Prism:

## Parser gem

Returns an `match_with_lvasgn` node:

```console
$ bundle exec ruby -rparser/ruby33 -ve 'p Parser::Ruby33.parse("/foo/ =~ bar")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:match_with_lvasgn,
  s(:regexp,
    s(:str, "foo"),
    s(:regopt)),
  s(:send, nil, :bar))
```

## Prism (`Prism::Translation::Parser`)

### Before

Returns an `send` node:

```console
$ bundle exec ruby -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("/foo/ =~ bar")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:send,
  s(:regexp,
    s(:str, "foo"),
    s(:regopt)), :=~,
  s(:send, nil, :bar))
```

### After

Returns an `match_with_lvasgn` node:

```console
$ bundle exec ruby -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("/foo/ =~ bar")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:match_with_lvasgn,
  s(:regexp,
    s(:str, "foo"),
    s(:regopt)),
  s(:send, nil, :bar))
```

## Background

Found due to incompatibility with RuboCop's `Performance/EndWith`, `Performance/StringInclude,
and `Performance/StartWith` cops.

## Note

This is the incompatibility when the receiver is a regular expression literal and `=~` is used.
Based on the node name `:match_with_lvasgn`, it appears that Prism's AST becomes more accurate
in cases like `visit_match_write_node` only.

However, as shown in the background, the current behavior of Parser gem is not like this.
Considering compatibility with the published AST of Parser gem, the AST incompatibility will be addressed.

This lvar-injecting feature appears to have not been supported by Parser gem for a long time:
https://github.com/whitequark/parser/issues/69#issuecomment-19506391

There seems to be no indication that it will be supported.

This PR prioritizes AST compatibility between the Parser gem and Prism.
However, it is unclear whether this is the best approach.

dff4abb170
2024-03-04 16:26:46 +00:00
Koichi ITO
f8dd2342bf [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 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 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.

e752e251d2
2024-02-29 16:06:40 +00:00
Kevin Newton
1ce3d9acbf [ruby/prism] Fix xstring heredoc parser translator
4e0f703975
2024-02-26 14:48:17 +00:00
Kevin Newton
99d0f687fc [ruby/prism] Fix parser translator for pinned expression
eeae07193b
2024-02-26 14:35:00 +00:00
Kevin Newton
af3145bb24 [ruby/prism] Handle negated numeric in parser translation
5877a95be4
2024-02-26 14:26:53 +00:00
Kevin Newton
13301587cf [ruby/prism] Fix block_pass for []=
bf79206220
2024-02-16 20:41:52 +00:00
Kevin Newton
14a7277da1 [ruby/prism] Speed up creating Ruby AST
When creating the Ruby AST, we were previously allocating Location
objects for every node and every inner location. Instead, this
commit changes it to pack both the start offset and length into a
single u64 and pass that into the nodes. Then, when the locations
are requested via a reader method, we lazily allocate the Location
objects.

de203dca83

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2024-02-15 20:39:50 +00:00
Kevin Newton
aad3c36bdf [ruby/prism] Support for Ruby 2.7
1a15b70a8e
2024-02-07 16:54:34 +00:00
Kevin Newton
0b5be2f9e9 Sync to latest prism 2024-02-05 11:07:07 -05:00
Kevin Newton
d2f004cf6a [ruby/prism] Fix hash pairs in patterns
b7ab29daa0
2024-02-02 21:16:04 +00:00
Kevin Newton
420a6349ec [ruby/prism] Small fixes for the parser translator
4327051c86
2024-02-02 13:36:23 -05:00
Kevin Newton
731367d0ab [ruby/prism] Fix up CI
224ea85565
2024-01-30 18:45:19 +00:00
Kevin Newton
e050097beb [ruby/prism] Raise diagnostics for parser
102b4a16f5
2024-01-29 16:09:47 +00:00
Kevin Newton
e256d44f98 [ruby/prism] Handle implicit rest in array pattern for parser gem
d3722d6660
2024-01-28 01:10:47 +00:00
Kevin Newton
f12ebe1188 [ruby/prism] Add parser translation
8cdec8070c
2024-01-27 19:59:42 +00:00