Skip to content

Commit

Permalink
refactor(rxjs-prefer-add): Rename.
Browse files Browse the repository at this point in the history
rxjs-no-wholesale makes more sense, as it can be use in combination with
rxjs-no-add and rxjs-no-patched.
  • Loading branch information
cartant committed Jul 17, 2017
1 parent 680ec2e commit e6648e5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The package includes the following rules:
| `rxjs-no-patched` | Disallows the calling of patched methods. Operators must be imported and called explicitly - not via the `Observable` prototype. | None |
| `rxjs-no-subject-unsubscribe` | Disallows the calling of `unsubscribe` directly upon `Subject` instances. For an explanation of why this can be a problem, see [this](https://stackoverflow.com/a/45112125/6680611) Stack Overflow answer. | None |
| `rxjs-no-unused-add` | Disallows the importation of patched observables or operators that are not used in the module. | None |
| `rxjs-prefer-add` | Disallows the importation of `rxjs` or `rxjs/Rx`. | None |
| `rxjs-no-wholesale` | Disallows the wholesale importation of `rxjs` or `rxjs/Rx`. | None |

### Options

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion fixtures/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"rules": {
"rxjs-add": { "severity": "error" },
"rxjs-no-unused-add": { "severity": "error" },
"rxjs-prefer-add": { "severity": "error" }
"rxjs-no-wholesale": { "severity": "error" }
},
"rulesDirectory": "../build/rules"
}
10 changes: 5 additions & 5 deletions source/fixtures-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe("fixtures", function (): void {
});
});

describe("import-all", () => {
describe("import-wholesale", () => {

it("should effect 'rxjs-prefer-add' errors", () => {
it("should effect 'rxjs-no-wholesale' errors", () => {

const result = lint("import-all");
const result = lint("import-wholesale");

expect(result).to.have.property("errorCount", 2);
expect(result.failures[0]).to.have.property("ruleName", "rxjs-prefer-add");
expect(result.failures[1]).to.have.property("ruleName", "rxjs-prefer-add");
expect(result.failures[0]).to.have.property("ruleName", "rxjs-no-wholesale");
expect(result.failures[1]).to.have.property("ruleName", "rxjs-no-wholesale");
});
});

Expand Down
42 changes: 42 additions & 0 deletions source/rules/rxjsNoWholesaleRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Copyright © 2017 Nicholas Jamieson. All Rights Reserved.
* 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";

export class Rule extends Lint.Rules.AbstractRule {

public static metadata: Lint.IRuleMetadata = {
description: "Disallows the wholesale importation of `rxjs` or `rxjs/Rx`.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-no-wholesale",
type: "style",
typescriptOnly: false
};

public static FAILURE_STRING = "Wholesale RxJS imports are forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {

public visitImportDeclaration(node: ts.ImportDeclaration): void {

const moduleSpecifier = node.moduleSpecifier.getText();

if (/^['"]rxjs(\/Rx)?['"]/.test(moduleSpecifier)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
}

super.visitImportDeclaration(node);
}
}
35 changes: 6 additions & 29 deletions source/rules/rxjsPreferAddRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,12 @@
import * as Lint from "tslint";
import * as ts from "typescript";

export class Rule extends Lint.Rules.AbstractRule {
import { Rule as NoWholesaleRule } from "./rxjsNoWholesaleRule";

public static metadata: Lint.IRuleMetadata = {
description: "Disallows the importation of `rxjs` or `rxjs/Rx`.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-prefer-add",
type: "style",
typescriptOnly: false
};
export class Rule extends NoWholesaleRule {

public static FAILURE_STRING = "RxJS add import is preferred";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {

public visitImportDeclaration(node: ts.ImportDeclaration): void {

const moduleSpecifier = node.moduleSpecifier.getText();

if (/^['"]rxjs(\/Rx)?['"]/.test(moduleSpecifier)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
}

super.visitImportDeclaration(node);
}
public static metadata: Lint.IRuleMetadata = Object.assign({}, NoWholesaleRule.metadata, {
deprecationMessage: "rxjs-prefer-add has been renamed to rxjs-no-wholesale.",
ruleName: "rxjs-prefer-add"
});
}

0 comments on commit e6648e5

Please sign in to comment.