-
-
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.
feat(no-nested-subscribe): Add rule.
- Loading branch information
Showing
5 changed files
with
130 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,63 @@ | ||
/** | ||
* @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 * as Lint from "tslint"; | ||
import * as ts from "typescript"; | ||
import * as tsutils from "tsutils"; | ||
import { tsquery } from "@phenomnomnominal/tsquery"; | ||
import { couldBeType } from "../support/util"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
|
||
public static metadata: Lint.IRuleMetadata = { | ||
description: "Disallows the calling of `subscribe` within a `subscribe` callback.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-no-nested-subscribe", | ||
type: "style", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Nested subscribe calls are forbidden"; | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
|
||
const failures: Lint.RuleFailure[] = []; | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
const subscribeQuery = `CallExpression PropertyAccessExpression[name.name="subscribe"]`; | ||
const propertyAccessExpressions = tsquery(sourceFile, subscribeQuery); | ||
propertyAccessExpressions.forEach(propertyAccessExpression => { | ||
const { parent: callExpression } = propertyAccessExpression; | ||
if (tsutils.isCallExpression(callExpression)) { | ||
if (tsutils.isPropertyAccessExpression(propertyAccessExpression)) { | ||
const type = typeChecker.getTypeAtLocation(propertyAccessExpression.expression); | ||
if (couldBeType(type, "Observable")) { | ||
callExpression.arguments.forEach(arg => { | ||
const propertyAccessExpressions = tsquery(arg, subscribeQuery); | ||
propertyAccessExpressions.forEach(propertyAccessExpression => { | ||
if (tsutils.isPropertyAccessExpression(propertyAccessExpression)) { | ||
const type = typeChecker.getTypeAtLocation(propertyAccessExpression.expression); | ||
if (couldBeType(type, "Observable")) { | ||
const { name } = propertyAccessExpression; | ||
failures.push(new Lint.RuleFailure( | ||
sourceFile, | ||
name.getStart(), | ||
name.getStart() + name.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
)); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
return failures; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
test/v6/fixtures/no-nested-subscribe/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,45 @@ | ||
import { of } from "rxjs"; | ||
|
||
of("foo").subscribe( | ||
value => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
); | ||
of("foo").subscribe({ | ||
next: value => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
of("foo").subscribe({ | ||
next(value) { of("bar").subscribe(); } | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
|
||
of("foo").subscribe( | ||
undefined, | ||
error => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
); | ||
of("foo").subscribe({ | ||
error: error => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
of("foo").subscribe({ | ||
error(error) { of("bar").subscribe(); } | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
|
||
of("foo").subscribe( | ||
undefined, | ||
undefined, | ||
() => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
); | ||
of("foo").subscribe({ | ||
complete: () => of("bar").subscribe() | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
of("foo").subscribe({ | ||
complete() { of("bar").subscribe(); } | ||
~~~~~~~~~ [no-nested-subscribe] | ||
}); | ||
|
||
[no-nested-subscribe]: Nested subscribe calls are forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-nested-subscribe/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-nested-subscribe": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |