Skip to content

Commit 852eca4

Browse files
authored
chore: error on accessing global that is a rune (#10877)
...rather than only when the function is invoked
1 parent b468978 commit 852eca4

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,29 +1260,32 @@ export function unwrap(value) {
12601260
if (DEV) {
12611261
/**
12621262
* @param {string} rune
1263-
* @param {string[]} [variants]
12641263
*/
1265-
function throw_rune_error(rune, variants = []) {
1264+
function throw_rune_error(rune) {
12661265
if (!(rune in globalThis)) {
12671266
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
1268-
// @ts-ignore
1269-
globalThis[rune] = () => {
1270-
throw new Error(`${rune}() is only available inside .svelte and .svelte.js/ts files`);
1271-
};
1272-
for (const variant of variants) {
1273-
// @ts-ignore
1274-
globalThis[rune][variant] = () => {
1267+
/** @type {any} */
1268+
let value; // let's hope noone modifies this global, but belts and braces
1269+
Object.defineProperty(globalThis, rune, {
1270+
configurable: true,
1271+
get: () => {
1272+
if (value !== undefined) {
1273+
return value;
1274+
}
12751275
throw new Error(
1276-
`${rune}.${variant}() is only available inside .svelte and .svelte.js/ts files`
1276+
`The ${rune} rune is only available inside .svelte and .svelte.js/ts files`
12771277
);
1278-
};
1279-
}
1278+
},
1279+
set: (v) => {
1280+
value = v;
1281+
}
1282+
});
12801283
}
12811284
}
12821285

1283-
throw_rune_error('$state', ['frozen']);
1284-
throw_rune_error('$effect', ['pre', 'root', 'active']);
1285-
throw_rune_error('$derived', ['by']);
1286+
throw_rune_error('$state');
1287+
throw_rune_error('$effect');
1288+
throw_rune_error('$derived');
12861289
throw_rune_error('$inspect');
12871290
throw_rune_error('$props');
12881291
throw_rune_error('$bindable');

0 commit comments

Comments
 (0)