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
Conversation
LGTM |
@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 |
comma-style
(fixes #6941)
LGTM |
@@ -18,7 +18,7 @@ module.exports = { | |||
category: "Stylistic Issues", | |||
recommended: false | |||
}, | |||
|
|||
fixable: 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.
I think we usually want "code" or "whitespace" here. Not sure which this is though!
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.
This would be "code". "whitespace" is basically just for spacing fixes.
LGTM |
LGTM |
return fixer.removeRange([ | ||
previousItemToken.range[1], | ||
commaToken.range[0] | ||
]); |
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.
This fixing seems to be for style === "last"
.
If style === "first"
, probably it should be fixer.removeRange([commaToken.range[1], currentItemToken.range[0]])
.
I think this rule should not remove comments. 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}`) |
As always @mysticatea to the rescue. Excellent idea. So far looks good. |
LGTM |
function getReplacedText(styleType, text) { | ||
switch (styleType) { | ||
case "between": | ||
return `,${text.replace("\n", "")}`; |
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.
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.
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.
@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
}
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.
Oops, sorry, it was my misunderstand.
I sometimes forget that String.prototype.replace
replaces only the first matched part.
LGTM, thank you! |
comma-style
(fixes #6941)comma-style
(fixes #6941)
Just want to double check that we are only doing fixes that are 100% safe with this now? |
@nzakas I think so. The logic is:
Then, it replaces In addition, in |
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? |
Sure, LGTM. |
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.