|
1 |
| -/** @import { ArrowFunctionExpression, Expression, FunctionDeclaration, FunctionExpression, Identifier, Pattern, PrivateIdentifier, Statement } from 'estree' */ |
| 1 | +/** @import { ArrowFunctionExpression, Expression, CallExpression, VariableDeclarator, FunctionDeclaration, FunctionExpression, Identifier, Pattern, PrivateIdentifier, Statement, VariableDeclaration, ModuleDeclaration, Directive } from 'estree' */ |
2 | 2 | /** @import { AST, Binding } from '#compiler' */
|
3 | 3 | /** @import { ClientTransformState, ComponentClientTransformState, ComponentContext } from './types.js' */
|
4 | 4 | /** @import { Analysis } from '../../types.js' */
|
5 | 5 | /** @import { Scope } from '../../scope.js' */
|
6 | 6 | import * as b from '../../../utils/builders.js';
|
7 | 7 | import { extract_identifiers, is_simple_expression } from '../../../utils/ast.js';
|
| 8 | +import { get_rune } from '../../scope.js'; |
8 | 9 | import {
|
9 | 10 | PROPS_IS_LAZY_INITIAL,
|
10 | 11 | PROPS_IS_IMMUTABLE,
|
@@ -312,3 +313,107 @@ export function create_derived_block_argument(node, context) {
|
312 | 313 | export function create_derived(state, arg) {
|
313 | 314 | return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
|
314 | 315 | }
|
| 316 | + |
| 317 | +/** |
| 318 | + * @param {(ModuleDeclaration | Statement | Directive)[]} statements |
| 319 | + * @param {ComponentContext} context |
| 320 | + * @returns {[(ModuleDeclaration | Statement | Directive)[], null | (ModuleDeclaration | Statement | Directive)[]]} |
| 321 | + */ |
| 322 | +export function wrap_unsafe_async_statements(statements, context) { |
| 323 | + /** @type {(ModuleDeclaration | Statement | Directive)[]} */ |
| 324 | + const new_statements = []; |
| 325 | + let target_block_statements = new_statements; |
| 326 | + let is_unsafe = true; |
| 327 | + |
| 328 | + const push_unsafe_statement = (/** @type {Statement} */ statement) => { |
| 329 | + if (is_unsafe) { |
| 330 | + const block_statments = [statement]; |
| 331 | + const script_template = b.stmt(b.call('$.script_effect', b.thunk(b.block(block_statments)))); |
| 332 | + target_block_statements.push(script_template); |
| 333 | + target_block_statements = block_statments; |
| 334 | + is_unsafe = false; |
| 335 | + } else { |
| 336 | + target_block_statements.push(statement); |
| 337 | + } |
| 338 | + }; |
| 339 | + |
| 340 | + for (const statement of statements) { |
| 341 | + const visited = /** @type {Statement} */ (context.visit(statement)); |
| 342 | + |
| 343 | + if ( |
| 344 | + statement.type === 'FunctionDeclaration' || |
| 345 | + statement.type === 'ClassDeclaration' || |
| 346 | + statement.type === 'EmptyStatement' || |
| 347 | + statement.type === 'ImportDeclaration' || |
| 348 | + statement.type === 'ExportNamedDeclaration' || |
| 349 | + statement.type === 'ExportAllDeclaration' || |
| 350 | + statement.type === 'ExportDefaultDeclaration' |
| 351 | + ) { |
| 352 | + target_block_statements.push(visited); |
| 353 | + continue; |
| 354 | + } |
| 355 | + |
| 356 | + if (statement.type === 'VariableDeclaration') { |
| 357 | + if (statement.declarations.length === 1) { |
| 358 | + const declarator = statement.declarations[0]; |
| 359 | + const init = declarator.init; |
| 360 | + |
| 361 | + // Safe declaration |
| 362 | + if ( |
| 363 | + init == null || |
| 364 | + init.type === 'Literal' || |
| 365 | + init.type === 'FunctionExpression' || |
| 366 | + init.type === 'ArrowFunctionExpression' || |
| 367 | + (init.type === 'ArrayExpression' && init.elements.length === 0) || |
| 368 | + (init.type === 'ObjectExpression' && init.properties.length === 0) |
| 369 | + ) { |
| 370 | + target_block_statements.push(visited); |
| 371 | + continue; |
| 372 | + } |
| 373 | + // Handle runes |
| 374 | + if (init.type === 'CallExpression') { |
| 375 | + const rune = get_rune(init, context.state.scope); |
| 376 | + |
| 377 | + if (rune === '$props' || rune === '$derived' || rune === '$derived.by') { |
| 378 | + target_block_statements.push(visited); |
| 379 | + continue; |
| 380 | + } |
| 381 | + if (rune === '$await') { |
| 382 | + target_block_statements.push(visited); |
| 383 | + is_unsafe = true; |
| 384 | + continue; |
| 385 | + } |
| 386 | + } |
| 387 | + } |
| 388 | + // TODO: we can probably better handle multiple declarators |
| 389 | + push_unsafe_statement(visited); |
| 390 | + continue; |
| 391 | + } |
| 392 | + |
| 393 | + if (statement.type === 'ExpressionStatement') { |
| 394 | + const expression = statement.expression; |
| 395 | + |
| 396 | + // Handle runes |
| 397 | + if (expression.type === 'CallExpression') { |
| 398 | + const rune = get_rune(expression, context.state.scope); |
| 399 | + |
| 400 | + if (rune === '$effect' || rune === '$effect.pre') { |
| 401 | + target_block_statements.push(visited); |
| 402 | + continue; |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + // Assume all expression statement expressions are unsafe |
| 407 | + push_unsafe_statement(visited); |
| 408 | + continue; |
| 409 | + } |
| 410 | + |
| 411 | + // Assume all other top-level statements are unsafe |
| 412 | + push_unsafe_statement(visited); |
| 413 | + } |
| 414 | + |
| 415 | + return [ |
| 416 | + new_statements, |
| 417 | + new_statements === target_block_statements ? null : target_block_statements |
| 418 | + ]; |
| 419 | +} |
0 commit comments