require "active_support/syntax_error_proxy" require "action_view/template/error" module Svelte class CompilerError < ActiveSupport::SyntaxErrorProxy # steep:ignore UnknownConstant attr_accessor :location, :suggestion, :template def initialize(message, location) @location = location @sugggestion = @location.dig(:suggestion) || "" # steep:ignore:start super(message) # : self # steep:ignore:end end def message to_s end def backtrace if suggestion ["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}, #{suggestion}"] + caller else ["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}"] + caller end end def cause nil end def bindings [template.send(:binding)] end def backtrace_locations traces = backtrace.map { |trace| file, line = trace.match(/^(.+?):(\d+).*$/, &:captures) || trace ActiveSupport::SyntaxErrorProxy::BacktraceLocation.new(file, line.to_i, trace) # steep:ignore UnknownConstant } traces.map { |loc| ActiveSupport::SyntaxErrorProxy::BacktraceLocationProxy.new(loc, self) } # steep:ignore UnknownConstant end def annotated_source_code location[:lineText].split("\n").map.with_index(1) { |line, index| indentation = " " * 4 "#{index}:#{indentation}#{line}" } end end class TemplateError < StandardError attr_reader :cause, :template SOURCE_CODE_RADIUS = 3 def initialize(template, error) raise("Did not provide cause error") unless error @cause = error raise("Did not provide template") unless template @template, @sub_templates = template, nil raise("Cause error is nil for TemplateError") unless @cause @cause.template = template # steep:ignore:start super(@cause.message) # : self # steep:ignore:end end def message @cause.message end def annotated_source_code @cause.annotated_source_code end # Following is copypasta-ed from ActionView::Template::Error def backtrace @cause.backtrace end def backtrace_locations @cause.backtrace_locations end def file_name @template.identifier end def sub_template_message if @sub_templates "Trace of template inclusion: " + @sub_templates.collect(&:inspect).join(", ") else "" end end def source_extract(indentation = 0) return [] unless (num = line_number) num = num.to_i source_code = @template.encode!.split("\n") start_on_line = [num - SOURCE_CODE_RADIUS - 1, 0].max end_on_line = [num + SOURCE_CODE_RADIUS - 1, source_code.length].min indent = end_on_line.to_s.size + indentation return [] unless (source_code = source_code[start_on_line..end_on_line]) formatted_code_for(source_code, start_on_line, indent) end def sub_template_of(template_path) @sub_templates ||= [] @sub_templates << template_path end def line_number @line_number ||= if file_name regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/ $1 if message =~ regexp || backtrace.find { |line| line =~ regexp } end end private def source_location if line_number "on line ##{line_number} of " else "in " end + file_name end def formatted_code_for(source_code, line_counter, indent) raise("line counter is nil") if line_counter.nil? indent_template = "%#{indent}s: %s" source_code.map do |line| line_counter += 1 indent_template % [line_counter, line] end end end end # {:id=>"", :location=>{:column=>13, :file=>"demo/e.html.svelte", :length=>3, :line=>7, :lineText=>"