-
-
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: Add no-ignore-takewhile-value rule.
- Loading branch information
Showing
6 changed files
with
135 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,66 @@ | ||
/** | ||
* @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"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
deprecationMessage: peer.v5 ? peer.v5NotSupportedMessage : undefined, | ||
description: "Disallows the ignoring of the `takeWhile` value.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-no-ignored-takewhile-value", | ||
type: "functionality", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Ignoring the takeWhile value is forbidden"; | ||
|
||
public applyWithProgram( | ||
sourceFile: ts.SourceFile, | ||
program: ts.Program | ||
): Lint.RuleFailure[] { | ||
const failures: Lint.RuleFailure[] = []; | ||
|
||
const callExpressions = tsquery( | ||
sourceFile, | ||
`CallExpression[expression.text="takeWhile"]` | ||
) as ts.CallExpression[]; | ||
callExpressions.forEach(callExpression => { | ||
let fail = false; | ||
|
||
const [arg] = callExpression.arguments; | ||
if (ts.isArrowFunction(arg) || ts.isFunctionExpression(arg)) { | ||
const [parameter] = arg.parameters; | ||
if (parameter) { | ||
const identifiers = tsquery( | ||
arg, | ||
`Identifier[name="${parameter.name.getText()}"]` | ||
) as ts.Identifier[]; | ||
fail = identifiers.length < 2; | ||
} else { | ||
fail = true; | ||
} | ||
} | ||
|
||
if (fail) { | ||
failures.push( | ||
new Lint.RuleFailure( | ||
sourceFile, | ||
arg.getStart(), | ||
arg.getStart() + arg.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
) | ||
); | ||
} | ||
}); | ||
return failures; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
test/v6/fixtures/no-ignored-takewhile-value/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,46 @@ | ||
import { Observable } from "rxjs"; | ||
import { takeWhile } from "rxjs/operators"; | ||
|
||
class Something { | ||
private _alive = true; | ||
constructor(private _source: Observable<string>) { | ||
|
||
_source.pipe( | ||
takeWhile(value => value) | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(value => value !== "42") | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(value => { return false; }) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-ignored-takewhile-value] | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(function (value) { return false; }) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-ignored-takewhile-value] | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(() => alive) | ||
~~~~~~~~~~~ [no-ignored-takewhile-value] | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(() => this._alive) | ||
~~~~~~~~~~~~~~~~~ [no-ignored-takewhile-value] | ||
).subscribe(); | ||
|
||
_source.pipe( | ||
takeWhile(() => this.alive()) | ||
~~~~~~~~~~~~~~~~~~ [no-ignored-takewhile-value] | ||
).subscribe(); | ||
} | ||
alive(): boolean { | ||
return this._alive; | ||
} | ||
} | ||
|
||
[no-ignored-takewhile-value]: Ignoring the takeWhile value is forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-ignored-takewhile-value/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"] | ||
} |
8 changes: 8 additions & 0 deletions
8
test/v6/fixtures/no-ignored-takewhile-value/default/tslint.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,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"rxjs-no-ignored-takewhile-value": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |
Shouldn't this message by
rxjs-no-ignored-takewhile-value
(missing-value
)?