Skip to content

Commit 64219ed

Browse files
committed
chore: change options from boolean to list (templatingMode: "functional")
1 parent c75c429 commit 64219ed

File tree

13 files changed

+35
-33
lines changed

13 files changed

+35
-33
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function client_component(analysis, options) {
167167
in_constructor: false,
168168
instance_level_snippets: [],
169169
module_level_snippets: [],
170-
prevent_template_cloning: options.preventTemplateCloning,
170+
is_functional_template_mode: options.templatingMode === 'functional',
171171

172172
// these are set inside the `Fragment` visitor, and cannot be used until then
173173
init: /** @type {any} */ (null),

packages/svelte/src/compiler/phases/3-transform/client/transform-template/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function get_template_function(namespace, state) {
2727
: contains_script_tag
2828
? '$.template_with_script'
2929
: '$.template'
30-
).concat(state.prevent_template_cloning ? '_fn' : '');
30+
).concat(state.is_functional_template_mode ? '_fn' : '');
3131
}
3232

3333
/**
@@ -75,7 +75,7 @@ export function transform_template(state, context, namespace, template_name, fla
7575

7676
/** @type {Expression[]} */
7777
const args = [
78-
state.prevent_template_cloning
78+
state.is_functional_template_mode
7979
? template_to_functions(state.template, namespace)
8080
: b.template([b.quasi(template_to_string(state.template), true)], [])
8181
];

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export interface ComponentClientTransformState extends ClientTransformState {
9494
};
9595
};
9696
readonly preserve_whitespace: boolean;
97-
readonly prevent_template_cloning?: boolean;
97+
readonly is_functional_template_mode?: boolean;
9898

9999
/** The anchor node for the current context */
100100
readonly node: Identifier;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function Fragment(node, context) {
3737
context.state,
3838
context.state.preserve_whitespace,
3939
context.state.options.preserveComments,
40-
context.state.prevent_template_cloning
40+
context.state.is_functional_template_mode
4141
);
4242

4343
if (hoisted.length === 0 && trimmed.length === 0) {
@@ -133,7 +133,7 @@ export function Fragment(node, context) {
133133
...context,
134134
state
135135
},
136-
context.state.prevent_template_cloning
136+
context.state.is_functional_template_mode
137137
);
138138

139139
body.push(b.var(id, b.call('$.text')));
@@ -146,7 +146,7 @@ export function Fragment(node, context) {
146146
() => b.id('$$anchor'),
147147
false,
148148
{ ...context, state },
149-
context.state.prevent_template_cloning
149+
context.state.is_functional_template_mode
150150
);
151151
} else {
152152
/** @type {(is_text: boolean) => Expression} */
@@ -157,7 +157,7 @@ export function Fragment(node, context) {
157157
expression,
158158
false,
159159
{ ...context, state },
160-
context.state.prevent_template_cloning
160+
context.state.is_functional_template_mode
161161
);
162162

163163
let flags = TEMPLATE_FRAGMENT;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function RegularElement(node, context) {
124124
kind: 'set_prop',
125125
args: [
126126
'is',
127-
context.state.prevent_template_cloning
127+
context.state.is_functional_template_mode
128128
? value.value
129129
: escape_html(value.value, true)
130130
]
@@ -312,7 +312,7 @@ export function RegularElement(node, context) {
312312
: [
313313
value === true
314314
? ''
315-
: context.state.prevent_template_cloning
315+
: context.state.is_functional_template_mode
316316
? value
317317
: escape_html(value, true)
318318
]
@@ -386,7 +386,7 @@ export function RegularElement(node, context) {
386386
state,
387387
node.name === 'script' || state.preserve_whitespace,
388388
state.options.preserveComments,
389-
state.prevent_template_cloning
389+
state.is_functional_template_mode
390390
);
391391

392392
/** @type {typeof state} */
@@ -438,7 +438,7 @@ export function RegularElement(node, context) {
438438
...context,
439439
state: child_state
440440
},
441-
context.state.prevent_template_cloning
441+
context.state.is_functional_template_mode
442442
);
443443

444444
if (needs_reset) {

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { build_template_chunk } from './utils.js';
1515
* @param {(is_text: boolean) => Expression} initial
1616
* @param {boolean} is_element
1717
* @param {ComponentContext} context
18-
* @param {boolean} [prevent_template_cloning]
18+
* @param {boolean} [is_functional_template_mode]
1919
*/
2020
export function process_children(
2121
nodes,
2222
initial,
2323
is_element,
2424
{ visit, state },
25-
prevent_template_cloning
25+
is_functional_template_mode
2626
) {
2727
const within_bound_contenteditable = state.metadata.bound_contenteditable;
2828
let prev = initial;
@@ -73,7 +73,9 @@ export function process_children(
7373
skipped += 1;
7474
state.template.push({
7575
kind: 'create_text',
76-
args: [sequence.map((node) => (prevent_template_cloning ? node.data : node.raw)).join('')]
76+
args: [
77+
sequence.map((node) => (is_functional_template_mode ? node.data : node.raw)).join('')
78+
]
7779
});
7880
return;
7981
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function sort_const_tags(nodes, state) {
141141
* @param {TransformState & { options: ValidatedCompileOptions }} state
142142
* @param {boolean} preserve_whitespace
143143
* @param {boolean} preserve_comments
144-
* @param {boolean} [prevent_template_cloning]
144+
* @param {boolean} [is_functional_template_mode]
145145
*/
146146
export function clean_nodes(
147147
parent,
@@ -154,7 +154,7 @@ export function clean_nodes(
154154
// rather than from `ClientTransformState` and `ServerTransformState`
155155
preserve_whitespace,
156156
preserve_comments,
157-
prevent_template_cloning
157+
is_functional_template_mode
158158
) {
159159
if (!state.analysis.runes) {
160160
nodes = sort_const_tags(nodes, state);
@@ -276,13 +276,13 @@ export function clean_nodes(
276276
// initial newline inside a `<pre>` is disregarded, if not followed by another newline
277277
if (
278278
parent.type === 'RegularElement' &&
279-
(parent.name === 'pre' || (prevent_template_cloning && parent.name === 'textarea')) &&
279+
(parent.name === 'pre' || (is_functional_template_mode && parent.name === 'textarea')) &&
280280
first?.type === 'Text'
281281
) {
282282
const text = first.data.replace(regex_starts_with_newline, '');
283283
if (text !== first.data) {
284284
const tmp = text.replace(regex_starts_with_newline, '');
285-
if (text === tmp || prevent_template_cloning) {
285+
if (text === tmp || is_functional_template_mode) {
286286
first.data = text;
287287
first.raw = first.raw.replace(regex_starts_with_newline, '');
288288
if (first.data === '') {

packages/svelte/src/compiler/types/index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ export interface CompileOptions extends ModuleCompileOptions {
114114
*/
115115
preserveWhitespace?: boolean;
116116
/**
117-
* If `true`, the template will get compiled to a series of `document.createElement` calls instead of using `template.innerHTML`.
117+
* If `functional`, the template will get compiled to a series of `document.createElement` calls, if `string` it will render the template tp a string and use `template.innerHTML`.
118118
*
119-
* @default false
119+
* @default 'string'
120120
*/
121-
preventTemplateCloning?: boolean;
121+
templatingMode?: 'string' | 'functional';
122122
/**
123123
* Set to `true` to force the compiler into runes mode, even if there are no indications of runes usage.
124124
* Set to `false` to force the compiler into ignoring runes, even if there are indications of runes usage.

packages/svelte/src/compiler/validate-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const validate_component_options =
110110

111111
preserveComments: boolean(false),
112112

113-
preventTemplateCloning: boolean(false),
113+
templatingMode: list(['string', 'functional']),
114114

115115
preserveWhitespace: boolean(false),
116116

packages/svelte/tests/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function compile_directory(
8080
filename: path.join(cwd, file),
8181
...compileOptions,
8282
generate,
83-
preventTemplateCloning: templating_mode === 'functional'
83+
templatingMode: templating_mode
8484
};
8585

8686
if (file.endsWith('.js')) {

packages/svelte/tests/runtime-browser/test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async function run_test(
106106
immutable: config.immutable,
107107
customElement: test_dir.includes('custom-elements-samples'),
108108
accessors: 'accessors' in config ? config.accessors : true,
109-
preventTemplateCloning: templating_mode === 'functional'
109+
templatingMode: templating_mode
110110
});
111111

112112
write(
@@ -171,7 +171,7 @@ async function run_test(
171171
immutable: config.immutable,
172172
customElement: test_dir.includes('custom-elements-samples'),
173173
accessors: 'accessors' in config ? config.accessors : true,
174-
preventTemplateCloning: templating_mode === 'functional'
174+
templatingMode: templating_mode
175175
});
176176

177177
return {

packages/svelte/tests/runtime-legacy/shared.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async function common_setup(
168168
immutable: config.immutable,
169169
accessors: 'accessors' in config ? config.accessors : true,
170170
runes,
171-
preventTemplateCloning: templating_mode === 'functional'
171+
templatingMode: templating_mode
172172
};
173173

174174
// load_compiled can be used for debugging a test. It means the compiler will not run on the input

packages/svelte/types/index.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,11 @@ declare module 'svelte/compiler' {
845845
*/
846846
preserveWhitespace?: boolean;
847847
/**
848-
* If `true`, the template will get compiled to a series of `document.createElement` calls instead of using `template.innerHTML`.
848+
* If `functional`, the template will get compiled to a series of `document.createElement` calls, if `string` it will render the template tp a string and use `template.innerHTML`.
849849
*
850-
* @default false
850+
* @default 'string'
851851
*/
852-
preventTemplateCloning?: boolean;
852+
templatingMode?: 'string' | 'functional';
853853
/**
854854
* Set to `true` to force the compiler into runes mode, even if there are no indications of runes usage.
855855
* Set to `false` to force the compiler into ignoring runes, even if there are indications of runes usage.
@@ -2561,11 +2561,11 @@ declare module 'svelte/types/compiler/interfaces' {
25612561
*/
25622562
preserveWhitespace?: boolean;
25632563
/**
2564-
* If `true`, the template will get compiled to a series of `document.createElement` calls instead of using `template.innerHTML`.
2564+
* If `functional`, the template will get compiled to a series of `document.createElement` calls, if `string` it will render the template tp a string and use `template.innerHTML`.
25652565
*
2566-
* @default false
2566+
* @default 'string'
25672567
*/
2568-
preventTemplateCloning?: boolean;
2568+
templatingMode?: 'string' | 'functional';
25692569
/**
25702570
* Set to `true` to force the compiler into runes mode, even if there are no indications of runes usage.
25712571
* Set to `false` to force the compiler into ignoring runes, even if there are indications of runes usage.

0 commit comments

Comments
 (0)