rails/actionview/lib/action_view/render_parser.rb
Hartley McGuire 0041af4c94
Ensure all RubyTracker RenderParsers are tested
Previously, only the PrismRenderParser or RipperRenderParser would be
tested depending on if the Prism gem is available. This meant that
PrismRenderParser was being tested on Ruby 3.3 and RipperRenderParser
was tested on Ruby < 3.3. Additionally, if someone were to add prism to
the rails/rails Gemfile because they wrote a tool that uses it then the
RipperRenderParser would end up completely untested.

This commit is a small refactor to enable testing both RenderParsers in
all Ruby versions so that the prism gem can be added to the Gemfile.
2024-01-24 19:16:59 -05:00

41 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module ActionView
module RenderParser # :nodoc:
ALL_KNOWN_KEYS = [:partial, :template, :layout, :formats, :locals, :object, :collection, :as, :status, :content_type, :location, :spacer_template]
RENDER_TYPE_KEYS = [:partial, :template, :layout]
class Base # :nodoc:
def initialize(name, code)
@name = name
@code = code
end
private
def directory
File.dirname(@name)
end
def partial_to_virtual_path(render_type, partial_path)
if render_type == :partial || render_type == :layout
partial_path.gsub(%r{(/|^)([^/]*)\z}, '\1_\2')
else
partial_path
end
end
end
# Check if prism is available. If it is, use it. Otherwise, use ripper.
begin
require "prism"
rescue LoadError
require "ripper"
require_relative "render_parser/ripper_render_parser"
Default = RipperRenderParser
else
require_relative "render_parser/prism_render_parser"
Default = PrismRenderParser
end
end
end