Skip to content

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

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 1 commit into from
Mar 22, 2024
Merged
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
33 changes: 18 additions & 15 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down