Skip to content

chore: make dev state globally available #12669

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

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { javascript_visitors_runes } from './visitors/javascript-runes.js';
import { javascript_visitors_legacy } from './visitors/javascript-legacy.js';
import { serialize_get_binding } from './utils.js';
import { render_stylesheet } from '../css/index.js';
import { filename } from '../../../state.js';
import { dev, filename } from '../../../state.js';

/**
* This function ensures visitor sets don't accidentally clobber each other
Expand Down Expand Up @@ -164,7 +164,7 @@ export function client_component(source, analysis, options) {
store_setup.push(
b.const(
binding.node,
options.dev
dev
? b.thunk(
b.sequence([
b.call('$.validate_store', store_reference, b.literal(name.slice(1))),
Expand Down Expand Up @@ -210,7 +210,7 @@ export function client_component(source, analysis, options) {
getter,
b.set(alias ?? name, [b.stmt(b.assignment('=', expression, b.id('$$value')))])
];
} else if (!options.dev) {
} else if (!dev) {
return b.init(alias ?? name, expression);
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export function client_component(source, analysis, options) {
(binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$')
);

if (analysis.runes && options.dev) {
if (dev && analysis.runes) {
const exports = analysis.exports.map(({ name, alias }) => b.literal(alias ?? name));
/** @type {ESTree.Literal[]} */
const bindable = [];
Expand Down Expand Up @@ -300,12 +300,12 @@ export function client_component(source, analysis, options) {
)
)
);
} else if (options.dev) {
} else if (dev) {
component_returned_object.push(b.spread(b.call(b.id('$.legacy_api'))));
}

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

