Skip to content

fix: cache call expressions in render tag arguments #12418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-avocados-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: cache call expressions in render tag arguments
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ function special(parser) {
end: parser.index,
expression: expression,
metadata: {
dynamic: false
dynamic: false,
args_with_call_expression: new Set()
}
});
}
Expand Down
22 changes: 20 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
extract_paths,
is_event_attribute,
is_text_attribute,
object
object,
unwrap_optional
} from '../../utils/ast.js';
import * as b from '../../utils/builders.js';
import { MathMLElements, ReservedKeywords, Runes, SVGElements } from '../constants.js';
Expand Down Expand Up @@ -441,6 +442,7 @@ export function analyze_component(root, source, options) {
has_props_rune: false,
component_slots: new Set(),
expression: null,
render_tag: null,
private_derived_state: [],
function_depth: scope.function_depth
};
Expand Down Expand Up @@ -512,6 +514,7 @@ export function analyze_component(root, source, options) {
reactive_statements: analysis.reactive_statements,
component_slots: new Set(),
expression: null,
render_tag: null,
private_derived_state: [],
function_depth: scope.function_depth
};
Expand Down Expand Up @@ -1289,14 +1292,26 @@ const common_visitors = {
}
},
CallExpression(node, context) {
const { expression } = context.state;
const { expression, render_tag } = context.state;
if (
(expression?.type === 'ExpressionTag' || expression?.type === 'SpreadAttribute') &&
!is_known_safe_call(node, context)
) {
expression.metadata.contains_call_expression = true;
}

if (render_tag) {
// Find out which of the render tag arguments contains this call expression
const arg_idx = unwrap_optional(render_tag.expression).arguments.findIndex(
(arg) => arg === node || context.path.includes(arg)
);

// -1 if this is the call expression of the render tag itself
if (arg_idx !== -1) {
render_tag.metadata.args_with_call_expression.add(arg_idx);
}
}

const callee = node.callee;
const rune = get_rune(node, context.state.scope);

Expand Down Expand Up @@ -1523,6 +1538,9 @@ const common_visitors = {
);

node.metadata.dynamic = binding !== null && binding.kind !== 'normal';
},
RenderTag(node, context) {
context.next({ ...context.state, render_tag: node });
}
};

Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ComponentAnalysis, ReactiveStatement } from '../types.js';
import type {
ClassDirective,
ExpressionTag,
RenderTag,
SpreadAttribute,
SvelteNode,
ValidatedCompileOptions
Expand All @@ -20,6 +21,8 @@ export interface AnalysisState {
component_slots: Set<string>;
/** The current {expression}, if any */
expression: ExpressionTag | ClassDirective | SpreadAttribute | null;
/** The current {@render ...} tag, if any */
render_tag: null | RenderTag;
private_derived_state: string[];
function_depth: number;
}
Expand Down
Loading
Loading