chore: add invariant utility (#17929)

With this, we can add invariants to the codebase so we can identify
problems like 'this batch already has roots scheduled', which indicate a
bug somewhere, without a) needing tests for scenarios that are
inherently hard to anticipate, or b) cluttering people's prod bundles
pull/17931/head
Rich Harris 1 month ago committed by GitHub
parent 043a7a26cd
commit a513da0445
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: add `invariant` helper for debugging

@ -42,6 +42,12 @@ Here, `List.svelte` is using `{@render children(item)` which means it expects `P
A snippet function was passed invalid arguments. Snippets should only be instantiated via `{@render ...}`
```
### invariant_violation
```
An invariant violation occurred, meaning Svelte's internal assumptions were flawed. This is a bug in Svelte, not your app — please open an issue at https://github.com/sveltejs/svelte, citing the following message: "%message%"
```
### lifecycle_outside_component
```

@ -34,6 +34,10 @@ Here, `List.svelte` is using `{@render children(item)` which means it expects `P
> A snippet function was passed invalid arguments. Snippets should only be instantiated via `{@render ...}`
## invariant_violation
> An invariant violation occurred, meaning Svelte's internal assumptions were flawed. This is a bug in Svelte, not your app — please open an issue at https://github.com/sveltejs/svelte, citing the following message: "%message%"
## lifecycle_outside_component
> `%name%(...)` can only be used during component initialisation

@ -1,4 +1,6 @@
import { DEV } from 'esm-env';
import { define_property } from './utils.js';
import * as e from './errors.js';
/**
* @param {string} label
@ -63,3 +65,15 @@ export function get_stack() {
return new_lines;
}
/**
* @param {boolean} condition
* @param {string} message
*/
export function invariant(condition, message) {
if (!DEV) {
throw new Error('invariant(...) was not guarded by if (DEV)');
}
if (!condition) e.invariant_violation(message);
}

@ -51,6 +51,23 @@ export function invalid_snippet_arguments() {
}
}
/**
* An invariant violation occurred, meaning Svelte's internal assumptions were flawed. This is a bug in Svelte, not your app please open an issue at https://github.com/sveltejs/svelte, citing the following message: "%message%"
* @param {string} message
* @returns {never}
*/
export function invariant_violation(message) {
if (DEV) {
const error = new Error(`invariant_violation\nAn invariant violation occurred, meaning Svelte's internal assumptions were flawed. This is a bug in Svelte, not your app — please open an issue at https://github.com/sveltejs/svelte, citing the following message: "${message}"\nhttps://svelte.dev/e/invariant_violation`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/invariant_violation`);
}
}
/**
* `%name%(...)` can only be used during component initialisation
* @param {string} name

Loading…
Cancel
Save