如何在eslint规则修复中修改javascript AST

时间:2017-03-27 10:18:35

标签: javascript eslint rule

我目前正在制作一个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)
...

如何克服这个缺点,能够剥离链式属性/方法?

0 个答案:

没有答案