Skip to content

Commit

Permalink
Fix: ignore aligning single line in key-spacing (fixes #11414) (#12652)
Browse files Browse the repository at this point in the history
* Fix: ignore aligning single line in key-spacing (fixes #11414)

* add test cases

* add tests for protect against regressions
  • Loading branch information
yeonjuan authored and btmills committed Dec 20, 2019
1 parent 9986d9e commit c5c7086
Show file tree
Hide file tree
Showing 2 changed files with 370 additions and 15 deletions.
49 changes: 34 additions & 15 deletions lib/rules/key-spacing.js
Expand Up @@ -42,6 +42,18 @@ function isSingleLine(node) {
return (node.loc.end.line === node.loc.start.line);
}

/**
* Checks whether the properties on a single line.
* @param {ASTNode[]} properties List of Property AST nodes.
* @returns {boolean} True if all properies is on a single line.
*/
function isSingleLineProperties(properties) {
const [firstProp] = properties,
lastProp = last(properties);

return firstProp.loc.start.line === lastProp.loc.end.line;
}

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

/**
* Verifies vertical alignment, taking into account groups of properties.
* @param {ASTNode} node ObjectExpression node being evaluated.
* @returns {void}
*/
function verifyAlignment(node) {
createGroups(node).forEach(group => {
verifyGroupAlignment(group.filter(isKeyValueProperty));
});
}

/**
* Verifies spacing of property conforms to specified options.
* @param {ASTNode} node Property node being evaluated.
Expand All @@ -611,17 +612,35 @@ module.exports = {

/**
* Verifies spacing of each property in a list.
* @param {ASTNode[]} properties List of Property AST nodes.
* @param {ASTNode[]} properties List of Property AST nodes.
* @param {Object} lineOptions Configured singleLine or multiLine options
* @returns {void}
*/
function verifyListSpacing(properties) {
function verifyListSpacing(properties, lineOptions) {
const length = properties.length;

for (let i = 0; i < length; i++) {
verifySpacing(properties[i], singleLineOptions);
verifySpacing(properties[i], lineOptions);
}
}

/**
* Verifies vertical alignment, taking into account groups of properties.
* @param {ASTNode} node ObjectExpression node being evaluated.
* @returns {void}
*/
function verifyAlignment(node) {
createGroups(node).forEach(group => {
const properties = group.filter(isKeyValueProperty);

if (properties.length > 0 && isSingleLineProperties(properties)) {
verifyListSpacing(properties, multiLineOptions);
} else {
verifyGroupAlignment(properties);
}
});
}

//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
Expand All @@ -631,7 +650,7 @@ module.exports = {
return {
ObjectExpression(node) {
if (isSingleLine(node)) {
verifyListSpacing(node.properties.filter(isKeyValueProperty));
verifyListSpacing(node.properties.filter(isKeyValueProperty), singleLineOptions);
} else {
verifyAlignment(node);
}
Expand Down

0 comments on commit c5c7086

Please sign in to comment.