Skip to content

Commit 1f065ef

Browse files
authored
Fix indent rule bugs (#72)
1 parent 2a572ca commit 1f065ef

8 files changed

+85
-15
lines changed

src/rules/indent-helpers/es.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isClosingParenToken,
1212
isNotClosingParenToken,
1313
isNotOpeningParenToken,
14+
isNotSemicolonToken,
1415
isOpeningBraceToken,
1516
isOpeningBracketToken,
1617
isOpeningParenToken,
@@ -336,7 +337,10 @@ export function defineVisitor(context: IndentContext): NodeListener {
336337
offsets.setOffsetToken(afterTokens, 1, fromToken)
337338

338339
// assertions
339-
const lastToken = sourceCode.getLastToken(node)!
340+
const lastToken = sourceCode.getLastToken(node, {
341+
filter: isNotSemicolonToken,
342+
includeComments: false,
343+
})!
340344
const assertionTokens = sourceCode.getTokensBetween(
341345
node.source,
342346
lastToken,
@@ -382,10 +386,9 @@ export function defineVisitor(context: IndentContext): NodeListener {
382386
const firstSpecifier = node.specifiers[0]
383387
if (!firstSpecifier || firstSpecifier.type === "ExportSpecifier") {
384388
// export {foo, bar}; or export {foo, bar} from "mod";
385-
const leftBraceTokens = sourceCode.getTokensBetween(
386-
exportToken,
387-
firstSpecifier,
388-
)
389+
const leftBraceTokens = firstSpecifier
390+
? sourceCode.getTokensBetween(exportToken, firstSpecifier)
391+
: [sourceCode.getTokenAfter(exportToken)!]
389392
const rightBraceToken = node.source
390393
? sourceCode.getTokenBefore(node.source, {
391394
filter: isClosingBraceToken,
@@ -417,7 +420,10 @@ export function defineVisitor(context: IndentContext): NodeListener {
417420
)
418421

419422
// assertions
420-
const lastToken = sourceCode.getLastToken(node)!
423+
const lastToken = sourceCode.getLastToken(node, {
424+
filter: isNotSemicolonToken,
425+
includeComments: false,
426+
})!
421427
const assertionTokens = sourceCode.getTokensBetween(
422428
node.source,
423429
lastToken,
@@ -673,7 +679,10 @@ export function defineVisitor(context: IndentContext): NodeListener {
673679
}
674680

675681
// assertions
676-
const lastToken = sourceCode.getLastToken(node)!
682+
const lastToken = sourceCode.getLastToken(node, {
683+
filter: isNotSemicolonToken,
684+
includeComments: false,
685+
})!
677686
const assertionTokens = sourceCode.getTokensBetween(
678687
node.source,
679688
lastToken,
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 2 spaces but found 0 spaces.",
9+
"line": 4,
10+
"column": 1
11+
},
12+
{
13+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
14+
"line": 5,
15+
"column": 1
16+
},
17+
{
18+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
19+
"line": 6,
20+
"column": 1
21+
},
22+
{
23+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
24+
"line": 7,
25+
"column": 1
26+
},
27+
{
28+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
29+
"line": 8,
30+
"column": 1
31+
},
32+
{
33+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
34+
"line": 9,
35+
"column": 1
36+
}
37+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
export
4+
{
5+
}
6+
;
7+
export
8+
{
9+
}
10+
</script>
11+
12+
<!--tests/fixtures/rules/indent/invalid/script-export02-input.svelte-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
export
4+
{
5+
}
6+
;
7+
export
8+
{
9+
}
10+
</script>
11+
12+
<!--tests/fixtures/rules/indent/invalid/script-export02-input.svelte-->

tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-errors.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
"column": 1
2626
},
2727
{
28-
"message": "Expected indentation of 6 spaces but found 0 spaces.",
28+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
2929
"line": 8,
3030
"column": 1
3131
},
3232
{
33-
"message": "Expected indentation of 4 spaces but found 0 spaces.",
33+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
3434
"line": 9,
3535
"column": 1
3636
}

tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion03-output.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
type
66
:
77
"json"
8-
}
9-
;
8+
}
9+
;
1010
</script>
1111

1212
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion03-input-->

tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-errors.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
"column": 1
2626
},
2727
{
28-
"message": "Expected indentation of 6 spaces but found 0 spaces.",
28+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
2929
"line": 8,
3030
"column": 1
3131
},
3232
{
33-
"message": "Expected indentation of 4 spaces but found 0 spaces.",
33+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
3434
"line": 9,
3535
"column": 1
3636
}

tests/fixtures/rules/indent/invalid/ts-v5/ts-import-assertion04-output.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
type
66
:
77
"json"
8-
}
9-
;
8+
}
9+
;
1010
</script>
1111

1212
<!--tests/fixtures/rules/indent/invalid/ts/ts-import-assertion04-input-->

0 commit comments

Comments
 (0)