-
-
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-ignored-notifier): Add rule.
- Loading branch information
Showing
5 changed files
with
176 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,68 @@ | ||
/** | ||
* @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 * 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 observables not composed from the notifier.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-no-ignored-notifier", | ||
type: "functionality", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Ignoring the notifier is forbidden"; | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
|
||
const failures: Lint.RuleFailure[] = []; | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
const identifiers = tsquery( | ||
sourceFile, | ||
`CallExpression Identifier[name=/(repeatWhen|retryWhen)/]` | ||
); | ||
identifiers.forEach(identifier => { | ||
const callExpression = identifier.parent as ts.CallExpression; | ||
if (callExpression.arguments.length > 0) { | ||
const type = typeChecker.getTypeAtLocation(callExpression); | ||
if (couldBeType(type, "MonoTypeOperatorFunction")) { | ||
const [arg] = callExpression.arguments; | ||
if (tsutils.isArrowFunction(arg) || tsutils.isFunctionExpression(arg)) { | ||
let fail = false; | ||
const [parameter] = arg.parameters; | ||
if (parameter) { | ||
fail = tsquery( | ||
arg.body, | ||
`Identifier[name=${parameter.name.getText()}]` | ||
).length === 0; | ||
} else { | ||
fail = true; | ||
} | ||
if (fail) { | ||
failures.push(new Lint.RuleFailure( | ||
sourceFile, | ||
identifier.getStart(), | ||
identifier.getStart() + identifier.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
return failures; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
test/v6/fixtures/no-ignored-notifier/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,86 @@ | ||
import { of, range } from "rxjs"; | ||
import { repeatWhen, retryWhen } from "rxjs/operators"; | ||
|
||
const source = of(42); | ||
|
||
const a = source.pipe( | ||
repeatWhen(notifications => notifications) | ||
); | ||
|
||
const b = source.pipe( | ||
repeatWhen( | ||
function (notifications) { | ||
return notifications; | ||
} | ||
) | ||
); | ||
|
||
const c = source.pipe( | ||
repeatWhen(notifications => range(0, 3)) | ||
~~~~~~~~~~ [no-ignored-notifier] | ||
); | ||
|
||
const d = source.pipe( | ||
repeatWhen(() => range(0, 3)) | ||
~~~~~~~~~~ [no-ignored-notifier] | ||
); | ||
|
||
const e = source.pipe( | ||
repeatWhen( | ||
~~~~~~~~~~ [no-ignored-notifier] | ||
function (notifications) { | ||
return range(0, 3); | ||
} | ||
) | ||
); | ||
|
||
const f = source.pipe( | ||
repeatWhen( | ||
~~~~~~~~~~ [no-ignored-notifier] | ||
function () { | ||
return range(0, 3); | ||
} | ||
) | ||
); | ||
|
||
const g = source.pipe( | ||
retryWhen(errors => errors) | ||
); | ||
|
||
const h = source.pipe( | ||
retryWhen( | ||
function (errors) { | ||
return errors; | ||
} | ||
) | ||
); | ||
|
||
const i = source.pipe( | ||
retryWhen(errors => range(0, 3)) | ||
~~~~~~~~~ [no-ignored-notifier] | ||
); | ||
|
||
const j = source.pipe( | ||
retryWhen(() => range(0, 3)) | ||
~~~~~~~~~ [no-ignored-notifier] | ||
); | ||
|
||
const k = source.pipe( | ||
retryWhen( | ||
~~~~~~~~~ [no-ignored-notifier] | ||
function (errors) { | ||
return range(0, 3); | ||
} | ||
) | ||
); | ||
|
||
const l = source.pipe( | ||
retryWhen( | ||
~~~~~~~~~ [no-ignored-notifier] | ||
function () { | ||
return range(0, 3); | ||
} | ||
) | ||
); | ||
|
||
[no-ignored-notifier]: Ignoring the notifier is forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-ignored-notifier/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-notifier": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |