Skip to content

Commit 6503cb8

Browse files
yeonjuankaicataldo
authored andcommitted
Update: Fix uglified object align in key-spacing (fixes #11414) (#12472)
1 parent 40791af commit 6503cb8

File tree

2 files changed

+449
-2
lines changed

2 files changed

+449
-2
lines changed

lib/rules/key-spacing.js

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

45+
/**
46+
* Checks whether both nodes are on the same line.
47+
* @param {ASTNode} nodeA AST Node being evaluated.
48+
* @param {ASTNode} nodeB AST Node being evaluated.
49+
* @returns {boolean} True if both nodes are on the same line.
50+
*/
51+
function isOnSameLine(nodeA, nodeB) {
52+
return (nodeA.loc.end.line === nodeB.loc.end.line);
53+
}
54+
55+
/**
56+
* Checks whether the properties of a node on the same line.
57+
* @param {ASTNode} node node
58+
* @returns {boolean} True if the properties of a node are on the same line.
59+
*/
60+
function isPropertiesOnSameLine(node) {
61+
if (node.properties.length <= 1) {
62+
return true;
63+
}
64+
const [firstProperty] = node.properties;
65+
66+
return node.properties.every(property => isOnSameLine(firstProperty, property));
67+
}
68+
4569
/**
4670
* Initializes a single option property from the configuration with defaults for undefined values
4771
* @param {Object} toOptions Object to be initialized
@@ -630,7 +654,7 @@ module.exports = {
630654

631655
return {
632656
ObjectExpression(node) {
633-
if (isSingleLine(node)) {
657+
if (isSingleLine(node) || isPropertiesOnSameLine(node)) {
634658
verifyListSpacing(node.properties.filter(isKeyValueProperty));
635659
} else {
636660
verifyAlignment(node);
@@ -643,7 +667,9 @@ module.exports = {
643667
// Obey beforeColon and afterColon in each property as configured
644668
return {
645669
Property(node) {
646-
verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions);
670+
const option = (isSingleLine(node.parent) || isPropertiesOnSameLine(node.parent)) ? singleLineOptions : multiLineOptions;
671+
672+
verifySpacing(node, option);
647673
}
648674
};
649675

0 commit comments

Comments
 (0)