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: add baseIndent option to vue/html-indent (fixes #292) #678

Merged
merged 2 commits into from
Nov 26, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/rules/html-indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This rule enforces a consistent indentation style in `<template>`. The default s
{
"vue/html-indent": ["error", type, {
"attribute": 1,
"baseIndent": 1,
"closeBracket": 0,
"alignAttributesVertically": true,
"ignores": []
Expand All @@ -74,6 +75,7 @@ This rule enforces a consistent indentation style in `<template>`. The default s

- `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`.
- `baseIndent` (`integer`) ... The multiplier of indentation for top-level statements. 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.
Expand Down Expand Up @@ -144,3 +146,15 @@ This rule enforces a consistent indentation style in `<template>`. The default s
/>
</template>
```

:+1: Examples of **correct** code for `{baseIndent: 0}`:

```html
<template>
<div>
<span>
Hello!
</span>
</div>
</template>
```
1 change: 1 addition & 0 deletions lib/rules/html-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = {
type: 'object',
properties: {
'attribute': { type: 'integer', minimum: 0 },
'baseIndent': { type: 'integer', minimum: 0 },
'closeBracket': { type: 'integer', minimum: 0 },
'switchCase': { type: 'integer', minimum: 0 },
'alignAttributesVertically': { type: 'boolean' },
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti

VElement (node) {
if (node.name !== 'pre') {
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, 1)
const isTopLevel = node.parent.type !== 'VElement'
const offset = isTopLevel ? options.baseIndent : 1
processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, offset)
} else {
const startTagToken = tokenStore.getFirstToken(node)
const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag)
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/html-indent/opts-base-indent-0.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--{"options":[4,{"baseIndent":0}]}-->
<template>
<div>
<span>
Hello!
</div>
After
</template>
8 changes: 8 additions & 0 deletions tests/fixtures/html-indent/opts-base-indent-1.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--{"options":[4,{"baseIndent":1}]}-->
<template>
<div>
<span>
Hello!
</div>
After
</template>
8 changes: 8 additions & 0 deletions tests/fixtures/html-indent/opts-base-indent-2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!--{"options":[4,{"baseIndent":2}]}-->
<template>
<div>
<span>
Hello!
</div>
After
</template>