Skip to content

Replace parser gem with prism for Witchcraft AST parsing#994

Merged
bbatsov merged 4 commits intotoptal:masterfrom
t6n:change_parser_to_prism
Mar 4, 2026
Merged

Replace parser gem with prism for Witchcraft AST parsing#994
bbatsov merged 4 commits intotoptal:masterfrom
t6n:change_parser_to_prism

Conversation

@t6n
Copy link
Copy Markdown
Contributor

@t6n t6n commented Jan 15, 2026

Description

Motivation

The parser gem only supports Ruby syntax up to version 3.3 and will not receive updates for Ruby 3.4+. Starting with Ruby 3.4, Prism is the
official parser built into Ruby itself and is the recommended tool for all AST-related operations going forward.

This change future-proofs the Witchcraft feature by switching to Prism before parser becomes incompatible with newer Ruby versions.

Changes

  • Replace require 'parser/current' with require 'prism'
  • Use Prism::Translation::ParserCurrent.parse instead of Parser::CurrentRuby.parse
  • Update check_requirements! to check for the Prism constant instead of Parser

Technical Notes

Prism provides a translation layer (Prism::Translation::ParserCurrent) that produces Parser::AST::Node objects identical to those from the parser
gem. This means:

  • All existing AST transformation methods continue to work unchanged
  • unparser remains fully compatible since it receives the same node types
  • No changes required to the AST manipulation logic (replace_lvar, replace_send, replace_self, etc.)

Dependencies

The Witchcraft feature now requires prism instead of parser as a direct dependency. Note that unparser still has parser as a transitive
dependency for its AST node classes, but this is handled internally and does not affect Chewy's public API.

@t6n t6n requested a review from a team as a code owner January 15, 2026 11:31
@bbatsov
Copy link
Copy Markdown
Member

bbatsov commented Feb 25, 2026

Yeah, that's a reasonable idea. I'll review it more carefully soon.

@bbatsov
Copy link
Copy Markdown
Member

bbatsov commented Feb 25, 2026

Master has been updated with CI fixes and compatibility changes (#998) — we now target Ruby 3.2+ and Rails 7.2+. Could you rebase this PR on top of master so CI can run properly? Thanks!

@bbatsov
Copy link
Copy Markdown
Member

bbatsov commented Feb 25, 2026

This is a great change — parser won't support Ruby 3.4+ and Prism is the right path forward.

@bbatsov
Copy link
Copy Markdown
Member

bbatsov commented Feb 25, 2026

One concern: Prism is bundled with Ruby 3.3+ but not available in Ruby 3.2, which we still support (required_ruby_version = '~> 3.2'). This PR would break Witchcraft on 3.2 unless we either:

  1. Add prism as a gem dependency (not ideal for an optional feature)
  2. Try to require 'prism' and fall back to parser if unavailable
  3. Wait until we drop Ruby 3.2 support

What do you think? Option 2 seems the most pragmatic if you want to keep the PR alive now.

@t6n
Copy link
Copy Markdown
Contributor Author

t6n commented Feb 26, 2026

@bbatsov thanks for reviewing. I added a fallback to parser.

@t6n
Copy link
Copy Markdown
Contributor Author

t6n commented Feb 27, 2026

@bbatsov It would be great if this PR could be included in version 8.0

@bbatsov bbatsov merged commit 884b1f3 into toptal:master Mar 4, 2026
7 checks passed
@bbatsov
Copy link
Copy Markdown
Member

bbatsov commented Mar 4, 2026

It's a bit late for Chewy 8.0, but this will land in 8.1. (hopefully soon)

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.

3 participants