Skip to content

Commit d5b569b

Browse files
authored
Fix issue where ordering could not be applied to non-standard paths in jsonc/sort-keys rule (#75)
* Fix issue where ordering could not be applied to non-standard paths in `jsonc/sort-keys` rule * fix
1 parent 053f677 commit d5b569b

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

lib/rules/sort-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function parseOptions(options: UserOptions): ParsedOption[] {
179179
if (/^[$_a-z][\w$]*$/iu.test(name)) {
180180
path = `.${name}${path}`
181181
} else {
182-
path = `[${name}]${path}`
182+
path = `[${JSON.stringify(name)}]${path}`
183183
}
184184
} else if (p.type === "JSONArrayExpression") {
185185
const index = p.elements.indexOf(curr as never)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"test:nyc": "nyc --reporter=lcov npm run test:base",
2727
"test:debug": "mocha --require ts-node/register/transpile-only --inspect \"tests/lib/**/*.ts\" --reporter dot",
2828
"pretest:integrations": "npm run build:ts",
29-
"test:integrations": "mocha --require ts-node/register \"tests-integrations/lib/**/*.ts\" --reporter dot --timeout 60000",
29+
"test:integrations": "mocha --require ts-node/register \"tests-integrations/lib/**/*.ts\" --reporter dot --timeout 120000",
3030
"update": "ts-node --transpile-only ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
3131
"update-only": "ts-node --transpile-only ./tools/update.ts",
3232
"new": "ts-node ./tools/new-rule.ts",

tests/lib/rules/sort-keys.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,57 @@ tester.run("sort-keys", rule as any, {
255255
"Expected object keys to be in specified order. 'type' should be before 'minItems'.",
256256
],
257257
},
258+
259+
// Other
260+
{
261+
code: `
262+
{
263+
"\t": {
264+
"b": 42,
265+
"a": 42,
266+
},
267+
"arr": [
268+
{
269+
"d": 42,
270+
"c": 42,
271+
},
272+
{
273+
"f": 42,
274+
"e": 42,
275+
},
276+
]
277+
}`,
278+
output: `
279+
{
280+
"\t": {
281+
"a": 42,
282+
"b": 42,
283+
},
284+
"arr": [
285+
{
286+
"d": 42,
287+
"c": 42,
288+
},
289+
{
290+
"e": 42,
291+
"f": 42,
292+
},
293+
]
294+
}`,
295+
options: [
296+
{
297+
pathPattern: '^\\["\\\\t"\\]$',
298+
order: { type: "asc" },
299+
},
300+
{
301+
pathPattern: "^arr\\[1\\]$",
302+
order: { type: "asc" },
303+
},
304+
],
305+
errors: [
306+
"Expected object keys to be in ascending order. 'a' should be before 'b'.",
307+
"Expected object keys to be in ascending order. 'e' should be before 'f'.",
308+
],
309+
},
258310
],
259311
})

0 commit comments

Comments
 (0)