Skip to content

Commit

Permalink
feat(no-connectable): Add rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Feb 28, 2019
1 parent 41d48e2 commit fdf2cda
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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/issues/85/tslint.json",
"test:debug": "tslint --test ./test/v6/fixtures/no-connectable/default/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
49 changes: 49 additions & 0 deletions source/rules/rxjsNoConnectableRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @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 { tsquery } from "@phenomnomnominal/tsquery";
import { couldBeFunction } from "../support/util";

export class Rule extends Lint.Rules.TypedRule {

public static metadata: Lint.IRuleMetadata = {
description: "Disallows using `publish` operators without selectors.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-no-connectable",
type: "functionality",
typescriptOnly: false
};

public static FAILURE_STRING = "Calling publish without a selector is forbidden";

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

const failures: Lint.RuleFailure[] = [];
const typeChecker = program.getTypeChecker();

const callIdentifiers = tsquery(
sourceFile,
`CallExpression Identifier[name=/^publish(Replay)?$/]`
);
callIdentifiers.forEach(identifier => {
const callExpression = identifier.parent as ts.CallExpression;
if (!callExpression.arguments.some(arg => couldBeFunction(typeChecker.getTypeAtLocation(arg)))) {
failures.push(new Lint.RuleFailure(
sourceFile,
identifier.getStart(),
identifier.getStart() + identifier.getWidth(),
Rule.FAILURE_STRING,
this.ruleName
));
}
});
return failures;
}
}
12 changes: 12 additions & 0 deletions test/v6/fixtures/no-connectable/default/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { of } from "rxjs";
import { publish, publishReplay } from "rxjs/operators";

const a = of(42).pipe(publish());
~~~~~~~ [no-connectable]
const b = of(42).pipe(publishReplay(1));
~~~~~~~~~~~~~ [no-connectable]

const c = of(42).pipe(publish(p => p));
const d = of(42).pipe(publishReplay(1, undefined, p => p));

[no-connectable]: Calling publish without a selector is forbidden
13 changes: 13 additions & 0 deletions test/v6/fixtures/no-connectable/default/tsconfig.json
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 test/v6/fixtures/no-connectable/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-no-connectable": { "severity": "error" }
},
"rulesDirectory": "../../../../../build/rules"
}

0 comments on commit fdf2cda

Please sign in to comment.