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

Update: auto-fix for comma-style (fixes #6941) #6957

Merged
merged 1 commit into from Sep 8, 2016
Merged

Conversation

gyandeeps
Copy link
Member

@gyandeeps gyandeeps commented Aug 22, 2016

What issue does this pull request address?

Introduce auto-fix for comma-style rule.

What changes did you make? (Give an overview)

Fix the position of comma.

Is there anything you'd like reviewers to focus on?

This is just a proof of concept PR.

UPDATE:

Its ready for review and merge.

@gyandeeps gyandeeps added the do not merge This pull request should not be merged yet label Aug 22, 2016
@eslintbot
Copy link

LGTM

@mention-bot
Copy link

@gyandeeps, thanks for your PR! By analyzing the annotation information on this pull request, we identified @vitorbal, @kaelzhang and @evansimmons-ck to be potential reviewers

@gyandeeps gyandeeps changed the title WIP: auto-fix for (fixes #6941) WIP: auto-fix for comma-style (fixes #6941) Aug 22, 2016
@eslintbot
Copy link

LGTM

@@ -18,7 +18,7 @@ module.exports = {
category: "Stylistic Issues",
recommended: false
},

fixable: true,
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 we usually want "code" or "whitespace" here. Not sure which this is though!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be "code". "whitespace" is basically just for spacing fixes.

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

return fixer.removeRange([
previousItemToken.range[1],
commaToken.range[0]
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixing seems to be for style === "last".
If style === "first", probably it should be fixer.removeRange([commaToken.range[1], currentItemToken.range[0]]).

@mysticatea
Copy link
Member

mysticatea commented Aug 25, 2016

I think this rule should not remove comments.
We often write comments between properties.

let obj = {
    foo: 1,  // a comment
    bar: 2,  // another comment

    /**
     * a doc comment
     */
    method: function() {
    },
}

Maybe...:

const text =
    sourceCode.text.slice(prevToken.range[1], commaToken.range[0]) +
    sourceCode.text.slice(commaToken.range[1], currentToken.range[0])
const range = [prevToken.range[1], currentToken.range[0]]

// first → last
fixer.replaceTextRange(range, `${text},`)

// last → first
fixer.replaceTextRange(range, `,${text}`)

@gyandeeps
Copy link
Member Author

As always @mysticatea to the rescue. Excellent idea. So far looks good.

@eslintbot
Copy link

LGTM

function getReplacedText(styleType, text) {
switch (styleType) {
case "between":
return `,${text.replace("\n", "")}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dangerous.

let obj = {
    a // a comment
    ,
    b
}

I think good to handle the same way as others for lonely comma's cases also. I.e. getFixerFunction(style, previousItemToken, commaToken, currentItemToken)

Or maybe it should not fix the lonely commas.

Copy link
Member Author

@gyandeeps gyandeeps Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mysticatea Sorry but I din't quite get what you trying to say here.

I have a unit test covering the above use case, and the fixed code would be

let obj = {
    a, // a comment
    b
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry, it was my misunderstand.
I sometimes forget that String.prototype.replace replaces only the first matched part.

@mysticatea
Copy link
Member

mysticatea commented Aug 29, 2016

LGTM, thank you!

@gyandeeps gyandeeps removed the do not merge This pull request should not be merged yet label Aug 30, 2016
@gyandeeps gyandeeps changed the title WIP: auto-fix for comma-style (fixes #6941) Update: auto-fix for comma-style (fixes #6941) Aug 30, 2016
@nzakas
Copy link
Member

nzakas commented Sep 3, 2016

Just want to double check that we are only doing fixes that are 100% safe with this now?

@mysticatea
Copy link
Member

mysticatea commented Sep 3, 2016

@nzakas I think so. The logic is:

let obj = { foo: 1 // comment\n  /**\n   * comment\n   */\n,   bar: 2 }
                  └┬──────────────────────────────────────────┘
                   └ Part A - between the end of the previous property and the current property.

let obj = { foo: 1 // comment\n  /**\n   * comment\n   */\n,   bar: 2 }
                  └┬──────────────────────────────────────┘
                   └ Part B - between the end of the previous property and the comma.

let obj = { foo: 1 // comment\n  /**\n   * comment\n   */\n,   bar: 2 }
                                                            └┬┘
        Part C - between the comma and the current property. ┘

Then, it replaces A (= B + "," + C) by "," + B + C or B + C + ",".
It will move the comma exactly.

In addition, in "first" cases, comma-spacing and indent rules will tweak the code in the next passes of autofix.

@ilyavolodin
Copy link
Member

I think this is ready to be merged, but I'm not 100% if all of the comments have been resolved. @mysticatea could you confirm and merge if you think it's good to go?

@mysticatea
Copy link
Member

Sure, LGTM.

@mysticatea mysticatea merged commit 7502eed into master Sep 8, 2016
@gyandeeps gyandeeps deleted the issue6941 branch September 8, 2016 13:58
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 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 Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants