Skip to content

Commit 4caf06c

Browse files
committed
leverage state
1 parent 51c553f commit 4caf06c

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,35 +1290,23 @@ const common_visitors = {
12901290
}
12911291
},
12921292
CallExpression(node, context) {
1293-
const { expression } = context.state;
1293+
const { expression, render_tag } = context.state;
12941294
if (
12951295
(expression?.type === 'ExpressionTag' || expression?.type === 'SpreadAttribute') &&
12961296
!is_known_safe_call(node, context)
12971297
) {
12981298
expression.metadata.contains_call_expression = true;
12991299
}
13001300

1301-
if (context.state.ast_type === 'template') {
1302-
// Find out if this is a call expression inside a render tag argument
1303-
let i = -1;
1304-
let parent = context.path.at(i);
1305-
1306-
while (parent && parent.type !== 'RenderTag') {
1307-
i -= 1;
1308-
parent = context.path.at(i);
1309-
}
1310-
1311-
if (parent) {
1312-
const arg_path_idx = i + (parent.expression.type === 'CallExpression' ? 2 : 3);
1301+
if (render_tag) {
1302+
// Find out which of the render tag arguments contains this call expression
1303+
const arg_idx = unwrap_optional(render_tag.expression).arguments.findIndex(
1304+
(arg) => arg === node || context.path.includes(arg)
1305+
);
13131306

1314-
if (arg_path_idx <= 0) {
1315-
const arg =
1316-
arg_path_idx === 0
1317-
? node
1318-
: /** @type {import('estree').Expression} */ (context.path.at(arg_path_idx));
1319-
const arg_idx = unwrap_optional(parent.expression).arguments.indexOf(arg);
1320-
parent.metadata.args_with_call_expression.add(arg_idx);
1321-
}
1307+
// -1 if this is the call expression of the render tag itself
1308+
if (arg_idx !== -1) {
1309+
render_tag.metadata.args_with_call_expression.add(arg_idx);
13221310
}
13231311
}
13241312

@@ -1548,6 +1536,9 @@ const common_visitors = {
15481536
);
15491537

15501538
node.metadata.dynamic = binding !== null && binding.kind !== 'normal';
1539+
},
1540+
RenderTag(node, context) {
1541+
context.next({ ...context.state, render_tag: node });
15511542
}
15521543
};
15531544

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ComponentAnalysis, ReactiveStatement } from '../types.js';
33
import type {
44
ClassDirective,
55
ExpressionTag,
6+
RenderTag,
67
SpreadAttribute,
78
SvelteNode,
89
ValidatedCompileOptions
@@ -20,6 +21,8 @@ export interface AnalysisState {
2021
component_slots: Set<string>;
2122
/** The current {expression}, if any */
2223
expression: ExpressionTag | ClassDirective | SpreadAttribute | null;
24+
/** The current {@render ...} tag, if any */
25+
render_tag: null | RenderTag;
2326
private_derived_state: string[];
2427
function_depth: number;
2528
}

0 commit comments

Comments
 (0)