-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
rxjsNoIgnoredObservableRule.ts
53 lines (48 loc) · 1.65 KB
/
rxjsNoIgnoredObservableRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-tslint-rules
*/
import { tsquery } from "@phenomnomnominal/tsquery";
import * as Lint from "tslint";
import * as ts from "typescript";
import * as peer from "../support/peer";
import { couldBeType } from "../support/util";
export class Rule extends Lint.Rules.TypedRule {
public static metadata: Lint.IRuleMetadata = {
deprecationMessage: peer.v5 ? peer.v5NotSupportedMessage : undefined,
description: "Disallows the ignoring of observables returned by functions.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: true,
ruleName: "rxjs-no-ignored-observable",
type: "functionality",
typescriptOnly: true,
};
public static FAILURE_STRING = "Ignoring a returned Observable is forbidden";
public applyWithProgram(
sourceFile: ts.SourceFile,
program: ts.Program
): Lint.RuleFailure[] {
const failures: Lint.RuleFailure[] = [];
const typeChecker = program.getTypeChecker();
const callExpressions = tsquery(
sourceFile,
`ExpressionStatement > CallExpression`
);
callExpressions.forEach((callExpression) => {
const type = typeChecker.getTypeAtLocation(callExpression);
if (couldBeType(type, "Observable")) {
failures.push(
new Lint.RuleFailure(
sourceFile,
callExpression.getStart(),
callExpression.getStart() + callExpression.getWidth(),
Rule.FAILURE_STRING,
this.ruleName
)
);
}
});
return failures;
}
}