Skip to content
Permalink
Browse files

Update: Fix uglified object align in key-spacing (fixes #11414) (#12472)

  • Loading branch information
yeonjuan authored and kaicataldo committed Nov 15, 2019
1 parent 40791af commit 6503cb8d99e549fece53b80b110e890a7978b9fd
Showing with 449 additions and 2 deletions.
  1. +28 −2 lib/rules/key-spacing.js
  2. +421 −0 tests/lib/rules/key-spacing.js
@@ -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
@@ -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);
@@ -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);
}
};

0 comments on commit 6503cb8

Please sign in to comment.
You can’t perform that action at this time.