pull/17038/head
Rich Harris 4 days ago
parent 7434f21ed4
commit 3e7589360a

@ -543,7 +543,9 @@ export function analyze_component(root, source, options) {
snippet_renderers: new Map(),
snippets: new Set(),
async_deriveds: new Set(),
pickled_awaits: new Set()
pickled_awaits: new Set(),
awaited_declarations: new Map(),
awaited_statements: new Map()
};
if (!runes) {
@ -687,6 +689,25 @@ export function analyze_component(root, source, options) {
e.legacy_rest_props_invalid(rest_props_refs[0].node);
}
if (instance.has_await) {
let awaiting = false;
let i = 0;
for (const node of instance.ast.body) {
const has_await = has_await_expression(node);
awaiting ||= has_await;
if (!awaiting) continue;
const id = b.id(`$$${i++}`);
analysis.awaited_statements.set(node, {
id,
has_await
});
}
}
for (const { ast, scope, scopes } of [module, instance, template]) {
/** @type {AnalysisState} */
const state = {

@ -5,7 +5,10 @@ import type {
ClassBody,
Identifier,
LabeledStatement,
Program
ModuleDeclaration,
Pattern,
Program,
Statement
} from 'estree';
import type { Scope, ScopeRoot } from './scope.js';
@ -108,6 +111,28 @@ export interface ComponentAnalysis extends Analysis {
* Every snippet that is declared locally
*/
snippets: Set<AST.SnippetBlock>;
/**
* A lookup of awaited declarations. If you have something this in `<script>`...
*
* let a = await get_a();
* let b = get_b();
*
* ...it will get transformed to something like this...
*
* let $$0 = $.run([], () => get_a());
* let $$1 = $.run([$$0], () => get_b());
*
* ...and references to `a` or `b` in the template should be mediated by `$$0` and `$$1`
*/
awaited_declarations: Map<
string,
{ id: Identifier; pattern: Pattern; updated_by: Set<Identifier> }
>;
/**
* Information about top-level instance statements that need to be transformed
* so that we can run the template synchronously
*/
awaited_statements: Map<Statement | ModuleDeclaration, { id: Identifier; has_await: boolean }>;
}
declare module 'estree' {

Loading…
Cancel
Save