Skip to content

fix: recognize script as module for Typescript type checking #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-otters-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: recognize script as module for Typescript type checking
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"eslint-plugin-regexp": "^2.7.0",
"eslint-plugin-svelte": "^2.46.1",
"eslint-plugin-yml": "^1.15.0",
"estree-walker": "^3.0.3",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't use this dependency.

"globals": "^15.12.0",
"locate-character": "^3.0.0",
"magic-string": "^0.30.14",
Expand Down
20 changes: 19 additions & 1 deletion src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export function analyzeTypeScriptInSvelte(

analyzeRenderScopes(code, ctx);

// 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`.
// see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
if (!hasExportDeclaration(result.ast)) {
ctx.appendVirtualScript("export {};");
}

ctx.appendOriginalToEnd();

return ctx;
Expand Down Expand Up @@ -118,6 +124,18 @@ export function analyzeTypeScript(
return ctx;
}

function hasExportDeclaration(ast: TSESParseForESLintResult["ast"]): boolean {
for (const node of ast.body) {
if (
node.type === "ExportNamedDeclaration" ||
node.type === "ExportDefaultDeclaration"
) {
return true;
}
}
return false;
}

/**
* Analyze the store reference names.
* Insert type definitions code to provide correct type information for variables that begin with `$`.
Expand Down Expand Up @@ -380,7 +398,7 @@ function analyzeRuneVariables(
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2615
case "$props": {
// Use type parameters to avoid `@typescript-eslint/no-unsafe-assignment` errors.
appendDeclareFunctionVirtualScripts(globalName, ["<T>(): T"]);
appendDeclareFunctionVirtualScripts(globalName, ["(): any"]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@baseballyama baseballyama Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But 1 test is failed, so I reverted it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is intentionally typed differently than Svelte core.

break;
}
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2626
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,27 @@
"column": 57
}
}
},
{
"type": "ExportNamedDeclaration",
"declaration": null,
"exportKind": "value",
"source": null,
"specifiers": [],
"range": [
108,
108
],
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 0
}
}
}
],
"endTag": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,27 @@
"column": 75
}
}
},
{
"type": "ExportNamedDeclaration",
"declaration": null,
"exportKind": "value",
"source": null,
"specifiers": [],
"range": [
105,
105
],
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 0
}
}
}
],
"endTag": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,27 @@
"column": 38
}
}
},
{
"type": "ExportNamedDeclaration",
"declaration": null,
"exportKind": "value",
"source": null,
"specifiers": [],
"range": [
94,
94
],
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 0
}
}
}
],
"endTag": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data: any[]; // data: any[]
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
row: Snippet<[any]>; // Snippet: Snippet<Parameters>, row: Snippet<[any]>
} = $props(); // $props(): { data: any[]; children: Snippet<[]>; row: Snippet<[any]>; }
} = $props(); // $props(): any
</script>

<table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data: T[]; // T: unknown, data: unknown[]
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
row: Snippet<[T]>; // Snippet: Snippet<Parameters>, T: unknown, row: Snippet<[unknown]>
} = $props(); // $props(): { data: unknown[]; children: Snippet<[]>; row: Snippet<[unknown]>; }
} = $props(); // $props(): any
</script>

<table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
data: A[]; // A: unknown, data: unknown[]
children: Snippet; // Snippet: Snippet<Parameters>, children: Snippet<[]>
row: Snippet<[A]>; // Snippet: Snippet<Parameters>, A: unknown, row: Snippet<[unknown]>
} = $props(); // $props(): { data: unknown[]; children: Snippet<[]>; row: Snippet<[unknown]>; }
} = $props(); // $props(): any
</script>

<table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
c: boolean; // c: boolean
d: number; // d: number
}
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
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
</script>

{a} <!-- a: number -->
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/parser/ast/svelte5/ts-$props02-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
let { name }: { name: string } = $props();
</script>

{name}
Loading
Loading