Skip to content

feat: more complex partial evaluation #15802

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function to_public_ast(source, ast, modern) {
if (modern) {
const clean = (/** @type {any} */ node) => {
delete node.metadata;
delete node.type_information;
};

ast.options?.attributes.forEach((attribute) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ const visitors = {
_(node, context) {
const n = context.next() ?? node;

// TODO there may come a time when we decide to preserve type annotations.
// until that day comes, we just delete them so they don't confuse esrap
const type_information = {};
if (Object.hasOwn(n, 'typeAnnotation')) {
type_information.annotation = n.typeAnnotation;
}
if (Object.hasOwn(n, 'typeParameters')) {
type_information.parameters = n.typeParameters;
}
if (Object.hasOwn(n, 'typeArguments')) {
type_information.arguments = n.typeArguments;
}
if (Object.hasOwn(n, 'returnType')) {
type_information.return = n.returnType;
}
Object.defineProperty(n, 'type_information', {
value: type_information,
writable: true,
configurable: true,
enumerable: false
});
delete n.typeAnnotation;
delete n.typeParameters;
delete n.typeArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,21 @@ export function build_template_chunk(
node.expression.name !== 'undefined' ||
state.scope.get('undefined')
) {
let value = memoize(
/** @type {Expression} */ (visit(node.expression, state)),
node.metadata.expression
);
let value = /** @type {Expression} */ (visit(node.expression, state));

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

if (!evaluated.is_known) {
value = memoize(value, node.metadata.expression);
}

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);
value = b.literal(evaluated.value ?? '');
}

return { value, has_state };
Expand All @@ -96,7 +97,7 @@ export function build_template_chunk(
}

if (evaluated.is_known) {
quasi.value.cooked += evaluated.value + '';
quasi.value.cooked += (evaluated.value ?? '') + '';
} else {
if (!evaluated.is_defined) {
// add `?? ''` where necessary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import * as b from '#compiler/builders';
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
import { regex_whitespaces_strict } from '../../../../patterns.js';
import { NUMBER } from '../../../../scope.js';

/** Opens an if/each block, so that we can remove nodes in the case of a mismatch */
export const block_open = b.literal(BLOCK_OPEN);
Expand Down Expand Up @@ -45,13 +46,19 @@ export function process_children(nodes, { visit, state }) {
quasi.value.cooked +=
node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
} else {
const evaluated = state.scope.evaluate(node.expression);

const expression = /** @type {Expression} */ (visit(node.expression));
const evaluated = state.scope.evaluate(expression);
if (evaluated.is_known) {
quasi.value.cooked += escape_html((evaluated.value ?? '') + '');
} else {
expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));

if (
(evaluated.values.size === 1 && [...evaluated.values][0] === NUMBER) ||
[...evaluated.values].every((value) => typeof value === 'string' && !/[&<]/.test(value))
) {
expressions.push(expression);
} else {
expressions.push(b.call('$.escape', expression));
}
quasi = b.quasi('', i + 1 === sequence.length);
quasis.push(quasi);
}
Expand Down
Loading
Loading