-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
New: Autofix some one-var reports #9671
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request! This generally looks good. I have a few suggestions for tests.
tests/lib/rules/one-var.js
Outdated
@@ -242,6 +243,9 @@ ruleTester.run("one-var", rule, { | |||
}, | |||
{ | |||
code: "function foo() { var bar = true; var baz = false; }", | |||
|
|||
// output: "function foo() { var bar = true, baz = false; }", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the commented code here?
lib/rules/one-var.js
Outdated
if (!filter) { | ||
filter = () => true; | ||
} | ||
return function *fix(fixer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In #8101 (comment) it seemed like a few people didn't want to use generator functions in the codebase, although I'm personally fine with it.
Another option would be to make fix
return an array, and use .map
rather than a for-of loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I’ll use arrow functions!
lib/rules/one-var.js
Outdated
* @returns {boolean} If the node has a `;` after it | ||
*/ | ||
function hasSemi(node) { | ||
return sourceCode.getLastToken(node).type === "semi"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by this line because semicolon tokens have type Punctuator
, not semi
(e.g. see the tokens
list in this AST). Are you sure this function ever returns true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out this is dead code, so I’m going to 🔥 it.
lib/rules/one-var.js
Outdated
* @param {...VariableDeclaration} declarations The `VariableDeclarations` to merge | ||
* @returns {Function} The fixer function | ||
*/ | ||
function mergeDeclarations() { // eslint-disable-line no-unused-vars |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to remove the unused code for now, and then add it afterwards once it's complete.
@@ -581,6 +646,7 @@ ruleTester.run("one-var", rule, { | |||
}, | |||
{ | |||
code: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3, baz = z; for (var d in baz) {}}", | |||
output: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3; var baz = z; for (var d in baz) {}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some tests that use destructuring, and different types of declarations?
For example:
var {foo} = 1, [bar] = 2;
// fixed to
var {foo} = 1; var [bar] = 2;
const foo = 1, bar = 2;
// fixed to
const foo = 1; const bar = 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
@@ -581,6 +646,7 @@ ruleTester.run("one-var", rule, { | |||
}, | |||
{ | |||
code: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3, baz = z; for (var d in baz) {}}", | |||
output: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3; var baz = z; for (var d in baz) {}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test where a variable declaration spans multiple lines?
For example, I would expect the following code
var foo = 1,
bar = 2;
...to get fixed to something like this:
var foo = 1;
var bar = 2;
It would also be fine for it to get fixed to something like this, because the indent
rule would adjust it afterwards:
var foo = 1;
var bar = 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here’s what I have now:
var foo = 1;
var
bar = 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if possible, this should be changed to the output that @not-an-aardvark suggested. We have no other rule that would be able to fix extra line break for the second declaration.
@@ -581,6 +646,7 @@ ruleTester.run("one-var", rule, { | |||
}, | |||
{ | |||
code: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3, baz = z; for (var d in baz) {}}", | |||
output: "var a = 1; var b = 2; var x, y; for (var z of foo) {var c = 3; var baz = z; for (var d in baz) {}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test where there is a comment between the variable declarators?
var foo = 1, // comment
bar = 2;
I would expect the comment to be preserved and appear somewhere in the output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here’s what I have now:
var foo = 1;
var // comment
bar = 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be possible to change that? I think putting var
and bar
on separate lines would be surprising to users.
… for one-var" This reverts commit 20b8d3d.
(Also make them pass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove package-lock.json please?
👍 ✅ |
Sorry, looks like we lost track of this :( Aside from the merge conflicts, where do we stand with this? |
It would be good to change #9671 (comment) -- aside from that I think this is ready to merge. |
There’s also #9671 (comment). I haven’t had much time to work on this, and those issues seem pretty hard to fix, so I haven’t fixed them :( |
Thanks for the quick follow-up! @j-f1 Do you plan on finishing this at some point? Is there anything you need from us? |
I haven’t gotten a chance to address the comments, and I don’t have enough experience with fixers to do this properly. Closing. |
I am looking into continuing this work. I will open a new PR when I have worked through #9671 (comment) |
What is the purpose of this pull request?
Add autofixing to a rule
Addresses part of, but does not close #9072
What changes did you make? (Give an overview)
I added autofixing to the “Split [...] declarations into multiple statements” messages. It replaces each
,
in a variable declaration with; var
(withvar
being replaced withlet
orconst
as appropriate)Is there anything you'd like reviewers to focus on?
Should I keep the last commit, or would it be better to remove it from this PR?