使用RuboCop 0.81.0自定义规则报错:未初始化常量RuboCop::Cop::Base
修复RuboCop 0.81.0中
uninitialized constant RuboCop::Cop::Base错误 你遇到的问题是因为RuboCop 0.81.0版本还未引入RuboCop::Cop::Base类,这个基础类是在后续更高版本的RuboCop中才推出的。在0.81.0版本里,自定义规则需要继承旧的基础类RuboCop::Cop::Cop。
修改你的自定义Cop代码,将继承的父类从Base替换为Cop即可:
return unless defined?(::RuboCop) module CustomCops class NoTimecop < ::RuboCop::Cop::Cop MSG = "`Timecop` の代わりにRails標準の `ActiveSupport::Testing::TimeHelpers` が使えませんか?".freeze def on_send(node) if node.source.include?("Timecop") && node.receiver.const_name == "Timecop" add_offense(node) end end end end
另外需要确认你的RuboCop配置文件(.rubocop.yml)中正确引入了自定义规则文件:
require: - ./lib/custom_cops/no_timecop.rb
内容的提问来源于stack exchange,提问作者Corey
相关产品推荐
相关产品推荐

