Skip to content

Commit

Permalink
Fix no ignored replay buffer edge cases (#82)
Browse files Browse the repository at this point in the history
* Handle edge cases

* Add some more test cases to default

* Create test cases for different import alias

* add tsconfig file

* Add tslint file
  • Loading branch information
maggie-x authored and cartant committed Feb 23, 2019
1 parent 709f5f1 commit a3aae92
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 23 deletions.
75 changes: 52 additions & 23 deletions source/rules/rxjsNoIgnoredReplayBufferRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
*/
/*tslint:disable:no-use-before-declare*/

import { tsquery } from "@phenomnomnominal/tsquery";
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.",
description:
"Disallows using `ReplaySubject`, `publishReplay` or `shareReplay` without specifying the buffer size.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: true,
Expand All @@ -24,24 +22,53 @@ export class Rule extends Lint.Rules.TypedRule {

public static FAILURE_STRING = "Ignoring the buffer size is forbidden";

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {

public applyWithProgram(
sourceFile: ts.SourceFile,
program: ts.Program
): Lint.RuleFailure[] {
const failures: Lint.RuleFailure[] = [];

const newIdentifiers = tsquery(
sourceFile,
`NewExpression Identifier[name="ReplaySubject"]`
`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
));
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 nestedNewIdentitiers = tsquery(
sourceFile,
`NewExpression PropertyAccessExpression Identifier[name="ReplaySubject"]`
);
nestedNewIdentitiers.forEach(identifier => {
const newExpression = identifier.parent.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
)
);
}
});

Expand All @@ -52,13 +79,15 @@ export class Rule extends Lint.Rules.TypedRule {
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
));
failures.push(
new Lint.RuleFailure(
sourceFile,
identifier.getStart(),
identifier.getStart() + identifier.getWidth(),
Rule.FAILURE_STRING,
this.ruleName
)
);
}
});
return failures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ const e = of(42).pipe(shareReplay(1));
const f = of(42).pipe(shareReplay());
~~~~~~~~~~~ [no-ignored-replay-buffer]

const g = new Thing(new ReplaySubject<number>(1));
const h = new Thing(new ReplaySubject<number>());
~~~~~~~~~~~~~ [no-ignored-replay-buffer]

[no-ignored-replay-buffer]: Ignoring the buffer size is forbidden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as Rx from "rxjs";
import { publishReplay, shareReplay } from "rxjs/operators";

const a = new Rx.ReplaySubject<string>(1);
const b = new Rx.ReplaySubject<string>();
~~~~~~~~~~~~~ [no-ignored-replay-buffer]

const c = Rx.of(42).pipe(publishReplay(1));
const d = Rx.of(42).pipe(publishReplay());
~~~~~~~~~~~~~ [no-ignored-replay-buffer]

const e = Rx.of(42).pipe(shareReplay(1));
const f = Rx.of(42).pipe(shareReplay());
~~~~~~~~~~~ [no-ignored-replay-buffer]


const g = new Thing(new Rx.ReplaySubject<number>(1));
const g = new Thing(new Rx.ReplaySubject<number>());
~~~~~~~~~~~~~ [no-ignored-replay-buffer]
class Mock {
private valid: Rx.ReplaySubject<number>;
private invalid: Rx.ReplaySubject<number>

constructor(){
this.valid = new Rx.ReplaySubject<number>(1);
this.invalid = new Rx.ReplaySubject<number>();
~~~~~~~~~~~~~ [no-ignored-replay-buffer]
}
}
[no-ignored-replay-buffer]: Ignoring the buffer size is forbidden
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"]
}
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"
}

0 comments on commit a3aae92

Please sign in to comment.