Skip to content

Fix error when {#await} with literal #125

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 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 50 additions & 16 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class ScriptLetContext {
| string[]
| ((helper: TypeGenHelper) => {
typings: string[]
preparationScript?: string
preparationScript?: string[]
}),
): void

Expand All @@ -353,7 +353,7 @@ export class ScriptLetContext {
| string[]
| ((helper: TypeGenHelper) => {
typings: string[]
preparationScript?: string
preparationScript?: string[]
}),
): void {
let arrayTypings: string[] = []
Expand All @@ -366,14 +366,16 @@ export class ScriptLetContext {
})
arrayTypings = generatedTypes.typings
if (generatedTypes.preparationScript) {
this.appendScriptWithoutOffset(
generatedTypes.preparationScript,
(node, tokens, comments, result) => {
tokens.length = 0
comments.length = 0
removeAllReference(node, result)
},
)
for (const preparationScript of generatedTypes.preparationScript) {
this.appendScriptWithoutOffset(
preparationScript,
(node, tokens, comments, result) => {
tokens.length = 0
comments.length = 0
removeAllScope(node, result)
},
)
}
}
}
}
Expand Down Expand Up @@ -445,7 +447,7 @@ export class ScriptLetContext {
column: typeAnnotation.loc.start.column,
}

removeAllReference(typeAnnotation, result)
removeAllScope(typeAnnotation, result)
}
}

Expand Down Expand Up @@ -869,23 +871,55 @@ function applyLocs(target: Locations | ESTree.Node, locs: Locations) {
}

/** Remove all reference */
function removeAllReference(
target: ESTree.Node,
result: ScriptLetCallbackOption,
) {
function removeAllScope(target: ESTree.Node, result: ScriptLetCallbackOption) {
const targetScopes = new Set<Scope>()
traverseNodes(target, {
visitorKeys: result.visitorKeys,
enterNode(node) {
const scope = result.scopeManager.acquire(node)
if (scope) {
targetScopes.add(scope)
return
}
if (node.type === "Identifier") {
const scope = result.getScope(node)
let scope = result.getScope(node)
if (
(scope.block as any).type === "TSTypeAliasDeclaration" &&
(scope.block as any).id === node
) {
scope = scope.upper!
}
if (targetScopes.has(scope)) {
return
}

removeIdentifierVariable(node, scope)
removeIdentifierReference(node, scope)
}
},
leaveNode() {
// noop
},
})

for (const scope of targetScopes) {
removeScope(result.scopeManager, scope)
}
}

/** Remove variable */
function removeIdentifierVariable(node: ESTree.Identifier, scope: Scope): void {
const varIndex = scope.variables.findIndex((v) =>
v.defs.some((def) => def.name === node),
)
if (varIndex >= 0) {
const variable = scope.variables[varIndex]
scope.variables.splice(varIndex, 1)
const name = node.name
if (variable === scope.set.get(name)) {
scope.set.delete(name)
}
}
}

/** Remove reference */
Expand Down
34 changes: 26 additions & 8 deletions src/parser/converts/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,29 @@ export function convertAwaitBlock(
},
({ generateUniqueId }) => {
const expression = ctx.getText(node.expression)
if (
node.expression.type === "Identifier" ||
node.expression.type === "Literal"
) {
if (node.expression.type === "Literal") {
return {
typings: [expression],
}
}
const idAwaitThenValue = generateUniqueId("AwaitThenValue")
if (node.expression.type === "Identifier") {
return {
preparationScript: [
generateAwaitThenValueType(idAwaitThenValue),
],
typings: [
`Parameters<Parameters<(typeof ${expression})["then"]>[0]>[0]`,
`${idAwaitThenValue}<(typeof ${expression})>`,
],
}
}
const id = generateUniqueId(expression)
return {
preparationScript: `const ${id} = ${expression};`,
typings: [
`Parameters<Parameters<(typeof ${id})["then"]>[0]>[0]`,
preparationScript: [
`const ${id} = ${expression};`,
generateAwaitThenValueType(idAwaitThenValue),
],
typings: [`${idAwaitThenValue}<(typeof ${id})>`],
}
},
)
Expand Down Expand Up @@ -465,3 +472,14 @@ function extractMustacheBlockTokens(
end: endSectionNameEnd,
})
}

/** Generate Awaited like type code */
function generateAwaitThenValueType(id: string) {
return `type ${id}<T> = T extends null | undefined
? T
: T extends { then(value: infer F): any }
? F extends (value: infer V, ...args: any) => any
? ${id}<V>
: never
: T;`
}
15 changes: 15 additions & 0 deletions tests/fixtures/parser/ast/ts-await-non-promise01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script lang="ts">
const str = 'abc' as const
</script>

{#await 1234 then number}
<p>The number is {number}</p>
{/await}

{#await str then s}
<p>The string is {s}</p>
{/await}

{#await str.slice(0) then s}
<p>The string is {s}</p>
{/await}
Loading