Skip to content

Commit f31be21

Browse files
authored
Update indent rule to support typescript v4.5 (#71)
* Update indent rule to support typescript v4.5 * update * update
1 parent 2cd9355 commit f31be21

25 files changed

+817
-16
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
!/.github
99
/prettier-playground
1010
/tests/fixtures/rules/indent/invalid/ts
11+
/tests/fixtures/rules/indent/invalid/ts-v5
1112
/tests/fixtures/rules/valid-compile/invalid/ts
1213
/tests/fixtures/rules/valid-compile/valid/ts

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@
6565
},
6666
"devDependencies": {
6767
"@ota-meshi/eslint-plugin": "^0.10.0",
68-
"@types/eslint": "^7.2.10",
68+
"@types/eslint": "^8.0.0",
6969
"@types/eslint-scope": "^3.7.0",
7070
"@types/eslint-visitor-keys": "^1.0.0",
7171
"@types/mocha": "^9.0.0",
7272
"@types/node": "^16.0.0",
73-
"@typescript-eslint/eslint-plugin": "^5.0.0-0",
74-
"@typescript-eslint/parser": "^5.0.0-0",
75-
"@typescript-eslint/parser-v4": "npm:@typescript-eslint/parser@5",
73+
"@typescript-eslint/eslint-plugin": "^5.4.0",
74+
"@typescript-eslint/parser": "^5.4.1-0",
75+
"@typescript-eslint/parser-v4": "npm:@typescript-eslint/parser@4",
7676
"env-cmd": "^10.1.0",
7777
"eslint": "^8.0.0",
7878
"eslint-config-prettier": "^8.3.0",
@@ -101,7 +101,7 @@
101101
"stylelint-config-standard": "^24.0.0",
102102
"svelte": "^3.37.0",
103103
"ts-node": "^10.0.0",
104-
"typescript": "^4.4.2",
104+
"typescript": "^4.5.2",
105105
"vue-eslint-editor": "^1.1.0",
106106
"vue-eslint-parser": "^8.0.0",
107107
"vuepress": "^1.8.2"

src/rules/indent-helpers/es.ts

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
334334
}
335335
offsets.setOffsetToken(fromToken, 0, exportToken)
336336
offsets.setOffsetToken(afterTokens, 1, fromToken)
337+
338+
// assertions
339+
const lastToken = sourceCode.getLastToken(node)!
340+
const assertionTokens = sourceCode.getTokensBetween(
341+
node.source,
342+
lastToken,
343+
)
344+
if (assertionTokens.length) {
345+
const assertToken = assertionTokens.shift()!
346+
offsets.setOffsetToken(assertToken, 0, exportToken)
347+
const assertionOpen = assertionTokens.shift()
348+
if (assertionOpen) {
349+
offsets.setOffsetToken(assertionOpen, 1, assertToken)
350+
offsets.setOffsetElementList(
351+
assertionTokens,
352+
assertionOpen,
353+
lastToken,
354+
1,
355+
)
356+
}
357+
}
337358
},
338359
ExportDefaultDeclaration(node: ESTree.ExportDefaultDeclaration) {
339360
const exportToken = sourceCode.getFirstToken(node)
@@ -365,10 +386,15 @@ export function defineVisitor(context: IndentContext): NodeListener {
365386
exportToken,
366387
firstSpecifier,
367388
)
368-
const rightBraceToken = sourceCode.getLastToken(node, {
369-
filter: isClosingBraceToken,
370-
includeComments: false,
371-
})!
389+
const rightBraceToken = node.source
390+
? sourceCode.getTokenBefore(node.source, {
391+
filter: isClosingBraceToken,
392+
includeComments: false,
393+
})!
394+
: sourceCode.getLastToken(node, {
395+
filter: isClosingBraceToken,
396+
includeComments: false,
397+
})!
372398
offsets.setOffsetToken(leftBraceTokens, 0, exportToken)
373399
offsets.setOffsetElementList(
374400
node.specifiers,
@@ -389,14 +415,41 @@ export function defineVisitor(context: IndentContext): NodeListener {
389415
1,
390416
fromToken,
391417
)
418+
419+
// assertions
420+
const lastToken = sourceCode.getLastToken(node)!
421+
const assertionTokens = sourceCode.getTokensBetween(
422+
node.source,
423+
lastToken,
424+
)
425+
if (assertionTokens.length) {
426+
const assertToken = assertionTokens.shift()!
427+
offsets.setOffsetToken(assertToken, 0, exportToken)
428+
const assertionOpen = assertionTokens.shift()
429+
if (assertionOpen) {
430+
offsets.setOffsetToken(assertionOpen, 1, assertToken)
431+
offsets.setOffsetElementList(
432+
assertionTokens,
433+
assertionOpen,
434+
lastToken,
435+
1,
436+
)
437+
}
438+
}
392439
}
393440
} else {
394441
// maybe babel-eslint
395442
}
396443
}
397444
},
398-
ExportSpecifier(node: ESTree.ExportSpecifier) {
399-
const [firstToken, ...tokens] = sourceCode.getTokens(node)
445+
ExportSpecifier(node: ESTree.ExportSpecifier | ESTree.ImportSpecifier) {
446+
const tokens = sourceCode.getTokens(node)
447+
let firstToken = tokens.shift()!
448+
if (firstToken.value === "type") {
449+
const typeToken = firstToken
450+
firstToken = tokens.shift()!
451+
offsets.setOffsetToken(firstToken, 0, typeToken)
452+
}
400453
offsets.setOffsetToken(tokens, 1, firstToken)
401454
},
402455
ForInStatement(node: ESTree.ForInStatement | ESTree.ForOfStatement) {
@@ -618,6 +671,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
618671
offsets.setOffsetToken(fromToken, 0, importToken)
619672
offsets.setOffsetToken(afterTokens, 1, fromToken)
620673
}
674+
675+
// assertions
676+
const lastToken = sourceCode.getLastToken(node)!
677+
const assertionTokens = sourceCode.getTokensBetween(
678+
node.source,
679+
lastToken,
680+
)
681+
if (assertionTokens.length) {
682+
const assertToken = assertionTokens.shift()!
683+
offsets.setOffsetToken(assertToken, 0, importToken)
684+
const assertionOpen = assertionTokens.shift()
685+
if (assertionOpen) {
686+
offsets.setOffsetToken(assertionOpen, 1, assertToken)
687+
offsets.setOffsetElementList(
688+
assertionTokens,
689+
assertionOpen,
690+
lastToken,
691+
1,
692+
)
693+
}
694+
}
621695
},
622696
ImportExpression(node: ESTree.ImportExpression) {
623697
const firstToken = sourceCode.getFirstToken(node)
@@ -636,11 +710,7 @@ export function defineVisitor(context: IndentContext): NodeListener {
636710
offsets.setOffsetToken(tokens, 1, firstToken)
637711
},
638712
ImportSpecifier(node: ESTree.ImportSpecifier) {
639-
if (node.local.range![0] !== node.imported.range![0]) {
640-
const tokens = sourceCode.getTokens(node)
641-
const firstToken = tokens.shift()!
642-
offsets.setOffsetToken(tokens, 1, firstToken)
643-
}
713+
visitor.ExportSpecifier(node)
644714
},
645715
LabeledStatement(
646716
node: ESTree.LabeledStatement | AST.SvelteReactiveStatement,

src/rules/indent-helpers/ts.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,27 @@ export function defineVisitor(context: IndentContext): NodeListener {
995995
1,
996996
)
997997
},
998+
ImportAttribute(node: TSESTree.ImportAttribute) {
999+
const firstToken = sourceCode.getFirstToken(node)
1000+
const keyTokens = getFirstAndLastTokens(sourceCode, node.key)
1001+
const prefixTokens = sourceCode.getTokensBetween(
1002+
firstToken,
1003+
keyTokens.firstToken,
1004+
)
1005+
offsets.setOffsetToken(prefixTokens, 0, firstToken)
1006+
1007+
offsets.setOffsetToken(keyTokens.firstToken, 0, firstToken)
1008+
1009+
const initToken = sourceCode.getFirstToken(node.value)
1010+
offsets.setOffsetToken(
1011+
[
1012+
...sourceCode.getTokensBetween(keyTokens.lastToken, initToken),
1013+
initToken,
1014+
],
1015+
1,
1016+
keyTokens.lastToken,
1017+
)
1018+
},
9981019
// ----------------------------------------------------------------------
9991020
// SINGLE TOKEN NODES
10001021
// ----------------------------------------------------------------------
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
4+
"line": 3,
5+
"column": 1
6+
},
7+
{
8+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
9+
"line": 4,
10+
"column": 1
11+
},
12+
{
13+
"message": "Expected indentation of 6 spaces but found 0 spaces.",
14+
"line": 5,
15+
"column": 1
16+
},
17+
{
18+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
19+
"line": 6,
20+
"column": 1
21+
},
22+
{
23+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
24+
"line": 7,
25+
"column": 1
26+
},
27+
{
28+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
29+
"line": 8,
30+
"column": 1
31+
}
32+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- prettier-ignore -->
2+
<script lang="ts">
3+
import foo from "./foo.json" assert
4+
{
5+
type
6+
:
7+
"json"
8+
}
9+
</script>
10+
11+
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion01-input-->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- prettier-ignore -->
2+
<script lang="ts">
3+
import foo from "./foo.json" assert
4+
{
5+
type
6+
:
7+
"json"
8+
}
9+
</script>
10+
11+
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion01-input-->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
4+
"line": 3,
5+
"column": 1
6+
},
7+
{
8+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
9+
"line": 4,
10+
"column": 1
11+
},
12+
{
13+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
14+
"line": 5,
15+
"column": 1
16+
},
17+
{
18+
"message": "Expected indentation of 6 spaces but found 0 spaces.",
19+
"line": 6,
20+
"column": 1
21+
},
22+
{
23+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
24+
"line": 7,
25+
"column": 1
26+
},
27+
{
28+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
29+
"line": 8,
30+
"column": 1
31+
},
32+
{
33+
"message": "Expected indentation of 10 spaces but found 0 spaces.",
34+
"line": 9,
35+
"column": 1
36+
},
37+
{
38+
"message": "Expected indentation of 12 spaces but found 0 spaces.",
39+
"line": 10,
40+
"column": 1
41+
},
42+
{
43+
"message": "Expected indentation of 12 spaces but found 0 spaces.",
44+
"line": 11,
45+
"column": 1
46+
},
47+
{
48+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
49+
"line": 12,
50+
"column": 1
51+
},
52+
{
53+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
54+
"line": 13,
55+
"column": 1
56+
},
57+
{
58+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
59+
"line": 14,
60+
"column": 1
61+
}
62+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- prettier-ignore -->
2+
<script lang="ts">
3+
import(
4+
"./foo.json",
5+
{
6+
assert
7+
:
8+
{
9+
type
10+
:
11+
"json"
12+
}
13+
}
14+
)
15+
</script>
16+
17+
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion02-input-->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- prettier-ignore -->
2+
<script lang="ts">
3+
import(
4+
"./foo.json",
5+
{
6+
assert
7+
:
8+
{
9+
type
10+
:
11+
"json"
12+
}
13+
}
14+
)
15+
</script>
16+
17+
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion02-input-->
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
4+
"line": 3,
5+
"column": 1
6+
},
7+
{
8+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
9+
"line": 4,
10+
"column": 1
11+
},
12+
{
13+
"message": "Expected indentation of 6 spaces but found 0 spaces.",
14+
"line": 5,
15+
"column": 1
16+
},
17+
{
18+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
19+
"line": 6,
20+
"column": 1
21+
},
22+
{
23+
"message": "Expected indentation of 8 spaces but found 0 spaces.",
24+
"line": 7,
25+
"column": 1
26+
},
27+
{
28+
"message": "Expected indentation of 6 spaces but found 0 spaces.",
29+
"line": 8,
30+
"column": 1
31+
},
32+
{
33+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
34+
"line": 9,
35+
"column": 1
36+
}
37+
]

0 commit comments

Comments
 (0)