-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 `$`. | ||
|
@@ -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"]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But 1 test is failed, so I reverted it. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script lang="ts"> | ||
let { name }: { name: string } = $props(); | ||
</script> | ||
|
||
{name} |
There was a problem hiding this comment.
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.