Skip to content

Commit d2e6045

Browse files
committed
Make the indent rules supports ECMAScript 2020
1 parent 2e1d15b commit d2e6045

27 files changed

+285
-10
lines changed

Diff for: docs/rules/html-indent.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description: enforce consistent indentation in `<template>`
1515
This rule enforces a consistent indentation style in `<template>`. The default style is 2 spaces.
1616

1717
- This rule checks all tags, also all expressions in directives and mustaches.
18-
- In the expressions, this rule supports ECMAScript 2017 syntaxes. It ignores unknown AST nodes, but it might be confused by non-standard syntaxes.
18+
- In the expressions, this rule supports ECMAScript 2020 syntaxes. It ignores unknown AST nodes, but it might be confused by non-standard syntaxes.
1919

2020
<eslint-code-block fix :rules="{'vue/html-indent': ['error']}">
2121

Diff for: lib/utils/indent-common.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const assert = require('assert')
1414
// Helpers
1515
// ------------------------------------------------------------------------------
1616

17-
const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionExpression', 'AssignmentExpression', 'AssignmentPattern', 'AwaitExpression', 'BinaryExpression', 'BlockStatement', 'BreakStatement', 'CallExpression', 'CatchClause', 'ClassBody', 'ClassDeclaration', 'ClassExpression', 'ConditionalExpression', 'ContinueStatement', 'DebuggerStatement', 'DoWhileStatement', 'EmptyStatement', 'ExperimentalRestProperty', 'ExperimentalSpreadProperty', 'ExportAllDeclaration', 'ExportDefaultDeclaration', 'ExportNamedDeclaration', 'ExportSpecifier', 'ExpressionStatement', 'ForInStatement', 'ForOfStatement', 'ForStatement', 'FunctionDeclaration', 'FunctionExpression', 'Identifier', 'IfStatement', 'ImportDeclaration', 'ImportDefaultSpecifier', 'ImportNamespaceSpecifier', 'ImportSpecifier', 'LabeledStatement', 'Literal', 'LogicalExpression', 'MemberExpression', 'MetaProperty', 'MethodDefinition', 'NewExpression', 'ObjectExpression', 'ObjectPattern', 'Program', 'Property', 'RestElement', 'ReturnStatement', 'SequenceExpression', 'SpreadElement', 'Super', 'SwitchCase', 'SwitchStatement', 'TaggedTemplateExpression', 'TemplateElement', 'TemplateLiteral', 'ThisExpression', 'ThrowStatement', 'TryStatement', 'UnaryExpression', 'UpdateExpression', 'VariableDeclaration', 'VariableDeclarator', 'WhileStatement', 'WithStatement', 'YieldExpression', 'VAttribute', 'VDirectiveKey', 'VDocumentFragment', 'VElement', 'VEndTag', 'VExpressionContainer', 'VFilter', 'VFilterSequenceExpression', 'VForExpression', 'VIdentifier', 'VLiteral', 'VOnExpression', 'VSlotScopeExpression', 'VStartTag', 'VText'])
17+
const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionExpression', 'AssignmentExpression', 'AssignmentPattern', 'AwaitExpression', 'BinaryExpression', 'BlockStatement', 'BreakStatement', 'CallExpression', 'CatchClause', 'ClassBody', 'ClassDeclaration', 'ClassExpression', 'ConditionalExpression', 'ContinueStatement', 'DebuggerStatement', 'DoWhileStatement', 'EmptyStatement', 'ExperimentalRestProperty', 'ExperimentalSpreadProperty', 'ExportAllDeclaration', 'ExportDefaultDeclaration', 'ExportNamedDeclaration', 'ExportSpecifier', 'ExpressionStatement', 'ForInStatement', 'ForOfStatement', 'ForStatement', 'FunctionDeclaration', 'FunctionExpression', 'Identifier', 'IfStatement', 'ImportDeclaration', 'ImportDefaultSpecifier', 'ImportExpression', 'ImportNamespaceSpecifier', 'ImportSpecifier', 'LabeledStatement', 'Literal', 'LogicalExpression', 'MemberExpression', 'MetaProperty', 'MethodDefinition', 'NewExpression', 'ObjectExpression', 'ObjectPattern', 'OptionalCallExpression', 'OptionalMemberExpression', 'Program', 'Property', 'RestElement', 'ReturnStatement', 'SequenceExpression', 'SpreadElement', 'Super', 'SwitchCase', 'SwitchStatement', 'TaggedTemplateExpression', 'TemplateElement', 'TemplateLiteral', 'ThisExpression', 'ThrowStatement', 'TryStatement', 'UnaryExpression', 'UpdateExpression', 'VariableDeclaration', 'VariableDeclarator', 'WhileStatement', 'WithStatement', 'YieldExpression', 'VAttribute', 'VDirectiveKey', 'VDocumentFragment', 'VElement', 'VEndTag', 'VExpressionContainer', 'VFilter', 'VFilterSequenceExpression', 'VForExpression', 'VIdentifier', 'VLiteral', 'VOnExpression', 'VSlotScopeExpression', 'VStartTag', 'VText'])
1818
const LT_CHAR = /[\r\n\u2028\u2029]/
1919
const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
2020
const BLOCK_COMMENT_PREFIX = /^\s*\*/
@@ -552,7 +552,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
552552
}
553553
return true
554554
}
555-
if (t === 'CallExpression' || t === 'NewExpression') {
555+
if (t === 'CallExpression' || t === 'NewExpression' || t === 'OptionalCallExpression') {
556556
const openParen = tokenStore.getTokenAfter(parent.callee, isNotRightParen)
557557
return parent.arguments.some(param =>
558558
getFirstAndLastTokens(param, openParen.range[1]).firstToken.range[0] === token.range[0]
@@ -1065,7 +1065,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
10651065
}
10661066
},
10671067

1068-
CallExpression (node) {
1068+
'CallExpression, OptionalCallExpression' (node) {
10691069
const firstToken = tokenStore.getFirstToken(node)
10701070
const rightToken = tokenStore.getLastToken(node)
10711071
const leftToken = tokenStore.getTokenAfter(node.callee, isLeftParen)
@@ -1074,6 +1074,15 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
10741074
processNodeList(node.arguments, leftToken, rightToken, 1)
10751075
},
10761076

1077+
ImportExpression (node) {
1078+
const firstToken = tokenStore.getFirstToken(node)
1079+
const rightToken = tokenStore.getLastToken(node)
1080+
const leftToken = tokenStore.getTokenAfter(firstToken, isLeftParen)
1081+
1082+
setOffset(leftToken, 1, firstToken)
1083+
processNodeList([node.source], leftToken, rightToken, 1)
1084+
},
1085+
10771086
CatchClause (node) {
10781087
const firstToken = tokenStore.getFirstToken(node)
10791088
const bodyToken = tokenStore.getFirstToken(node.body)
@@ -1185,12 +1194,16 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
11851194

11861195
'ForInStatement, ForOfStatement' (node) {
11871196
const forToken = tokenStore.getFirstToken(node)
1188-
const leftParenToken = tokenStore.getTokenAfter(forToken)
1197+
const awaitToken = (node.await && tokenStore.getTokenAfter(forToken)) || null
1198+
const leftParenToken = tokenStore.getTokenAfter(awaitToken || forToken)
11891199
const leftToken = tokenStore.getTokenAfter(leftParenToken)
11901200
const inToken = tokenStore.getTokenAfter(leftToken, isNotRightParen)
11911201
const rightToken = tokenStore.getTokenAfter(inToken)
11921202
const rightParenToken = tokenStore.getTokenBefore(node.body, isNotLeftParen)
11931203

1204+
if (awaitToken != null) {
1205+
setOffset(awaitToken, 0, forToken)
1206+
}
11941207
setOffset(leftParenToken, 1, forToken)
11951208
setOffset(leftToken, 1, leftParenToken)
11961209
setOffset(inToken, 1, leftToken)
@@ -1367,7 +1380,7 @@ module.exports.defineVisitor = function create (context, tokenStore, defaultOpti
13671380
setOffset([colonToken, bodyToken], 1, labelToken)
13681381
},
13691382

1370-
'MemberExpression, MetaProperty' (node) {
1383+
'MemberExpression, MetaProperty, OptionalMemberExpression' (node) {
13711384
const objectToken = tokenStore.getFirstToken(node)
13721385
if (node.computed) {
13731386
const leftBracketToken = tokenStore.getTokenBefore(node.property, isLeftBracket)

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
},
5353
"devDependencies": {
5454
"@types/node": "^4.2.16",
55-
"@typescript-eslint/parser": "^2.6.1",
55+
"@typescript-eslint/parser": "^2.13.0",
5656
"acorn": "^7.1.0",
5757
"babel-eslint": "^10.0.2",
5858
"chai": "^4.1.0",
@@ -65,7 +65,7 @@
6565
"lodash": "^4.17.4",
6666
"mocha": "^5.2.0",
6767
"nyc": "^12.0.2",
68-
"typescript": "^3.5.2",
68+
"typescript": "^3.7.4",
6969
"vue-eslint-editor": "^0.1.4",
7070
"vuepress": "^0.14.5"
7171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<template>
4+
<div v-bind:a="a
5+
??
6+
b
7+
"/>
8+
</template>

Diff for: tests/fixtures/html-indent/optional-chaining-01.vue

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
4+
<template>
5+
<div v-bind:a="
6+
obj
7+
?.aaa
8+
?.bbb
9+
?.ccc
10+
"/>
11+
</template>

Diff for: tests/fixtures/html-indent/optional-chaining-02.vue

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<template>
4+
<div v-bind:a="
5+
obj?.
6+
aaa?.
7+
bbb?.
8+
ccc
9+
"/>
10+
</template>

Diff for: tests/fixtures/html-indent/optional-chaining-03.vue

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<template>
4+
<div v-bind:a="
5+
obj?.
6+
[aaa]?.
7+
[bbb]
8+
?.[ccc]
9+
"/>
10+
</template>

Diff for: tests/fixtures/html-indent/optional-chaining-04.vue

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<template>
4+
<div v-bind:a="
5+
obj?.
6+
(
7+
aaa
8+
)?.
9+
(
10+
bbb
11+
)
12+
?.(
13+
ccc
14+
)
15+
"/>
16+
</template>

Diff for: tests/fixtures/script-indent/bigint-01.vue

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--{}-->
2+
<script>
3+
var a =
4+
10n
5+
+
6+
5n
7+
</script>

Diff for: tests/fixtures/script-indent/for-await-of-01.vue

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--{}-->
2+
<script>
3+
async function fn() {
4+
for
5+
await
6+
(
7+
a
8+
of
9+
b
10+
)
11+
{
12+
;
13+
}
14+
}
15+
</script>
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--{}-->
2+
<script>
3+
const fs = import
4+
(
5+
'fs'
6+
)
7+
</script>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{}-->
2+
<script>
3+
const module = import
4+
(
5+
m.
6+
n
7+
)
8+
</script>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!--{}-->
2+
<script>
3+
async function fn() {
4+
return await
5+
import
6+
(
7+
m.
8+
n
9+
)
10+
}
11+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<script>
4+
var v = a
5+
??
6+
b
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{"parserOptions": {"parser":"babel-eslint"}}-->
2+
<script>
3+
var v = a
4+
??
5+
b
6+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script>
3+
var v = a
4+
??
5+
b
6+
</script>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--{}-->
2+
<script>
3+
var obj = {
4+
a:
5+
1,
6+
...obj1,
7+
b:
8+
2,
9+
...
10+
obj2,
11+
}
12+
</script>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<script>
4+
obj
5+
?.aaa
6+
?.bbb
7+
?.ccc
8+
</script>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<script>
4+
obj?.
5+
aaa?.
6+
bbb?.
7+
ccc
8+
</script>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<script>
4+
obj?.
5+
[aaa]?.
6+
[bbb]
7+
?.[ccc]
8+
</script>
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<!-- TODO If ESLint supports the new syntax by default, remove the parser specification and test. -->
3+
<script>
4+
obj?.
5+
(
6+
aaa
7+
)?.
8+
(
9+
bbb
10+
)
11+
?.(
12+
ccc
13+
)
14+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--{"parserOptions": {"parser":"babel-eslint"}}-->
2+
<script>
3+
obj
4+
?.aaa
5+
?.bbb
6+
?.ccc;
7+
8+
obj?.
9+
aaa?.
10+
bbb?.
11+
ccc;
12+
13+
obj?.
14+
[aaa]?.
15+
[bbb]
16+
?.[ccc];
17+
18+
obj?.
19+
(
20+
aaa
21+
)?.
22+
(
23+
bbb
24+
)
25+
?.(
26+
ccc
27+
)
28+
</script>
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script>
3+
obj
4+
?.aaa
5+
?.bbb
6+
?.ccc;
7+
8+
obj?.
9+
aaa?.
10+
bbb?.
11+
ccc;
12+
13+
obj?.
14+
[aaa]?.
15+
[bbb]
16+
?.[ccc];
17+
18+
obj?.
19+
(
20+
aaa
21+
)?.
22+
(
23+
bbb
24+
)
25+
?.(
26+
ccc
27+
)
28+
</script>

Diff for: tests/fixtures/script-indent/rest-properties-01.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!--{}-->
2+
<script>
3+
({
4+
a,
5+
...
6+
b
7+
} = {})
8+
</script>

Diff for: tests/fixtures/script-indent/try-statement-04.vue

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!--{}-->
2+
<script>
3+
try
4+
{
5+
}
6+
catch
7+
{
8+
;
9+
}
10+
finally
11+
{
12+
;
13+
}
14+
</script>

Diff for: tests/lib/rules/html-indent.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ function loadPatterns (additionalValid, additionalInvalid) {
4242
const code0 = fs.readFileSync(path.join(FIXTURE_ROOT, filename), 'utf8')
4343
const code = code0.replace(/^<!--(.+?)-->/, `<!--${filename}-->`)
4444
const baseObj = JSON.parse(/^<!--(.+?)-->/.exec(code0)[1])
45+
if ('parser' in baseObj) {
46+
baseObj.parser = require.resolve(baseObj.parser)
47+
}
48+
if ('parserOptions' in baseObj && 'parser' in baseObj.parserOptions) {
49+
baseObj.parserOptions.parser = require.resolve(baseObj.parserOptions.parser)
50+
}
4551
return Object.assign(baseObj, { code, filename })
4652
})
4753
const invalid = valid
@@ -97,7 +103,7 @@ function unIndent (strings) {
97103
const tester = new RuleTester({
98104
parser: require.resolve('vue-eslint-parser'),
99105
parserOptions: {
100-
ecmaVersion: 2017,
106+
ecmaVersion: 2020,
101107
ecmaFeatures: {
102108
globalReturn: true
103109
}

0 commit comments

Comments
 (0)