Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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