-
-
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
appendDummyExport(ctx); | ||
} | ||
Comment on lines
+83
to
+87
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. Here is main part of this PR. |
||
|
||
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 `$`. | ||
|
@@ -319,6 +337,34 @@ function analyzeDollarDollarVariables( | |
} | ||
} | ||
|
||
/** Append dummy export */ | ||
function appendDummyExport(ctx: VirtualTypeScriptContext) { | ||
ctx.appendVirtualScript(`export namespace SvelteEslintParserModuleMarker {}`); | ||
ctx.restoreContext.addRestoreStatementProcess((node, result) => { | ||
if ( | ||
node.type !== "ExportNamedDeclaration" || | ||
node.declaration?.type !== "TSModuleDeclaration" || | ||
node.declaration.kind !== "namespace" || | ||
node.declaration.id.type !== "Identifier" || | ||
node.declaration.id.name !== "SvelteEslintParserModuleMarker" | ||
) { | ||
return false; | ||
} | ||
const program = result.ast; | ||
program.body.splice(program.body.indexOf(node), 1); | ||
|
||
const scopeManager = result.scopeManager as ScopeManager; | ||
|
||
// Remove `declare` variable | ||
removeAllScopeAndVariableAndReference(node, { | ||
visitorKeys: result.visitorKeys, | ||
scopeManager, | ||
}); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
/** | ||
* Analyze Runes. | ||
* Insert type definitions code to provide correct type information for Runes. | ||
|
@@ -569,24 +615,29 @@ function analyzeRenderScopes( | |
) { | ||
ctx.appendOriginal(code.script.length); | ||
const renderFunctionName = ctx.generateUniqueId("render"); | ||
ctx.appendVirtualScript(`function ${renderFunctionName}(){`); | ||
ctx.appendVirtualScript(`export function ${renderFunctionName}(){`); | ||
ctx.appendOriginal(code.script.length + code.render.length); | ||
ctx.appendVirtualScript(`}`); | ||
ctx.restoreContext.addRestoreStatementProcess((node, result) => { | ||
if ( | ||
node.type !== "FunctionDeclaration" || | ||
node.id.name !== renderFunctionName | ||
node.type !== "ExportNamedDeclaration" || | ||
node.declaration?.type !== "FunctionDeclaration" || | ||
node.declaration?.id?.name !== renderFunctionName | ||
) { | ||
return false; | ||
} | ||
const program = result.ast; | ||
program.body.splice(program.body.indexOf(node), 1, ...node.body.body); | ||
for (const body of node.body.body) { | ||
program.body.splice( | ||
program.body.indexOf(node), | ||
1, | ||
...node.declaration.body.body, | ||
); | ||
for (const body of node.declaration.body.body) { | ||
body.parent = program; | ||
} | ||
|
||
const scopeManager = result.scopeManager as ScopeManager; | ||
removeFunctionScope(node, scopeManager); | ||
removeFunctionScope(node.declaration, scopeManager); | ||
return true; | ||
}); | ||
} | ||
|
@@ -843,7 +894,7 @@ function transformForReactiveStatement( | |
const functionId = ctx.generateUniqueId("reactiveStatementScopeFunction"); | ||
const originalBody = statement.body; | ||
ctx.appendOriginal(originalBody.range[0]); | ||
ctx.appendVirtualScript(`function ${functionId}(){`); | ||
ctx.appendVirtualScript(`export function ${functionId}(){`); | ||
ctx.appendOriginal(originalBody.range[1]); | ||
ctx.appendVirtualScript(`}`); | ||
ctx.appendOriginal(statement.range[1]); | ||
|
@@ -854,14 +905,18 @@ function transformForReactiveStatement( | |
} | ||
const reactiveStatement = node as TSESTree.LabeledStatement; | ||
const body = reactiveStatement.body; | ||
if (body.type !== "FunctionDeclaration" || body.id.name !== functionId) { | ||
if ( | ||
body.type !== "ExportNamedDeclaration" || | ||
body.declaration?.type !== "FunctionDeclaration" || | ||
body.declaration?.id?.name !== functionId | ||
) { | ||
return false; | ||
} | ||
reactiveStatement.body = body.body.body[0]; | ||
reactiveStatement.body = body.declaration.body.body[0]; | ||
reactiveStatement.body.parent = reactiveStatement; | ||
|
||
const scopeManager = result.scopeManager as ScopeManager; | ||
removeFunctionScope(body, scopeManager); | ||
removeFunctionScope(body.declaration, scopeManager); | ||
return true; | ||
}); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script lang="ts"> | ||
let { name }: { name: string } = $props(); | ||
</script> | ||
|
||
{name} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.