Skip to content

Commit 6a9cfa3

Browse files
committed
feedback
1 parent 64ca35f commit 6a9cfa3

File tree

12 files changed

+39
-94
lines changed

12 files changed

+39
-94
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/Fragment.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Expression, Identifier, Statement } from 'estree' */
1+
/** @import { Expression, Identifier, Statement, TemplateElement } from 'estree' */
22
/** @import { AST, Namespace } from '#compiler' */
33
/** @import { SourceLocation } from '#shared' */
44
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
@@ -212,12 +212,34 @@ function join_template(items) {
212212
let quasi = b.quasi('');
213213
const template = b.template([quasi], []);
214214

215+
/**
216+
* @param {Expression} expression
217+
*/
218+
function push(expression) {
219+
if (expression.type === 'TemplateLiteral') {
220+
for (let i = 0; i < expression.expressions.length; i += 1) {
221+
const q = expression.quasis[i];
222+
const e = expression.expressions[i];
223+
224+
quasi.value.cooked += /** @type {string} */ (q.value.cooked);
225+
push(e);
226+
}
227+
228+
const last = /** @type {TemplateElement} */ (expression.quasis.at(-1));
229+
quasi.value.cooked += /** @type {string} */ (last.value.cooked);
230+
} else if (expression.type === 'Literal') {
231+
/** @type {string} */ (quasi.value.cooked) += expression.value;
232+
} else {
233+
template.expressions.push(expression);
234+
template.quasis.push((quasi = b.quasi('')));
235+
}
236+
}
237+
215238
for (const item of items) {
216239
if (typeof item === 'string') {
217240
quasi.value.cooked += item;
218241
} else {
219-
template.expressions.push(item);
220-
template.quasis.push((quasi = b.quasi('')));
242+
push(item);
221243
}
222244
}
223245

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
import { process_children } from './shared/fragment.js';
2828
import {
2929
build_render_statement,
30-
build_template_literal,
30+
build_template_chunk,
3131
build_update,
3232
build_update_assignment,
3333
get_states_and_calls
@@ -364,7 +364,7 @@ export function RegularElement(node, context) {
364364
b.assignment(
365365
'=',
366366
b.member(context.state.node, 'textContent'),
367-
build_template_literal(trimmed, context.visit, child_state).value
367+
build_template_chunk(trimmed, context.visit, child_state).value
368368
)
369369
)
370370
);

packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { ComponentContext } from '../types' */
33
import * as b from '../../../../utils/builders.js';
4-
import { build_template_literal } from './shared/utils.js';
4+
import { build_template_chunk } from './shared/utils.js';
55

66
/**
77
* @param {AST.TitleElement} node
88
* @param {ComponentContext} context
99
*/
1010
export function TitleElement(node, context) {
11-
const { has_state, value } = build_template_literal(
11+
const { has_state, value } = build_template_chunk(
1212
/** @type {any} */ (node.fragment.nodes),
1313
context.visit,
1414
context.state

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { is_ignored } from '../../../../../state.js';
66
import { get_attribute_expression, is_event_attribute } from '../../../../../utils/ast.js';
77
import * as b from '../../../../../utils/builders.js';
88
import { build_getter, create_derived } from '../../utils.js';
9-
import { build_template_literal, build_update } from './utils.js';
9+
import { build_template_chunk, build_update } from './utils.js';
1010

1111
/**
1212
* @param {Array<AST.Attribute | AST.SpreadAttribute>} attributes
@@ -200,7 +200,7 @@ export function build_attribute_value(value, context) {
200200
};
201201
}
202202

203-
return build_template_literal(value, context.visit, context.state);
203+
return build_template_chunk(value, context.visit, context.state);
204204
}
205205

206206
/**

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { cannot_be_set_statically } from '../../../../../../utils.js';
55
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
66
import * as b from '../../../../../utils/builders.js';
7-
import { build_template_literal, build_update } from './utils.js';
7+
import { build_template_chunk, build_update } from './utils.js';
88

99
/**
1010
* Processes an array of template nodes, joining sibling text/expression nodes
@@ -69,7 +69,7 @@ export function process_children(nodes, initial, is_element, { visit, state }) {
6969

7070
state.template.push(' ');
7171

72-
const { has_state, has_call, value } = build_template_literal(sequence, visit, state);
72+
const { has_state, has_call, value } = build_template_chunk(sequence, visit, state);
7373

7474
// if this is a standalone `{expression}`, make sure we handle the case where
7575
// no text node was created because the expression was empty during SSR

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function get_states_and_calls(values) {
3838
* @param {ComponentClientTransformState} state
3939
* @returns {{ value: Expression, has_state: boolean, has_call: boolean }}
4040
*/
41-
export function build_template_literal(values, visit, state) {
41+
export function build_template_chunk(values, visit, state) {
4242
/** @type {Expression[]} */
4343
const expressions = [];
4444

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export {
155155
$window as window,
156156
$document as document
157157
} from './dom/operations.js';
158+
export { attr } from '../shared/attributes.js';
158159
export { snapshot } from '../shared/clone.js';
159160
export { noop, fallback } from '../shared/utils.js';
160161
export {

packages/svelte/src/internal/server/index.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/** @import { Component, Payload, RenderOutput } from '#server' */
33
/** @import { Store } from '#shared' */
44
export { FILENAME, HMR } from '../../constants.js';
5+
import { attr } from '../shared/attributes.js';
56
import { is_promise, noop } from '../shared/utils.js';
67
import { subscribe_to_store } from '../../store/utils.js';
78
import {
@@ -153,33 +154,6 @@ export function head(payload, fn) {
153154
head_payload.out += BLOCK_CLOSE;
154155
}
155156

156-
/**
157-
* `<div translate={false}>` should be rendered as `<div translate="no">` and _not_
158-
* `<div translate="false">`, which is equivalent to `<div translate="yes">`. There
159-
* may be other odd cases that need to be added to this list in future
160-
* @type {Record<string, Map<any, string>>}
161-
*/
162-
const replacements = {
163-
translate: new Map([
164-
[true, 'yes'],
165-
[false, 'no']
166-
])
167-
};
168-
169-
/**
170-
* @template V
171-
* @param {string} name
172-
* @param {V} value
173-
* @param {boolean} [is_boolean]
174-
* @returns {string}
175-
*/
176-
export function attr(name, value, is_boolean = false) {
177-
if (value == null || (!value && is_boolean) || (value === '' && name === 'class')) return '';
178-
const normalized = (name in replacements && replacements[name].get(value)) || value;
179-
const assignment = is_boolean ? '' : `="${escape_html(normalized, true)}"`;
180-
return ` ${name}${assignment}`;
181-
}
182-
183157
/**
184158
* @param {Payload} payload
185159
* @param {boolean} is_html
@@ -549,6 +523,8 @@ export function once(get_value) {
549523
};
550524
}
551525

526+
export { attr };
527+
552528
export { html } from './blocks/html.js';
553529

554530
export { push, pop } from './context.js';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<input READONLY={true} REQUIRED={false}>
1+
<input READONLY={!0} REQUIRED={!1}>

packages/svelte/tests/snapshot/samples/inline-module-vars/_expected/client/index.svelte.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)