-
-
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
104 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 | ||
*/ | ||
/*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 using `ReplaySubject`, `publishReplay` or `shareReplay` without specifying the buffer size.", | ||
options: null, | ||
optionsDescription: "Not configurable.", | ||
requiresTypeInfo: true, | ||
ruleName: "rxjs-no-ignored-replay-buffer", | ||
type: "functionality", | ||
typescriptOnly: true | ||
}; | ||
|
||
public static FAILURE_STRING = "Ignoring the buffer size is forbidden"; | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
|
||
const failures: Lint.RuleFailure[] = []; | ||
|
||
const newIdentifiers = tsquery( | ||
sourceFile, | ||
`NewExpression Identifier[name="ReplaySubject"]` | ||
); | ||
newIdentifiers.forEach(identifier => { | ||
const newExpression = identifier.parent as ts.NewExpression; | ||
if (!newExpression.arguments || newExpression.arguments.length === 0) { | ||
failures.push(new Lint.RuleFailure( | ||
sourceFile, | ||
identifier.getStart(), | ||
identifier.getStart() + identifier.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
)); | ||
} | ||
}); | ||
|
||
const callIdentifiers = tsquery( | ||
sourceFile, | ||
`CallExpression Identifier[name=/(publishReplay|shareReplay)/]` | ||
); | ||
callIdentifiers.forEach(identifier => { | ||
const callExpression = identifier.parent as ts.CallExpression; | ||
if (callExpression.arguments.length === 0) { | ||
failures.push(new Lint.RuleFailure( | ||
sourceFile, | ||
identifier.getStart(), | ||
identifier.getStart() + identifier.getWidth(), | ||
Rule.FAILURE_STRING, | ||
this.ruleName | ||
)); | ||
} | ||
}); | ||
return failures; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/v6/fixtures/no-ignored-replay-buffer/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,16 @@ | ||
import { of, ReplaySubject } from "rxjs"; | ||
import { publishReplay, shareReplay } from "rxjs/operators"; | ||
|
||
const a = new ReplaySubject<string>(1); | ||
const b = new ReplaySubject<string>(); | ||
~~~~~~~~~~~~~ [no-ignored-replay-buffer] | ||
|
||
const c = of(42).pipe(publishReplay(1)); | ||
const d = of(42).pipe(publishReplay()); | ||
~~~~~~~~~~~~~ [no-ignored-replay-buffer] | ||
|
||
const e = of(42).pipe(shareReplay(1)); | ||
const f = of(42).pipe(shareReplay()); | ||
~~~~~~~~~~~ [no-ignored-replay-buffer] | ||
|
||
[no-ignored-replay-buffer]: Ignoring the buffer size is forbidden |
13 changes: 13 additions & 0 deletions
13
test/v6/fixtures/no-ignored-replay-buffer/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-replay-buffer/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-replay-buffer": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../../../build/rules" | ||
} |