check evaluated values as well, fix minor issue

pull/15893/head
ComputerGuy 1 year ago
parent 98356b753b
commit 89dd5d83a0

@ -4,8 +4,12 @@
import { walk } from 'zimmerframe';
import { object } from '../../../../../utils/ast.js';
import * as b from '#compiler/builders';
import * as w from '../../../../../warnings.js';
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
import { regex_is_valid_identifier } from '../../../../patterns.js';
import {
regex_is_valid_identifier,
regex_bidirectional_control_characters
} from '../../../../patterns.js';
import is_reference from 'is-reference';
import { dev, is_ignored, locator } from '../../../../../state.js';
import { create_derived } from '../../utils.js';
@ -77,7 +81,10 @@ export function build_template_chunk(
// 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);
if (regex_bidirectional_control_characters.test((evaluated.value ?? '') + '')) {
w.bidirectional_control_characters_detected(node);
}
value = b.literal((evaluated.value ?? '') + '');
}
return { value, has_state };
@ -96,7 +103,10 @@ export function build_template_chunk(
}
if (evaluated.is_known) {
quasi.value.cooked += evaluated.value + '';
if (regex_bidirectional_control_characters.test((evaluated.value ?? '') + '')) {
w.bidirectional_control_characters_detected(node);
}
quasi.value.cooked += (evaluated.value ?? '') + '';
} else {
if (!evaluated.is_defined) {
// add `?? ''` where necessary

@ -9,8 +9,12 @@ import {
EMPTY_COMMENT
} from '../../../../../../internal/server/hydration.js';
import * as b from '#compiler/builders';
import * as w from '../../../../../warnings.js';
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
import { regex_whitespaces_strict } from '../../../../patterns.js';
import {
regex_bidirectional_control_characters,
regex_whitespaces_strict
} from '../../../../patterns.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);
@ -48,6 +52,9 @@ export function process_children(nodes, { visit, state }) {
const evaluated = state.scope.evaluate(node.expression);
if (evaluated.is_known) {
if (regex_bidirectional_control_characters.test((evaluated.value ?? '') + '')) {
w.bidirectional_control_characters_detected(node);
}
quasi.value.cooked += escape_html((evaluated.value ?? '') + '');
} else {
expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));

@ -23,12 +23,12 @@ export const STRING = Symbol('string');
/** @type {Record<string, [type: NUMBER | STRING | UNKNOWN, fn?: Function]>} */
const globals = {
BigInt: [NUMBER, BigInt],
BigInt: [NUMBER],
'Math.min': [NUMBER, Math.min],
'Math.max': [NUMBER, Math.max],
'Math.random': [NUMBER],
'Math.floor': [NUMBER, Math.floor],
// @ts-expect-error
// @ts-ignore
'Math.f16round': [NUMBER, Math.f16round],
'Math.round': [NUMBER, Math.round],
'Math.abs': [NUMBER, Math.abs],

Loading…
Cancel
Save