-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding a new no-ignored-unsubscribe rule. (#592)
Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
Showing
12 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"eslint-plugin-svelte": minor | ||
--- | ||
|
||
feat: add new `svelte/no-ignored-unsubscribe` rule. |
This file contains 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 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 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,48 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/no-ignored-unsubscribe' | ||
description: 'disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores.' | ||
--- | ||
|
||
# svelte/no-ignored-unsubscribe | ||
|
||
> disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores. | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule fails if an "unsubscriber" returned by call to `subscribe()` is neither assigned to a variable or property or passed to a function. | ||
|
||
One should always unsubscribe from a store when it is no longer needed. Otherwise, the subscription will remain active and constitute a **memory leak**. | ||
This rule helps to find such cases by ensuring that the unsubscriber (the return value from the store's `subscribe` method) is not ignored. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-ignored-unsubscribe: "error" */ | ||
import myStore from './my-stores'; | ||
/* ✓ GOOD */ | ||
const unsubscribe = myStore.subscribe(() => {}); | ||
/* ✗ BAD */ | ||
myStore.subscribe(() => {}); | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-ignored-unsubscribe.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-ignored-unsubscribe.ts) |
This file contains 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,32 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
import { createRule } from '../utils'; | ||
|
||
export default createRule('no-ignored-unsubscribe', { | ||
meta: { | ||
docs: { | ||
description: | ||
'disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores.', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
fixable: undefined, | ||
hasSuggestions: false, | ||
messages: { | ||
forbidden: 'Ignoring returned value of the subscribe method is forbidden.' | ||
}, | ||
schema: [], | ||
type: 'problem' | ||
}, | ||
create: (context) => { | ||
return { | ||
"ExpressionStatement > CallExpression > MemberExpression.callee[property.name='subscribe']": ( | ||
node: TSESTree.MemberExpression | ||
) => { | ||
context.report({ | ||
messageId: 'forbidden', | ||
node: node.property | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
This file contains 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
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/no-ignored-unsubscribe/invalid/test01-errors.yaml
This file contains 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,4 @@ | ||
- message: Ignoring returned value of the subscribe method is forbidden. | ||
line: 6 | ||
column: 6 | ||
suggestions: null |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/no-ignored-unsubscribe/invalid/test01-input.svelte
This file contains 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,9 @@ | ||
<script> | ||
import { writable } from 'svelte/store'; | ||
const foo = writable(0); | ||
foo.subscribe(() => { | ||
console.log('foo changed'); | ||
}); | ||
</script> |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/rules/no-ignored-unsubscribe/valid/test01-input.svelte
This file contains 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,9 @@ | ||
<script> | ||
import { writable } from 'svelte/store'; | ||
const foo = writable(0); | ||
const unsubscribe = foo.subscribe(() => { | ||
console.log('foo changed'); | ||
}); | ||
</script> |
12 changes: 12 additions & 0 deletions
12
tests/fixtures/rules/no-ignored-unsubscribe/valid/test02-input.svelte
This file contains 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 @@ | ||
<script> | ||
import { writable } from 'svelte/store'; | ||
const foo = writable(0); | ||
const unsubscribers = []; | ||
unsubscribers.push( | ||
foo.subscribe(() => { | ||
console.log('foo changed'); | ||
}) | ||
); | ||
</script> |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/rules/no-ignored-unsubscribe/valid/test03-input.svelte
This file contains 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,3 @@ | ||
<script> | ||
a(foo.subscribe); | ||
</script> |
This file contains 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-ignored-unsubscribe'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-ignored-unsubscribe', rule as any, loadTestCases('no-ignored-unsubscribe')); |