Skip to content

Commit

Permalink
fix(switchMap): Match camel-case actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Feb 17, 2018
1 parent 993db49 commit c1caa8c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
5 changes: 5 additions & 0 deletions fixtures/no-unsafe-switchmap/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const empty = Observable.empty() as Observable<never>;

const GET_SOMETHING = "GET_SOMETHING";
const PUT_SOMETHING = "PUT_SOMETHING";
const GetSomething = GET_SOMETHING;
const PutSomething = PUT_SOMETHING;

const patchedGetEffect = actions.ofType("GET_SOMETHING").do(() => {}).switchMap(() => empty);
const patchedPutEffect = actions.ofType("PUT_SOMETHING").do(() => {}).switchMap(() => empty);
Expand Down Expand Up @@ -50,3 +52,6 @@ const pipedSymbolPutEffect = actions.ofType(PUT_SOMETHING).pipe(tap(() => {}), s

const pipedOfTypeGetEffect = actions.pipe(ofType(GET_SOMETHING), tap(() => {}), switchMap(() => empty));
const pipedOfTypePutEffect = actions.pipe(ofType(PUT_SOMETHING), tap(() => {}), switchMap(() => empty));

const pipedOfTypeCamelCaseGetEffect = actions.pipe(ofType(GetSomething), tap(() => {}), switchMap(() => empty));
const pipedOfTypeCamelCasePutEffect = actions.pipe(ofType(PutSomething), tap(() => {}), switchMap(() => empty));
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"url": "https://github.com/cartant/rxjs-tslint-rules/issues"
},
"dependencies": {
"decamelize": "^2.0.0",
"resolve": "^1.4.0",
"tslib": "^1.8.0"
},
"description": "TSLint rules for RxJS",
"devDependencies": {
"@types/chai": "^4.0.0",
"@types/decamelize": "^1.2.0",
"@types/mocha": "^2.2.41",
"@types/node": "^9.3.0",
"@types/resolve": "^0.0.6",
Expand Down Expand Up @@ -50,7 +52,7 @@
"dist:clean": "rimraf dist",
"lint": "tslint --project tsconfig.json source/**/*.ts",
"prepublishOnly": "yarn run test && yarn run dist",
"test": "yarn run lint && yarn run test:build && mocha build/fixtures-spec.js",
"test": "yarn run lint && yarn run test:build && mocha build/**/*-spec.js",
"test:build": "yarn run test:clean && tsc -p tsconfig.json",
"test:clean": "rimraf build"
},
Expand Down
2 changes: 1 addition & 1 deletion source/fixtures-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("fixtures", function (): void {

it("should effect 'no-unsafe-switchmap' errors", () => {
const result = lint("no-unsafe-switchmap", "tslint.json");
expect(result).to.have.property("errorCount", 11);
expect(result).to.have.property("errorCount", 12);
result.failures.forEach(failure => expect(failure).to.have.property("ruleName", "rxjs-no-unsafe-switchmap"));
});
});
Expand Down
54 changes: 54 additions & 0 deletions source/rules/rxjsNoUnsafeSwitchmapRule-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @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
*/

import { expect } from "chai";
import * as decamelize from "decamelize";
import { Walker } from "./rxjsNoUnsafeSwitchmapRule";

