Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Prism as a Ruby parser #446

Merged
merged 1 commit into from Mar 5, 2024
Merged

Support Prism as a Ruby parser #446

merged 1 commit into from Mar 5, 2024

Conversation

koic
Copy link
Member

@koic koic commented Mar 4, 2024

Follow up rubocop/rubocop-ast#277

In Prism (Prism::Translation::Parser), match-with-lvasgn can be distinctly differentiated.

It is unclear whether to conform to the current behavior of the Parser gem, but initially, def_node_matcher has been updated to accept the following incompatibilities for Performance/EndWith, Performance/StringInclude, and Performance/StartWith cops to ensure it works with Prism 0.24.0 as well.

Parser gem

Returns an match_with_lvasgn node:

$ 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))

Returns an match_with_lvasgn node:

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

This lvar-injecting feature appears to have not been supported by Parser gem for a long time: whitequark/parser#69 (comment)

Prism

Returns an send node:

$ bundle exec ruby -Ilib -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))

Returns an match_with_lvasgn node:

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

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the support_prism branch 3 times, most recently from ea2bc14 to 231cea8 Compare March 4, 2024 16:11
Follow up rubocop/rubocop-ast#277

In Prism (`Prism::Translation::Parser`), `match-with-lvasgn` can be distinctly differentiated.

It is unclear whether to conform to the current behavior of the Parser gem, but initially,
`def_node_matcher` has been updated to accept the following incompatibilities for
`Performance/EndWith`, `Performance/StringInclude`, and `Performance/StartWith` cops
to ensure it works with Prism 0.24.0 as well.

## 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))
```

Returns an `match_with_lvasgn` node:

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

This lvar-injecting feature appears to have not been supported by Parser gem for a long time:
whitequark/parser#69 (comment)

## Prism

Returns an `send` node:

```console
$ bundle exec ruby -Ilib -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))
```

Returns an `match_with_lvasgn` node:

```console
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("/(?<foo>)foo/ =~ bar")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
s(:match_with_lvasgn,
  s(:regexp,
    s(:str, "(?<foo>)foo"),
    s(:regopt)),
  s(:send, nil, :bar))
```
@koic koic merged commit d4d1875 into rubocop:master Mar 5, 2024
14 checks passed
@koic koic deleted the support_prism branch March 5, 2024 16:14
@@ -36,7 +41,7 @@ end
desc 'Run RuboCop over itself'
RuboCop::RakeTask.new(:internal_investigation)

task default: %i[documentation_syntax_check spec internal_investigation]
task default: %i[documentation_syntax_check spec prism_spec internal_investigation]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t prism_spec effectively just a duplicate of rspec task when executed from non-prism CI jobs, @koic ? Same in rubocop-rails.
The suggestion is to run the default rake task in the prism ci job, and remove prism_spec. Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, efforts are made to ensure that prism_spec passes locally. If it becomes too troublesome, it might be removed, but currently, having prism_spec operational is more convenient. So, I'd like to quickly identify issues related to Prism. Cases where tests cannot pass with Prism can be handled with unsupported_on: :prism or broken_on: :prism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants