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

...rather than only when the function is invoked
pull/10869/head
Simon H 5 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) { if (DEV) {
/** /**
* @param {string} rune * @param {string} rune
* @param {string[]} [variants]
*/ */
function throw_rune_error(rune, variants = []) { function throw_rune_error(rune) {
if (!(rune in globalThis)) { if (!(rune in globalThis)) {
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message // TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
// @ts-ignore /** @type {any} */
globalThis[rune] = () => { let value; // let's hope noone modifies this global, but belts and braces
throw new Error(`${rune}() is only available inside .svelte and .svelte.js/ts files`); Object.defineProperty(globalThis, rune, {
}; configurable: true,
for (const variant of variants) { get: () => {
// @ts-ignore if (value !== undefined) {
globalThis[rune][variant] = () => { return value;
}
throw new Error( 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('$state');
throw_rune_error('$effect', ['pre', 'root', 'active']); throw_rune_error('$effect');
throw_rune_error('$derived', ['by']); throw_rune_error('$derived');
throw_rune_error('$inspect'); throw_rune_error('$inspect');
throw_rune_error('$props'); throw_rune_error('$props');
throw_rune_error('$bindable'); throw_rune_error('$bindable');

Loading…
Cancel
Save