Skip to content

Commit e3d4a0f

Browse files
authored
chore: tweak render tag logic (#15109)
1 parent 24b6fab commit e3d4a0f

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

packages/svelte/src/compiler/phases/1-parse/state/tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ function special(parser) {
706706
expression: /** @type {AST.RenderTag['expression']} */ (expression),
707707
metadata: {
708708
dynamic: false,
709-
args_with_call_expression: new Set(),
709+
arguments: [],
710710
path: [],
711711
snippets: new Set()
712712
}

packages/svelte/src/compiler/phases/2-analyze/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,6 @@ export function analyze_component(root, source, options) {
605605
has_props_rune: false,
606606
component_slots: new Set(),
607607
expression: null,
608-
render_tag: null,
609608
private_derived_state: [],
610609
function_depth: scope.function_depth,
611610
instance_scope: instance.scope,
@@ -677,7 +676,6 @@ export function analyze_component(root, source, options) {
677676
reactive_statements: analysis.reactive_statements,
678677
component_slots: new Set(),
679678
expression: null,
680-
render_tag: null,
681679
private_derived_state: [],
682680
function_depth: scope.function_depth
683681
};

packages/svelte/src/compiler/phases/2-analyze/types.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export interface AnalysisState {
1919
component_slots: Set<string>;
2020
/** Information about the current expression/directive/block value */
2121
expression: ExpressionMetadata | null;
22-
/** The current {@render ...} tag, if any */
23-
render_tag: null | AST.RenderTag;
2422
private_derived_state: string[];
2523
function_depth: number;
2624

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

-12
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,6 @@ export function CallExpression(node, context) {
186186
break;
187187
}
188188

189-
if (context.state.render_tag) {
190-
// Find out which of the render tag arguments contains this call expression
191-
const arg_idx = unwrap_optional(context.state.render_tag.expression).arguments.findIndex(
192-
(arg) => arg === node || context.path.includes(arg)
193-
);
194-
195-
// -1 if this is the call expression of the render tag itself
196-
if (arg_idx !== -1) {
197-
context.state.render_tag.metadata.args_with_call_expression.add(arg_idx);
198-
}
199-
}
200-
201189
if (node.callee.type === 'Identifier') {
202190
const binding = context.state.scope.get(node.callee.name);
203191

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as e from '../../../errors.js';
55
import { validate_opening_tag } from './shared/utils.js';
66
import { mark_subtree_dynamic } from './shared/fragment.js';
77
import { is_resolved_snippet } from './shared/snippets.js';
8+
import { create_expression_metadata } from '../../nodes.js';
89

910
/**
1011
* @param {AST.RenderTag} node
@@ -15,7 +16,8 @@ export function RenderTag(node, context) {
1516

1617
node.metadata.path = [...context.path];
1718

18-
const callee = unwrap_optional(node.expression).callee;
19+
const expression = unwrap_optional(node.expression);
20+
const callee = expression.callee;
1921

2022
const binding = callee.type === 'Identifier' ? context.state.scope.get(callee.name) : null;
2123

@@ -52,5 +54,15 @@ export function RenderTag(node, context) {
5254

5355
mark_subtree_dynamic(context.path);
5456

55-
context.next({ ...context.state, render_tag: node });
57+
context.visit(callee);
58+
59+
for (const arg of expression.arguments) {
60+
const metadata = create_expression_metadata();
61+
node.metadata.arguments.push(metadata);
62+
63+
context.visit(arg, {
64+
...context.state,
65+
expression: metadata
66+
});
67+
}
5668
}

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ import * as b from '../../../../utils/builders.js';
1010
*/
1111
export function RenderTag(node, context) {
1212
context.state.template.push('<!>');
13-
const callee = unwrap_optional(node.expression).callee;
14-
const raw_args = unwrap_optional(node.expression).arguments;
13+
14+
const expression = unwrap_optional(node.expression);
15+
16+
const callee = expression.callee;
17+
const raw_args = expression.arguments;
1518

1619
/** @type {Expression[]} */
1720
let args = [];
1821
for (let i = 0; i < raw_args.length; i++) {
19-
const raw = raw_args[i];
20-
const arg = /** @type {Expression} */ (context.visit(raw));
21-
if (node.metadata.args_with_call_expression.has(i)) {
22+
let thunk = b.thunk(/** @type {Expression} */ (context.visit(raw_args[i])));
23+
const { has_call } = node.metadata.arguments[i];
24+
25+
if (has_call) {
2226
const id = b.id(context.state.scope.generate('render_arg'));
23-
context.state.init.push(b.var(id, b.call('$.derived_safe_equal', b.thunk(arg))));
27+
context.state.init.push(b.var(id, b.call('$.derived_safe_equal', thunk)));
2428
args.push(b.thunk(b.call('$.get', id)));
2529
} else {
26-
args.push(b.thunk(arg));
30+
args.push(thunk);
2731
}
2832
}
2933

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export namespace AST {
166166
/** @internal */
167167
metadata: {
168168
dynamic: boolean;
169-
args_with_call_expression: Set<number>;
169+
arguments: ExpressionMetadata[];
170170
path: SvelteNode[];
171171
/** The set of locally-defined snippets that this render tag could correspond to,
172172
* used for CSS pruning purposes */

0 commit comments

Comments
 (0)