pull/14315/head
Dominic Gannaway 2 years ago
parent bc6f857ea5
commit 4bfe67fb62

@ -33,6 +33,7 @@ export function read_script(parser, start, attributes) {
/** @type {Program} */
let ast;
debugger;
try {
ast = acorn.parse(source, parser.ts, true);
} catch (err) {

@ -2,6 +2,7 @@
/** @import { Context } from '../types' */
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';
import * as e from '../../../errors.js';
import { can_hoist_snippet } from '../../3-transform/utils.js';
/**
* @param {AST.SnippetBlock} node
@ -22,6 +23,16 @@ export function SnippetBlock(node, context) {
context.next({ ...context.state, parent_element: null });
const local_scope = context.state.scope;
const can_hoist =
context.path.length === 1 &&
context.path[0].type === 'Fragment' &&
can_hoist_snippet(node, local_scope, context.state.scopes);
node.metadata = {
can_hoist
};
const { path } = context;
const parent = path.at(-2);
if (!parent) return;

@ -4,7 +4,6 @@
import { dev } from '../../../../state.js';
import { extract_paths } from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js';
import { can_hoist_snippet } from '../../utils.js';
import { get_value } from './shared/declarations.js';
/**
@ -81,12 +80,10 @@ export function SnippetBlock(node, context) {
}
const declaration = b.const(node.expression, snippet);
const local_scope = context.state.scope;
const can_hoist = can_hoist_snippet(node, local_scope, context.state.scopes);
// Top-level snippets are hoisted so they can be referenced in the `<script>`
if (context.path.length === 1 && context.path[0].type === 'Fragment') {
if (can_hoist) {
if (node.metadata.can_hoist) {
context.state.analysis.module_level_snippets.push(declaration);
} else {
context.state.analysis.top_level_snippets.push(declaration);

@ -2,7 +2,6 @@
/** @import { AST } from '#compiler' */
/** @import { ComponentContext, } from '../types.js' */
import * as b from '../../../../utils/builders.js';
import { can_hoist_snippet } from '../../utils.js';
/**
* @param {AST.SnippetBlock} node
@ -18,9 +17,7 @@ export function SnippetBlock(node, context) {
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
fn.___snippet = true;
const can_hoist = can_hoist_snippet(node, context.state.scope, context.state.scopes);
if (context.path.length === 1 && context.path[0].type === 'Fragment' && can_hoist) {
if (node.metadata.can_hoist) {
context.state.hoisted.push(fn);
} else {
context.state.init.push(fn);

@ -438,6 +438,9 @@ export namespace AST {
expression: Identifier;
parameters: Pattern[];
body: Fragment;
metadata: {
can_hoist: boolean;
};
}
export interface Attribute extends BaseNode {

Loading…
Cancel
Save