SorbetView
Sorbet type-checking for Rails view templates (ERB). Extracts Ruby code from templates, generates typed Ruby files, and provides LSP support for editor integration.
Features
- Type-check Ruby code inside ERB templates with Sorbet
- LSP proxy server for hover, completion, go-to-definition in
.erbfiles - ViewComponent
erb_templateheredoc support - Source mapping between templates and generated Ruby for accurate error reporting
- Tapioca DSL compiler for helper method RBI generation
Installation
Add to your Gemfile:
gem 'sorbet_view'Then run:
bundle installSetup
Generate a config file:
bundle exec sv initThis creates .sorbet_view.yml:
input_dirs:
- app/views
exclude_paths: []
output_dir: sorbet/templates
extra_includes: []
skip_missing_locals: trueConfiguration Options
| Option | Default | Description |
|---|---|---|
input_dirs |
['app/views'] |
Directories to scan for .erb templates |
exclude_paths |
[] |
Paths to exclude from scanning |
output_dir |
'sorbet/templates' |
Where generated Ruby files are written |
extra_includes |
[] |
Additional modules to include in generated classes |
extra_body |
'' |
Additional code to include in generated classes |
skip_missing_locals |
true |
Skip partials without locals: declaration |
sorbet_path |
'srb' |
Path to the Sorbet binary |
typed_level |
'true' |
Sorbet typed level for generated files |
component_dirs |
[] |
Directories to scan for ViewComponent files |
Usage
Compile templates
bundle exec sv compileCompile and type-check
bundle exec sv tcPass extra arguments to Sorbet after --:
bundle exec sv tc -- --error-url-base=https://srb.help/LSP server
bundle exec sv lspUsed by the VSCode extension for editor integration.
Clean generated files
bundle exec sv cleanDeclaring locals in partials
Use magic comments to declare partial locals and their types:
<%# locals: (user:, admin: false) %>
<%# locals_sig: sig { params(user: User, admin: T::Boolean).void } %>
<h1><%= user.name %></h1>
<% if admin %>
<p>Admin</p>
<% end %>ViewComponent support
Add component_dirs to your config:
component_dirs:
- app/componentsComponents using erb_template heredocs are automatically detected and compiled.
Tapioca integration
SorbetView includes a Tapioca DSL compiler that generates RBI files for controller helper methods. Run:
bundle exec tapioca dslRequirements
License
MIT License. See LICENSE for details.