steep & rbs types

This commit is contained in:
Reese Armstrong 2024-08-03 21:49:06 -04:00
commit 3bfe997b6e
Signed by: reesericci
GPG key ID: 86A9594B08970A4B

3
.gitignore vendored
View file

@ -9,4 +9,5 @@
/test/dummy/storage/
/test/dummy/tmp/
*.gem
/node_modules/
/node_modules/
.gem_rbs_collection/

10
Steepfile Normal file
View file

@ -0,0 +1,10 @@
target :lib do
signature "sig"
check "lib"
ignore "lib/generators"
ignore "lib/svelte/railtie.rb"
library "activesupport"
library "actionview"
end

View file

@ -1,19 +1,21 @@
require "active_support/syntax_error_proxy"
require "action_view/template/error"
module Svelte
class CompilerError < ActiveSupport::SyntaxErrorProxy
class CompilerError < ActiveSupport::SyntaxErrorProxy # steep:ignore UnknownConstant
attr_accessor :location, :suggestion, :template
def initialize(message, location)
@location = location
@sugggestion = location.dig(:suggestion) || ""
super(message)
@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
@ -21,24 +23,24 @@ module Svelte
["#{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)
ActiveSupport::SyntaxErrorProxy::BacktraceLocation.new(file, line.to_i, trace) # steep:ignore UnknownConstant
}
return traces.map { |loc| ActiveSupport::SyntaxErrorProxy::BacktraceLocationProxy.new(loc, self) }
return 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
@ -46,22 +48,107 @@ module Svelte
}
end
end
class TemplateError < ActionView::Template::Error
def initialize(template, error)
@cause = error
@template, @sub_templates = template, nil
error.template = template
end
class TemplateError < ActionViewError # steep:ignore UnknownConstant
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
def annotated_source_code
source_extract(4)
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=>"<h1>{hola}, {nam}</h1>", :namespace=>"file", :suggestion=>""}, :notes=>[], :pluginName=>"esbuild-svelte", :text=>"'nam' is not defined"}
# {:id=>"", :location=>{:column=>13, :file=>"demo/e.html.svelte", :length=>3, :line=>7, :lineText=>"<h1>{hola}, {nam}</h1>", :namespace=>"file", :suggestion=>""}, :notes=>[], :pluginName=>"esbuild-svelte", :text=>"'nam' is not defined"}

344
rbs_collection.lock.yaml Normal file
View file

@ -0,0 +1,344 @@
---
path: ".gem_rbs_collection"
gems:
- name: actioncable
version: '7.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: actionmailer
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: actionpack
version: '6.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: actionview
version: '6.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activejob
version: '6.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activemodel
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activerecord
version: '7.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activestorage
version: '6.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: activesupport
version: '7.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: ast
version: '2.4'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: base64
version: '0'
source:
type: stdlib
- name: bigdecimal
version: '0'
source:
type: stdlib
- name: cgi
version: '0'
source:
type: stdlib
- name: concurrent-ruby
version: '1.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: connection_pool
version: '2.4'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: csv
version: '0'
source:
type: stdlib
- name: date
version: '0'
source:
type: stdlib
- name: dbm
version: '0'
source:
type: stdlib
- name: erb
version: '0'
source:
type: stdlib
- name: fileutils
version: '0'
source:
type: stdlib
- name: forwardable
version: '0'
source:
type: stdlib
- name: globalid
version: '1.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: i18n
version: '1.10'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: io-console
version: '0'
source:
type: stdlib
- name: json
version: '0'
source:
type: stdlib
- name: listen
version: '3.9'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: logger
version: '0'
source:
type: stdlib
- name: mail
version: '2.8'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: marcel
version: '1.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: mini_mime
version: '0.1'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: minitest
version: '0'
source:
type: stdlib
- name: monitor
version: '0'
source:
type: stdlib
- name: mutex_m
version: '0'
source:
type: stdlib
- name: net-protocol
version: '0'
source:
type: stdlib
- name: net-smtp
version: '0'
source:
type: stdlib
- name: nokogiri
version: '1.11'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: optparse
version: '0'
source:
type: stdlib
- name: parser
version: '3.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: pathname
version: '0'
source:
type: stdlib
- name: pstore
version: '0'
source:
type: stdlib
- name: psych
version: '0'
source:
type: stdlib
- name: rack
version: '2.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rails-dom-testing
version: '2.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: railties
version: '6.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rainbow
version: '3.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rake
version: '13.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rbs
version: 3.5.2
source:
type: rubygems
- name: rdoc
version: '0'
source:
type: stdlib
- name: securerandom
version: '0'
source:
type: stdlib
- name: singleton
version: '0'
source:
type: stdlib
- name: strscan
version: '0'
source:
type: stdlib
- name: tempfile
version: '0'
source:
type: stdlib
- name: thor
version: '1.2'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: time
version: '0'
source:
type: stdlib
- name: timeout
version: '0'
source:
type: stdlib
- name: tsort
version: '0'
source:
type: stdlib
- name: tzinfo
version: '2.0'
source:
type: git
name: ruby/gem_rbs_collection
revision: 7062b1ca78ffa29275176daa236f9e1cf4018723
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: uri
version: '0'
source:
type: stdlib
gemfile_lock_path: Gemfile.lock

18
rbs_collection.yaml Normal file
View file

@ -0,0 +1,18 @@
# Download sources
sources:
- type: git
name: ruby/gem_rbs_collection
remote: https://github.com/ruby/gem_rbs_collection.git
revision: main
repo_dir: gems
# You can specify local directories as sources also.
# - type: local
# path: path/to/your/local/repository
# A directory to install the downloaded RBSs
path: .gem_rbs_collection
gems:
- name: ffi
ignore: true

View file

@ -1,8 +1,8 @@
module Svelte
class CompilerError
# TODO: ActiveSupport::SyntaxErrorProxy is untyped, so this is an incomplete type definition
attr_accessor location: { file: String, line: Integer, lineText: String, suggestion: String? }
attr_accessor suggestion: String
attr_accessor location: { file: String, line: Integer, lineText: String, suggestion: String }
attr_accessor suggestion: String
attr_accessor template: ActionView::Template
attr_accessor message: String
@ -16,6 +16,23 @@ module Svelte
end
class TemplateError
def initialize: () -> self
SOURCE_CODE_RADIUS: Integer
attr_reader template: ActionView::Template
attr_reader cause: Svelte::CompilerError
def initialize: (ActionView::Template, CompilerError) -> self
def message: -> String
def annotated_source_code: -> Array[String]
# ActionView::Template::Error stuff here
def file_name: -> String
def line_number: -> String?
def source_extract: (Integer?) -> Array[String]
def backtrace: -> Array[String]
private
def formatted_code_for: (Array[String], Integer?, Integer) -> Array[String]
def source_location: -> String
end
end

View file

@ -0,0 +1,3 @@
module Svelte
VERSION: String
end