Skip to content

Latest commit

 

History

History
75 lines (55 loc) · 2.52 KB

prefer-const.md

File metadata and controls

75 lines (55 loc) · 2.52 KB
pageClass sidebarDepth title description since
rule-details
0
svelte/prefer-const
Require `const` declarations for variables that are never reassigned after declared
v3.0.0-next.6

svelte/prefer-const

Require const declarations for variables that are never reassigned after declared

  • 🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

📖 Rule Details

This rule reports the same as the base ESLint prefer-const rule, except that ignores Svelte reactive values such as $derived and $props as default. If this rule is active, make sure to disable the base prefer-const rule, as it will conflict with this rule.

<script>
  /* eslint svelte/prefer-const: "error" */

  // ✓ GOOD
  const { a, b } = $props();
  let c = $state('');
  let d = $derived(a * 2);
  let e = $derived.by(() => b * 2);

  // ✗ BAD
  let obj = { a, b };
  let g = $state(0);
  let h = $state({ count: 1 });
</script>

<input bind:value={c} />
<input bind:value={h.count} />

🔧 Options

{
  "svelte/prefer-const": [
    "error",
    {
      "destructuring": "any",
      "ignoreReadonly": true,
      "excludedRunes": ["$props", "$derived"]
    }
  ]
}
  • destructuring: The kind of the way to address variables in destructuring. There are 2 values:
    • any (default): if any variables in destructuring should be const, this rule warns for those variables.
    • all: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them.
  • ignoreReadonly: If true, this rule will ignore variables that are read between the declaration and the first assignment.
  • excludedRunes: An array of rune names that should be ignored. Even if a rune is declared with let, it will still be ignored.

📚 Further Reading

🚀 Version

This rule was introduced in eslint-plugin-svelte v3.0.0-next.6

🔍 Implementation

Taken with ❤️ from ESLint core