Skip to content

Commit

Permalink
feat: Add ignoreComputedKeys option in sort-keys rule (#19162)
Browse files Browse the repository at this point in the history
* feat: Add `ignoreComputedKeys` option in `sort-keys` rule

Fixes #19153

* Update docs/src/rules/sort-keys.md

Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>

---------

Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
  • Loading branch information
mdjermanovic and nzakas authored Nov 26, 2024
1 parent 0c6b842 commit 8f70eb1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
30 changes: 30 additions & 0 deletions docs/src/rules/sort-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The 2nd option is an object which has the following properties.
* `minKeys` - Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is `2`, which means by default all objects with unsorted keys will result in lint errors.
* `natural` - if `true`, enforce properties to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
* `allowLineSeparatedGroups` - if `true`, the rule allows to group object keys through line breaks. In other words, a blank line after a property will reset the sorting of keys. Default is `false`.
* `ignoreComputedKeys` - if `true`, the rule ignores all computed keys and doesn't report unsorted properties separated by them. A computed key will reset the sorting of the following non-computed keys. Default is `false`.

Example for a list:

Expand Down Expand Up @@ -372,6 +373,35 @@ var obj7 = {

:::

### ignoreComputedKeys

Examples of **correct** code for the `{ignoreComputedKeys: true}` option:

::: correct

```js
/*eslint sort-keys: ["error", "asc", {ignoreComputedKeys: true}]*/

let obj1 = {
[b]: 1,
a: 2
}

let obj2 = {
c: 1,
[b]: 2,
a: 3
}

let obj3 = {
c: 1,
["b"]: 2,
a: 3
}
```

:::

## When Not To Use It

If you don't want to notify about properties' order, then it's safe to disable this rule.
Expand Down
11 changes: 10 additions & 1 deletion lib/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = {
defaultOptions: ["asc", {
allowLineSeparatedGroups: false,
caseSensitive: true,
ignoreComputedKeys: false,
minKeys: 2,
natural: false
}],
Expand Down Expand Up @@ -112,6 +113,9 @@ module.exports = {
},
allowLineSeparatedGroups: {
type: "boolean"
},
ignoreComputedKeys: {
type: "boolean"
}
},
additionalProperties: false
Expand All @@ -124,7 +128,7 @@ module.exports = {
},

create(context) {
const [order, { caseSensitive, natural, minKeys, allowLineSeparatedGroups }] = context.options;
const [order, { caseSensitive, natural, minKeys, allowLineSeparatedGroups, ignoreComputedKeys }] = context.options;
const insensitive = !caseSensitive;
const isValidOrder = isValidOrders[
order + (insensitive ? "I" : "") + (natural ? "N" : "")
Expand Down Expand Up @@ -160,6 +164,11 @@ module.exports = {
return;
}

if (ignoreComputedKeys && node.computed) {
stack.prevName = null; // reset sort
return;
}

const prevName = stack.prevName;
const numKeys = stack.numKeys;
const thisName = getPropertyName(node);
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@ ruleTester.run("sort-keys", rule, {
`,
options: ["asc", { allowLineSeparatedGroups: true }],
languageOptions: { ecmaVersion: 2018 }
},

// ignoreComputedKeys
{
code: "var obj = { ['b']: 1, a: 2 }",
options: ["asc", { ignoreComputedKeys: true }]
},
{
code: "var obj = { a: 1, [c]: 2, b: 3 }",
options: ["asc", { ignoreComputedKeys: true }]
},
{
code: "var obj = { c: 1, ['b']: 2, a: 3 }",
options: ["asc", { ignoreComputedKeys: true }]
}
],
invalid: [
Expand Down Expand Up @@ -2268,6 +2282,22 @@ ruleTester.run("sort-keys", rule, {
}
}
]
},
{
code: "var obj = { d: 1, ['c']: 2, b: 3, a: 4 }",
options: ["asc", { ignoreComputedKeys: true, minKeys: 4 }],
errors: [
{
messageId: "sortKeys",
data: {
natural: "",
insensitive: "",
order: "asc",
thisName: "a",
prevName: "b"
}
}
]
}
]
});

0 comments on commit 8f70eb1

Please sign in to comment.