Skip to content
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

Closed
wants to merge 9 commits into from
Closed

Conversation

j-f1
Copy link
Contributor

@j-f1 j-f1 commented Dec 1, 2017

What is the purpose of this pull request?

Add autofixing to a rule

Addresses part of, but does not cl​ose #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  (with var being replaced with let or const 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?

@j-f1 j-f1 changed the title Autofix some one-var rules New: Autofix some one-var rules Dec 1, 2017
@aladdin-add aladdin-add added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules labels Dec 1, 2017
Copy link
Member

@not-an-aardvark not-an-aardvark left a 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.

@@ -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; }",
Copy link
Member

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?

if (!filter) {
filter = () => true;
}
return function *fix(fixer) {
Copy link
Member

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.

Copy link
Contributor Author

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!

* @returns {boolean} If the node has a `;` after it
*/
function hasSemi(node) {
return sourceCode.getLastToken(node).type === "semi";
Copy link
Member

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?

Copy link
Contributor Author

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.

* @param {...VariableDeclaration} declarations The `VariableDeclarations` to merge
* @returns {Function} The fixer function
*/
function mergeDeclarations() { // eslint-disable-line no-unused-vars
Copy link
Member

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) {}}",
Copy link
Member

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;

Copy link
Contributor Author

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) {}}",
Copy link
Member

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;

Copy link
Contributor Author

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;

Copy link
Member

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) {}}",
Copy link
Member

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.

Copy link
Contributor Author

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;

Copy link
Member

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.

Copy link
Member

@ilyavolodin ilyavolodin left a 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?

@j-f1
Copy link
Contributor Author

j-f1 commented Dec 8, 2017

Could you remove package-lock.json please?

👍 ✅

@j-f1 j-f1 changed the title New: Autofix some one-var rules New: Autofix some one-var messages Dec 12, 2017
@j-f1 j-f1 changed the title New: Autofix some one-var messages New: Autofix some one-var reports Dec 12, 2017
@aladdin-add aladdin-add added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 13, 2017
@kaicataldo
Copy link
Member

Sorry, looks like we lost track of this :( Aside from the merge conflicts, where do we stand with this?

@not-an-aardvark
Copy link
Member

It would be good to change #9671 (comment) -- aside from that I think this is ready to merge.

@j-f1
Copy link
Contributor Author

j-f1 commented Jan 24, 2018

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 :(

@kaicataldo
Copy link
Member

Thanks for the quick follow-up!

@j-f1 Do you plan on finishing this at some point? Is there anything you need from us?

@j-f1
Copy link
Contributor Author

j-f1 commented Feb 8, 2018

I haven’t gotten a chance to address the comments, and I don’t have enough experience with fixers to do this properly. Closing.

@j-f1 j-f1 closed this Feb 8, 2018
@Nokel81
Copy link
Contributor

Nokel81 commented Mar 1, 2018

I am looking into continuing this work. I will open a new PR when I have worked through #9671 (comment)

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 8, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants