Skip to content

Commit 4b1b886

Browse files
authored
chore: make dev state globally available (#12669)
* make `dev` state globally available * tidy up
1 parent 2d9bc25 commit 4b1b886

File tree

11 files changed

+69
-71
lines changed

11 files changed

+69
-71
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { javascript_visitors_runes } from './visitors/javascript-runes.js';
1212
import { javascript_visitors_legacy } from './visitors/javascript-legacy.js';
1313
import { serialize_get_binding } from './utils.js';
1414
import { render_stylesheet } from '../css/index.js';
15-
import { filename } from '../../../state.js';
15+
import { dev, filename } from '../../../state.js';
1616

1717
/**
1818
* This function ensures visitor sets don't accidentally clobber each other
@@ -164,7 +164,7 @@ export function client_component(source, analysis, options) {
164164
store_setup.push(
165165
b.const(
166166
binding.node,
167-
options.dev
167+
dev
168168
? b.thunk(
169169
b.sequence([
170170
b.call('$.validate_store', store_reference, b.literal(name.slice(1))),
@@ -210,7 +210,7 @@ export function client_component(source, analysis, options) {
210210
getter,
211211
b.set(alias ?? name, [b.stmt(b.assignment('=', expression, b.id('$$value')))])
212212
];
213-
} else if (!options.dev) {
213+
} else if (!dev) {
214214
return b.init(alias ?? name, expression);
215215
}
216216
}
@@ -238,7 +238,7 @@ export function client_component(source, analysis, options) {
238238
(binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$')
239239
);
240240

241-
if (analysis.runes && options.dev) {
241+
if (dev && analysis.runes) {
242242
const exports = analysis.exports.map(({ name, alias }) => b.literal(alias ?? name));
243243
/** @type {ESTree.Literal[]} */
244244
const bindable = [];
@@ -300,12 +300,12 @@ export function client_component(source, analysis, options) {
300300
)
301301
)
302302
);
303-
} else if (options.dev) {
303+
} else if (dev) {
304304
component_returned_object.push(b.spread(b.call(b.id('$.legacy_api'))));
305305
}
306306

307307
const push_args = [b.id('$$props'), b.literal(analysis.runes)];
308-
if (options.dev) push_args.push(b.id(analysis.name));
308+
if (dev) push_args.push(b.id(analysis.name));
309309

