aladdin-add / eslint-plugin

autofix some errors reported by eslint rules.
106 stars 10 forks source link

no-unused-expressions #32

Open g-plane opened 5 years ago

g-plane commented 5 years ago

文档:https://eslint.org/docs/rules/no-unused-expressions

直接移除 node。 但如果子节点中包含 MemberExpression,则不修复。因为会有以下情况:

var obj = Object.defineProperty({}, 'prop', {
  get() { /* 产生副作用 */ }
})

obj.prop

当然也不排除这个 fix 过于激进,所以也可以考虑不实现这个 fix。

aladdin-add commented 5 years ago

严格来说,全局变量也有这种情况(比较少见):

var obj = Object.defineProperty(window, 'prop', {
  get() { /* 产生副作用 */ }
});

prop; // window.prop
g-plane commented 5 years ago

那就考虑采用「白名单」的方式,例如只修复 BinaryExpression 等。

aladdin-add commented 5 years ago

一般get函数是没有副作用的,所以移除掉的话也算合理。

aladdin-add commented 5 years ago

what if cb && cb();?

g-plane commented 5 years ago

The hasSideEffect function will be improved in the future to detect more cases.

Once the improvement completed, some rules besides no-unused-expressions can be done.