# svelte/prefer-svelte-reactivity

disallow using mutable instances of built-in classes where a reactive alternative is provided by svelte/reactivity

  • βš™οΈ This rule is included in "plugin:svelte/recommended".

# πŸ“– Rule Details

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 mutable instances of the built-in versions in Svelte code.

This rule only runs in runes mode (and in .svelte.js / .svelte.ts files). In legacy (non-runes) components, Svelte does not track the svelte/reactivity classes, so switching to them does not help there.

Note that runes mode is detected from parserOptions.svelteConfig, <svelte:options runes />, or the use of runes such as $state or $props. If you enable runes with compilerOptions.runes in svelte.config.js, pass that config as parserOptions.svelteConfig (see the User Guide). Otherwise, components that use no runes are treated as legacy components and are not checked.

Also, in legacy components, this rule does not report instances that are passed to runes code (for example, as a prop to a runes component). Those instances could still benefit from the reactive classes.

This rule does not report an instance that is created inside a function and never leaves it, because nothing can observe it after the function returns. An instance leaves the function when it is returned, stored (e.g. assigned to another variable or put in an object or array), passed to a function, or used by a nested function or a rune such as $derived(...). A few built-in functions that only copy the value do not count when the instance is their first argument: Array.from(), JSON.stringify(), String(), Number(), Boolean() and new Date() / new Map() / new Set() / new URL() / new URLSearchParams() (with a JSON.stringify() replacer function or an Array.from() thisArg, they count). Replacing instance methods or hooks (such as toJSON or toString), accessing unknown members, or writing properties other than native URL setters also prevents this exemption. If the rule cannot tell whether the instance leaves the function, it reports it.

<script>
  /* eslint svelte/prefer-svelte-reactivity: "error" */
  const { page } = $props();

  /* βœ“ GOOD */
  function getNextPage() {
    const url = new URL(page.url);
    url.pathname = '/next';
    return url.toString();
  }

  /* βœ— BAD */
  function createSet() {
    const set = new Set();
    set.add(1);
    return set;
  }

  const groups = $state([]);
  function addGroup() {
    const set = new Set();
    set.add(1);
    groups.push(set);
  }
</script>
<script>
  /* eslint svelte/prefer-svelte-reactivity: "error" */

  import {
    SvelteDate,
    SvelteMap,
    SvelteSet,
    SvelteURL,
    SvelteURLSearchParams
  } from 'svelte/reactivity';

  /* βœ“ GOOD */

  const a = new Date(8.64e15);
  const b = new Map([
    [1, 'one'],
    [2, 'two']
  ]);
  const c = new Set([1, 2, 1, 3, 3]);
  const d = new URL('https://svelte.dev/');
  const e = new URLSearchParams('foo=1&bar=2');

  // These don't modify the instances
  a.getTime();
  b.keys();
  c.has(1);
  d.port;
  e.entries();

  const f = new SvelteDate(8.64e15);
  const g = new SvelteMap([
    [1, 'one'],
    [2, 'two']
  ]);
  const h = new SvelteSet([1, 2, 1, 3, 3]);
  const i = new SvelteURL('https://svelte.dev/');
  const j = new SvelteURLSearchParams('foo=1&bar=2');

  // These modify the instances
  f.getTime();
  g.keys();
  h.has(1);
  i.port;
  j.entries();

  /* βœ— BAD */

  const k = new Date(8.64e15);
  const l = new Map([
    [1, 'one'],
    [2, 'two']
  ]);
  const m = new Set([1, 2, 1, 3, 3]);
  const n = new URL('https://svelte.dev/');
  const o = new URLSearchParams('foo=1&bar=2');

  // These modify the instances
  k.setMonth(3);
  l.clear();
  m.delete(2);
  n.port = 80;
  o.sort();
</script>
// In svelte.js files, exported variables are also reported
/* eslint svelte/prefer-svelte-reactivity: "error" */

/* βœ— BAD */

const a = new Date(8.64e15);
const b = new Map([
  [1, 'one'],
  [2, 'two']
]);
const c = new Set([1, 2, 1, 3, 3]);
const d = new URL('https://svelte.dev/');
const e = new URLSearchParams('foo=1&bar=2');

export { a, b, c, d as dd };
export default e;

# πŸ”§ Options

Nothing.

# πŸ“š Further Reading

# πŸš€ Version

This rule was introduced in eslint-plugin-svelte v3.11.0

# πŸ” Implementation