Skip to content

Commit

Permalink
feat(rxjs-no-do): Add rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jul 18, 2017
1 parent 549b1cc commit 1d7b352
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The package includes the following rules:
| --- | --- | --- |
| `rxjs-add` | Enforces the importation of patched observables and operators used in the module. | See below |
| `rxjs-no-add` | Disallows the importation of patched observables and operators. | None |
| `rxjs-no-do` | I do without `do` operators. [Do you not?](https://youtu.be/spG-Yj0zEyc) | None |
| `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 |
Expand Down
5 changes: 5 additions & 0 deletions fixtures/no-do/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Observable } from "rxjs";
import "rxjs/add/observable/of";
import "rxjs/add/operator/do";

const ob = Observable.of(1).do((value) => console.log(value));
13 changes: 13 additions & 0 deletions fixtures/no-do/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 fixtures/no-do/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-no-do": { "severity": "error" }
},
"rulesDirectory": "../../build/rules"
}
11 changes: 11 additions & 0 deletions source/fixtures-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ describe("fixtures", function (): void {
});
});

describe("no-do", () => {

it("should effect 'rxjs-no-do' errors", () => {

const result = lint("no-do", "tslint.json");

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

describe("no-errors", () => {

it("should effect no errors", () => {
Expand Down
41 changes: 41 additions & 0 deletions source/rules/rxjsNoDoRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @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";

import { UsedWalker } from "../support/used-walker";

export class Rule extends Lint.Rules.TypedRule {

public static metadata: Lint.IRuleMetadata = {
description: "Disallows the use of the do operator.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: true,
ruleName: "rxjs-no-do",
type: "style",
typescriptOnly: true
};

public static FAILURE_STRING = "RxJS do operator is forbidden";

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

return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), program));
}
}

class Walker extends UsedWalker {

protected onSourceFileEnd(): void {

if (this.usedOperators["do"]) {
this.usedOperators["do"].forEach((node) => this.addFailureAtNode(node, Rule.FAILURE_STRING));
}
}
}

0 comments on commit 1d7b352

Please sign in to comment.