From 70c72cc8c0eb7c57385a45026f8e1fe311d23a7f Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Sun, 26 Apr 2026 10:03:53 -0400 Subject: [PATCH] Add ability to configure parser This provides the ability to set the configured parser, switching between ripper and prism when prism is available. --- lib/power_assert/configuration.rb | 16 ++++++++++++++-- lib/power_assert/context.rb | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/power_assert/configuration.rb b/lib/power_assert/configuration.rb index 825c2d2..0052fa7 100644 --- a/lib/power_assert/configuration.rb +++ b/lib/power_assert/configuration.rb @@ -1,7 +1,7 @@ module PowerAssert class << self def configuration - @configuration ||= Configuration[false, true, false, :p] + @configuration ||= Configuration[false, true, false, :p, :ripper] end def configure @@ -9,7 +9,7 @@ def configure end end - class Configuration < Struct.new(:lazy_inspection, :_redefinition, :colorize_message, :inspector) + class Configuration < Struct.new(:lazy_inspection, :_redefinition, :colorize_message, :inspector, :parser) def colorize_message=(bool) if bool require 'irb/color' @@ -41,6 +41,18 @@ def inspector=(inspector) end super end + + def parser=(parser) + case parser + when :prism + require 'prism' + when :ripper + require 'ripper' + else + raise ArgumentError, "unknown parser: #{parser}" + end + super + end end private_constant :Configuration end diff --git a/lib/power_assert/context.rb b/lib/power_assert/context.rb index cddc442..b383303 100644 --- a/lib/power_assert/context.rb +++ b/lib/power_assert/context.rb @@ -30,7 +30,8 @@ def initialize(assertion_proc_or_source, assertion_method, source_binding) line ||= File.open(path) {|fp| fp.each_line.drop(lineno - 1).first } end if line - @parser = Parser::RipperParser.new(line, path, lineno, @assertion_proc.binding, assertion_method.to_s, @assertion_proc) + parser_class = PowerAssert.configuration.parser == :prism ? Parser::PrismParser : Parser::RipperParser + @parser = parser_class.new(line, path, lineno, @assertion_proc.binding, assertion_method.to_s, @assertion_proc) end end end