feat: implement autofix for sort-keys#136
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements autofix functionality for the sort-keys ESLint rule, allowing objects with unsorted keys to be automatically reordered when running with the --fix option.
- Adds autofix capability to the sort-keys rule by marking it as
fixable: "code" - Implements a sorting algorithm that respects existing configuration options (asc/desc, case sensitivity, natural sorting, line-separated groups)
- Updates comprehensive test coverage with expected output for all invalid test cases
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/rules/sort-keys.js | Adds fixable metadata and implements autofix logic with key comparison and object reconstruction |
| tests/rules/sort-keys.test.js | Adds expected output assertions for all invalid test cases to verify autofix behavior |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
src/rules/sort-keys.js
Outdated
| comment.range[1] <= node.range[1], | ||
| ); | ||
|
|
||
| if (hasComments) { |
There was a problem hiding this comment.
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.
| sensitivity, | ||
| sortName, | ||
| }, | ||
| fix(fixer) { |
There was a problem hiding this comment.
This aims to sort the entire object at once, not just the reported property?
|
@Pixel998 Are you still working on this PR? |
|
@Pixel998 are you still working on this? |
|
Yes, I was focused on v10 work, but now that's complete I'm picking this up again. |
|
I've implemented an autofixer for our Currently, when we autofix, we swap members but leave any comments in their original positions. This means that after sorting, comments can end up describing the wrong properties, which breaks documentation. For example, consider this JSONC before sorting: {
// Port number for connections
"port": 3000,
// Database configuration
"host": "localhost"
}After our autofix sorts alphabetically, the members swap but the comments stay: {
// Port number for connections
"host": "localhost", // WRONG: This comment now describes the host
// Database configuration
"port": 3000 // WRONG: This comment now describes the port
}We have a few options:
@eslint/eslint-team, what are your thoughts on this? |
|
I agree with Pixel998 that comments should also be moved together with the property (option 3).
{
// INCLUDED
"key": "value", // INCLUDED
}
|
|
TSC Summary: The blocker for this PR is deciding how to handle comments in JSONC/JSON5 files when auto-fixing sorting. There is currently no definitive team consensus on this change. TSC Question: Which option should we choose from the following, and what common design convention was used when implementing auto-fix in JavaScript rules such as
|
|
@Pixel998, still working on this one? Happy to take it if not. |
|
In the 2026-01-08 TSC meeting, we agreed to option 2 - don't autofix when comments are present. |
|
@sethamus Thanks for checking in. I’m currently busy with other work, so please feel free to take it. |
Prerequisites checklist
What is the purpose of this pull request?
This PR enables autofix for the
sort-keysrule so objects with unsorted keys are automatically reordered when running with--fix.What changes did you make? (Give an overview)
Related Issues
Fixes #122
Is there anything you'd like reviewers to focus on?