-
-
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
167 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,55 @@ | ||
/** | ||
* @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 tsutils from "tsutils"; | ||
import * as ts from "typescript"; | ||
import { couldBeType } from "../support/util"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: | ||
"Disallows redundant notifications from completed or errored observables.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-no-redundant-notify", | ||
type: "functionality", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Redundant notifications are forbidden"; | ||
|
||
public applyWithProgram( | ||
sourceFile: ts.SourceFile, | ||
program: ts.Program | ||
): Lint.RuleFailure[] { | ||
const failures: Lint.RuleFailure[] = []; | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
const query = `ExpressionStatement[expression.expression.name.text=/(complete|error)/] ~ ExpressionStatement[expression.expression.name.text=/(next|complete|error)/]`; | ||
const expressionStatements = tsquery(sourceFile, query); | ||
expressionStatements.forEach(node => { | ||
const expressionStatement = node as ts.ExpressionStatement; | ||
if (ts.isCallExpression(expressionStatement.expression)) { | ||
const callExpression = expressionStatement.expression; | ||
if (ts.isPropertyAccessExpression(callExpression.expression)) { | ||
const { name } = callExpression.expression; | ||
failures.push( | ||
new Lint.RuleFailure( | ||
sourceFile, | ||
name.getStart(), | ||
name.getStart() + name.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
return failures; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
test/v6/fixtures/no-redundant-notify/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,90 @@ | ||
import { Observable, Subject } from "rxjs"; | ||
|
||
const examples = [ | ||
new Observable<number>(observer => { | ||
observer.next(42); | ||
observer.complete(); | ||
}), | ||
new Observable<number>(observer => { | ||
observer.next(42); | ||
observer.error(new Error("Kaboom!")); | ||
}), | ||
new Observable<number>(observer => { | ||
observer.complete(); | ||
observer.next(42); | ||
~~~~ [no-redundant-notify] | ||
}), | ||
new Observable<number>(observer => { | ||
observer.complete(); | ||
observer.complete(); | ||
~~~~~~~~ [no-redundant-notify] | ||
}), | ||
new Observable<number>(observer => { | ||
observer.complete(); | ||
observer.error(new Error("Kaboom!")); | ||
~~~~~ [no-redundant-notify] | ||
}), | ||
new Observable<number>(observer => { | ||
observer.error(new Error("Kaboom!")); | ||
observer.next(42); | ||
~~~~ [no-redundant-notify] | ||
}), | ||
new Observable<number>(observer => { | ||
observer.error(new Error("Kaboom!")); | ||
observer.complete(); | ||
~~~~~~~~ [no-redundant-notify] | ||
}), | ||
new Observable<number>(observer => { | ||
observer.error(new Error("Kaboom!")); | ||
observer.error(new Error("Kaboom!")); | ||
~~~~~ [no-redundant-notify] | ||
} | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.next(42); | ||
subject.complete(); | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.next(42); | ||
subject.error(new Error("Kaboom!")); | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.complete(); | ||
subject.next(42); | ||
~~~~ [no-redundant-notify] | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.complete(); | ||
subject.complete(); | ||
~~~~~~~~ [no-redundant-notify] | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.complete(); | ||
subject.error(new Error("Kaboom!")); | ||
~~~~~ [no-redundant-notify] | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.error(new Error("Kaboom!")); | ||
subject.next(42); | ||
~~~~ [no-redundant-notify] | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.error(new Error("Kaboom!")); | ||
subject.complete(); | ||
~~~~~~~~ [no-redundant-notify] | ||
}, | ||
() => { | ||
const subject = new Subject<number>(); | ||
subject.error(new Error("Kaboom!")); | ||
subject.error(new Error("Kaboom!")); | ||
~~~~~ [no-redundant-notify] | ||
} | ||
]; | ||
|
||
[no-redundant-notify]: Redundant notifications are forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-redundant-notify/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-redundant-notify": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |