Skip to content

Commit fad193a

Browse files
committed
"hoistable" is a misnomer
1 parent 5f9072c commit fad193a

File tree

10 files changed

+39
-41
lines changed

10 files changed

+39
-41
lines changed

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export function Attribute(node, context) {
4040
const delegated_event = get_delegated_event(node.name.slice(2), expression, context);
4141

4242
if (delegated_event !== null) {
43-
if (delegated_event.hoistable) {
44-
delegated_event.function.metadata.hoistable = true;
43+
if (delegated_event.hoisted) {
44+
delegated_event.function.metadata.hoisted = true;
4545
}
4646

4747
node.metadata.delegated = delegated_event;
@@ -70,7 +70,7 @@ function get_delegated_event(event_name, handler, context) {
7070
}
7171

7272
/** @type {DelegatedEvent} */
73-
const non_hoistable = { hoistable: false };
73+
const non_hoistable = { hoisted: false };
7474
/** @type {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression | null} */
7575
let target_function = null;
7676
let binding = null;
@@ -196,7 +196,7 @@ function get_delegated_event(event_name, handler, context) {
196196
visited_references.add(reference);
197197
}
198198

199-
return { hoistable: true, function: target_function };
199+
return { hoisted: true, function: target_function };
200200
}
201201

202202
/**

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/function.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
export function visit_function(node, context) {
99
// TODO retire this in favour of a more general solution based on bindings
1010
node.metadata = {
11-
// module context -> already hoisted
12-
hoistable: context.state.ast_type === 'module' ? 'impossible' : false,
13-
hoistable_params: [],
11+
hoisted: false,
12+
hoisted_params: [],
1413
scope: context.state.scope
1514
};
1615

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export function build_proxy_reassignment(value, proxy_reference) {
481481
* @param {ComponentContext} context
482482
* @returns {Pattern[]}
483483
*/
484-
function get_hoistable_params(node, context) {
484+
function get_hoisted_params(node, context) {
485485
const scope = context.state.scope;
486486

487487
/** @type {Identifier[]} */
@@ -549,15 +549,15 @@ function get_hoistable_params(node, context) {
549549
* @param {ComponentContext} context
550550
* @returns {Pattern[]}
551551
*/
552-
export function build_hoistable_params(node, context) {
553-
const hoistable_params = get_hoistable_params(node, context);
554-
node.metadata.hoistable_params = hoistable_params;
552+
export function build_hoisted_params(node, context) {
553+
const hoisted_params = get_hoisted_params(node, context);
554+
node.metadata.hoisted_params = hoisted_params;
555555

556556
/** @type {Pattern[]} */
557557
const params = [];
558558

559559
if (node.params.length === 0) {
560-
if (hoistable_params.length > 0) {
560+
if (hoisted_params.length > 0) {
561561
// For the event object
562562
params.push(b.id('_'));
563563
}
@@ -567,7 +567,7 @@ export function build_hoistable_params(node, context) {
567567
}
568568
}
569569

570-
params.push(...hoistable_params);
570+
params.push(...hoisted_params);
571571
return params;
572572
}
573573

packages/svelte/src/compiler/phases/3-transform/client/visitors/FunctionDeclaration.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { FunctionDeclaration } from 'estree' */
22
/** @import { ComponentContext } from '../types' */
3-
import { build_hoistable_params } from '../utils.js';
3+
import { build_hoisted_params } from '../utils.js';
44
import * as b from '../../../../utils/builders.js';
55

66
/**
@@ -10,8 +10,8 @@ import * as b from '../../../../utils/builders.js';
1010
export function FunctionDeclaration(node, context) {
1111
const state = { ...context.state, in_constructor: false };
1212

13-
if (node.metadata?.hoistable === true) {
14-
const params = build_hoistable_params(node, context);
13+
if (node.metadata?.hoisted === true) {
14+
const params = build_hoisted_params(node, context);
1515
const body = context.visit(node.body, state);
1616

1717
context.state.hoisted.push(/** @type {FunctionDeclaration} */ ({ ...node, params, body }));

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
is_state_source,
1414
should_proxy_or_freeze
1515
} from '../utils.js';
16-
import { is_hoistable_function } from '../../utils.js';
16+
import { is_hoisted_function } from '../../utils.js';
1717

1818
/**
1919
* @param {VariableDeclaration} node
@@ -36,11 +36,11 @@ export function VariableDeclaration(node, context) {
3636
rune === '$state.snapshot' ||
3737
rune === '$state.is'
3838
) {
39-
if (init != null && is_hoistable_function(init)) {
40-
const hoistable_function = context.visit(init);
39+
if (init != null && is_hoisted_function(init)) {
4140
context.state.hoisted.push(
42-
b.declaration('const', declarator.id, /** @type {Expression} */ (hoistable_function))
41+
b.declaration('const', declarator.id, /** @type {Expression} */ (context.visit(init)))
4342
);
43+
4444
continue;
4545
}
4646
declarations.push(/** @type {VariableDeclarator} */ (context.visit(declarator)));
@@ -219,11 +219,9 @@ export function VariableDeclaration(node, context) {
219219
if (!has_state && !has_props) {
220220
const init = declarator.init;
221221

222-
if (init != null && is_hoistable_function(init)) {
223-
const hoistable_function = context.visit(init);
224-
222+
if (init != null && is_hoisted_function(init)) {
225223
context.state.hoisted.push(
226-
b.declaration('const', declarator.id, /** @type {Expression} */ (hoistable_function))
224+
b.declaration('const', declarator.id, /** @type {Expression} */ (context.visit(init)))
227225
);
228226

229227
continue;

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/events.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ export function visit_event_attribute(node, context) {
3232
}
3333

3434
// Hoist function if we can, otherwise we leave the function as is
35-
if (node.metadata.delegated.hoistable) {
35+
if (node.metadata.delegated.hoisted) {
3636
if (node.metadata.delegated.function === tag.expression) {
3737
const func_name = context.state.scope.root.unique('on_' + event_name);
3838
context.state.hoisted.push(b.var(func_name, handler));
3939
handler = func_name;
4040
}
4141

42-
const hoistable_params = /** @type {Expression[]} */ (
43-
node.metadata.delegated.function.metadata.hoistable_params
42+
const hoisted_params = /** @type {Expression[]} */ (
43+
node.metadata.delegated.function.metadata.hoisted_params
4444
);
45+
4546
// When we hoist a function we assign an array with the function and all
4647
// hoisted closure params.
47-
const args = [handler, ...hoistable_params];
48+
const args = [handler, ...hoisted_params];
4849
delegated_assignment = b.array(args);
4950
} else {
5051
delegated_assignment = handler;

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/function.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @import { ArrowFunctionExpression, FunctionExpression, Node } from 'estree' */
22
/** @import { ComponentContext } from '../../types' */
3-
import { build_hoistable_params } from '../../utils.js';
3+
import { build_hoisted_params } from '../../utils.js';
44

55
/**
66
* @param {ArrowFunctionExpression | FunctionExpression} node
@@ -20,8 +20,8 @@ export const visit_function = (node, context) => {
2020
state = { ...context.state, in_constructor: false };
2121
}
2222

23-
if (metadata?.hoistable === true) {
24-
const params = build_hoistable_params(node, context);
23+
if (metadata?.hoisted === true) {
24+
const params = build_hoisted_params(node, context);
2525

2626
return /** @type {FunctionExpression} */ ({
2727
...node,

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import { dev } from '../../state.js';
2020
* @param {Node} node
2121
* @returns {boolean}
2222
*/
23-
export function is_hoistable_function(node) {
23+
export function is_hoisted_function(node) {
2424
if (
2525
node.type === 'ArrowFunctionExpression' ||
2626
node.type === 'FunctionExpression' ||
2727
node.type === 'FunctionDeclaration'
2828
) {
29-
return node.metadata?.hoistable === true;
29+
return node.metadata?.hoisted === true;
3030
}
3131
return false;
3232
}

packages/svelte/src/compiler/phases/types.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@ export interface ComponentAnalysis extends Analysis {
8282
declare module 'estree' {
8383
interface ArrowFunctionExpression {
8484
metadata: {
85-
hoistable: boolean | 'impossible';
86-
hoistable_params: Pattern[];
85+
hoisted: boolean;
86+
hoisted_params: Pattern[];
8787
scope: Scope;
8888
};
8989
}
9090

9191
interface FunctionExpression {
9292
metadata: {
93-
hoistable: boolean | 'impossible';
94-
hoistable_params: Pattern[];
93+
hoisted: boolean;
94+
hoisted_params: Pattern[];
9595
scope: Scope;
9696
};
9797
}
9898

9999
interface FunctionDeclaration {
100100
metadata: {
101-
hoistable: boolean | 'impossible';
102-
hoistable_params: Pattern[];
101+
hoisted: boolean;
102+
hoisted_params: Pattern[];
103103
scope: Scope;
104104
};
105105
}

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ export interface OnDirective extends BaseNode {
214214

215215
export type DelegatedEvent =
216216
| {
217-
hoistable: true;
217+
hoisted: true;
218218
function: ArrowFunctionExpression | FunctionExpression | FunctionDeclaration;
219219
}
220-
| { hoistable: false };
220+
| { hoisted: false };
221221

222222
/** A `style:` directive */
223223
export interface StyleDirective extends BaseNode {

0 commit comments

Comments
 (0)