Skip to content

Commit 147c765

Browse files
mysticateamichalsnik
authored andcommitted
Update: add baseIndent option to vue/html-indent (fixes #292) (#678)
1 parent 61a667f commit 147c765

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

docs/rules/html-indent.md

+14
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ This rule enforces a consistent indentation style in `<template>`. The default s
6565
{
6666
"vue/html-indent": ["error", type, {
6767
"attribute": 1,
68+
"baseIndent": 1,
6869
"closeBracket": 0,
6970
"alignAttributesVertically": true,
7071
"ignores": []
@@ -74,6 +75,7 @@ This rule enforces a consistent indentation style in `<template>`. The default s
7475

7576
- `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.
7677
- `attribute` (`integer`) ... The multiplier of indentation for attributes. Default is `1`.
78+
- `baseIndent` (`integer`) ... The multiplier of indentation for top-level statements. Default is `1`.
7779
- `closeBracket` (`integer`) ... The multiplier of indentation for right brackets. Default is `0`.
7880
- `alignAttributesVertically` (`boolean`) ... Condition for whether attributes should be vertically aligned to the first attribute in multiline case or not. Default is `true`
7981
- `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.
@@ -144,3 +146,15 @@ This rule enforces a consistent indentation style in `<template>`. The default s
144146
/>
145147
</template>
146148
```
149+
150+
:+1: Examples of **correct** code for `{baseIndent: 0}`:
151+
152+
```html
153+
<template>
154+
<div>
155+
<span>
156+
Hello!
157+
</span>
158+
</div>
159+
</template>
160+
```

lib/rules/html-indent.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
type: 'object',
4545
properties: {
4646
'attribute': { type: 'integer', minimum: 0 },
47+
'baseIndent': { type: 'integer', minimum: 0 },
4748
'closeBracket': { type: 'integer', minimum: 0 },
4849
'switchCase': { type: 'integer', minimum: 0 },
4950
'alignAttributesVertically': { type: 'boolean' },

lib/utils/indent-common.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
882882

883883
VElement (node) {
884884
if (node.name !== 'pre') {
885-
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, 1)
885+
const isTopLevel = node.parent.type !== 'VElement'
886+
const offset = isTopLevel ? options.baseIndent : 1
887+
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, offset)
886888
} else {
887889
const startTagToken = tokenStore.getFirstToken(node)
888890
const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"options":[4,{"baseIndent":0}]}-->
2+
<template>
3+
<div>
4+
<span>
5+
Hello!
6+
</div>
7+
After
8+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"options":[4,{"baseIndent":1}]}-->
2+
<template>
3+
<div>
4+
<span>
5+
Hello!
6+
</div>
7+
After
8+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"options":[4,{"baseIndent":2}]}-->
2+
<template>
3+
<div>
4+
<span>
5+
Hello!
6+
</div>
7+
After
8+
</template>

0 commit comments

Comments
 (0)