chore: error on accessing global that is a rune (#10877)

...rather than only when the function is invoked
pull/10869/head
Simon H 9 months ago committed by GitHub
parent b468978e4d
commit 852eca4ee6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1260,29 +1260,32 @@ export function unwrap(value) {
if (DEV) {
/**
* @param {string} rune
* @param {string[]} [variants]
*/
function throw_rune_error(rune, variants = []) {
function throw_rune_error(rune) {
if (!(rune in globalThis)) {
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
// @ts-ignore
globalThis[rune] = () => {
throw new Error(`${rune}() is only available inside .svelte and .svelte.js/ts files`);
};
for (const variant of variants) {
// @ts-ignore
globalThis[rune][variant] = () => {
/** @type {any} */
let value; // let's hope noone modifies this global, but belts and braces
Object.defineProperty(globalThis, rune, {
configurable: true,
get: () => {
if (value !== undefined) {
return value;
}
throw new Error(
`${rune}.${variant}() is only available inside .svelte and .svelte.js/ts files`
`The ${rune} rune is only available inside .svelte and .svelte.js/ts files`
);
};
}
},
set: (v) => {
value = v;
}
});
}
}
throw_rune_error('$state', ['frozen']);
throw_rune_error('$effect', ['pre', 'root', 'active']);
throw_rune_error('$derived', ['by']);
throw_rune_error('$state');
throw_rune_error('$effect');
throw_rune_error('$derived');
throw_rune_error('$inspect');
throw_rune_error('$props');
throw_rune_error('$bindable');

Loading…
Cancel
Save