Skip to content

Commit fd33ceb

Browse files
committed
New: vue/script-indent (fixes #118)
1 parent fdce743 commit fd33ceb

File tree

113 files changed

+1819
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1819
-32
lines changed

.eslintignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
node_modules
2-
lib/*-rules.js
3-
coverage
4-
.nyc_output
1+
/.nyc_output
2+
/coverage
3+
/lib/*-rules.js
4+
/node_modules
5+
/tests/fixtures
6+
/tests/integrations/*/node_modules

docs/rules/script-indent.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Enforce consistent indentation (script-indent)
2+
3+
This rule is similar to core [indent](https://eslint.org/docs/rules/indent) rule, but it has an option for inside of `<script>` tag.
4+
5+
## Rule Details
6+
7+
This rule has some options.
8+
9+
```json
10+
{
11+
"script-indent": ["error", TYPE, {
12+
"baseIndent": 0,
13+
"switchCase": 0,
14+
"ignores": []
15+
}]
16+
}
17+
```
18+
19+
- `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.
20+
- `baseIndent` (`integer`) ... The multiplier of indentation for top-level statements. Default is `0`.
21+
- `switchCase` (`integer`) ... The multiplier of indentation for `case`/`default` clauses. Default is `0`.
22+
- `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.
23+
24+
:+1: Examples of **correct** code for this rule:
25+
26+
```js
27+
/*eslint script-indent: "error"*/
28+
<script>
29+
let a = {
30+
foo: 1,
31+
bar: 2
32+
}
33+
let b = {
34+
foo: 1,
35+
bar: 2
36+
},
37+
c = {
38+
foo: 1,
39+
bar: 2
40+
}
41+
const d = {
42+
foo: 1,
43+
bar: 2
44+
},
45+
e = {
46+
foo: 1,
47+
bar: 2
48+
}
49+
</script>
50+
```
51+
52+
:+1: Examples of **correct** code for this rule:
53+
54+
```js
55+
/*eslint script-indent: ["error", 2, {"baseIndent": 1}]*/
56+
<script>
57+
let a = {
58+
foo: 1,
59+
bar: 2
60+
}
61+
let b = {
62+
foo: 1,
63+
bar: 2
64+
},
65+
c = {
66+
foo: 1,
67+
bar: 2
68+
}
69+
const d = {
70+
foo: 1,
71+
bar: 2
72+
},
73+
e = {
74+
foo: 1,
75+
bar: 2
76+
}
77+
</script>
78+
```
79+
80+
## Related rules
81+
82+
- [indent](https://eslint.org/docs/rules/indent)
83+
- [vue/html-indent](./html-indent.md)

lib/rules/html-indent.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ const utils = require('../utils')
1818

1919
module.exports = {
2020
create (context) {
21-
const mainSelector = "VElement[parent.type='VDocumentFragment']:exit"
2221
const tokenStore =
2322
context.parserServices.getTemplateBodyTokenStore &&
2423
context.parserServices.getTemplateBodyTokenStore()
25-
const visitor = indentCommon.defineVisitor(context, mainSelector, tokenStore)
24+
const visitor = indentCommon.defineVisitor(context, tokenStore, { baseIndent: 1 })
2625

2726
return utils.defineTemplateBodyVisitor(context, visitor)
2827
},

lib/rules/script-indent.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author Toru Nagashima
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const indentCommon = require('../utils/indent-common')
12+
13+
// ------------------------------------------------------------------------------
14+
// Rule Definition
15+
// ------------------------------------------------------------------------------
16+
17+
module.exports = {
18+
create (context) {
19+
return indentCommon.defineVisitor(context, context.getSourceCode(), {})
20+
},
21+
meta: {
22+
docs: {
23+
description: 'enforce consistent indentation in `<template>`',
24+
category: 'strongly-recommended'
25+
},
26+
fixable: 'whitespace',
27+
schema: [
28+
{
29+
anyOf: [
30+
{ type: 'integer', minimum: 1 },
31+
{ enum: ['tab'] }
32+
]
33+
},
34+
{
35+
type: 'object',
36+
properties: {
37+
'baseIndent': { type: 'integer', minimum: 0 },
38+
'switchCase': { type: 'integer', minimum: 0 },
39+
'ignores': {
40+
type: 'array',
41+
items: {
42+
allOf: [
43+
{ type: 'string' },
44+
{ not: { type: 'string', pattern: ':exit$' }},
45+
{ not: { type: 'string', pattern: '^\\s*$' }}
46+
]
47+
},
48+
uniqueItems: true,
49+
additionalItems: false
50+
}
51+
},
52+
additionalProperties: false
53+
}
54+
]
55+
}
56+
}

0 commit comments

Comments
 (0)