const component_block = b.block([
...store_setup,
Expand Down Expand Up @@ -345,10 +345,10 @@ export function client_component(source, analysis, options) {
}

const should_inject_context =
dev ||
analysis.needs_context ||
analysis.reactive_statements.size > 0 ||
component_returned_object.length > 0 ||
options.dev;
component_returned_object.length > 0;

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

if (options.dev) {
if (dev) {
if (filename) {
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
body.unshift(
Expand Down Expand Up @@ -498,7 +498,7 @@ export function client_component(source, analysis, options) {
)
)
);
} else if (options.dev) {
} else if (dev) {
component_block.body.unshift(b.stmt(b.call('$.check_target', b.id('new.target'))));
}

Expand Down
16 changes: 8 additions & 8 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
PROPS_IS_RUNES,
PROPS_IS_UPDATED
} from '../../../../constants.js';
import { dev } from '../../../state.js';

/**
* @template {ClientTransformState} State
Expand Down Expand Up @@ -212,7 +213,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
assignment.right =
private_state.kind === 'frozen_state'
? b.call('$.freeze', value)
: serialize_proxy_reassignment(value, private_state.id, state);
: serialize_proxy_reassignment(value, private_state.id);
return assignment;
}
}
Expand All @@ -225,7 +226,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
should_proxy_or_freeze(value, context.state.scope)
? private_state.kind === 'frozen_state'
? b.call('$.freeze', value)
: serialize_proxy_reassignment(value, private_state.id, state)
: serialize_proxy_reassignment(value, private_state.id)
: value
);
}
Expand All @@ -249,7 +250,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
assignment.right =
public_state.kind === 'frozen_state'
? b.call('$.freeze', value)
: serialize_proxy_reassignment(value, public_state.id, state);
: serialize_proxy_reassignment(value, public_state.id);
return assignment;
}
}
Expand Down Expand Up @@ -317,7 +318,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
context.state.analysis.runes &&
!options?.skip_proxy_and_freeze &&
should_proxy_or_freeze(value, context.state.scope)
? serialize_proxy_reassignment(value, left_name, state)
? serialize_proxy_reassignment(value, left_name)
: value
);
} else if (binding.kind === 'frozen_state') {
Expand All @@ -340,7 +341,7 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
!options?.skip_proxy_and_freeze &&
should_proxy_or_freeze(value, context.state.scope) &&
binding.kind === 'bindable_prop'
? serialize_proxy_reassignment(value, left_name, state)
? serialize_proxy_reassignment(value, left_name)
: value
);
} else {
Expand Down Expand Up @@ -435,10 +436,9 @@ export function serialize_set_binding(node, context, fallback, prefix, options)
/**
* @param {Expression} value
* @param {PrivateIdentifier | string} proxy_reference
* @param {ClientTransformState} state
*/
export function serialize_proxy_reassignment(value, proxy_reference, state) {
return state.options.dev
export function serialize_proxy_reassignment(value, proxy_reference) {
return dev
? b.call(
'$.proxy',
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../utils.js';
import { extract_paths } from '../../../../utils/ast.js';
import { regex_invalid_identifier_chars } from '../../../patterns.js';
import { dev } from '../../../../state.js';

/** @type {ComponentVisitors} */
export const javascript_visitors_runes = {
Expand Down Expand Up @@ -148,11 +149,7 @@ export const javascript_visitors_runes = {
'set',
definition.key,
[value],
[
b.stmt(
b.call('$.set', member, serialize_proxy_reassignment(value, field.id, state))
)
]
[b.stmt(b.call('$.set', member, serialize_proxy_reassignment(value, field.id)))]
)
);
}
Expand All @@ -170,7 +167,7 @@ export const javascript_visitors_runes = {
);
}

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

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

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

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

if (state.options.dev) {
if (dev) {
if (operator === '===' || operator === '!==') {
return b.call(
'$.strict_equals',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { regex_is_valid_identifier } from '../../../patterns.js';
import { javascript_visitors_runes } from './javascript-runes.js';
import { sanitize_template_string } from '../../../../utils/sanitize_template_string.js';
import { walk } from 'zimmerframe';
import { locator } from '../../../../state.js';
import { dev, locator } from '../../../../state.js';
import is_reference from 'is-reference';

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

if (
expression.type === 'MemberExpression' &&
context.state.options.dev &&
context.state.analysis.runes
) {
if (dev && expression.type === 'MemberExpression' && context.state.analysis.runes) {
context.state.init.push(serialize_validate_binding(context.state, attribute, expression));
}

if (attribute.name === 'this') {
bind_this = attribute.expression;
} else {
if (context.state.options.dev) {
if (dev) {
binding_initializers.push(
b.stmt(b.call(b.id('$.add_owner_effect'), b.thunk(expression), b.id(component_name)))
);
Expand Down Expand Up @@ -894,9 +890,7 @@ function serialize_inline_component(node, component_name, context, anchor = cont
push_prop(
b.init(
'children',
context.state.options.dev
? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn)
: slot_fn
dev ? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn) : slot_fn
)
);

Expand Down Expand Up @@ -968,7 +962,7 @@ function serialize_inline_component(node, component_name, context, anchor = cont
b.block([
...binding_initializers,
b.stmt(
context.state.options.dev
dev
? b.call('$.validate_dynamic_component', b.thunk(prev(b.id('$$anchor'))))
: prev(b.id('$$anchor'))
)
Expand Down Expand Up @@ -1696,7 +1690,7 @@ export const template_visitors = {
*/
const add_template = (template_name, args) => {
let call = b.call(get_template_function(namespace, state), ...args);
if (context.state.options.dev) {
if (dev) {
call = b.call(
'$.add_locations',
call,
Expand Down Expand Up @@ -1837,7 +1831,7 @@ export const template_visitors = {

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

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

Expand Down Expand Up @@ -1987,7 +1981,7 @@ export const template_visitors = {
/** @type {SourceLocation} */
let location = [-1, -1];

if (context.state.options.dev) {
if (dev) {
const loc = locator(node.start);
if (loc) {
location[0] = loc.line;
Expand Down Expand Up @@ -2370,7 +2364,7 @@ export const template_visitors = {

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

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

const location = context.state.options.dev && locator(node.start);
const location = dev && locator(node.start);

context.state.init.push(
b.stmt(
Expand Down Expand Up @@ -2614,7 +2608,7 @@ export const template_visitors = {

// we need to eagerly evaluate the expression in order to hit any
// 'Cannot access x before initialization' errors
if (context.state.options.dev) {
if (dev) {
declarations.push(b.stmt(getter));
}

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

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

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

if (context.state.options.dev) {
if (dev) {
snippet = b.call('$.wrap_snippet', b.id(context.state.analysis.name), snippet);
}

Expand Down Expand Up @@ -2908,7 +2902,7 @@ export const template_visitors = {
type === 'AwaitBlock' ||
type === 'KeyBlock'
)) &&
context.state.options.dev &&
dev &&
context.state.analysis.runes
) {
context.state.init.push(
Expand Down
Loading
Loading