ruby-on-rails - 如何在 rake 任务上运行 sorbet typecheck
问题描述
我注意到默认情况下,srb init
etc 不会在 rake 任务上放置 # typed 标志。但是,在 VSCode 上,它确实在 rake 任务上显示错误(例如缺少常量)。
我尝试添加# typed: true
到 rake 任务,但它会立即显示错误,例如“命名空间在 Root 中不可用”。有没有人尝试过检查您的 rake 任务?这样做的设置是什么?
解决方案
Rake monkeypatches 全局main
对象(即顶级代码)以扩展其 DSL:
# Extend the main object with the DSL commands. This allows top-level
# calls to task, etc. to work from a Rakefile without polluting the
# object inheritance tree.
self.extend Rake::DSL
Sorbet 无法模拟对象的单个实例(在本例中main
)与该实例的类(在本例中)具有不同的继承层次结构Object
。
为了解决这个问题,我们建议重构Rakefile
一个继承层次结构明确的新类:
# -- my_rake_tasks.rb --
# (1) Make a proper class inside a file with a *.rb extension
class MyRakeTasks
# (2) Explicitly extend Rake::DSL in this class
extend Rake::DSL
# (3) Define tasks like normal:
task :test do
puts 'Testing...'
end
# ... more tasks ...
end
# -- Rakefile --
# (4) Require that file from the Rakefile
require_relative './my_rake_tasks'
或者,我们可以编写一个 RBIObject
使其看起来像extend Rake::DSL
. 这个 RBI大部分是错误的:并非所有实例Object
都有 this extend
,只有一个实例有。我们不建议这种方法,因为它可能使它看起来像一些代码类型检查,即使方法喜欢task
和namespace
未定义。如果你想这样做,你可以写这个 RBI 文件Object
:
# -- object.rbi --
# Warning!! Monkeypatches all Object's everywhere!
class Object
extend Rake::DSL
end
推荐阅读
- ajax - 点击时更新页面网址而不重新加载
- c# - C# LibVLCSharp 播放器直接馈送媒体
- mongodb - 如何在mongodb(3.4)中使用聚合实现不区分大小写的排序
- python - 遍历数据框中的字典并使用条件进行评估
- go - 在 Go 中,如何跟踪哪个导入导致变量初始化时发生的错误?
- java - 如何使用 Azure SDK for Java v12 删除单个 Blob 文件?
- c++ - 如果 char 被分配为非字符,则无限循环
- c# - C# 为 System.Single 创建同义类型,其中包含功能子集
- blazor - 当 EditForm 的模型更改时,列表会更新。如何预防?
- java - 如何从 Micronaut 框架中的依赖项中注册控制器?