-
-
Notifications
You must be signed in to change notification settings - Fork 30
Update: add autofixing to test-case-property-ordering. (fixes #31) #32
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
Conversation
Hmm, this doesn't seem ideal. Why not keep the old spacing, and just swap the properties around? That way, comments will also be preserved. |
021d9bc
to
e1a202b
Compare
ping @not-an-aardvark |
let trailing = source.slice(propertyStart, propertyEnd); | ||
|
||
// for last property, should check & add trailling commas. | ||
if (j === properties.length - 1 && sourceCode.getTokenAfter(last).value !== ',') { |
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.
Instead of having a special case for trailing commas, would it work to just set finalEnd
to the trailing comma if it exists?
const finalEnd = sourceCode.getTokenAfter(last);
const insertedIndex = orderMsg.indexOf(keyNames[j]); | ||
const propertyCode = sourceCode.getText(properties[j]); | ||
const propertyStart = properties[j].range[1]; | ||
const propertyEnd = j < properties.length - 1 ? properties[j + 1].range[0] : finalEnd; |
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 these variable names are confusing because propertyStart
and propertyEnd
describe the boundaries of the whitespace after the property, not the property itself. Maybe whitespaceStart
and whitespaceEnd
would be better.
[start, finalEnd], | ||
result.join('') | ||
); | ||
}, |
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.
Would it be possible to use the new feature in ESLint 4.1.0 where you can return an array of autofixes?
That way, this autofix would be very simple, because it wouldn't be necessary to deal with whitespace or trailing commas:
fix(fixer) {
return orderMsg.map((key, index) => {
const propertyToInsert = test.properties[keyNames.indexOf(key)];
return fixer.replaceText(test.properties[index], sourceCode.getText(propertyToInsert));
});
}
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.
awesome!!!
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.
LGTM. Thanks!
enable it on eslint codebase: eslint/eslint#9040
my concern is we didn't enable
object-curly-newline
, this may fixing some code like this:looks so, hmm... strange!