make things a bit less weird

pull/12722/head
Rich Harris 2 years ago
parent ba7a85a075
commit b7795ed3e8

@ -40,7 +40,7 @@ export function Attribute(node, context) {
const delegated_event = get_delegated_event(node.name.slice(2), expression, context);
if (delegated_event !== null) {
if (delegated_event.type === 'hoistable') {
if (delegated_event.hoistable) {
delegated_event.function.metadata.hoistable = true;
}
@ -70,7 +70,7 @@ function get_delegated_event(event_name, handler, context) {
}
/** @type {DelegatedEvent} */
const non_hoistable = { type: 'non-hoistable' };
const non_hoistable = { hoistable: false };
/** @type {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | null} */
let target_function = null;
let binding = null;
@ -198,7 +198,7 @@ function get_delegated_event(event_name, handler, context) {
visited_references.add(reference);
}
return { type: 'hoistable', function: target_function };
return { hoistable: true, function: target_function };
}
/**

@ -161,7 +161,7 @@ export function build_event_attribute(node, context) {
}
// Hoist function if we can, otherwise we leave the function as is
if (node.metadata.delegated.type === 'hoistable') {
if (node.metadata.delegated.hoistable) {
if (node.metadata.delegated.function === tag.expression) {
const func_name = context.state.scope.root.unique('on_' + event_name);
context.state.hoisted.push(b.var(func_name, handler));

@ -139,7 +139,7 @@ export function build_update_assignment(state, id, init, value, update) {
* @param {null | ExpressionMetadata} metadata
* @param {ComponentContext} context
*/
export function build_event_handler(modifiers, expression, metadata, { state, visit }) {
export function build_event_handler(modifiers, expression, metadata, context) {
/** @type {Expression} */
let handler;
@ -154,7 +154,12 @@ export function build_event_handler(modifiers, expression, metadata, { state, vi
b.block([
b.return(
b.call(
b.member(/** @type {Expression} */ (visit(handler)), b.id('apply'), false, true),
b.member(
/** @type {Expression} */ (context.visit(handler)),
b.id('apply'),
false,
true
),
b.this,
b.id('$$args')
)
@ -170,10 +175,10 @@ export function build_event_handler(modifiers, expression, metadata, { state, vi
)
) {
// Create a derived dynamic event handler
const id = b.id(state.scope.generate('event_handler'));
const id = b.id(context.state.scope.generate('event_handler'));
state.init.push(
b.var(id, b.call('$.derived', b.thunk(/** @type {Expression} */ (visit(handler)))))
context.state.init.push(
b.var(id, b.call('$.derived', b.thunk(/** @type {Expression} */ (context.visit(handler)))))
);
handler = b.function(
@ -190,7 +195,7 @@ export function build_event_handler(modifiers, expression, metadata, { state, vi
])
);
} else if (handler.type === 'Identifier') {
const binding = state.scope.get(handler.name);
const binding = context.state.scope.get(handler.name);
if (
binding !== null &&
@ -198,13 +203,13 @@ export function build_event_handler(modifiers, expression, metadata, { state, vi
) {
handler = dynamic_handler();
} else {
handler = /** @type {Expression} */ (visit(handler));
handler = /** @type {Expression} */ (context.visit(handler));
}
} else {
handler = dynamic_handler();
}
} else {
state.analysis.needs_props = true;
context.state.analysis.needs_props = true;
// Function + .call to preserve "this" context as much as possible
handler = b.function(

@ -214,10 +214,10 @@ export interface OnDirective extends BaseNode {
export type DelegatedEvent =
| {
type: 'hoistable';
hoistable: true;
function: ArrowFunctionExpression | FunctionExpression | FunctionDeclaration;
}
| { type: 'non-hoistable' };
| { hoistable: false };
/** A `style:` directive */
export interface StyleDirective extends BaseNode {

Loading…
Cancel
Save