feat: Adding a new no-ignored-unsubscribe rule. #592
Merged
+138
−0
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.
Hey @ota-meshi
First of all, thanks for this package, it's really a life saver!
I would like here to propose a new rule for the eslint Svelte plugin:
no-ignored-unsubscribe
.This rule fails if an "unsubscriber" returned by call to
subscribe()
is neither assigned to a variable or property nor 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.I'm actually writing this rule after being bite by such an issue => https://github.com/workadventure/workadventure/pull/3527/files
About the code:
The rule is heavily inspired by a similar rule in eslint-plugin-rxjs: https://github.com/cartant/eslint-plugin-rxjs/blob/main/docs/rules/no-ignored-subscription.md
I did simplify the code a bit. As it is now, the rule will generate a few false positives (it is triggered on all method named
subscribe
, whether the underlying object is a store or not). In practice, I think this is acceptable.There is alas no simple way to check the type of the underlying object. The RxJS rule relies on analyzing the code to try to find the type of the object. I tested a similar approach. It has a number of false negatives, and the code is really more complex as it relies on doing some type testing with typescript-eslint (as described here).
Let me know what you think!