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

Add autofixer to attribute-indentation rule #2271

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Each rule has emojis denoting:

| Name | ✅ | 💅 | ⌨️ | 🔧 |
| :-------------------------------------------------------------------------------------------------------- | :-- | :-- | :-- | --- |
| [attribute-indentation](./docs/rule/attribute-indentation.md) | | | | |
| [attribute-indentation](./docs/rule/attribute-indentation.md) | | | | 🔧 |
| [block-indentation](./docs/rule/block-indentation.md) | | 💅 | | 🔧 |
| [builtin-component-arguments](./docs/rule/builtin-component-arguments.md) | ✅ | | | |
| [deprecated-inline-view-helper](./docs/rule/deprecated-inline-view-helper.md) | ✅ | | | |
Expand Down
2 changes: 2 additions & 0 deletions docs/rule/attribute-indentation.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# attribute-indentation

🔧 The `--fix` option on the command line can automatically fix some of the problems reported by this rule.

This rule requires the positional params, attributes, and block params of the helper/component to be indented by moving them to multiple lines when the open invocation has more than 80 characters (configurable).

## Examples
Expand Down
23 changes: 23 additions & 0 deletions lib/helpers/ast-node-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,33 @@ function attributeTextValue(node) {
}
}

/**
* This function allows to get a node inside a node given a path
*
* @param {Node} node
* @param {String[]} path - A list of property to search for in the node
* @returns {Node}
*
* @example
* getNodeFromPath(node, ['program', 'body', 1]) === node.program.body[1]
*/
export function getNodeFromPath(node, path) {
let value = node;

if (path) {
for (const property of path) {
value = value[property];
}
}

return value;
}

export default {
attributeTextValue,
childrenFor,
findAttribute,
getNodeFromPath,
hasAttribute,
hasChildren,
hasAnyAttribute,
Expand Down