Skip to content

Commit 60da125

Browse files
committed
Fixed edge case in imports rule
1 parent ceb8164 commit 60da125

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

docs/imports.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ console.log("hi");
3535
import { createSignal, Component } from "solid-js";
3636
console.log("hi");
3737

38+
import { createSignal } from "solid-js/web";
39+
import "solid-js";
40+
// after eslint --fix:
41+
import { createSignal } from "solid-js";
42+
43+
import { createSignal } from "solid-js/web";
44+
import {} from "solid-js";
45+
// after eslint --fix:
46+
import { createSignal } from "solid-js";
47+
3848
import { createEffect } from "solid-js/web";
3949
import { render } from "solid-js";
4050
// after eslint --fix:

src/utils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,16 @@ export function appendImports(
127127
return fixer.insertTextAfter(otherSpecifier, `, { ${identifiersString} }`);
128128
}
129129
if (importNode.specifiers.length === 0) {
130-
// import 'source' => import { B, C, D } from 'source'
131-
const importToken = sourceCode.getFirstToken(importNode);
132-
return importToken
133-
? fixer.insertTextAfter(importToken, ` { ${identifiersString} } from`)
134-
: null;
130+
const [importToken, maybeBrace] = sourceCode.getFirstTokens(importNode, { count: 2 });
131+
if (maybeBrace?.value === "{") {
132+
// import {} from 'source' => import { B, C, D } from 'source'
133+
return fixer.insertTextAfter(maybeBrace, ` ${identifiersString} `);
134+
} else {
135+
// import 'source' => import { B, C, D } from 'source'
136+
return importToken
137+
? fixer.insertTextAfter(importToken, ` { ${identifiersString} } from`)
138+
: null;
139+
}
135140
}
136141
return null;
137142
}

test/rules/imports.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ import { createSignal, Component } from "solid-js";
5858
console.log('hi');`,
5959
parser: require.resolve("@typescript-eslint/parser"),
6060
},
61+
{
62+
code: `import { createSignal } from "solid-js/web";
63+
import "solid-js";`,
64+
errors: [
65+
{
66+
messageId: "prefer-source",
67+
data: { name: "createSignal", source: "solid-js" },
68+
},
69+
],
70+
output: `
71+
import { createSignal } from "solid-js";`,
72+
},
73+
{
74+
code: `import { createSignal } from "solid-js/web";
75+
import {} from "solid-js";`,
76+
errors: [
77+
{
78+
messageId: "prefer-source",
79+
data: { name: "createSignal", source: "solid-js" },
80+
},
81+
],
82+
output: `
83+
import { createSignal } from "solid-js";`,
84+
},
6185
// Two-part fix, output here is first pass...
6286
{
6387
code: `import { createEffect } from "solid-js/web";

0 commit comments

Comments
 (0)