Skip to content

fix: improve partial evaluation #15781

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 26 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ export function build_template_chunk(
node.metadata.expression
);

has_state ||= node.metadata.expression.has_state;
const evaluated = state.scope.evaluate(value);

has_state ||= node.metadata.expression.has_state && !evaluated.is_known;

if (values.length === 1) {
// If we have a single expression, then pass that in directly to possibly avoid doing
// extra work in the template_effect (instead we do the work in set_text).
if (evaluated.is_known) {
value = b.literal(evaluated.value);
}

return { value, has_state };
}

Expand All @@ -89,8 +95,6 @@ export function build_template_chunk(
}
}

const evaluated = state.scope.evaluate(value);

if (evaluated.is_known) {
quasi.value.cooked += evaluated.value + '';
} else {
Expand Down
128 changes: 126 additions & 2 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,21 @@
binding.kind === 'rest_prop' ||
binding.kind === 'bindable_prop';

if (binding.initial?.type === 'EachBlock' && binding.initial.index === expression.name) {
this.values.add(NUMBER);
break;
}

if (!binding.updated && binding.initial !== null && !is_prop) {
const evaluation = binding.scope.evaluate(/** @type {Expression} */ (binding.initial));
for (const value of evaluation.values) {
this.values.add(value);
}
break;
}

// TODO each index is always defined
} else if (expression.name === 'undefined') {
this.values.add(undefined);
break;
}

// TODO glean what we can from reassignments
Expand Down Expand Up @@ -336,6 +342,124 @@
break;
}

case 'CallExpression':
{
const rune = get_rune(expression, scope);
if (
rune &&
scope.get(
object(/** @type {Identifier | MemberExpression} */ (expression.callee))?.name ?? ''
) === null
) {
switch (rune) {
case '$state':
case '$state.raw':
if (expression.arguments.length) {
const evaluated = scope.evaluate(
/** @type {Expression} */ (expression.arguments[0])
);
for (let value of evaluated.values) {
this.values.add(value);
}
} else {
this.values.add(undefined);
}
break;
case '$props.id':
this.values.add(STRING);
break;
case '$effect.tracking':
this.values.add(false);
this.values.add(true);
break;
case '$derived': {
const evaluated = scope.evaluate(
/** @type {Expression} */ (expression.arguments[0])
);
for (let value of evaluated.values) {
this.values.add(value);
}
break;
}
case '$derived.by':
if (expression.arguments[0]?.type === 'ArrowFunctionExpression') {
if (expression.arguments[0].body?.type !== 'BlockStatement') {
const evaluated = scope.evaluate(
/** @type {Expression} */ (expression.arguments[0].body)
);
for (let value of evaluated.values) {
this.values.add(value);
}
break;
}
}
default: {

Check failure on line 396 in packages/svelte/src/compiler/phases/scope.js

View workflow job for this annotation

GitHub Actions / Lint

Expected a 'break' statement before 'default'
this.values.add(UNKNOWN);
}
}
} else if (
expression.callee.type === 'Identifier' &&
scope.get(expression.callee.name) === null
) {
switch (expression.callee.name) {
case 'Number': {
if (expression.arguments.length) {
const arg = scope.evaluate(/** @type {Expression} */ (expression.arguments[0]));
if (arg.is_known) {
this.values.add(Number(arg.value));
} else if (arg.is_number) {
this.values.add(NUMBER);
} else {
for (let value of arg.values) {
switch (value) {
case STRING:
case NUMBER:
case UNKNOWN:
this.values.add(NUMBER);
break;
default:
this.values.add(Number(value));
}
}
}
} else {
this.values.add(0);
}
break;
}
case 'String':
if (expression.arguments.length) {
const arg = scope.evaluate(/** @type {Expression} */ (expression.arguments[0]));
if (arg.is_known) {
this.values.add(String(arg.value));
} else if (arg.is_number || arg.is_string) {
this.values.add(STRING);
} else {
for (let value of arg.values) {
switch (value) {
case STRING:
case NUMBER:
case UNKNOWN:
this.values.add(STRING);
break;
default:
this.values.add(String(value));
}
}
}
} else {
this.values.add('');
}
break;
default:
this.values.add(UNKNOWN);
}
} else {
this.values.add(UNKNOWN);
}
}
break;

default: {
this.values.add(UNKNOWN);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../../test';

export default test({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root_1 = $.template(`<p></p>`);

export default function Each_index_non_null($$anchor) {
var fragment = $.comment();
var node = $.first_child(fragment);

$.each(node, 0, () => Array(10), $.index, ($$anchor, $$item, i) => {
var p = root_1();

p.textContent = `index: ${i}`;
$.append($$anchor, p);
});

$.append($$anchor, fragment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as $ from 'svelte/internal/server';

export default function Each_index_non_null($$payload) {
const each_array = $.ensure_array_like(Array(10));

$$payload.out += `<!--[-->`;

for (let i = 0, $$length = each_array.length; i < $$length; i++) {
$$payload.out += `<p>index: ${$.escape(i)}</p>`;
}

$$payload.out += `<!--]-->`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#each Array(10), i}
<p>index: {i}</p>
{/each}
Loading