Skip to content

Commit

Permalink
Update: Fix uglified object align in key-spacing (fixes #11414) (#12472)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan authored and kaicataldo committed Nov 15, 2019
1 parent 40791af commit 6503cb8
Show file tree
Hide file tree
Showing 2 changed files with 449 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/rules/key-spacing.js
Expand Up @@ -42,6 +42,30 @@ function isSingleLine(node) {
return (node.loc.end.line === node.loc.start.line);
}

/**
* Checks whether both nodes are on the same line.
* @param {ASTNode} nodeA AST Node being evaluated.
* @param {ASTNode} nodeB AST Node being evaluated.
* @returns {boolean} True if both nodes are on the same line.
*/
function isOnSameLine(nodeA, nodeB) {
return (nodeA.loc.end.line === nodeB.loc.end.line);
}

/**
* Checks whether the properties of a node on the same line.
* @param {ASTNode} node node
* @returns {boolean} True if the properties of a node are on the same line.
*/
function isPropertiesOnSameLine(node) {
if (node.properties.length <= 1) {
return true;
}
const [firstProperty] = node.properties;

return node.properties.every(property => isOnSameLine(firstProperty, property));
}

/**
* Initializes a single option property from the configuration with defaults for undefined values
* @param {Object} toOptions Object to be initialized
Expand Down Expand Up @@ -630,7 +654,7 @@ module.exports = {

return {
ObjectExpression(node) {
if (isSingleLine(node)) {
if (isSingleLine(node) || isPropertiesOnSameLine(node)) {
verifyListSpacing(node.properties.filter(isKeyValueProperty));
} else {
verifyAlignment(node);
Expand All @@ -643,7 +667,9 @@ module.exports = {
// Obey beforeColon and afterColon in each property as configured
return {
Property(node) {
verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions);
const option = (isSingleLine(node.parent) || isPropertiesOnSameLine(node.parent)) ? singleLineOptions : multiLineOptions;

verifySpacing(node, option);
}
};

Expand Down

0 comments on commit 6503cb8

Please sign in to comment.