Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/flat-lemons-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: add `invariant` helper for debugging
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/shared-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/shared-errors/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/src/internal/shared/dev.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { DEV } from 'esm-env';
import { define_property } from './utils.js';
import * as e from './errors.js';

/**
* @param {string} label
Expand Down Expand Up @@ -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);
}
17 changes: 17 additions & 0 deletions packages/svelte/src/internal/shared/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading