-
-
Notifications
You must be signed in to change notification settings - Fork 50
feat: implement stores-no-async
#225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ota-meshi
merged 9 commits into
sveltejs:main
from
baseballyama:feature/stores-no-async
Aug 23, 2022
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
104f8b2
feat: implement stores-no-async rule
baseballyama 7dcaa23
test: add tests
baseballyama 796551a
docs: add docs
baseballyama 9904b9f
chore: add changeset
baseballyama c05ef7c
fix: remove since
baseballyama 715d04a
fix: changeset
baseballyama 37d1de7
fix: use ReferenceTracker
baseballyama 99e0996
fix: handled if fn is undefined
baseballyama 0b8b4ce
fix: handle FunctionExpression also
baseballyama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/no-store-async" | ||
description: "disallow using async/await inside svelte stores" | ||
since: "v3.1.0" | ||
--- | ||
|
||
# svelte/no-store-async | ||
|
||
> disallow using async/await inside svelte stores | ||
|
||
- :gear: This rule is included in `"plugin:svelte/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports all uses of async/await inside svelte stores. | ||
Because it causes issues with the auto-unsubscribing features. | ||
|
||
<ESLintCodeBlock language="javascript"> | ||
|
||
<!--eslint-skip--> | ||
|
||
```js | ||
/* eslint svelte/no-store-async: "error" */ | ||
|
||
import { writable, readable, derived } from "svelte/store" | ||
|
||
/* ✓ GOOD */ | ||
const w1 = writable(false, () => {}) | ||
const r1 = readable(false, () => {}) | ||
const d1 = derived(a1, ($a1) => {}) | ||
|
||
/* ✗ BAD */ | ||
const w2 = writable(false, async () => {}) | ||
const r2 = readable(false, async () => {}) | ||
const d2 = derived(a1, async ($a1) => {}) | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [Svelte - Docs > 4. Prefix stores with $ to access their values / Store contract](https://svelte.dev/docs#component-format-script-4-prefix-stores-with-$-to-access-their-values-store-contract) | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in eslint-plugin-svelte v3.1.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-store-async.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-store-async.ts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,44 @@ | ||||||
import { createRule } from "../utils" | ||||||
import type * as ESTree from "estree" | ||||||
|
||||||
export default createRule("no-store-async", { | ||||||
meta: { | ||||||
docs: { | ||||||
description: | ||||||
"disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features", | ||||||
category: "Possible Errors", | ||||||
recommended: true, | ||||||
default: "error", | ||||||
}, | ||||||
schema: [], | ||||||
messages: { | ||||||
unexpected: "Do not pass async functions to svelte stores.", | ||||||
}, | ||||||
type: "problem", | ||||||
}, | ||||||
create(context) { | ||||||
return { | ||||||
CallExpression(node: ESTree.CallExpression) { | ||||||
if (node.callee.type !== "Identifier") return | ||||||
const { name } = node.callee | ||||||
if (name !== "writable" && name !== "readable" && name !== "derived") | ||||||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return | ||||||
const [, fn] = node.arguments | ||||||
if (fn.type !== "ArrowFunctionExpression" || !fn.async) return | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should check if the argument exists.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! Sorry for that!
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
const start = fn.loc?.start ?? { line: 1, column: 0 } | ||||||
context.report({ | ||||||
node: fn, | ||||||
loc: { | ||||||
start, | ||||||
end: { | ||||||
line: start.line, | ||||||
column: start.column + 5, | ||||||
}, | ||||||
}, | ||||||
messageId: "unexpected", | ||||||
}) | ||||||
}, | ||||||
} | ||||||
}, | ||||||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/fixtures/rules/no-store-async/invalid/test01-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
- message: Do not pass async functions to svelte stores. | ||
line: 3 | ||
column: 28 | ||
suggestions: null | ||
- message: Do not pass async functions to svelte stores. | ||
line: 6 | ||
column: 28 | ||
suggestions: null | ||
- message: Do not pass async functions to svelte stores. | ||
line: 9 | ||
column: 24 | ||
suggestions: null |
11 changes: 11 additions & 0 deletions
11
tests/fixtures/rules/no-store-async/invalid/test01-input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { writable, readable, derived } from "svelte/store" | ||
|
||
const w2 = writable(false, async () => { | ||
/** do nothing */ | ||
}) | ||
const r2 = readable(false, async () => { | ||
/** do nothing */ | ||
}) | ||
const d2 = derived(a1, async ($a1) => { | ||
/** do nothing */ | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { writable, readable, derived } from "svelte/store" | ||
|
||
const w1 = writable(false, () => { | ||
/** do nothing */ | ||
}) | ||
const r1 = readable(false, () => { | ||
/** do nothing */ | ||
}) | ||
const d1 = derived(a1, ($a1) => { | ||
/** do nothing */ | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/no-store-async" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run("no-store-async", rule as any, loadTestCases("no-store-async")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.