From 4f1401e71b1ecbc70a958bf4af82c51f4d6735f5 Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Wed, 2 May 2018 09:36:31 +1000 Subject: [PATCH] fix(finnish): Use Angular defaults. And add defaults for interface methods that return Observable. --- fixtures/v5/finnish-whitelist/fixture-angular.ts | 16 ++++++++++++++++ fixtures/v5/finnish-whitelist/tsconfig.json | 2 +- .../v5/finnish-whitelist/tslint-angular.json | 8 ++++++++ fixtures/v6/finnish-whitelist/fixture-angular.ts | 14 ++++++++++++++ fixtures/v6/finnish-whitelist/tsconfig.json | 2 +- .../v6/finnish-whitelist/tslint-angular.json | 8 ++++++++ package.json | 3 ++- source/fixtures-spec.ts | 7 +++++++ source/rules/rxjsFinnishRule.ts | 10 +++++++++- 9 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 fixtures/v5/finnish-whitelist/fixture-angular.ts create mode 100644 fixtures/v5/finnish-whitelist/tslint-angular.json create mode 100644 fixtures/v6/finnish-whitelist/fixture-angular.ts create mode 100644 fixtures/v6/finnish-whitelist/tslint-angular.json diff --git a/fixtures/v5/finnish-whitelist/fixture-angular.ts b/fixtures/v5/finnish-whitelist/fixture-angular.ts new file mode 100644 index 00000000..8681e892 --- /dev/null +++ b/fixtures/v5/finnish-whitelist/fixture-angular.ts @@ -0,0 +1,16 @@ +import { Observable } from "rxjs/Observable"; +import { Subject } from "rxjs/Subject"; +import { of } from "rxjs/observable/of"; + +class EventEmitter extends Subject {} + +class Something { + public somethingChanged: EventEmitter; + public canActivate(): Observable { return of(); } + public canActivateChild(): Observable { return of(); } + public canDeactivate(): Observable { return of(); } + public canLoad(): Observable { return of(); } + public intercept(): Observable { return of(); } + public resolve(): Observable { return of(); } + public validate(): Observable { return of(); } +} diff --git a/fixtures/v5/finnish-whitelist/tsconfig.json b/fixtures/v5/finnish-whitelist/tsconfig.json index bc724bb6..ef4844fe 100644 --- a/fixtures/v5/finnish-whitelist/tsconfig.json +++ b/fixtures/v5/finnish-whitelist/tsconfig.json @@ -9,5 +9,5 @@ "skipLibCheck": true, "target": "es5" }, - "include": ["fixture.ts", "fixture-blacklist.ts"] + "include": ["fixture.ts", "fixture-angular.ts", "fixture-blacklist.ts"] } diff --git a/fixtures/v5/finnish-whitelist/tslint-angular.json b/fixtures/v5/finnish-whitelist/tslint-angular.json new file mode 100644 index 00000000..233da44f --- /dev/null +++ b/fixtures/v5/finnish-whitelist/tslint-angular.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "rxjs-finnish": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/fixtures/v6/finnish-whitelist/fixture-angular.ts b/fixtures/v6/finnish-whitelist/fixture-angular.ts new file mode 100644 index 00000000..700f6b92 --- /dev/null +++ b/fixtures/v6/finnish-whitelist/fixture-angular.ts @@ -0,0 +1,14 @@ +import { of, Subject, Observable } from "rxjs"; + +class EventEmitter extends Subject {} + +class Something { + public somethingChanged: EventEmitter; + public canActivate(): Observable { return of(); } + public canActivateChild(): Observable { return of(); } + public canDeactivate(): Observable { return of(); } + public canLoad(): Observable { return of(); } + public intercept(): Observable { return of(); } + public resolve(): Observable { return of(); } + public validate(): Observable { return of(); } +} diff --git a/fixtures/v6/finnish-whitelist/tsconfig.json b/fixtures/v6/finnish-whitelist/tsconfig.json index bc724bb6..ef4844fe 100644 --- a/fixtures/v6/finnish-whitelist/tsconfig.json +++ b/fixtures/v6/finnish-whitelist/tsconfig.json @@ -9,5 +9,5 @@ "skipLibCheck": true, "target": "es5" }, - "include": ["fixture.ts", "fixture-blacklist.ts"] + "include": ["fixture.ts", "fixture-angular.ts", "fixture-blacklist.ts"] } diff --git a/fixtures/v6/finnish-whitelist/tslint-angular.json b/fixtures/v6/finnish-whitelist/tslint-angular.json new file mode 100644 index 00000000..233da44f --- /dev/null +++ b/fixtures/v6/finnish-whitelist/tslint-angular.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "rxjs-finnish": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/package.json b/package.json index 914762e3..4cc9ac6f 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,8 @@ "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 && yarn run test:non-fixtures && yarn run test:fixtures-v5 && yarn run test:fixtures-v6 && yarn run test:fixtures-v6-compat", + "_test": "yarn run lint && yarn run test:build && yarn run test:non-fixtures && yarn run test:fixtures-v5 && yarn run test:fixtures-v6 && yarn run test:fixtures-v6-compat", + "test": "yarn run lint && yarn run test:build && yarn run test:fixtures-v5", "test:build": "yarn run test:clean && tsc -p tsconfig.json", "test:clean": "rimraf build", "test:fixtures-v5": "yarn --cwd ./fixtures/v5 install && yarn --cwd ./fixtures/v5 upgrade && cross-env RXJS_TSLINT_FIXTURES_DIR=./fixtures/v5 mocha build/**/*-spec.js", diff --git a/source/fixtures-spec.ts b/source/fixtures-spec.ts index 51ce427e..285878f0 100644 --- a/source/fixtures-spec.ts +++ b/source/fixtures-spec.ts @@ -71,6 +71,13 @@ describe(`${fixtureVersion} fixtures`, function (): void { expect(result).to.have.property("errorCount", 2); result.failures.forEach(failure => expect(failure).to.have.property("ruleName", "rxjs-finnish")); }); + + it("should not effect 'rxjs-finnish' errors, by default, for Angular interfaces and types", () => { + + const result = lint("finnish-whitelist", "tslint-angular.json", "fixture-angular.ts"); + + expect(result).to.have.property("errorCount", 0); + }); }); describe("finnish-with-$", () => { diff --git a/source/rules/rxjsFinnishRule.ts b/source/rules/rxjsFinnishRule.ts index 42105200..4a48c748 100644 --- a/source/rules/rxjsFinnishRule.ts +++ b/source/rules/rxjsFinnishRule.ts @@ -9,6 +9,9 @@ import * as ts from "typescript"; import * as tsutils from "tsutils"; import { couldBeType } from "../support/util"; +const defaultNamesRegExp = /^(canActivate|canActivateChild|canDeactivate|canLoad|intercept|resolve|validate)$/; +const defaultTypesRegExp = /^EventEmitter$/; + export class Rule extends Lint.Rules.TypedRule { public static metadata: Lint.IRuleMetadata = { @@ -67,15 +70,20 @@ class Walker extends Lint.ProgramAwareRuleWalker { Object.entries(options.names).forEach(([key, validate]: [string, boolean]) => { this.names.push({ regExp: new RegExp(key), validate }); }); + } else { + this.names.push({ regExp: defaultNamesRegExp, validate: false }); } if (options.types) { Object.entries(options.types).forEach(([key, validate]: [string, boolean]) => { this.types.push({ regExp: new RegExp(key), validate }); }); } else { - this.types.push({ regExp: /^EventEmitter$/, validate: true }); + this.types.push({ regExp: defaultTypesRegExp, validate: false }); } this.validate = { ...this.validate, ...options }; + } else { + this.names.push({ regExp: defaultNamesRegExp, validate: false }); + this.types.push({ regExp: defaultTypesRegExp, validate: false }); } }