|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "svelte/require-store-reactive-access" |
| 5 | +description: "disallow to use of the store itself as an operand. Need to use $ prefix or get function." |
| 6 | +--- |
| 7 | + |
| 8 | +# svelte/require-store-reactive-access |
| 9 | + |
| 10 | +> disallow to use of the store itself as an operand. Need to use $ prefix or get function. |
| 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 | +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. |
| 14 | + |
| 15 | +## :book: Rule Details |
| 16 | + |
| 17 | +This rule disallow to use of the store itself as an operand. |
| 18 | +You should access the store value using the `$` prefix or the `get` function. |
| 19 | + |
| 20 | +<ESLintCodeBlock fix> |
| 21 | + |
| 22 | +<!--eslint-skip--> |
| 23 | + |
| 24 | +```svelte |
| 25 | +<script> |
| 26 | + /* eslint svelte/require-store-reactive-access: "error" */ |
| 27 | + import { writable, get } from "svelte/store" |
| 28 | + const storeValue = writable("world") |
| 29 | + const color = writable("red") |
| 30 | +
|
| 31 | + /* ✓ GOOD */ |
| 32 | + $: message = `Hello ${$storeValue}` |
| 33 | +
|
| 34 | + /* ✗ BAD */ |
| 35 | + $: message = `Hello ${storeValue}` |
| 36 | +</script> |
| 37 | +
|
| 38 | +<!-- ✓ GOOD --> |
| 39 | +<p>{$storeValue}</p> |
| 40 | +<p>{get(storeValue)}</p> |
| 41 | +
|
| 42 | +<p class={$storeValue} /> |
| 43 | +<p style:color={$color} /> |
| 44 | +
|
| 45 | +<MyComponent prop="Hello {$storeValue}" /> |
| 46 | +<MyComponent bind:this={$storeValue} /> |
| 47 | +<MyComponent --style-props={$storeValue} /> |
| 48 | +<MyComponent {...$storeValue} /> |
| 49 | +
|
| 50 | +<!-- ✗ BAD --> |
| 51 | +<p>{storeValue}</p> |
| 52 | +
|
| 53 | +<p class={storeValue} /> |
| 54 | +<p style:color /> |
| 55 | +
|
| 56 | +<MyComponent prop="Hello {storeValue}" /> |
| 57 | +<MyComponent bind:this={storeValue} /> |
| 58 | +<MyComponent --style-props={storeValue} /> |
| 59 | +<MyComponent {...storeValue} /> |
| 60 | +``` |
| 61 | + |
| 62 | +</ESLintCodeBlock> |
| 63 | + |
| 64 | +This rule checks the usage of store variables only if the store can be determined within a single file. |
| 65 | +However, when using `@typescript-eslint/parser` and full type information, this rule uses the type information to determine if the expression is a store. |
| 66 | + |
| 67 | +<!--eslint-skip--> |
| 68 | + |
| 69 | +```ts |
| 70 | +// fileName: my-stores.ts |
| 71 | +import { writable } from "svelte/store" |
| 72 | +export const storeValue = writable("hello") |
| 73 | +``` |
| 74 | + |
| 75 | +<!--eslint-skip--> |
| 76 | + |
| 77 | +```svelte |
| 78 | +<script lang="ts"> |
| 79 | + /* eslint svelte/require-store-reactive-access: "error" */ |
| 80 | + import { storeValue } from "./my-stores" |
| 81 | +</script> |
| 82 | +
|
| 83 | +<!-- ✓ GOOD --> |
| 84 | +<p>{$storeValue}</p> |
| 85 | +
|
| 86 | +<!-- ✗ BAD --> |
| 87 | +<p>{storeValue}</p> |
| 88 | +``` |
| 89 | + |
| 90 | +## :wrench: Options |
| 91 | + |
| 92 | +Nothing. |
| 93 | + |
| 94 | +## :mag: Implementation |
| 95 | + |
| 96 | +- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/require-store-reactive-access.ts) |
| 97 | +- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/require-store-reactive-access.ts) |
0 commit comments