Skip to content

Commit

Permalink
feat(no-compat): Add no-compat rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jan 18, 2019
1 parent 6362de4 commit ae4403f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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/70/tslint.json",
"test:debug": "tslint --test ./test/v6/fixtures/no-compat/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
47 changes: 47 additions & 0 deletions source/rules/rxjsNoCompatRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @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";

export class Rule extends Lint.Rules.AbstractRule {

public static metadata: Lint.IRuleMetadata = {
deprecationMessage: undefined,
description: "Disallows importation from locations that depend upon 'rxjs-compat'.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-no-compat",
type: "functionality",
typescriptOnly: false
};

public static FAILURE_STRING = "'rxjs-compat' dependent import locations are forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const failures: Lint.RuleFailure[] = [];
const importDeclarations = tsquery(
sourceFile,
`ImportDeclaration:has(StringLiteral[value=/^rxjs/])`
);
importDeclarations.forEach(node => {
const importDeclaration = node as ts.ImportDeclaration;
const { moduleSpecifier } = importDeclaration;
if (!/^['"]rxjs(\/(ajax|operators|testing|webSocket))?['"]$/.test(moduleSpecifier.getText())) {
failures.push(new Lint.RuleFailure(
sourceFile,
moduleSpecifier.getStart(),
moduleSpecifier.getStart() + moduleSpecifier.getWidth(),
Rule.FAILURE_STRING,
this.ruleName
));
}
});
return failures;
}
}
25 changes: 25 additions & 0 deletions test/v6/fixtures/no-compat/default/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Observable } from "rxjs";
import { ajax } from "rxjs/ajax";
import { concatMap } from "rxjs/operators";
import { TestScheduler } from "rxjs/testing";
import { webSocket } from "rxjs/webSocket";

import * as Rx from "rxjs/Rx";
~~~~~~~~~ [no-compat]
import { Observable } from "rxjs/Observable";
~~~~~~~~~~~~~~~~~ [no-compat]
import { Subject } from "rxjs/Subject";
~~~~~~~~~~~~~~ [no-compat]
import { merge } from "rxjs/observable/merge";
~~~~~~~~~~~~~~~~~~~~~~~ [no-compat]
import { mergeMap } from "rxjs/operator/mergeMap";
~~~~~~~~~~~~~~~~~~~~~~~~ [no-compat]
import { asap } from "rxjs/scheduler/asap";
~~~~~~~~~~~~~~~~~~~~~ [no-compat]

import "rxjs/add/observable/merge";
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-compat]
import "rxjs/add/operator/mergeMap";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-compat]

[no-compat]: 'rxjs-compat' dependent import locations are forbidden
13 changes: 13 additions & 0 deletions test/v6/fixtures/no-compat/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-compat/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-no-compat": { "severity": "error" }
},
"rulesDirectory": "../../../../../build/rules"
}

0 comments on commit ae4403f

Please sign in to comment.