|
| 1 | +--- |
| 2 | +pageClass: 'rule-details' |
| 3 | +sidebarDepth: 0 |
| 4 | +title: 'svelte/prefer-svelte-reactivity' |
| 5 | +description: 'disallow using built-in classes where a reactive alternative is provided by svelte/reactivity' |
| 6 | +--- |
| 7 | + |
| 8 | +# svelte/prefer-svelte-reactivity |
| 9 | + |
| 10 | +> disallow using built-in classes where a reactive alternative is provided by svelte/reactivity |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> |
| 13 | +- :gear: This rule is included in `"plugin:svelte/recommended"`. |
| 14 | + |
| 15 | +## :book: Rule Details |
| 16 | + |
| 17 | +The built-in `Date`, `Map`, `Set`, `URL` and `URLSearchParams` classes are often used in frontend code, however, their properties and methods are not reactive. Because of that, Svelte provides reactive versions of these 5 builtins as part of the "svelte/reactivity" package. This rule reports usage of the built-in versions in Svelte code. |
| 18 | + |
| 19 | +<!--eslint-skip--> |
| 20 | + |
| 21 | +```svelte |
| 22 | +<script> |
| 23 | + /* eslint svelte/prefer-svelte-reactivity: "error" */ |
| 24 | +
|
| 25 | + import { |
| 26 | + SvelteDate, |
| 27 | + SvelteMap, |
| 28 | + SvelteSet, |
| 29 | + SvelteURL, |
| 30 | + SvelteURLSearchParams |
| 31 | + } from 'svelte/reactivity'; |
| 32 | +
|
| 33 | + /* ✓ GOOD */ |
| 34 | +
|
| 35 | + const a = new SvelteDate(8.64e15); |
| 36 | + const b = new SvelteMap([ |
| 37 | + [1, 'one'], |
| 38 | + [2, 'two'] |
| 39 | + ]); |
| 40 | + const c = new SvelteSet([1, 2, 1, 3, 3]); |
| 41 | + const d = new SvelteURL('https://svelte.dev/'); |
| 42 | + const e = new SvelteURLSearchParams('foo=1&bar=2'); |
| 43 | +
|
| 44 | + /* ✗ BAD */ |
| 45 | +
|
| 46 | + const f = new Date(8.64e15); |
| 47 | + const g = new Map([ |
| 48 | + [1, 'one'], |
| 49 | + [2, 'two'] |
| 50 | + ]); |
| 51 | + const h = new Set([1, 2, 1, 3, 3]); |
| 52 | + const i = new URL('https://svelte.dev/'); |
| 53 | + const j = new URLSearchParams('foo=1&bar=2'); |
| 54 | +</script> |
| 55 | +``` |
| 56 | + |
| 57 | +## :wrench: Options |
| 58 | + |
| 59 | +Nothing. |
| 60 | + |
| 61 | +## :books: Further Reading |
| 62 | + |
| 63 | +- [svelte/reactivity documentation](https://svelte.dev/docs/svelte/svelte-reactivity) |
| 64 | + |
| 65 | +## :mag: Implementation |
| 66 | + |
| 67 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/prefer-svelte-reactivity.ts) |
| 68 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/prefer-svelte-reactivity.ts) |
0 commit comments