310310
const component_block = b.block([
311311
...store_setup,
@@ -345,10 +345,10 @@ export function client_component(source, analysis, options) {
345345
}
346346

347347
const should_inject_context =
348+
dev ||
348349
analysis.needs_context ||
349350
analysis.reactive_statements.size > 0 ||
350-
component_returned_object.length > 0 ||
351-
options.dev;
351+
component_returned_object.length > 0;
352352

353353
if (should_inject_context) {
354354
component_block.body.unshift(b.stmt(b.call('$.push', ...push_args)));
@@ -462,7 +462,7 @@ export function client_component(source, analysis, options) {
462462
body.push(b.export_default(component));
463463
}
464464

465-
if (options.dev) {
465+
if (dev) {
466466
if (filename) {
467467
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
468468
body.unshift(
@@ -498,7 +498,7 @@ export function client_component(source, analysis, options) {
498498
)
499499
)
500500
);
501-
} else if (options.dev) {
501+
} else if (dev) {
502502
component_block.body.unshift(b.stmt(b.call('$.check_target', b.id('new.target'))));
503503
}
504504

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PROPS_IS_RUNES,
1717
PROPS_IS_UPDATED
1818
} from '../../../../constants.js';
19+
import { dev } from '../../../state.js';
1920

2021
/**
2122
* @template {ClientTransformState} State
@@ -212,7 +213,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
212213
assignment.right =
213214
private_state.kind === 'frozen_state'
214215
? b.call('$.freeze', value)
215-
: serialize_proxy_reassignment(value, private_state.id, state);
216+
: serialize_proxy_reassignment(value, private_state.id);
216217
return assignment;
217218
}
218219
}
@@ -225,7 +226,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
225226
should_proxy_or_freeze(value, context.state.scope)
226227
? private_state.kind === 'frozen_state'
227228
? b.call('$.freeze', value)
228-
: serialize_proxy_reassignment(value, private_state.id, state)
229+
: serialize_proxy_reassignment(value, private_state.id)
229230
: value
230231
);
231232
}
@@ -249,7 +250,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
249250
assignment.right =
250251
public_state.kind === 'frozen_state'
251252
? b.call('$.freeze', value)
252-
: serialize_proxy_reassignment(value, public_state.id, state);
253+
: serialize_proxy_reassignment(value, public_state.id);
253254
return assignment;
254255
}
255256
}
@@ -317,7 +318,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
317318
context.state.analysis.runes &&
318319
!options?.skip_proxy_and_freeze &&
319320
should_proxy_or_freeze(value, context.state.scope)
320-
? serialize_proxy_reassignment(value, left_name, state)
321+
? serialize_proxy_reassignment(value, left_name)
321322
: value
322323
);
323324
} else if (binding.kind === 'frozen_state') {
@@ -340,7 +341,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
340341
!options?.skip_proxy_and_freeze &&
341342
should_proxy_or_freeze(value, context.state.scope) &&
342343
binding.kind === 'bindable_prop'
343-
? serialize_proxy_reassignment(value, left_name, state)
344+
? serialize_proxy_reassignment(value, left_name)
344345
: value
345346
);
346347
} else {
@@ -435,10 +436,9 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
435436
/**
436437
* @param {Expression} value
437438
* @param {PrivateIdentifier | string} proxy_reference
438-
* @param {ClientTransformState} state
439439
*/
440-
export function serialize_proxy_reassignment(value, proxy_reference, state) {
441-
return state.options.dev
440+
export function serialize_proxy_reassignment(value, proxy_reference) {
441+
return dev
442442
? b.call(
443443
'$.proxy',
444444
value,

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '../utils.js';
1515
import { extract_paths } from '../../../../utils/ast.js';
1616
import { regex_invalid_identifier_chars } from '../../../patterns.js';
17+
import { dev } from '../../../../state.js';
1718

1819
/** @type {ComponentVisitors} */
1920
export const javascript_visitors_runes = {
@@ -148,11 +149,7 @@ export const javascript_visitors_runes = {
148149
'set',
149150
definition.key,
150151
[value],
151-
[
152-
b.stmt(
153-
b.call('$.set', member, serialize_proxy_reassignment(value, field.id, state))
154-
)
155-
]
152+
[b.stmt(b.call('$.set', member, serialize_proxy_reassignment(value, field.id)))]
156153
)
157154
);
158155
}
@@ -170,7 +167,7 @@ export const javascript_visitors_runes = {
170167
);
171168
}
172169

173-
if ((field.kind === 'derived' || field.kind === 'derived_call') && state.options.dev) {
170+
if (dev && (field.kind === 'derived' || field.kind === 'derived_call')) {
174171
body.push(
175172
b.method(
176173
'set',
@@ -188,7 +185,7 @@ export const javascript_visitors_runes = {
188185
body.push(/** @type {MethodDefinition} **/ (visit(definition, child_state)));
189186
}
190187

191-
if (state.options.dev && public_state.size > 0) {
188+
if (dev && public_state.size > 0) {
192189
// add an `[$.ADD_OWNER]` method so that a class with state fields can widen ownership
193190
body.push(
194191
b.method(
@@ -248,7 +245,7 @@ export const javascript_visitors_runes = {
248245
/** @type {Expression[]} */
249246
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
250247

251-
if (state.options.dev) {
248+
if (dev) {
252249
// include rest name, so we can provide informative error messages
253250
args.push(b.literal(declarator.id.name));
254251
}
@@ -287,7 +284,7 @@ export const javascript_visitors_runes = {
287284
/** @type {Expression[]} */
288285
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
289286

290-
if (state.options.dev) {
287+
if (dev) {
291288
// include rest name, so we can provide informative error messages
292289
args.push(b.literal(/** @type {Identifier} */ (property.argument).name));
293290
}
@@ -474,7 +471,7 @@ export const javascript_visitors_runes = {
474471
BinaryExpression(node, { state, visit, next }) {
475472
const operator = node.operator;
476473

477-
if (state.options.dev) {
474+
if (dev) {
478475
if (operator === '===' || operator === '!==') {
479476
return b.call(
480477
'$.strict_equals',

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { regex_is_valid_identifier } from '../../../patterns.js';
5353
import { javascript_visitors_runes } from './javascript-runes.js';
5454
import { sanitize_template_string } from '../../../../utils/sanitize_template_string.js';
5555
import { walk } from 'zimmerframe';
56-
import { locator } from '../../../../state.js';
56+
import { dev, locator } from '../../../../state.js';
5757
import is_reference from 'is-reference';
5858

5959
/**
@@ -780,18 +780,14 @@ function serialize_inline_component(node, component_name, context, anchor = cont
780780
} else if (attribute.type === 'BindDirective') {
781781
const expression = /** @type {Expression} */ (context.visit(attribute.expression));
782782

783-
if (
784-
expression.type === 'MemberExpression' &&
785-
context.state.options.dev &&
786-
context.state.analysis.runes
787-
) {
783+
if (dev && expression.type === 'MemberExpression' && context.state.analysis.runes) {
788784
context.state.init.push(serialize_validate_binding(context.state, attribute, expression));
789785
}
790786

791787
if (attribute.name === 'this') {
792788
bind_this = attribute.expression;
793789
} else {
794-
if (context.state.options.dev) {
790+
if (dev) {
795791
binding_initializers.push(
796792
b.stmt(b.call(b.id('$.add_owner_effect'), b.thunk(expression), b.id(component_name)))
797793
);
@@ -894,9 +890,7 @@ function serialize_inline_component(node, component_name, context, anchor = cont
894890
push_prop(
895891
b.init(
896892
'children',
897-
context.state.options.dev
898-
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn)
899-
: slot_fn
893+
dev ? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn) : slot_fn
900894
)
901895
);
902896

@@ -968,7 +962,7 @@ function serialize_inline_component(node, component_name, context, anchor = cont
968962
b.block([
969963
...binding_initializers,
970964
b.stmt(
971-
context.state.options.dev
965+
dev
972966
? b.call('$.validate_dynamic_component', b.thunk(prev(b.id('$$anchor'))))
973967
: prev(b.id('$$anchor'))
974968
)
@@ -1696,7 +1690,7 @@ export const template_visitors = {
16961690
*/
16971691
const add_template = (template_name, args) => {
16981692
let call = b.call(get_template_function(namespace, state), ...args);
1699-
if (context.state.options.dev) {
1693+
if (dev) {
17001694
call = b.call(
17011695
'$.add_locations',
17021696
call,
@@ -1837,7 +1831,7 @@ export const template_visitors = {
18371831

18381832
// we need to eagerly evaluate the expression in order to hit any
18391833
// 'Cannot access x before initialization' errors
1840-
if (state.options.dev) {
1834+
if (dev) {
18411835
state.init.push(b.stmt(b.call('$.get', declaration.id)));
18421836
}
18431837
} else {
@@ -1871,7 +1865,7 @@ export const template_visitors = {
18711865

18721866
// we need to eagerly evaluate the expression in order to hit any
18731867
// 'Cannot access x before initialization' errors
1874-
if (state.options.dev) {
1868+
if (dev) {
18751869
state.init.push(b.stmt(b.call('$.get', tmp)));
18761870
}
18771871

@@ -1987,7 +1981,7 @@ export const template_visitors = {
19871981
/** @type {SourceLocation} */
19881982
let location = [-1, -1];
19891983

1990-
if (context.state.options.dev) {
1984+
if (dev) {
19911985
const loc = locator(node.start);
19921986
if (loc) {
19931987
location[0] = loc.line;
@@ -2370,7 +2364,7 @@ export const template_visitors = {
23702364

23712365
const get_tag = b.thunk(/** @type {Expression} */ (context.visit(node.tag)));
23722366

2373-
if (context.state.options.dev && context.state.metadata.namespace !== 'foreign') {
2367+
if (dev && context.state.metadata.namespace !== 'foreign') {
23742368
if (node.fragment.nodes.length > 0) {
23752369
context.state.init.push(b.stmt(b.call('$.validate_void_dynamic_element', get_tag)));
23762370
}
@@ -2395,7 +2389,7 @@ export const template_visitors = {
23952389
).body
23962390
);
23972391

2398-
const location = context.state.options.dev && locator(node.start);
2392+
const location = dev && locator(node.start);
23992393

24002394
context.state.init.push(
24012395
b.stmt(
@@ -2614,7 +2608,7 @@ export const template_visitors = {
26142608

26152609
// we need to eagerly evaluate the expression in order to hit any
26162610
// 'Cannot access x before initialization' errors
2617-
if (context.state.options.dev) {
2611+
if (dev) {
26182612
declarations.push(b.stmt(getter));
26192613
}
26202614

@@ -2641,7 +2635,7 @@ export const template_visitors = {
26412635
declarations.push(b.let(node.index, index));
26422636
}
26432637

2644-
if (context.state.options.dev && (flags & EACH_KEYED) !== 0) {
2638+
if (dev && (flags & EACH_KEYED) !== 0) {
26452639
context.state.init.push(
26462640
b.stmt(b.call('$.validate_each_keys', b.thunk(collection), key_function))
26472641
);
@@ -2834,7 +2828,7 @@ export const template_visitors = {
28342828

28352829
// we need to eagerly evaluate the expression in order to hit any
28362830
// 'Cannot access x before initialization' errors
2837-
if (context.state.options.dev) {
2831+
if (dev) {
28382832
declarations.push(b.stmt(getters[name]));
28392833
}
28402834
}
@@ -2848,7 +2842,7 @@ export const template_visitors = {
28482842
/** @type {Expression} */
28492843
let snippet = b.arrow(args, body);
28502844

2851-
if (context.state.options.dev) {
2845+
if (dev) {
28522846
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
28532847
}
28542848

@@ -2908,7 +2902,7 @@ export const template_visitors = {
29082902
type === 'AwaitBlock' ||
29092903
type === 'KeyBlock'
29102904
)) &&
2911-
context.state.options.dev &&
2905+
dev &&
29122906
context.state.analysis.runes
29132907
) {
29142908
context.state.init.push(

0 commit comments

Comments
 (0)