我目前正在制作一个eslint规则,其修复应删除链式属性。
在当前示例中,b
应该被删除,所以
a.b() // or
a.b.c()
应该成为:
a() // or
a.c()
我的第一稿天真地接受了源的给定部分并删除了不需要的属性:
fix: fixer => {
const range = getChainedAttribute(node, 'b').range;
range[0] -= 1; // strip also the prepending dot
return fixer.remove(getChainedAttribute(node, 'b'));
}
(getChainedAttribute
是一个辅助函数,它返回链式属性。)
虽然此修复程序按预期工作,但它失败并出现以下eslint异常:
Rule should not modify AST.
Actual:
[object Object]
Expected:
[object Object]
assertASTDidntChange (node_modules/eslint/lib/testers/rule-tester.js:406:24)
...
如何克服这个缺点,能够剥离链式属性/方法?