Skip to content

Conversation

@Pixel998
Copy link
Contributor

@Pixel998 Pixel998 commented Aug 19, 2025

Prerequisites checklist

What is the purpose of this pull request?

This PR enables autofix for the sort-keys rule so objects with unsorted keys are automatically reordered when running with --fix.

What changes did you make? (Give an overview)

  • Added a fixer that reorders object keys.
  • Updated tests.

Related Issues

Fixes #122

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

@eslintbot eslintbot added this to Triage Aug 19, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Aug 19, 2025
@lumirlumir lumirlumir requested a review from Copilot August 19, 2025 10:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

comment.range[1] <= node.range[1],
);

if (hasComments) {
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The 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.
@mdjermanovic mdjermanovic moved this from Needs Triage to Implementing in Triage Aug 23, 2025
sensitivity,
sortName,
},
fix(fixer) {
Copy link
Member

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?

@Zamiell
Copy link

Zamiell commented Oct 12, 2025

@Pixel998 Are you still working on this PR?

@nzakas
Copy link
Member

nzakas commented Dec 2, 2025

@Pixel998 are you still working on this?

@Pixel998
Copy link
Contributor Author

Pixel998 commented Dec 3, 2025

Yes, I was focused on v10 work, but now that's complete I'm picking this up again.

@Pixel998
Copy link
Contributor Author

I've implemented an autofixer for our sort-keys rule, but we need to decide how to handle comments in JSONC/JSON5 files.

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:

  1. Keep current behavior - Accept that comments become misleading after sorting
  2. Don't autofix when comments are present - Safe but limits usefulness
  3. Swap members with their attached comments - Preserves documentation but complex to implement

@eslint/eslint-team, what are your thoughts on this?

@Pixel998 Pixel998 moved this from Implementing to Feedback Needed in Triage Dec 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Feedback Needed

Development

Successfully merging this pull request may close these issues.

Rule Change: make sort-keys rule fixable

6 participants