-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: implement autofix for sort-keys #136
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
Open
Pixel998
wants to merge
1
commit into
eslint:main
Choose a base branch
from
Pixel998:sort-keys-autofix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,8 @@ const rule = { | |
meta: { | ||
type: "suggestion", | ||
|
||
fixable: "code", | ||
|
||
defaultOptions: [ | ||
"asc", | ||
{ | ||
|
@@ -198,6 +200,33 @@ const rule = { | |
return false; | ||
} | ||
|
||
/** | ||
* Compares two key names according to the rule options and returns a number | ||
* suitable for Array.prototype.sort. | ||
* @param {string} a First key to compare. | ||
* @param {string} b Second key to compare. | ||
* @returns {number} Negative if a < b, positive if a > b, 0 if equal. | ||
*/ | ||
function compareKeys(a, b) { | ||
let a1 = a; | ||
let b1 = b; | ||
if (!caseSensitive) { | ||
a1 = a1.toLowerCase(); | ||
b1 = b1.toLowerCase(); | ||
} | ||
if (natural) { | ||
const r = naturalCompare(a1, b1); | ||
return directionShort === "asc" ? r : -r; | ||
} | ||
if (a1 < b1) { | ||
return directionShort === "asc" ? -1 : 1; | ||
} | ||
if (a1 > b1) { | ||
return directionShort === "asc" ? 1 : -1; | ||
} | ||
return 0; | ||
} | ||
|
||
return { | ||
Object(node) { | ||
let prevMember; | ||
|
@@ -226,6 +255,126 @@ const rule = { | |
sensitivity, | ||
sortName, | ||
}, | ||
fix(fixer) { | ||
const text = context.sourceCode.text; | ||
|
||
// If there are any comment tokens within this object, do not attempt to autofix | ||
const hasComments = | ||
context.sourceCode.comments.some( | ||
comment => | ||
comment.range[0] >= node.range[0] && | ||
comment.range[1] <= node.range[1], | ||
); | ||
|
||
if (hasComments) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment detection logic should be documented to explain why autofix is disabled when comments are present within the object, as this might not be obvious to maintainers. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
return null; | ||
} | ||
|
||
const groups = []; | ||
let groupStart = 0; | ||
for (let i = 1; i < node.members.length; i++) { | ||
if ( | ||
allowLineSeparatedGroups && | ||
isLineSeparated( | ||
node.members[i - 1], | ||
node.members[i], | ||
) | ||
) { | ||
groups.push({ | ||
start: groupStart, | ||
end: i - 1, | ||
}); | ||
groupStart = i; | ||
} | ||
} | ||
groups.push({ | ||
start: groupStart, | ||
end: node.members.length - 1, | ||
}); | ||
|
||
const fixes = []; | ||
for (const group of groups) { | ||
const groupLen = | ||
group.end - group.start + 1; | ||
if (groupLen <= 1) { | ||
continue; | ||
} | ||
|
||
let isSorted = true; | ||
for ( | ||
let i = group.start + 1; | ||
i <= group.end; | ||
i++ | ||
) { | ||
const prevKey = getKey( | ||
node.members[i - 1], | ||
); | ||
const thisKey = getKey(node.members[i]); | ||
if (compareKeys(prevKey, thisKey) > 0) { | ||
isSorted = false; | ||
break; | ||
} | ||
} | ||
if (isSorted) { | ||
continue; | ||
} | ||
|
||
const items = []; | ||
const seps = []; | ||
for ( | ||
let i = group.start; | ||
i <= group.end; | ||
i++ | ||
) { | ||
const m = node.members[i]; | ||
items.push({ | ||
key: getKey(m), | ||
start: m.range[0], | ||
end: m.range[1], | ||
text: text.slice( | ||
m.range[0], | ||
m.range[1], | ||
), | ||
}); | ||
|
||
if (i < group.end) { | ||
seps.push( | ||
text.slice( | ||
node.members[i].range[1], | ||
node.members[i + 1] | ||
.range[0], | ||
), | ||
); | ||
} | ||
} | ||
|
||
const replaceStart = items[0].start; | ||
const replaceEnd = items.at(-1).end; | ||
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const rebuilt = items | ||
.slice() | ||
.sort((a, b) => | ||
compareKeys(a.key, b.key), | ||
) | ||
.map( | ||
(item, i) => | ||
item.text + | ||
(i < seps.length | ||
? seps[i] | ||
: ""), | ||
) | ||
.join(""); | ||
|
||
fixes.push( | ||
fixer.replaceTextRange( | ||
[replaceStart, replaceEnd], | ||
rebuilt, | ||
), | ||
); | ||
} | ||
|
||
return fixes; | ||
}, | ||
}); | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aims to sort the entire object at once, not just the reported property?