fix: account for empty statements when visiting in transform async (#17524)

pull/17517/head
Paolo Ricciuti 8 months ago committed by GitHub
parent 1ff7dd6123
commit 917ea2d4ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: account for empty statements when visiting in transform async

@ -49,22 +49,25 @@ export function transform_body(instance_body, runner, transform) {
if (instance_body.async.length > 0) { if (instance_body.async.length > 0) {
const thunks = instance_body.async.map((s) => { const thunks = instance_body.async.map((s) => {
if (s.node.type === 'VariableDeclarator') { if (s.node.type === 'VariableDeclarator') {
const visited = /** @type {ESTree.VariableDeclaration} */ ( const visited = /** @type {ESTree.VariableDeclaration | ESTree.EmptyStatement} */ (
transform(b.var(s.node.id, s.node.init)) transform(b.var(s.node.id, s.node.init))
); );
const statements = visited.declarations.map((node) => { const statements =
if ( visited.type === 'VariableDeclaration'
node.id.type === 'Identifier' && ? visited.declarations.map((node) => {
(node.id.name.startsWith('$$d') || node.id.name.startsWith('$$array')) if (
) { node.id.type === 'Identifier' &&
// this is an intermediate declaration created in VariableDeclaration.js; (node.id.name.startsWith('$$d') || node.id.name.startsWith('$$array'))
// subsequent statements depend on it ) {
return b.var(node.id, node.init); // this is an intermediate declaration created in VariableDeclaration.js;
} // subsequent statements depend on it
return b.var(node.id, node.init);
}
return b.stmt(b.assignment('=', node.id, node.init ?? b.void0)); return b.stmt(b.assignment('=', node.id, node.init ?? b.void0));
}); })
: [];
if (statements.length === 1) { if (statements.length === 1) {
const statement = /** @type {ESTree.ExpressionStatement} */ (statements[0]); const statement = /** @type {ESTree.ExpressionStatement} */ (statements[0]);

@ -0,0 +1,5 @@
import { test } from '../../test';
export default test({
async test() {}
});

@ -0,0 +1,6 @@
<script>
await Promise.resolve(42);
const { name } = $props();
</script>
{name}
Loading…
Cancel
Save