Skip to content

Commit a7442f6

Browse files
committed
add a FILENAME symbol
1 parent 436cc99 commit a7442f6

File tree

13 files changed

+42
-21
lines changed

13 files changed

+42
-21
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,14 @@ export function client_component(source, analysis, options) {
458458

459459
if (options.dev) {
460460
if (filename) {
461-
// add `App.filename = 'App.svelte'` so that we can print useful messages later
461+
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
462462
body.unshift(
463463
b.stmt(
464-
b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.literal(filename))
464+
b.assignment(
465+
'=',
466+
b.member(b.id(analysis.name), b.id('$.FILENAME'), true),
467+
b.literal(filename)
468+
)
465469
)
466470
);
467471
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ export const template_visitors = {
16401640
call = b.call(
16411641
'$.add_locations',
16421642
call,
1643-
b.member(b.id(context.state.analysis.name), b.id('filename')),
1643+
b.member(b.id(context.state.analysis.name), b.id('$.FILENAME'), true),
16441644
serialize_locations(state.locations)
16451645
);
16461646
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,10 +2249,14 @@ export function server_component(analysis, options) {
22492249
}
22502250

22512251
if (options.dev && filename) {
2252-
// add `App.filename = 'App.svelte'` so that we can print useful messages later
2252+
// add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later
22532253
body.unshift(
22542254
b.stmt(
2255-
b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.literal(filename))
2255+
b.assignment(
2256+
'=',
2257+
b.member(b.id(analysis.name), b.id('$.FILENAME'), true),
2258+
b.literal(filename)
2259+
)
22562260
)
22572261
);
22582262
}

packages/svelte/src/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const ELEMENT_PRESERVE_ATTRIBUTE_CASE = 1 << 1;
3030

3131
export const UNINITIALIZED = Symbol();
3232

33+
// Dev-time component properties
34+
export const FILENAME = Symbol('filename');
35+
export const ORIGINAL = Symbol('original');
36+
3337
/** List of elements that require raw contents and should not have SSR comments put in them */
3438
export const RawTextElements = ['textarea', 'script', 'style', 'title'];
3539

packages/svelte/src/internal/client/dev/legacy.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import * as e from '../errors.js';
22
import { current_component_context } from '../runtime.js';
3+
import { FILENAME } from '../../../constants.js';
34
import { get_component } from './ownership.js';
45

5-
/** @param {Function & { filename: string }} target */
6+
/** @param {Function & { [FILENAME]: string }} target */
67
export function check_target(target) {
78
if (target) {
8-
e.component_api_invalid_new(target.filename ?? 'a component', target.name);
9+
e.component_api_invalid_new(target[FILENAME] ?? 'a component', target.name);
910
}
1011
}
1112

@@ -15,8 +16,8 @@ export function legacy_api() {
1516
/** @param {string} method */
1617
function error(method) {
1718
// @ts-expect-error
18-
const parent = get_component()?.filename ?? 'Something';
19-
e.component_api_changed(parent, method, component.filename);
19+
const parent = get_component()?.[FILENAME] ?? 'Something';
20+
e.component_api_changed(parent, method, component[FILENAME]);
2021
}
2122

2223
return {

packages/svelte/src/internal/client/dev/ownership.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { render_effect, user_pre_effect } from '../reactivity/effects.js';
66
import { dev_current_component_function } from '../runtime.js';
77
import { get_prototype_of } from '../../shared/utils.js';
88
import * as w from '../warnings.js';
9+
import { FILENAME } from '../../../constants.js';
910

1011
/** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>>} */
1112
const boundaries = {};
@@ -115,8 +116,8 @@ export function add_owner(object, owner, global = false) {
115116
if (metadata && !has_owner(metadata, component)) {
116117
let original = get_owner(metadata);
117118

118-
if (owner.filename !== component.filename) {
119-
w.ownership_invalid_binding(component.filename, owner.filename, original.filename);
119+
if (owner[FILENAME] !== component[FILENAME]) {
120+
w.ownership_invalid_binding(component[FILENAME], owner[FILENAME], original[FILENAME]);
120121
}
121122
}
122123
}
@@ -236,9 +237,9 @@ export function check_ownership(metadata) {
236237
let original = get_owner(metadata);
237238

238239
// @ts-expect-error
239-
if (original.filename !== component.filename) {
240+
if (original[FILENAME] !== component[FILENAME]) {
240241
// @ts-expect-error
241-
w.ownership_invalid_mutation(component.filename, original.filename);
242+
w.ownership_invalid_mutation(component[FILENAME], original[FILENAME]);
242243
} else {
243244
w.ownership_invalid_mutation();
244245
}

packages/svelte/src/internal/client/dom/blocks/html.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as w from '../../warnings.js';
88
import { hash } from '../../../../utils.js';
99
import { DEV } from 'esm-env';
1010
import { dev_current_component_function } from '../../runtime.js';
11+
import { FILENAME } from '../../../../constants.js';
1112

1213
/**
1314
* @param {Element} element
@@ -23,8 +24,8 @@ function check_hash(element, server_hash, value) {
2324
const loc = element.__svelte_meta?.loc;
2425
if (loc) {
2526
location = `near ${loc.file}:${loc.line}:${loc.column}`;
26-
} else if (dev_current_component_function?.filename) {
27-
location = `in ${dev_current_component_function.filename}`;
27+
} else if (dev_current_component_function?.[FILENAME]) {
28+
location = `in ${dev_current_component_function[FILENAME]}`;
2829
}
2930

3031
w.hydration_html_changed(

packages/svelte/src/internal/client/dom/blocks/svelte-element.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { current_component_context, current_effect } from '../../runtime.js';
2121
import { DEV } from 'esm-env';
2222
import { EFFECT_TRANSPARENT } from '../../constants.js';
2323
import { assign_nodes } from '../template.js';
24+
import { FILENAME } from '../../../../constants.js';
2425

2526
/**
2627
* @param {Comment | Element} node
@@ -38,7 +39,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
3839
hydrate_next();
3940
}
4041

41-
var filename = DEV && location && current_component_context?.function.filename;
42+
var filename = DEV && location && current_component_context?.function[FILENAME];
4243

4344
/** @type {string | null} */
4445
var tag;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { FILENAME, ORIGINAL } from '../../constants.js';
12
export { add_locations } from './dev/elements.js';
23
export { hmr } from './dev/hmr.js';
34
export {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { mutate, set, source } from './reactivity/sources.js';
2929
import { update_derived } from './reactivity/deriveds.js';
3030
import * as e from './errors.js';
3131
import { lifecycle_outside_component } from '../shared/errors.js';
32+
import { FILENAME } from '../../constants.js';
3233

3334
const FLUSH_MICROTASK = 0;
3435
const FLUSH_SYNC = 1;
@@ -226,7 +227,7 @@ function handle_error(error, effect, component_context) {
226227

227228
while (current_context !== null) {
228229
/** @type {string} */
229-
var filename = current_context.function?.filename;
230+
var filename = current_context.function?.[FILENAME];
230231

231232
if (filename) {
232233
const file = filename.split('/').pop();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { untrack } from './runtime.js';
22
import { get_descriptor, is_array } from '../shared/utils.js';
33
import * as e from './errors.js';
4+
import { FILENAME } from '../../constants.js';
45

56
/** regex of all html void element names */
67
const void_element_names =
@@ -70,7 +71,7 @@ export function validate_each_keys(collection, key_fn) {
7071
* @param {Record<string, any>} $$props
7172
* @param {string[]} bindable
7273
* @param {string[]} exports
73-
* @param {Function & { filename: string }} component
74+
* @param {Function & { [FILENAME]: string }} component
7475
*/
7576
export function validate_prop_bindings($$props, bindable, exports, component) {
7677
for (const key in $$props) {
@@ -79,11 +80,11 @@ export function validate_prop_bindings($$props, bindable, exports, component) {
7980

8081
if (setter) {
8182
if (exports.includes(key)) {
82-
e.bind_invalid_export(component.filename, key, name);
83+
e.bind_invalid_export(component[FILENAME], key, name);
8384
}
8485

8586
if (!bindable.includes(key)) {
86-
e.bind_not_bindable(key, component.filename, name);
87+
e.bind_not_bindable(key, component[FILENAME], name);
8788
}
8889
}
8990
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
interactive_elements,
44
is_tag_valid_with_parent
55
} from '../../constants.js';
6+
import { FILENAME } from '../../constants.js';
67
import { current_component } from './context.js';
78

89
/**
@@ -56,7 +57,7 @@ function print_error(payload, parent, child) {
5657
* @param {number} column
5758
*/
5859
export function push_element(payload, tag, line, column) {
59-
var filename = /** @type {import('#server').Component} */ (current_component).function.filename;
60+
var filename = /** @type {import('#server').Component} */ (current_component).function[FILENAME];
6061
var child = { tag, parent, filename, line, column };
6162

6263
if (parent !== null && !is_tag_valid_with_parent(tag, parent.tag)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @import { Component, Payload, RenderOutput } from '#server' */
22
/** @import { Store } from '#shared' */
3+
export { FILENAME, ORIGINAL } from '../../constants.js';
34
import { is_promise, noop } from '../shared/utils.js';
45
import { subscribe_to_store } from '../../store/utils.js';
56
import {

0 commit comments

Comments
 (0)