Skip to content

Commit

Permalink
feat(generics): Check from, of, BehaviorSubject.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Apr 24, 2019
1 parent bd4b4d8 commit 0a8e162
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"test": "yarn run lint && yarn run test:build && yarn run test:mocha && yarn run test:tslint-v5 && yarn run test:tslint-v6 && yarn run test:tslint-v6-compat",
"test:build": "yarn run test:clean && tsc -p tsconfig.json",
"test:clean": "rimraf build",
"test:debug": "tslint --test ./test/v6/fixtures/no-nested-subscribe/prototype/tslint.json",
"test:debug": "tslint --test ./test/v6/fixtures/no-explicit-generics/**/tslint.json",
"test:issues": "yarn run test:clean && tsc -p tsconfig.json && tslint --test ./test/v6/fixtures/issues/**/tslint.json",
"test:mocha": "mocha build/**/*-spec.js",
"test:tslint-v5": "yarn --cwd ./test/v5 install && yarn --cwd ./test/v5 upgrade && tslint --test ./test/v5/fixtures/**/tslint.json",
Expand Down
42 changes: 27 additions & 15 deletions source/rules/rxjsNoExplicitGenericsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,53 @@ import * as Lint from "tslint";
import * as ts from "typescript";
import { tsquery } from "@phenomnomnominal/tsquery";

export class Rule extends Lint.Rules.TypedRule {
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
description: "Disallows explicit generic type arguments.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-no-explicit-generics",
type: "functionality",
typescriptOnly: false
typescriptOnly: true
};

public static FAILURE_STRING =
"Explicit generic type arguments are forbidden";

public applyWithProgram(
sourceFile: ts.SourceFile,
program: ts.Program
): Lint.RuleFailure[] {
const failures: Lint.RuleFailure[] = [];
const callIdentifiers = tsquery(
sourceFile,
`CallExpression[expression.name.text="pipe"] > CallExpression[typeArguments.length>0] > Identifier`
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const identifiers: ts.Identifier[] = [];

identifiers.push(
...(tsquery(
sourceFile,
`CallExpression[expression.name.text="pipe"] > CallExpression[typeArguments.length>0] > Identifier`
) as ts.Identifier[])
);

identifiers.push(
...(tsquery(
sourceFile,
`CallExpression[typeArguments.length>0] > Identifier[name=/^(from|of)$/]`
) as ts.Identifier[])
);

callIdentifiers.forEach(identifier => {
failures.push(
identifiers.push(
...(tsquery(
sourceFile,
`NewExpression[typeArguments.length > 0] > Identifier[name="BehaviorSubject"]`
) as ts.Identifier[])
);

return identifiers.map(
identifier =>
new Lint.RuleFailure(
sourceFile,
identifier.getStart(),
identifier.getStart() + identifier.getWidth(),
Rule.FAILURE_STRING,
this.ruleName
)
);
});
return failures;
);
}
}
19 changes: 17 additions & 2 deletions test/v6/fixtures/no-explicit-generics/default/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { of } from "rxjs";
import { BehaviorSubject, from, of } from "rxjs";
import { scan } from "rxjs/operators";

const a = of(1, 2, 3);
const a = of(42, 54);
const b = a.pipe(
scan<number, string>((acc, value) => `${acc},${value}`, "")
~~~~ [no-explicit-generics]
);
const c = a.pipe(
scan((acc: string, value: number) => `${acc},${value}`, "")
);
const c2 = a.pipe(
scan((acc, value): string => `${acc},${value}`, "")
);

const d = new BehaviorSubject<number>(42);
~~~~~~~~~~~~~~~ [no-explicit-generics]
const e = new BehaviorSubject(42);

const f = from<number>([42, 54]);
~~~~ [no-explicit-generics]
const g = from([42, 54]);

const h = of<number>(42, 54);
~~ [no-explicit-generics]
const i = of(42, 54);

[no-explicit-generics]: Explicit generic type arguments are forbidden

0 comments on commit 0a8e162

Please sign in to comment.