generating-sorbet-inline
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSorbet Inline Generation Skill
Sorbet内联签名生成技能
Generate or update Sorbet type signatures using blocks directly in Ruby source files. Supports both full generation from scratch and partial updates for individual changed files. Sorbet signatures are valid Ruby code that enable both static and runtime type checking.
sig {}使用块直接在Ruby源文件中生成或更新Sorbet类型签名。支持从头开始完整生成,以及对单个变更文件进行部分更新。Sorbet签名是合法的Ruby代码,可同时支持静态和运行时类型检查。
sig {}Instructions
操作步骤
When generating Sorbet inline signatures, always follow these steps.
Copy this checklist and track your progress:
Sorbet Inline Generation Progress:
- [ ] Step 1: Analyze the Ruby source
- [ ] Step 2: Add Sorbet signatures
- [ ] Step 3: Eliminate `T.untyped` in signatures
- [ ] Step 4: Review and refine signatures
- [ ] Step 5: Validate signatures with Sorbet生成Sorbet内联签名时,请始终遵循以下步骤。
复制此检查清单并跟踪进度:
Sorbet内联签名生成进度:
- [ ] 步骤1:分析Ruby源代码
- [ ] 步骤2:添加Sorbet签名
- [ ] 步骤3:消除签名中的`T.untyped`
- [ ] 步骤4:审核并优化签名
- [ ] 步骤5:使用Sorbet验证签名Rules
规则
- You MUST NOT run Ruby code of the project.
- You MUST NOT use . Infer the proper type instead.
T.untyped - You MUST NOT use - it bypasses type checking entirely.
T.unsafe - You MUST NOT use - it forces types without verification.
T.cast - You MUST ask the user to provide more details if something is not clear.
- You MUST prepend any command with if the project has Gemfile.
bundle exec - You MUST use block syntax for method signatures.
sig { } - You MUST add to classes/modules before using
extend T::Sig.sig - You MUST focus on method signatures only. Skip local variables, intermediate expressions, and other non-method annotations.
- You MUST NOT use or generate files. This skill is for inline signatures only.
.rbi - You MUST preserve the existing sigil level if one exists. Do not upgrade or change strictness without explicit user consent.
# typed: - You MUST use the tracking file when processing multiple files to ensure no files are missed.
- 禁止运行项目的Ruby代码。
- 禁止使用,应推断出正确的类型。
T.untyped - 禁止使用——它会完全绕过类型检查。
T.unsafe - 禁止使用——它会强制指定类型而不进行验证。
T.cast - 若有内容不明确,必须要求用户提供更多细节。
- 如果项目包含Gemfile,必须在命令前添加前缀。
bundle exec - 必须使用块语法定义方法签名。
sig { } - 在使用之前,必须向类/模块添加
sig。extend T::Sig - 必须仅关注方法签名。跳过局部变量、中间表达式和其他非方法注解。
- 禁止使用或生成文件。本技能仅适用于内联签名。
.rbi - 如果文件已存在标记级别,请保留该级别。未经用户明确同意,不得升级或更改严格性。
# typed: - 处理多个文件时,必须使用跟踪文件以确保没有遗漏任何文件。
Multi-File Processing
多文件处理
When processing multiple Ruby files, create a tracking file to ensure all files are covered:
-
Create tracking file:
.sorbet-inline-generation-todo.tmp[ ] app/models/user.rb [ ] app/models/post.rb [ ] app/services/auth_service.rb -
Process files one by one:
- Take the next pending entry
[ ] - Complete all steps (1-5) for that file
- Mark as processed
[x] - Save the tracking file
- Continue to next pending entry
- Take the next pending
-
Cleanup: Remove the tracking file after all files are processed:bash
rm .sorbet-inline-generation-todo.tmp
If interrupted, the tracking file allows resuming from where you left off.
处理多个Ruby文件时,创建跟踪文件以确保所有文件都被覆盖:
-
创建跟踪文件:
.sorbet-inline-generation-todo.tmp[ ] app/models/user.rb [ ] app/models/post.rb [ ] app/services/auth_service.rb -
逐个处理文件:
- 选取下一个待处理的条目
[ ] - 完成该文件的所有步骤(1-5)
- 标记为已处理
[x] - 保存跟踪文件
- 继续处理下一个待处理条目
- 选取下一个待处理的
-
清理:所有文件处理完成后,删除跟踪文件:bash
rm .sorbet-inline-generation-todo.tmp
若处理被中断,跟踪文件可让你从中断处恢复。
1. Analyze the Ruby Source
1. 分析Ruby源代码
Always perform this step.
Read and understand the Ruby source file:
- Identify all classes, modules, methods, constants and instance variables.
- Note inheritance, module inclusion and definitions based on metaprogramming.
- Note visibility modifiers - ,
public,private.protected - Note existing sigil level at the top of the file.
# typed: - Note type parameters for generic classes.
必须执行此步骤。
阅读并理解Ruby源文件:
- 识别所有类、模块、方法、常量和实例变量。
- 记录基于元编程的继承、模块包含和定义。
- 记录可见性修饰符——、
public、private。protected - 记录文件顶部已有的标记级别。
# typed: - 记录泛型类的类型参数。
2. Add Sorbet Signatures
2. 添加Sorbet签名
Always perform this step.
-
First, check if the file already has asigil at the top:
# typed:- If sigil exists: Preserve the existing level. Do not change it without user consent.
- If no sigil exists: Add as a sensible default (allows gradual typing).
# typed: true
Sigil levels (least to most strict):<ignore<false<true<strictstrong -
Addto the class/module:
extend T::Sigrubyclass MyClass extend T::Sig end -
Then add type signatures usingblocks:
sig {}
Example - Before:
ruby
class User
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
def greet(greeting)
"#{greeting}, #{@name}!"
end
endExample - After:
ruby
undefined必须执行此步骤。
-
首先,检查文件顶部是否已有标记:
# typed:- 若标记存在:保留现有级别。未经用户同意,不得更改。
- 若标记不存在:添加作为合理默认值(支持渐进式类型标注)。
# typed: true
标记级别(从最宽松到最严格):<ignore<false<true<strictstrong -
向类/模块添加:
extend T::Sigrubyclass MyClass extend T::Sig end -
然后使用块添加类型签名:
sig {}
示例 - 之前:
ruby
class User
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
def greet(greeting)
"#{greeting}, #{@name}!"
end
end示例 - 之后:
ruby
undefinedtyped: true
typed: true
class User
extend T::Sig
sig { returns(String) }
attr_reader :name
sig { returns(Integer) }
attr_reader :age
sig { params(name: String, age: Integer).void }
def initialize(name, age)
@name = name
@age = age
end
sig { params(greeting: String).returns(String) }
def greet(greeting)
"#{greeting}, #{@name}!"
end
end
- Focus on method and attribute signatures only
- See [syntax.md](reference/syntax.md) for the full Sorbet syntax guideclass User
extend T::Sig
sig { returns(String) }
attr_reader :name
sig { returns(Integer) }
attr_reader :age
sig { params(name: String, age: Integer).void }
def initialize(name, age)
@name = name
@age = age
end
sig { params(greeting: String).returns(String) }
def greet(greeting)
"#{greeting}, #{@name}!"
end
end
- 仅关注方法和属性签名
- 完整的Sorbet语法指南请参见[syntax.md](reference/syntax.md)3. Eliminate T.untyped
in Signatures
T.untyped3. 消除签名中的T.untyped
T.untypedAlways perform this step.
- Review all signatures and replace with proper types.
T.untyped - Use code context, method calls, and tests to infer types.
- Use only as a last resort when type cannot be determined.
T.untyped
必须执行此步骤。
- 审核所有签名,将替换为正确的类型。
T.untyped - 使用代码上下文、方法调用和测试来推断类型。
- 仅在无法确定类型的最后情况下使用。
T.untyped
4. Review and Refine Signatures
4. 审核并优化签名
Always perform this step.
- Verify signatures are correct, coherent, and complete.
- Remove unnecessary types.
T.untyped - Ensure all methods and attributes have signatures.
- Fix any errors and repeat until signatures are correct.
必须执行此步骤。
- 验证签名是否正确、连贯且完整。
- 移除不必要的类型。
T.untyped - 确保所有方法和属性都有签名。
- 修复所有错误并重复此步骤,直到签名正确。
5. Validate Signatures with Sorbet
5. 使用Sorbet验证签名
Always perform this step.
Run Sorbet type checker to validate signatures:
bash
srb tcOr with bundle:
bash
bundle exec srb tcThis checks:
- Signature syntax correctness
- Type consistency
- Method parameter/return type matching
- Instance variable initialization
Fix any errors reported and repeat until validation passes.
必须执行此步骤。
运行Sorbet类型检查器以验证签名:
bash
srb tc或使用bundle:
bash
bundle exec srb tc此命令检查:
- 签名语法正确性
- 类型一致性
- 方法参数/返回类型匹配
- 实例变量初始化
修复报告的所有错误并重复此步骤,直到验证通过。
References
参考资料
- syntax.md - Sorbet signature syntax guide
- sorbet_examples/ - Real-world Sorbet examples from production gems
- Sorbet documentation - Official Sorbet docs
- syntax.md - Sorbet签名语法指南
- sorbet_examples/ - 来自生产级gem的真实Sorbet示例
- Sorbet官方文档 - Sorbet官方文档