Skip to content

Commit 6055811

Browse files
committed
fix
1 parent 2feb963 commit 6055811

16 files changed

+55
-207
lines changed

Diff for: src/parser/typescript/analyze/index.ts

+49-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function analyzeTypeScriptInSvelte(
8383
// When performing type checking on TypeScript code that is not a module, the error `Cannot redeclare block-scoped variable 'xxx'`. occurs. To fix this, add an `export`.
8484
// see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
8585
if (!hasExportDeclaration(result.ast)) {
86-
ctx.appendVirtualScript("export {};");
86+
appendDummyExport(ctx);
8787
}
8888

8989
ctx.appendOriginalToEnd();
@@ -337,6 +337,34 @@ function analyzeDollarDollarVariables(
337337
}
338338
}
339339

340+
/** Append dummy export */
341+
function appendDummyExport(ctx: VirtualTypeScriptContext) {
342+
ctx.appendVirtualScript(`export namespace SvelteEslintParserModuleMarker {}`);
343+
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
344+
if (
345+
node.type !== "ExportNamedDeclaration" ||
346+
node.declaration?.type !== "TSModuleDeclaration" ||
347+
node.declaration.kind !== "namespace" ||
348+
node.declaration.id.type !== "Identifier" ||
349+
node.declaration.id.name !== "SvelteEslintParserModuleMarker"
350+
) {
351+
return false;
352+
}
353+
const program = result.ast;
354+
program.body.splice(program.body.indexOf(node), 1);
355+
356+
const scopeManager = result.scopeManager as ScopeManager;
357+
358+
// Remove `declare` variable
359+
removeAllScopeAndVariableAndReference(node, {
360+
visitorKeys: result.visitorKeys,
361+
scopeManager,
362+
});
363+
364+
return true;
365+
});
366+
}
367+
340368
/**
341369
* Analyze Runes.
342370
* Insert type definitions code to provide correct type information for Runes.
@@ -398,7 +426,7 @@ function analyzeRuneVariables(
398426
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2615
399427
case "$props": {
400428
// Use type parameters to avoid `@typescript-eslint/no-unsafe-assignment` errors.
401-
appendDeclareFunctionVirtualScripts(globalName, ["(): any"]);
429+
appendDeclareFunctionVirtualScripts(globalName, ["<T>(): T"]);
402430
break;
403431
}
404432
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2626
@@ -587,24 +615,29 @@ function analyzeRenderScopes(
587615
) {
588616
ctx.appendOriginal(code.script.length);
589617
const renderFunctionName = ctx.generateUniqueId("render");
590-
ctx.appendVirtualScript(`function ${renderFunctionName}(){`);
618+
ctx.appendVirtualScript(`export function ${renderFunctionName}(){`);
591619
ctx.appendOriginal(code.script.length + code.render.length);
592620
ctx.appendVirtualScript(`}`);
593621
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
594622
if (
595-
node.type !== "FunctionDeclaration" ||
596-
node.id.name !== renderFunctionName
623+
node.type !== "ExportNamedDeclaration" ||
624+
node.declaration?.type !== "FunctionDeclaration" ||
625+
node.declaration?.id?.name !== renderFunctionName
597626
) {
598627
return false;
599628
}
600629
const program = result.ast;
601-
program.body.splice(program.body.indexOf(node), 1, ...node.body.body);
602-
for (const body of node.body.body) {
630+
program.body.splice(
631+
program.body.indexOf(node),
632+
1,
633+
...node.declaration.body.body,
634+
);
635+
for (const body of node.declaration.body.body) {
603636
body.parent = program;
604637
}
605638

606639
const scopeManager = result.scopeManager as ScopeManager;
607-
removeFunctionScope(node, scopeManager);
640+
removeFunctionScope(node.declaration, scopeManager);
608641
return true;
609642
});
610643
}
@@ -861,7 +894,7 @@ function transformForReactiveStatement(
861894
const functionId = ctx.generateUniqueId("reactiveStatementScopeFunction");
862895
const originalBody = statement.body;
863896
ctx.appendOriginal(originalBody.range[0]);
864-
ctx.appendVirtualScript(`function ${functionId}(){`);
897+
ctx.appendVirtualScript(`export function ${functionId}(){`);
865898
ctx.appendOriginal(originalBody.range[1]);
866899
ctx.appendVirtualScript(`}`);
867900
ctx.appendOriginal(statement.range[1]);
@@ -872,14 +905,18 @@ function transformForReactiveStatement(
872905
}
873906
const reactiveStatement = node as TSESTree.LabeledStatement;
874907
const body = reactiveStatement.body;
875-
if (body.type !== "FunctionDeclaration" || body.id.name !== functionId) {
908+
if (
909+
body.type !== "ExportNamedDeclaration" ||
910+
body.declaration?.type !== "FunctionDeclaration" ||
911+
body.declaration?.id?.name !== functionId
912+
) {
876913
return false;
877914
}
878-
reactiveStatement.body = body.body.body[0];
915+
reactiveStatement.body = body.declaration.body.body[0];
879916
reactiveStatement.body.parent = reactiveStatement;
880917

881918
const scopeManager = result.scopeManager as ScopeManager;
882-
removeFunctionScope(body, scopeManager);
919+
removeFunctionScope(body.declaration, scopeManager);
883920
return true;
884921
});
885922
}

Diff for: tests/fixtures/parser/ast/svelte5/docs/runes/08-$props-ts-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -502,27 +502,6 @@
502502
"column": 57
503503
}
504504
}
505-
},
506-
{
507-
"type": "ExportNamedDeclaration",
508-
"declaration": null,
509-
"exportKind": "value",
510-
"source": null,
511-
"specifiers": [],
512-
"range": [
513-
108,
514-
108
515-
],
516-
"loc": {
517-
"start": {
518-
"line": 5,
519-
"column": 0
520-
},
521-
"end": {
522-
"line": 5,
523-
"column": 0
524-
}
525-
}
526505
}
527506
],
528507
"endTag": {

Diff for: tests/fixtures/parser/ast/svelte5/docs/runes/08-4-$bindable-ts-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -427,27 +427,6 @@
427427
"column": 75
428428
}
429429
}
430-
},
431-
{
432-
"type": "ExportNamedDeclaration",
433-
"declaration": null,
434-
"exportKind": "value",
435-
"source": null,
436-
"specifiers": [],
437-
"range": [
438-
105,
439-
105
440-
],
441-
"loc": {
442-
"start": {
443-
"line": 4,
444-
"column": 0
445-
},
446-
"end": {
447-
"line": 4,
448-
"column": 0
449-
}
450-
}
451430
}
452431
],
453432
"endTag": {

Diff for: tests/fixtures/parser/ast/svelte5/docs/runes/11-3-$inspect-ts-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -394,27 +394,6 @@
394394
"column": 38
395395
}
396396
}
397-
},
398-
{
399-
"type": "ExportNamedDeclaration",
400-
"declaration": null,
401-
"exportKind": "value",
402-
"source": null,
403-
"specifiers": [],
404-
"range": [
405-
94,
406-
94
407-
],
408-
"loc": {
409-
"start": {
410-
"line": 6,
411-
"column": 0
412-
},
413-
"end": {
414-
"line": 6,
415-
"column": 0
416-
}
417-
}
418397
}
419398
],
420399
"endTag": {

Diff for: tests/fixtures/parser/ast/svelte5/docs/snippets/10-typing-snippets-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
data: any[]; // data: any[]
66
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
77
row: Snippet<[any]>; // Snippet: Snippet<Parameters>, row: Snippet<[any]>
8-
} = $props(); // $props(): any
8+
} = $props(); // $props(): { data: any[]; children: Snippet<[]>; row: Snippet<[any]>; }
99
</script>
1010

1111
<table>

Diff for: tests/fixtures/parser/ast/svelte5/docs/snippets/11-typing-snippets-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
data: T[]; // T: unknown, data: unknown[]
66
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
77
row: Snippet<[T]>; // Snippet: Snippet<Parameters>, T: unknown, row: Snippet<[unknown]>
8-
} = $props(); // $props(): any
8+
} = $props(); // $props(): { data: unknown[]; children: Snippet<[]>; row: Snippet<[unknown]>; }
99
</script>
1010

1111
<table>

Diff for: tests/fixtures/parser/ast/svelte5/generics01-snippets-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
data: A[]; // A: unknown, data: unknown[]
77
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
88
row: Snippet<[A]>; // Snippet: Snippet<Parameters>, A: unknown, row: Snippet<[unknown]>
9-
} = $props(); // $props(): any
9+
} = $props(); // $props(): { data: unknown[]; children: Snippet<[]>; row: Snippet<[unknown]>; }
1010
</script>
1111

1212
<table>

Diff for: tests/fixtures/parser/ast/svelte5/ts-$props01-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
c: boolean; // c: boolean
66
d: number; // d: number
77
}
8-
let { a, b, c, ...everythingElse }: MyProps = $props(); // a: number, a: number, b: string, b: string, c: boolean, c: boolean, everythingElse: { d: number; }, MyProps: MyProps, $props(): any
8+
let { a, b, c, ...everythingElse }: MyProps = $props(); // a: number, a: number, b: string, b: string, c: boolean, c: boolean, everythingElse: { d: number; }, MyProps: MyProps, $props(): MyProps
99
</script>
1010

1111
{a} <!-- a: number -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
let { name }: { name: string } = $props(); // name: string, name: string, name: string, $props(): any
2+
let { name }: { name: string } = $props(); // name: string, name: string, name: string, $props(): { name: string; }
33
</script>
44

55
{name} <!-- name: string -->

Diff for: tests/fixtures/parser/ast/svelte5/ts-event03-type-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
const {onfoo}:{ // onfoo: (e: { detail: number; }) => void, onfoo: (e: { detail: number; }) => void
33
onfoo: (e: { detail: number }) => void // e: { detail: number; }, onfoo: (e: { detail: number; }) => void
4-
} = $props() // $props(): any
4+
} = $props() // $props(): { onfoo: (e: { detail: number; }) => void; }
55
onfoo({detail: 1}) // onfoo({detail: 1}): void
66
</script>
77

Diff for: tests/fixtures/parser/ast/ts-$$props01-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,6 @@
167167
"column": 15
168168
}
169169
}
170-
},
171-
{
172-
"type": "ExportNamedDeclaration",
173-
"declaration": null,
174-
"exportKind": "value",
175-
"source": null,
176-
"specifiers": [],
177-
"range": [
178-
58,
179-
58
180-
],
181-
"loc": {
182-
"start": {
183-
"line": 6,
184-
"column": 0
185-
},
186-
"end": {
187-
"line": 6,
188-
"column": 0
189-
}
190-
}
191170
}
192171
],
193172
"endTag": {

Diff for: tests/fixtures/parser/ast/ts-$$slots01-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,6 @@
132132
"column": 11
133133
}
134134
}
135-
},
136-
{
137-
"type": "ExportNamedDeclaration",
138-
"declaration": null,
139-
"exportKind": "value",
140-
"source": null,
141-
"specifiers": [],
142-
"range": [
143-
56,
144-
56
145-
],
146-
"loc": {
147-
"start": {
148-
"line": 6,
149-
"column": 0
150-
},
151-
"end": {
152-
"line": 6,
153-
"column": 0
154-
}
155-
}
156135
}
157136
],
158137
"endTag": {

Diff for: tests/fixtures/parser/ast/ts-$$slots02-no-slot-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,6 @@
132132
"column": 11
133133
}
134134
}
135-
},
136-
{
137-
"type": "ExportNamedDeclaration",
138-
"declaration": null,
139-
"exportKind": "value",
140-
"source": null,
141-
"specifiers": [],
142-
"range": [
143-
41,
144-
41
145-
],
146-
"loc": {
147-
"start": {
148-
"line": 4,
149-
"column": 0
150-
},
151-
"end": {
152-
"line": 4,
153-
"column": 0
154-
}
155-
}
156135
}
157136
],
158137
"endTag": {

Diff for: tests/fixtures/parser/ast/ts-$$slots03-named-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,6 @@
132132
"column": 11
133133
}
134134
}
135-
},
136-
{
137-
"type": "ExportNamedDeclaration",
138-
"declaration": null,
139-
"exportKind": "value",
140-
"source": null,
141-
"specifiers": [],
142-
"range": [
143-
67,
144-
67
145-
],
146-
"loc": {
147-
"start": {
148-
"line": 6,
149-
"column": 0
150-
},
151-
"end": {
152-
"line": 6,
153-
"column": 0
154-
}
155-
}
156135
}
157136
],
158137
"endTag": {

Diff for: tests/fixtures/parser/ast/ts-$$slots04-named-output.json

-21
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,6 @@
132132
"column": 11
133133
}
134134
}
135-
},
136-
{
137-
"type": "ExportNamedDeclaration",
138-
"declaration": null,
139-
"exportKind": "value",
140-
"source": null,
141-
"specifiers": [],
142-
"range": [
143-
92,
144-
92
145-
],
146-
"loc": {
147-
"start": {
148-
"line": 7,
149-
"column": 0
150-
},
151-
"end": {
152-
"line": 7,
153-
"column": 0
154-
}
155-
}
156135
}
157136
],
158137
"endTag": {

0 commit comments

Comments
 (0)