From 6631ee42aa018576119fc97e1165759511d667c4 Mon Sep 17 00:00:00 2001 From: Stefan Thiemann Date: Thu, 15 Jan 2026 12:15:12 +0100 Subject: [PATCH 1/3] Replace parser gem with prism for Witchcraft AST parsing --- lib/chewy/index/witchcraft.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/chewy/index/witchcraft.rb b/lib/chewy/index/witchcraft.rb index a6b8dc0a7..21faa905f 100644 --- a/lib/chewy/index/witchcraft.rb +++ b/lib/chewy/index/witchcraft.rb @@ -1,6 +1,6 @@ begin require 'method_source' - require 'parser/current' + require 'prism' require 'unparser' rescue LoadError nil @@ -24,7 +24,7 @@ def witchcraft! def check_requirements! messages = [] messages << "MethodSource gem is required for the Witchcraft, please add `gem 'method_source'` to your Gemfile" unless Proc.method_defined?(:source) - messages << "Parser gem is required for the Witchcraft, please add `gem 'parser'` to your Gemfile" unless '::Parser'.safe_constantize + messages << "Prism gem is required for the Witchcraft, please add `gem 'prism'` to your Gemfile" unless '::Prism'.safe_constantize messages << "Unparser gem is required for the Witchcraft, please add `gem 'unparser'` to your Gemfile" unless '::Unparser'.safe_constantize messages = messages.join("\n") @@ -164,7 +164,7 @@ def proc_fields_for(parent, nesting) end def source_for(proc, nesting) - ast = Parser::CurrentRuby.parse(proc.source) + ast = Prism::Translation::ParserCurrent.parse(proc.source) lambdas = exctract_lambdas(ast) raise "No lambdas found, try to reformat your code:\n`#{proc.source}`" unless lambdas From 1fe964298323c6f104fe576229e264b4eaa14ff7 Mon Sep 17 00:00:00 2001 From: Stefan Thiemann Date: Thu, 26 Feb 2026 10:36:57 +0100 Subject: [PATCH 2/3] use parser as fallback --- lib/chewy/index/witchcraft.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/chewy/index/witchcraft.rb b/lib/chewy/index/witchcraft.rb index 21faa905f..a5e8cc693 100644 --- a/lib/chewy/index/witchcraft.rb +++ b/lib/chewy/index/witchcraft.rb @@ -1,6 +1,10 @@ begin require 'method_source' - require 'prism' + begin + require 'prism' + rescue LoadError + require 'parser/current' + end require 'unparser' rescue LoadError nil @@ -24,7 +28,11 @@ def witchcraft! def check_requirements! messages = [] messages << "MethodSource gem is required for the Witchcraft, please add `gem 'method_source'` to your Gemfile" unless Proc.method_defined?(:source) - messages << "Prism gem is required for the Witchcraft, please add `gem 'prism'` to your Gemfile" unless '::Prism'.safe_constantize + if RUBY_VERSION >= '3.3' + messages << "Prism gem is required for the Witchcraft, please add `gem 'prism'` to your Gemfile" unless '::Prism'.safe_constantize + else + messages << "Parser gem is required for the Witchcraft, please add `gem 'parser'` to your Gemfile" unless '::Parser'.safe_constantize + end messages << "Unparser gem is required for the Witchcraft, please add `gem 'unparser'` to your Gemfile" unless '::Unparser'.safe_constantize messages = messages.join("\n") @@ -164,7 +172,11 @@ def proc_fields_for(parent, nesting) end def source_for(proc, nesting) - ast = Prism::Translation::ParserCurrent.parse(proc.source) + ast = if defined?(Prism) + Prism::Translation::ParserCurrent.parse(proc.source) + else + Parser::CurrentRuby.parse(proc.source) + end lambdas = exctract_lambdas(ast) raise "No lambdas found, try to reformat your code:\n`#{proc.source}`" unless lambdas From 8ea3f581463dba4ec378dff22158340b4d49d1dd Mon Sep 17 00:00:00 2001 From: Stefan Thiemann Date: Fri, 27 Feb 2026 07:35:49 +0100 Subject: [PATCH 3/3] fix: rubocop --- lib/chewy/index/witchcraft.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/chewy/index/witchcraft.rb b/lib/chewy/index/witchcraft.rb index a5e8cc693..be6112767 100644 --- a/lib/chewy/index/witchcraft.rb +++ b/lib/chewy/index/witchcraft.rb @@ -172,12 +172,7 @@ def proc_fields_for(parent, nesting) end def source_for(proc, nesting) - ast = if defined?(Prism) - Prism::Translation::ParserCurrent.parse(proc.source) - else - Parser::CurrentRuby.parse(proc.source) - end - lambdas = exctract_lambdas(ast) + lambdas = exctract_lambdas(ast_from_proc(proc)) raise "No lambdas found, try to reformat your code:\n`#{proc.source}`" unless lambdas @@ -204,6 +199,14 @@ def source_for(proc, nesting) Unparser.unparse(source) end + def ast_from_proc(proc) + if defined?(Prism) + Prism::Translation::ParserCurrent.parse(proc.source) + else + Parser::CurrentRuby.parse(proc.source) + end + end + def exctract_lambdas(node) return unless node.is_a?(Parser::AST::Node)