-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
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
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,51 @@ | ||
/** | ||
* @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 | ||
*/ | ||
/*tslint:disable:no-use-before-declare*/ | ||
|
||
import { tsquery } from "@phenomnomnominal/tsquery"; | ||
import * as Lint from "tslint"; | ||
import * as ts from "typescript"; | ||
import { couldBeType } from "../support/util"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/v6/fixtures/no-ignored-observable/default/fixture.ts.lint
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,23 @@ | ||
import { Observable, of } from "rxjs"; | ||
|
||
function functionSource() { | ||
return of(42); | ||
} | ||
|
||
const arrowSource = () => of(42); | ||
|
||
function sink(source: Observable<number>) { | ||
} | ||
|
||
const a = functionSource(); | ||
const b = arrowSource(); | ||
sink(functionSource()); | ||
sink(arrowSource()); | ||
|
||
functionSource(); | ||
~~~~~~~~~~~~~~~~ [no-ignored-observable] | ||
|
||
arrowSource(); | ||
~~~~~~~~~~~~~ [no-ignored-observable] | ||
|
||
[no-ignored-observable]: Ignoring a returned Observable is forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-ignored-observable/default/tsconfig.json
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"lib": ["es2015"], | ||
"noEmit": true, | ||
"paths": { | ||
"rxjs": ["../../node_modules/rxjs"] | ||
}, | ||
"skipLibCheck": true, | ||
"target": "es5" | ||
}, | ||
"include": ["fixture.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,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"rxjs-no-ignored-observable": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |