diff --git a/docs/rules/html-indent.md b/docs/rules/html-indent.md
index 9a325d9c7..51d7cca65 100644
--- a/docs/rules/html-indent.md
+++ b/docs/rules/html-indent.md
@@ -23,33 +23,38 @@ This rule enforces a consistent indentation style in ``. The default s
```html
-
- Hello.
-
+
+ Hello.
+
```
```html
-
- Hello.
-
-
- {{
- displayMessage
- }}
-
+
+ Hello.
+
+
+ World.
+
+
+ {{
+ displayMessage
+ }}
+
```
@@ -57,33 +62,35 @@ This rule enforces a consistent indentation style in ``. The default s
```json
{
- "vue/html-indent": ["error", type, {
- "attribute": 1,
- "closeBracket": 0,
- "ignores": []
- }]
+ "vue/html-indent": ["error", type, {
+ "attribute": 1,
+ "closeBracket": 0,
+ "alignAttributesVertically": true,
+ "ignores": []
+ }]
}
```
- `type` (`number | "tab"`) ... The type of indentation. Default is `2`. If this is a number, it's the number of spaces for one indent. If this is `"tab"`, it uses one tab for one indent.
- `attribute` (`integer`) ... The multiplier of indentation for attributes. Default is `1`.
- `closeBracket` (`integer`) ... The multiplier of indentation for right brackets. Default is `0`.
+- `alignAttributesVertically` (`boolean`) ... Condition for whether attributes should be vertically aligned to the first attribute in multiline case or not. Default is `true`
- `ignores` (`string[]`) ... The selector to ignore nodes. The AST spec is [here](https://github.com/mysticatea/vue-eslint-parser/blob/master/docs/ast.md). You can use [esquery](https://github.com/estools/esquery#readme) to select nodes. Default is an empty array.
:+1: Examples of **correct** code for `{attribute: 1, closeBracket: 1}`:
```html
-
- Text
-
+
+ Text
+
```
@@ -91,16 +98,16 @@ This rule enforces a consistent indentation style in ``. The default s
```html
-
- Text
-
+
+ Text
+
```
@@ -108,9 +115,31 @@ This rule enforces a consistent indentation style in ``. The default s
```html
-
+
+
+```
+
+:+1: Examples of **correct** code for `{alignAttributesVertically: true}`:
+
+```html
+
+
+
+```
+
+:+1: Examples of **correct** code for `{alignAttributesVertically: false}`:
+
+```html
+
+
```
diff --git a/lib/rules/html-indent.js b/lib/rules/html-indent.js
index f1f968ed6..a888395f6 100644
--- a/lib/rules/html-indent.js
+++ b/lib/rules/html-indent.js
@@ -34,6 +34,7 @@ function parseOptions (type, options) {
attribute: 1,
closeBracket: 0,
switchCase: 0,
+ alignAttributesVertically: true,
ignores: []
}
@@ -53,6 +54,9 @@ function parseOptions (type, options) {
if (Number.isSafeInteger(options.switchCase)) {
ret.switchCase = options.switchCase
}
+ if (options.alignAttributesVertically != null) {
+ ret.alignAttributesVertically = options.alignAttributesVertically
+ }
if (options.ignores != null) {
ret.ignores = options.ignores
}
@@ -304,8 +308,9 @@ function create (context) {
* @param {number} offset The offset to set.
* @returns {void}
*/
- function processNodeList (nodeList, leftToken, rightToken, offset) {
+ function processNodeList (nodeList, leftToken, rightToken, offset, alignVertically) {
let t
+ alignVertically = alignVertically != null ? alignVertically : true
if (nodeList.length >= 1) {
let lastToken = leftToken
@@ -349,11 +354,20 @@ function create (context) {
setOffset(baseToken, offset, leftToken)
}
- // Align the rest tokens to the first token.
+ // Set baseline
if (nodeList.some(isBeginningOfLine)) {
setBaseline(baseToken)
}
- setOffset(alignTokens, 0, baseToken)
+
+ if (alignVertically) {
+ // Align the rest tokens to the first token.
+ setOffset(alignTokens, 0, baseToken)
+ } else {
+ // Align tokens relatively to passed root node
+ // So it's possible to force proper position for VAttributes
+ const rootNode = nodeList && nodeList[0].parent
+ setOffset(alignTokens, offset, template.getFirstToken(rootNode))
+ }
}
}
@@ -760,7 +774,7 @@ function create (context) {
const openToken = template.getFirstToken(node)
const closeToken = template.getLastToken(node)
- processNodeList(node.attributes, openToken, null, options.attribute)
+ processNodeList(node.attributes, openToken, null, options.attribute, options.alignAttributesVertically)
if (closeToken != null && closeToken.type.endsWith('TagClose')) {
setOffset(closeToken, options.closeBracket, openToken)
}
@@ -1294,6 +1308,7 @@ module.exports = {
'attribute': { type: 'integer', minimum: 0 },
'closeBracket': { type: 'integer', minimum: 0 },
'switchCase': { type: 'integer', minimum: 0 },
+ 'alignAttributesVertically': { type: 'boolean' },
'ignores': {
type: 'array',
items: {
diff --git a/tests/lib/rules/html-indent.js b/tests/lib/rules/html-indent.js
index d7e78407b..20088eb09 100644
--- a/tests/lib/rules/html-indent.js
+++ b/tests/lib/rules/html-indent.js
@@ -1269,6 +1269,40 @@ tester.run('html-indent', rule, {
options: [4, { switchCase: 1 }]
},
+ // options.alignAttributesVertically
+ {
+ code: unIndent`
+
+
+
+ `,
+ options: [2, {
+ alignAttributesVertically: false
+ }]
+ },
+ {
+ code: unIndent`
+
+
+
+ `,
+ options: [2, {
+ alignAttributesVertically: false
+ }]
+ },
+
// Comments
unIndent`
@@ -1467,6 +1501,28 @@ tester.run('html-indent', rule, {
"
/>
+ `,
+ unIndent`
+
+
+
+ `,
+ unIndent`
+
+
+
+ `,
+ unIndent`
+
+