如何在耙任务上运行冰糕类型检查

问题描述 投票:2回答:1

我注意到默认情况下,srb init等不会在rake任务上放置#typeed标志。但是,在VSCode上,它确实在rake任务上显示错误(例如,缺少常量)。

我已经尝试将# typed: true添加到rake任务中,但是它将立即显示诸如“ Root中没有命名空间”之类的错误。有没有人尝试过检查您的耙子任务?要进行什么设置?

ruby-on-rails rake sorbet
1个回答
0
投票

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

lib/rake/dsl_definition.rb

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'

或者,我们可以为Object编写一个RBI,使其看起来像extend Rake::DSL。此RBI是几乎是错误的:并非Object的所有实例都具有此extend,只有一个实例具有。我们DO NOT建议这种方法,因为即使未定义tasknamespace之类的方法,它也可能使它看起来像某些代码类型检查。如果仍然要执行此操作,则可以为Object编写此RBI文件:

# -- object.rbi --

# Warning!! Monkeypatches all Object's everywhere!
class Object
  extend Rake::DSL
end
© www.soinside.com 2019 - 2024. All rights reserved.