Skip to content

Commit 8f70eb1

Browse files
mdjermanovicnzakas
andauthored
feat: Add ignoreComputedKeys option in sort-keys rule (#19162)
* 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>
1 parent 0c6b842 commit 8f70eb1

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

docs/src/rules/sort-keys.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The 2nd option is an object which has the following properties.
8989
* `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.
9090
* `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.
9191
* `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`.
92+
* `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`.
9293

9394
Example for a list:
9495

@@ -372,6 +373,35 @@ var obj7 = {
372373

373374
:::
374375

376+
### ignoreComputedKeys
377+
378+
Examples of **correct** code for the `{ignoreComputedKeys: true}` option:
379+
380+
::: correct
381+
382+
```js
383+
/*eslint sort-keys: ["error", "asc", {ignoreComputedKeys: true}]*/
384+
385+
let obj1 = {
386+
[b]: 1,
387+
a: 2
388+
}
389+
390+
let obj2 = {
391+
c: 1,
392+
[b]: 2,
393+
a: 3
394+
}
395+
396+
let obj3 = {
397+
c: 1,
398+
["b"]: 2,
399+
a: 3
400+
}
401+
```
402+
403+
:::
404+
375405
## When Not To Use It
376406

377407
If you don't want to notify about properties' order, then it's safe to disable this rule.

lib/rules/sort-keys.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module.exports = {
8383
defaultOptions: ["asc", {
8484
allowLineSeparatedGroups: false,
8585
caseSensitive: true,
86+
ignoreComputedKeys: false,
8687
minKeys: 2,
8788
natural: false
8889
}],
@@ -112,6 +113,9 @@ module.exports = {
112113
},
113114
allowLineSeparatedGroups: {
114115
type: "boolean"
116+
},
117+
ignoreComputedKeys: {
118+
type: "boolean"
115119
}
116120
},
117121
additionalProperties: false
@@ -124,7 +128,7 @@ module.exports = {
124128
},
125129

126130
create(context) {
127-
const [order, { caseSensitive, natural, minKeys, allowLineSeparatedGroups }] = context.options;
131+
const [order, { caseSensitive, natural, minKeys, allowLineSeparatedGroups, ignoreComputedKeys }] = context.options;
128132
const insensitive = !caseSensitive;
129133
const isValidOrder = isValidOrders[
130134
order + (insensitive ? "I" : "") + (natural ? "N" : "")
@@ -160,6 +164,11 @@ module.exports = {
160164
return;
161165
}
162166

167+
if (ignoreComputedKeys && node.computed) {
168+
stack.prevName = null; // reset sort
169+
return;
170+
}
171+
163172
const prevName = stack.prevName;
164173
const numKeys = stack.numKeys;
165174
const thisName = getPropertyName(node);

tests/lib/rules/sort-keys.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ ruleTester.run("sort-keys", rule, {
358358
`,
359359
options: ["asc", { allowLineSeparatedGroups: true }],
360360
languageOptions: { ecmaVersion: 2018 }
361+
},
362+
363+
// ignoreComputedKeys
364+
{
365+
code: "var obj = { ['b']: 1, a: 2 }",
366+
options: ["asc", { ignoreComputedKeys: true }]
367+
},
368+
{
369+
code: "var obj = { a: 1, [c]: 2, b: 3 }",
370+
options: ["asc", { ignoreComputedKeys: true }]
371+
},
372+
{
373+
code: "var obj = { c: 1, ['b']: 2, a: 3 }",
374+
options: ["asc", { ignoreComputedKeys: true }]
361375
}
362376
],
363377
invalid: [
@@ -2268,6 +2282,22 @@ ruleTester.run("sort-keys", rule, {
22682282
}
22692283
}
22702284
]
2285+
},
2286+
{
2287+
code: "var obj = { d: 1, ['c']: 2, b: 3, a: 4 }",
2288+
options: ["asc", { ignoreComputedKeys: true, minKeys: 4 }],
2289+
errors: [
2290+
{
2291+
messageId: "sortKeys",
2292+
data: {
2293+
natural: "",
2294+
insensitive: "",
2295+
order: "asc",
2296+
thisName: "a",
2297+
prevName: "b"
2298+
}
2299+
}
2300+
]
22712301
}
22722302
]
22732303
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy