Skip to content

Commit 8385ecd

Browse files
authored
feat: multiline properties in rule key-spacing with option align (#16532)
Fixes #16490
1 parent a4e89db commit 8385ecd

2 files changed

Lines changed: 226 additions & 21 deletions

File tree

lib/rules/key-spacing.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ module.exports = {
334334

335335
const sourceCode = context.getSourceCode();
336336

337+
/**
338+
* Determines if the given property is key-value property.
339+
* @param {ASTNode} property Property node to check.
340+
* @returns {boolean} Whether the property is a key-value property.
341+
*/
342+
function isKeyValueProperty(property) {
343+
return !(
344+
(property.method ||
345+
property.shorthand ||
346+
property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
347+
);
348+
}
349+
337350
/**
338351
* Checks whether a property is a member of the property group it follows.
339352
* @param {ASTNode} lastMember The last Property known to be in the group.
@@ -342,9 +355,9 @@ module.exports = {
342355
*/
343356
function continuesPropertyGroup(lastMember, candidate) {
344357
const groupEndLine = lastMember.loc.start.line,
345-
candidateStartLine = candidate.loc.start.line;
358+
candidateValueStartLine = (isKeyValueProperty(candidate) ? candidate.value : candidate).loc.start.line;
346359

347-
if (candidateStartLine - groupEndLine <= 1) {
360+
if (candidateValueStartLine - groupEndLine <= 1) {
348361
return true;
349362
}
350363

@@ -358,7 +371,7 @@ module.exports = {
358371
if (
359372
leadingComments.length &&
360373
leadingComments[0].loc.start.line - groupEndLine <= 1 &&
361-
candidateStartLine - last(leadingComments).loc.end.line <= 1
374+
candidateValueStartLine - last(leadingComments).loc.end.line <= 1
362375
) {
363376
for (let i = 1; i < leadingComments.length; i++) {
364377
if (leadingComments[i].loc.start.line - leadingComments[i - 1].loc.end.line > 1) {
@@ -371,19 +384,6 @@ module.exports = {
371384
return false;
372385
}
373386

374-
/**
375-
* Determines if the given property is key-value property.
376-
* @param {ASTNode} property Property node to check.
377-
* @returns {boolean} Whether the property is a key-value property.
378-
*/
379-
function isKeyValueProperty(property) {
380-
return !(
381-
(property.method ||
382-
property.shorthand ||
383-
property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
384-
);
385-
}
386-
387387
/**
388388
* Starting from the given a node (a property.key node here) looks forward
389389
* until it finds the last token before a colon punctuator and returns it.

tests/lib/rules/key-spacing.js

Lines changed: 210 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ ruleTester.run("key-spacing", rule, {
278278
" method() {",
279279
" return 42;",
280280
" },",
281-
" baz: 456",
281+
" baz: 456,",
282+
" 10: ",
283+
" 10",
282284
"};"
283285
].join("\n"),
284286
options: [{ align: "value" }],
@@ -360,6 +362,10 @@ ruleTester.run("key-spacing", rule, {
360362
" bat: function() {",
361363
" return this.a;",
362364
" },",
365+
" barfoo:",
366+
" [",
367+
" 1",
368+
" ],",
363369
" baz: 42",
364370
"};"
365371
].join("\n"),
@@ -633,6 +639,10 @@ ruleTester.run("key-spacing", rule, {
633639
" internalGroup: {",
634640
" internal : true,",
635641
" ext : false",
642+
" },",
643+
" func3:",
644+
" function () {",
645+
" var test3 = true;",
636646
" }",
637647
"})"
638648
].join("\n"),
@@ -971,6 +981,80 @@ ruleTester.run("key-spacing", rule, {
971981
}
972982
}],
973983
parserOptions: { ecmaVersion: 6 }
984+
},
985+
986+
// https://github.com/eslint/eslint/issues/16490
987+
{
988+
code: `
989+
var foo =
990+
{
991+
id: 1,
992+
code: 2,
993+
[n]: 3,
994+
message:
995+
"some value on the next line",
996+
};
997+
`,
998+
options: [{
999+
align: "value"
1000+
}],
1001+
parserOptions: { ecmaVersion: 6 }
1002+
},
1003+
{
1004+
code: `
1005+
var foo =
1006+
{
1007+
id : 1,
1008+
code : 2,
1009+
message :
1010+
"some value on the next line",
1011+
};
1012+
`,
1013+
options: [{
1014+
align: "colon",
1015+
beforeColon: true
1016+
}]
1017+
},
1018+
{
1019+
code: `
1020+
({
1021+
a: 1,
1022+
// different group
1023+
bcd:
1024+
2
1025+
})
1026+
`,
1027+
options: [{
1028+
align: "value"
1029+
}]
1030+
},
1031+
{
1032+
code: `
1033+
({
1034+
foo : 1,
1035+
bar : 2,
1036+
foobar :
1037+
3
1038+
})
1039+
`,
1040+
options: [{
1041+
align: "value",
1042+
beforeColon: true,
1043+
mode: "minimum"
1044+
}]
1045+
},
1046+
{
1047+
code: `
1048+
({
1049+
oneLine: 1,
1050+
["some key " +
1051+
"spanning multiple lines"]: 2
1052+
})
1053+
`,
1054+
options: [{
1055+
align: "value"
1056+
}],
1057+
parserOptions: { ecmaVersion: 6 }
9741058
}],
9751059
invalid: [{
9761060
code: "var a ={'key' : value };",
@@ -1464,7 +1548,9 @@ ruleTester.run("key-spacing", rule, {
14641548
" method() {",
14651549
" return 42;",
14661550
" },",
1467-
" baz: 456",
1551+
" baz: 456,",
1552+
" 10: ",
1553+
" 10",
14681554
"};"
14691555
].join("\n"),
14701556
output: [
@@ -1473,7 +1559,9 @@ ruleTester.run("key-spacing", rule, {
14731559
" method() {",
14741560
" return 42;",
14751561
" },",
1476-
" baz: 456",
1562+
" baz: 456,",
1563+
" 10: ",
1564+
" 10",
14771565
"};"
14781566
].join("\n"),
14791567
options: [{ align: "value" }],
@@ -2374,6 +2462,123 @@ ruleTester.run("key-spacing", rule, {
23742462
{ messageId: "extraValue", data: { computed: "", key: "🎁" }, line: 4, column: 21, type: "Literal" },
23752463
{ messageId: "extraValue", data: { computed: "", key: "🇮🇳" }, line: 5, column: 23, type: "Literal" }
23762464
]
2377-
}
2378-
]
2465+
},
2466+
2467+
// https://github.com/eslint/eslint/issues/16490
2468+
{
2469+
code: `
2470+
var foo =
2471+
{
2472+
id: 1,
2473+
code: 2,
2474+
[n]: 3,
2475+
message:
2476+
"some value on the next line",
2477+
};
2478+
`,
2479+
output: `
2480+
var foo =
2481+
{
2482+
id: 1,
2483+
code: 2,
2484+
[n]: 3,
2485+
message:
2486+
"some value on the next line",
2487+
};
2488+
`,
2489+
options: [{
2490+
align: "value"
2491+
}],
2492+
parserOptions: { ecmaVersion: 6 },
2493+
errors: [
2494+
{ messageId: "extraValue", data: { computed: "", key: "id" }, line: 4, column: 19, type: "Literal" },
2495+
{ messageId: "extraValue", data: { computed: "", key: "code" }, line: 5, column: 21, type: "Literal" },
2496+
{ messageId: "extraValue", data: { computed: "computed ", key: "n" }, line: 6, column: 20, type: "Literal" }
2497+
]
2498+
},
2499+
{
2500+
code: `
2501+
var foo =
2502+
{
2503+
id : 1,
2504+
code : 2,
2505+
message :
2506+
"some value on the next line",
2507+
};
2508+
`,
2509+
output: `
2510+
var foo =
2511+
{
2512+
id : 1,
2513+
code : 2,
2514+
message :
2515+
"some value on the next line",
2516+
};
2517+
`,
2518+
options: [{
2519+
align: "colon",
2520+
beforeColon: true
2521+
}],
2522+
errors: [
2523+
{ messageId: "extraKey", data: { computed: "", key: "id" }, line: 4, column: 19, type: "Identifier" },
2524+
{ messageId: "extraKey", data: { computed: "", key: "code" }, line: 5, column: 21, type: "Identifier" }
2525+
]
2526+
},
2527+
{
2528+
code: `
2529+
({
2530+
a: 1,
2531+
// different group
2532+
bcd:
2533+
2
2534+
})
2535+
`,
2536+
output: `
2537+
({
2538+
a: 1,
2539+
// different group
2540+
bcd:
2541+
2
2542+
})
2543+
`,
2544+
options: [{
2545+
align: "value"
2546+
}],
2547+
errors: [
2548+
{ messageId: "extraValue", data: { computed: "", key: "a" }, line: 3, column: 18, type: "Literal" }
2549+
]
2550+
},
2551+
{
2552+
code: [
2553+
"({",
2554+
" singleLine : 10,",
2555+
" newGroup :",
2556+
" function() {",
2557+
" var test3 = true;",
2558+
" }",
2559+
"})"
2560+
].join("\n"),
2561+
output: [
2562+
"({",
2563+
" singleLine: 10,",
2564+
" newGroup:",
2565+
" function() {",
2566+
" var test3 = true;",
2567+
" }",
2568+
"})"
2569+
].join("\n"),
2570+
options: [{
2571+
multiLine: {
2572+
beforeColon: false
2573+
},
2574+
align: {
2575+
on: "colon",
2576+
beforeColon: true
2577+
}
2578+
}],
2579+
errors: [
2580+
{ messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 2, column: 15, type: "Identifier" },
2581+
{ messageId: "extraKey", data: { computed: "", key: "newGroup" }, line: 3, column: 13, type: "Identifier" }
2582+
]
2583+
}]
23792584
});

0 commit comments

Comments
 (0)