我想阻止使用 of notify,CustomController但不是AnotherCustomController。rubocop 中是否有一個匹配器允許它檢查notify是否來自特定的類?
class CustomController
def notify(message)
puts 'notify is deprecated'
end
end
class AnotherCustomController
def notify(message)
puts message
end
end
目前,(send nil? :notify ...)兩者都匹配。我只想從 CustomController 匹配。
class MatchClass < CustomController
def run
# This should be matched by rubocop
notify('test')
end
end
class NotMatchClass < AnotherCustomController
def run
# This shouldn't be matched by rubocop
notify('this is another notify method')
end
end
謝謝
uj5u.com熱心網友回復:
在一般情況下,這是不可能的,因為rubocop靜態分析事物。
如果我給你以下代碼,你將無法告訴我它是否應該匹配:
class Example < SomeOtherClassYouDoNotKnow
def run
notify("You would need to know the definition of the parent class")
end
end
def anywhere
something.notify("You need to know what `something` is")
end
您可以查找您給出的特定模式,但您可能同時遇到誤報和誤報。
uj5u.com熱心網友回復:
我找不到繼承類名,但我可以找到代碼的路徑。所以我確實喜歡這個。
node.location.expression.source_buffer.name.include?('app/jobs')
uj5u.com熱心網友回復:
當我想棄用一個方法并阻止人們使用它時,我會使用 Ruby StdLibDeprecate將其標記為棄用并顯示警告:
class CustomController
extend Gem::Deprecate
def notify(message)
puts 'notify is deprecated'
end
deprecate :notify, "AnotherCustomController#notify", 2023, 11
end
class AnotherCustomController
def notify(message)
puts message
end
end
呼叫CustomController#notify回傳如下警告:
Customercontroller#notify is deprecated; use AnotherCustomController#notify instead. It will be removed on or after 2023-11-01.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530362.html