describe("rxjs-no-unsafe-switchmap", () => {

describe("createRegExp", () => {

const regExp = Walker.createRegExp(["add"]);

it("should match action literals", () => {

expect(`"ADD"`).to.match(regExp);
expect(`"ADD_SOMETHING"`).to.match(regExp);
expect(`"SOMETHING_ADD"`).to.match(regExp);

expect(`'ADD'`).to.match(regExp);
expect(`'ADD_SOMETHING'`).to.match(regExp);
expect(`'SOMETHING_ADD'`).to.match(regExp);

expect("`ADD`").to.match(regExp);
expect("`ADD_SOMETHING`").to.match(regExp);
expect("`SOMETHING_ADD`").to.match(regExp);
});

it("should match action symbols", () => {

expect("ADD").to.match(regExp);
expect("ADD_SOMETHING").to.match(regExp);
expect("SOMETHING_ADD").to.match(regExp);

expect(decamelize("Add")).to.match(regExp);
expect(decamelize("AddSomething")).to.match(regExp);
expect(decamelize("SomethingAdd")).to.match(regExp);
});

it("should not match fragments within words", () => {

expect("READD").to.not.match(regExp);
expect("Readd").to.not.match(regExp);

expect("ADDER").to.not.match(regExp);
expect("Adder").to.not.match(regExp);

expect("LADDER").to.not.match(regExp);
expect("Ladder").to.not.match(regExp);
});
});
});
19 changes: 11 additions & 8 deletions source/rules/rxjsNoUnsafeSwitchmapRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import * as Lint from "tslint";
import * as ts from "typescript";
import * as tsutils from "tsutils";
import * as decamelize from "decamelize";

import { couldBeType, isReferenceType } from "../support/util";

export class Rule extends Lint.Rules.TypedRule {
Expand Down Expand Up @@ -48,7 +50,7 @@ export class Rule extends Lint.Rules.TypedRule {
}
}

class Walker extends Lint.ProgramAwareRuleWalker {
export class Walker extends Lint.ProgramAwareRuleWalker {

public static ACTIONS_REGEXP = /action(s|\$)?/i;
public static METHODS_REGEXP = /(ofType|pipe)/;
Expand All @@ -66,7 +68,7 @@ class Walker extends Lint.ProgramAwareRuleWalker {
private allowRegExp: RegExp | null;
private disallowRegExp: RegExp | null;

private static createRegExp(value: any): RegExp | null {
public static createRegExp(value: any): RegExp | null {

if (!value || !value.length) {
return null;
Expand All @@ -76,7 +78,7 @@ class Walker extends Lint.ProgramAwareRuleWalker {
return new RegExp(value, flags);
}
const fragments = value as string[];
const joined = fragments.map(fragment => `(\\b|[_'"])${fragment}(\\b|[_'"])`).join("|");
const joined = fragments.map(fragment => `(\\b|_)${fragment}(\\b|_)`).join("|");
return new RegExp(`(${joined})`, flags);
}

Expand All @@ -89,6 +91,7 @@ class Walker extends Lint.ProgramAwareRuleWalker {
this.allowRegExp = Walker.createRegExp(options.allow);
this.disallowRegExp = Walker.createRegExp(options.disallow);
} else {
this.allowRegExp = null;
this.disallowRegExp = Walker.createRegExp(Walker.DEFAULT_DISALLOW);
}
}
Expand Down Expand Up @@ -131,12 +134,12 @@ class Walker extends Lint.ProgramAwareRuleWalker {

private shouldDisallow(args: ts.NodeArray<ts.Expression>): boolean {

const names = args.map(arg => arg.getText());
if (this.allowRegExp && !names.every(name => this.allowRegExp.test(name))) {
return true;
const names = args.map(arg => decamelize(arg.getText()));
if (this.allowRegExp) {
return !names.every(name => this.allowRegExp.test(name));
}
if (this.disallowRegExp && names.some(name => this.disallowRegExp.test(name))) {
return true;
if (this.disallowRegExp) {
return names.some(name => this.disallowRegExp.test(name));
}
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"

"@types/decamelize@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/decamelize/-/decamelize-1.2.0.tgz#aabc2306e6c229356636cab7fcb823cfe170f056"

"@types/mocha@^2.2.41":
version "2.2.48"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab"
Expand Down Expand Up @@ -136,6 +140,12 @@ [email protected]:
dependencies:
ms "2.0.0"

decamelize@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-2.0.0.tgz#656d7bbc8094c4c788ea53c5840908c9c7d063c7"
dependencies:
xregexp "4.0.0"

deep-eql@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
Expand Down Expand Up @@ -364,3 +374,7 @@ typescript@~2.7.1:
wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-4.0.0.tgz#e698189de49dd2a18cc5687b05e17c8e43943020"

0 comments on commit c1caa8c

Please sign in to comment.