What is eslint-plugin-rxjs?
eslint-plugin-rxjs is an ESLint plugin that provides linting rules for RxJS to help developers write more consistent and error-free reactive code. It enforces best practices and helps catch common mistakes when working with RxJS.
What are eslint-plugin-rxjs's main functionalities?
Enforce Creation Operators
This rule disallows the creation of new Observables using the constructor. Instead, it encourages the use of creation operators like `of`, `from`, etc.
/* eslint rxjs/no-create: 'error' */
import { Observable } from 'rxjs';
const observable = new Observable(observer => {
observer.next('Hello World');
observer.complete();
});
No Ignored Subscription
This rule ensures that subscriptions are not ignored. It helps in avoiding memory leaks by making sure that all subscriptions are properly handled.
/* eslint rxjs/no-ignored-subscription: 'error' */
import { of } from 'rxjs';
of('Hello World').subscribe();
No Subject Unsubscribe
This rule disallows calling `unsubscribe` on Subjects, as it can lead to unexpected behavior. Instead, it encourages the use of `complete`.
/* eslint rxjs/no-subject-unsubscribe: 'error' */
import { Subject } from 'rxjs';
const subject = new Subject();
subject.unsubscribe();
Other packages similar to eslint-plugin-rxjs
eslint-plugin-rxjs-angular
eslint-plugin-rxjs-angular is an ESLint plugin specifically designed for Angular projects using RxJS. It provides additional rules that are tailored for Angular's use of RxJS, such as enforcing the use of Angular's `async` pipe and ensuring proper use of Angular's `HttpClient`.
eslint-plugin-rxjs
This package contains a bunch of ESLint rules for RxJS. Essentially, it's a re-implementation of the rules that are in the rxjs-tslint-rules
package. (The Angular-specific rules in rxjs-tslint-rules
have been re-implemented in eslint-plugin-rxjs-angular
.)
Some of the rules are rather opinionated and are not included in the recommended
configuration. Developers can decide for themselves whether they want to enable opinionated rules.
Almost all of these rules require the TypeScript parser for ESLint.
Install
Install the ESLint TypeScript parser using npm:
npm install @typescript-eslint/parser --save-dev
Install the package using npm:
npm install eslint-plugin-rxjs --save-dev
Configure the parser
and the parserOptions
for ESLint. Here, I use a .eslintrc.js
file for the configuration:
const { join } = require("path");
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2019,
project: join(__dirname, "./tsconfig.json"),
sourceType: "module"
},
plugins: ["rxjs"],
extends: [],
rules: {
"rxjs/no-async-subscribe": "error",
"rxjs/no-ignored-observable": "error",
"rxjs/no-ignored-subscription": "error",
"rxjs/no-nested-subscribe": "error",
"rxjs/no-unbound-methods": "error",
"rxjs/throw-error": "error"
}
};
Or, using the recommended
configuration:
const { join } = require("path");
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2019,
project: join(__dirname, "./tsconfig.json"),
sourceType: "module"
},
extends: ["plugin:rxjs/recommended"],
};
Rules
The package includes the following rules.
Rules marked with ✅ are recommended and rules marked with 🔧 have fixers.