-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
no-unused-vars #33
Comments
为什么呢?我觉得未使用的局部变量和函数也可以移除啊 |
这里有一个问题是和编辑器集成:用户开发过程中,不希望这些被移除。但是这种情况下,用户自己可以选择不要开启这个fix |
我考虑到的是,有时候变量只是「临时性」的未被使用(也就是还没写将会使用这个变量的代码),如果此时被移除,让用户再补上就比较烦。 而移除 import 则是借鉴了 Go 语言下的一个工具( |
嗯,所以这条规则的autofix不推荐在开发过程中使用,类似于 no-debugger. 我觉得可以考虑都移除掉,而且这个fix是安全的,不会影响执行结果。 |
函数参数应该不能移除,否则会改变函数签名(参数个数发生了变化,进而影响调用者)。 |
我们应该考虑用户什么时候会开启 --fix 选项,什么时候不开启。 |
用户选择不开启这个fix,可以不打开这个规则啊,使用内置的就可以。 通过和 {
"rules": {
"autofix/rule1": "error",
....
"no-autofix/rule2": "error"
...
}
} |
Has this been implemented? I would like to remove unused vars, imports, etc automatically |
Currently no, just a plan. |
Working for removing imports now. |
@aladdin-add 对于这种情况您怎么考虑? let a = b() |
可以考虑移除a的定义(没有被使用的话)? let a = b();
// =>
b(); 并不一定在一个pr里面实现,最多的场景可能就是 import/require -- 可以先实现这个。 |
这样可能不好处理: let a = b(), c = d() 遇到多个定义的就不修复? |
理论上可以fix,不过可以后面再实现。thoughts? |
现在实现也没问题的,应该不会太复杂,就是针对上面那种情况不知道思路。 |
比如就刚才那个,有什么好的方案吗? let a = b(), c = d() |
b();
d(); 有什么问题么? |
假定 |
b();
let c = d(); |
感觉这对于某些代码来说,修复后的结果可能有点怪:(但符合语法) // before
let a = "";
// after
""; |
haha, interesting! let s = "use strict";
=>
"use strict"; 如果 "=" 后面是字面量的话,可以把整个语句移除掉。也就是说:
|
也许可以考虑直接删除没有副作用的表达式: // before
let a = 123
// after
// (no code)
// before
let b = c
// after
// (no code) 如何? |
我重新想了下,还是允许这种代码:
如果启用 |
[a()]; |
有一个例外就是, |
so? |
所以,如果开启 // before
let array = [a()]
// after
// (no code) |
首先,这里有2个问题:
|
对于第一点,我觉得意义不大,因为 auto fixing 本来就是方便用户,减少用户手工修改代码,但仅仅把表达式提取出来,最终还是要让用户去处理。 |
Following 😰 |
那最后的方案是怎样? |
|
@merlinpatt @corysimmons That PR was merged. Please wait for a new release. |
Will this convert... import {Foo, Bar} from 'foobar'
const x = Foo ...to... import {Foo} from 'foobar'
const x = Foo ? |
yes, as the test case: https://github.com/aladdin-add/eslint-plugin-autofix/blob/b7168039c48d399d2293260c956375688b1e8881/tests/lib/rules/no-unused-vars.js#L70-L75 btw, it has been released, you can give it a try: |
Non-related: it seems that our releases don't follow Semver. |
If it's pre-1.0.0, then you don't have to follow semver. That said... a lot of projects keep their releases pre-1.0.0 just because they're lazy. 😇 If you guys like the current API then you should go ahead and bump to 1.0.0 and start following semver. |
This is great! Thank you for your work @g-plane and @aladdin-add ! Combined with https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md#newlines-between-ignorealwaysalways-and-inside-groupsnever this does everything I want! This config replaces https://twitter.com/john_papa/status/991111269674569733 and turns it into a CLI you can run like {
"scripts": {
"lint": "eslint '{,!(node_modules)/**/}*.js'"
},
"eslintConfig": {
"plugins": [
"autofix"
],
"rules": {
"autofix/no-unused-vars": "error",
"import/order": [
"error",
{
"groups": [
[
"builtin",
"external"
],
[
"parent",
"sibling",
"index"
]
],
"newlines-between": "always"
}
]
}
}
} ^ 😍😍😍😍😍😍 Thank you!--->>> https://gist.github.com/corysimmons/d6ee1c9c4a5e557980e4da20ce01e1d3 |
nice! the credit goes to @g-plane 💯 |
Oh, sorry @g-plane I can't read your language so I have no idea who did what, but thanks to whoever did this! <3 <3 |
Great work! Thank you @g-plane and @aladdin-add ! One issue I noticed is that before autofix
after autofix
coma is left With unused imports everything works great. |
This will crash the app |
@serhiipalash Good catch! |
#44 . |
@serhiipalash @corysimmons This has been fixed at #45 . |
Auto fixing produces wrong syntax before autofix const connect = (data, { name }) => ({ data, });> // "name" is unused after autofix const connect = (data, { }) => ({ data, }); |
@adam1985 This is valid syntax. |
文档:https://eslint.org/docs/rules/no-unused-vars
仅仅移除 ES6 的
import
。The text was updated successfully, but these errors were encountered: