Skip to content

Commit 519a411

Browse files
authored
Fixed false positives for trailing comma with import in indent rule. (#32)
1 parent 2b4f0d3 commit 519a411

10 files changed

+835
-9
lines changed

src/rules/indent-helpers/es.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -576,27 +576,35 @@ export function defineVisitor(context: IndentContext): NodeListener {
576576

577577
const namedSpecifiers: ESTree.ImportSpecifier[] = []
578578
for (const specifier of node.specifiers) {
579-
let removeTokens
580579
if (specifier.type === "ImportSpecifier") {
581580
namedSpecifiers.push(specifier)
582-
removeTokens = sourceCode.getTokens(specifier)
583581
} else {
584-
removeTokens = sourceCode.getTokens(specifier)
582+
const removeTokens = sourceCode.getTokens(specifier)
585583
removeTokens.shift()
586-
}
587-
for (const token of removeTokens) {
588-
const i = beforeTokens.indexOf(token)
589-
if (i >= 0) {
590-
beforeTokens.splice(i, 1)
584+
for (const token of removeTokens) {
585+
const i = beforeTokens.indexOf(token)
586+
if (i >= 0) {
587+
beforeTokens.splice(i, 1)
588+
}
591589
}
592590
}
593591
}
594592
if (namedSpecifiers.length) {
595593
const leftBrace = sourceCode.getTokenBefore(namedSpecifiers[0])!
596594
const rightBrace = sourceCode.getTokenAfter(
597595
namedSpecifiers[namedSpecifiers.length - 1],
598-
)
596+
{ filter: isClosingBraceToken, includeComments: false },
597+
)!
599598
offsets.setOffsetElementList(namedSpecifiers, leftBrace, rightBrace, 1)
599+
for (const token of sourceCode.getTokensBetween(
600+
leftBrace,
601+
rightBrace,
602+
)) {
603+
const i = beforeTokens.indexOf(token)
604+
if (i >= 0) {
605+
beforeTokens.splice(i, 1)
606+
}
607+
}
600608
}
601609

602610
if (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 4 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 4 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+
{
38+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
39+
"line": 10,
40+
"column": 1
41+
},
42+
{
43+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
44+
"line": 11,
45+
"column": 1
46+
},
47+
{
48+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
49+
"line": 12,
50+
"column": 1
51+
},
52+
{
53+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
54+
"line": 13,
55+
"column": 1
56+
},
57+
{
58+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
59+
"line": 14,
60+
"column": 1
61+
},
62+
{
63+
"message": "Expected indentation of 2 spaces but found 0 spaces.",
64+
"line": 15,
65+
"column": 1
66+
},
67+
{
68+
"message": "Expected indentation of 4 spaces but found 0 spaces.",
69+
"line": 16,
70+
"column": 1
71+
}
72+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
import {
4+
A,
5+
B,
6+
C,
7+
} from
8+
"mod"
9+
import {
10+
D,
11+
} from
12+
"mod"
13+
import {E,
14+
F,
15+
} from
16+
"mod"
17+
</script>
18+
19+
<!--tests/fixtures/rules/indent/invalid/script-import01-input.svelte-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- prettier-ignore -->
2+
<script>
3+
import {
4+
A,
5+
B,
6+
C,
7+
} from
8+
"mod"
9+
import {
10+
D,
11+
} from
12+
"mod"
13+
import {E,
14+
F,
15+
} from
16+
"mod"
17+
</script>
18+
19+
<!--tests/fixtures/rules/indent/invalid/script-import01-input.svelte-->

0 commit comments

Comments
 (0)