Skip to content

Commit c5c7086

Browse files
yeonjuanbtmills
authored andcommitted
Fix: ignore aligning single line in key-spacing (fixes #11414) (#12652)
* Fix: ignore aligning single line in key-spacing (fixes #11414) * add test cases * add tests for protect against regressions
1 parent 9986d9e commit c5c7086

2 files changed

Lines changed: 370 additions & 15 deletions

File tree

lib/rules/key-spacing.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ function isSingleLine(node) {
4242
return (node.loc.end.line === node.loc.start.line);
4343
}
4444

45+
/**
46+
* Checks whether the properties on a single line.
47+
* @param {ASTNode[]} properties List of Property AST nodes.
48+
* @returns {boolean} True if all properies is on a single line.
49+
*/
50+
function isSingleLineProperties(properties) {
51+
const [firstProp] = properties,
52+
lastProp = last(properties);
53+
54+
return firstProp.loc.start.line === lastProp.loc.end.line;
55+
}
56+
4557
/**
4658
* Initializes a single option property from the configuration with defaults for undefined values
4759
* @param {Object} toOptions Object to be initialized
@@ -583,17 +595,6 @@ module.exports = {
583595
}
584596
}
585597

586-
/**
587-
* Verifies vertical alignment, taking into account groups of properties.
588-
* @param {ASTNode} node ObjectExpression node being evaluated.
589-
* @returns {void}
590-
*/
591-
function verifyAlignment(node) {
592-
createGroups(node).forEach(group => {
593-
verifyGroupAlignment(group.filter(isKeyValueProperty));
594-
});
595-
}
596-
597598
/**
598599
* Verifies spacing of property conforms to specified options.
599600
* @param {ASTNode} node Property node being evaluated.
@@ -611,17 +612,35 @@ module.exports = {
611612

612613
/**
613614
* Verifies spacing of each property in a list.
614-
* @param {ASTNode[]} properties List of Property AST nodes.
615+
* @param {ASTNode[]} properties List of Property AST nodes.
616+
* @param {Object} lineOptions Configured singleLine or multiLine options
615617
* @returns {void}
616618
*/
617-
function verifyListSpacing(properties) {
619+
function verifyListSpacing(properties, lineOptions) {
618620
const length = properties.length;
619621

620622
for (let i = 0; i < length; i++) {
621-
verifySpacing(properties[i], singleLineOptions);
623+
verifySpacing(properties[i], lineOptions);
622624
}
623625
}
624626

627+
/**
628+
* Verifies vertical alignment, taking into account groups of properties.
629+
* @param {ASTNode} node ObjectExpression node being evaluated.
630+
* @returns {void}
631+
*/
632+
function verifyAlignment(node) {
633+
createGroups(node).forEach(group => {
634+
const properties = group.filter(isKeyValueProperty);
635+
636+
if (properties.length > 0 && isSingleLineProperties(properties)) {
637+
verifyListSpacing(properties, multiLineOptions);
638+
} else {
639+
verifyGroupAlignment(properties);
640+
}
641+
});
642+
}
643+
625644
//--------------------------------------------------------------------------
626645
// Public API
627646
//--------------------------------------------------------------------------
@@ -631,7 +650,7 @@ module.exports = {
631650
return {
632651
ObjectExpression(node) {
633652
if (isSingleLine(node)) {
634-
verifyListSpacing(node.properties.filter(isKeyValueProperty));
653+
verifyListSpacing(node.properties.filter(isKeyValueProperty), singleLineOptions);
635654
} else {
636655
verifyAlignment(node);
637656
}

0 commit comments

Comments
 (0)