From 215cf0a670312667ab8e8803c03d6f80d59196f7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 28 Feb 2017 08:05:36 -0800 Subject: [PATCH 1/4] Add "ban-types" to `tslint:latest`. --- src/configs/latest.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/configs/latest.ts b/src/configs/latest.ts index 0a10c824dec..9a5f8524dd4 100644 --- a/src/configs/latest.ts +++ b/src/configs/latest.ts @@ -52,6 +52,16 @@ export const rules = { "arrow-return-shorthand": true, "no-unnecessary-initializer": true, "no-misused-new": true, + + // added in v4.5 + "ban-types": [ + true, + ["Object", "Avoid using the `Object` type. Did you mean `object`?"], + ["Function", "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."], + ["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"], + ["Number", "Avoid using the `Number` type. Did you mean `number`?"], + ["String", "Avoid using the `String` type. Did you mean `string`?"] + ], }; // tslint:enable object-literal-sort-keys From ff232320ead45d75d5a553dc8c6e8b00fd70e0eb Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 28 Feb 2017 08:34:06 -0800 Subject: [PATCH 2/4] Fix lint --- package.json | 4 ++-- src/configs/latest.ts | 2 +- src/formatterLoader.ts | 15 ++++----------- src/index.ts | 6 ++++-- test/ruleLoaderTests.ts | 19 ------------------- 5 files changed, 11 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index e301095a471..382c14b033f 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "compile:test": "tsc -p test", "lint": "npm-run-all -p lint:core lint:test lint:from-bin", "lint:core": "tslint \"src/**/*.ts\"", - "lint:test": "tslint \"test/**/*.ts\" -e \"test/**/*.test.ts\"", - "lint:from-bin": "node bin/tslint \"{src,test}/**/*.ts\" -e \"test/**/*.test.ts\"", + "lint:test": "tslint \"test/**/*.ts\" -e \"test/files/**\" -e \"test/rules/**\"", + "lint:from-bin": "node bin/tslint \"{src,test}/**/*.ts\" -e \"test/files/**\" -e \"test/rules/**\"", "test": "npm-run-all test:pre -p test:mocha test:rules", "test:pre": "cd ./test/config && npm install", "test:mocha": "mocha --reporter spec --colors \"build/test/**/*Tests.js\" build/test/assert.js", diff --git a/src/configs/latest.ts b/src/configs/latest.ts index 9a5f8524dd4..76db94b0911 100644 --- a/src/configs/latest.ts +++ b/src/configs/latest.ts @@ -60,7 +60,7 @@ export const rules = { ["Function", "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."], ["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"], ["Number", "Avoid using the `Number` type. Did you mean `number`?"], - ["String", "Avoid using the `String` type. Did you mean `string`?"] + ["String", "Avoid using the `String` type. Did you mean `string`?"], ], }; // tslint:enable object-literal-sort-keys diff --git a/src/formatterLoader.ts b/src/formatterLoader.ts index 942f40f8db9..5da7a24d56e 100644 --- a/src/formatterLoader.ts +++ b/src/formatterLoader.ts @@ -17,23 +17,16 @@ import * as fs from "fs"; import * as path from "path"; +import {FormatterFunction} from "./index"; import {camelize} from "./utils"; const moduleDirectory = path.dirname(module.filename); const CORE_FORMATTERS_DIRECTORY = path.resolve(moduleDirectory, ".", "formatters"); -function isFunction(variable: any): variable is Function { - return typeof variable === "function"; -} - -function isString(variable: any): variable is string { - return typeof variable === "string"; -} - -export function findFormatter(name: string | Function, formattersDirectory?: string) { - if (isFunction(name)) { +export function findFormatter(name: string | FormatterFunction, formattersDirectory?: string) { + if (typeof name === "function") { return name; - } else if (isString(name)) { + } else if (typeof name === "string") { name = name.trim(); const camelizedName = camelize(`${name}Formatter`); diff --git a/src/index.ts b/src/index.ts index faf6ede9279..835ac72fc15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,13 +38,15 @@ export interface LintResult { failureCount: number; failures: RuleFailure[]; fixes?: RuleFailure[]; - format: string | Function; + format: string | FormatterFunction; output: string; } +export type FormatterFunction = (failures: RuleFailure[]) => string; + export interface ILinterOptions { fix: boolean; - formatter?: string | Function; + formatter?: string | FormatterFunction; formattersDirectory?: string; rulesDirectory?: string | string[]; } diff --git a/test/ruleLoaderTests.ts b/test/ruleLoaderTests.ts index 2c81f20b18f..c40ad28b760 100644 --- a/test/ruleLoaderTests.ts +++ b/test/ruleLoaderTests.ts @@ -110,23 +110,4 @@ describe("Rule Loader", () => { assert.isFalse(testFailed, "List of rules doesn't match list of tests"); }); - - it("ensures that `.ts` files in `rules/` end in `.test.ts` to avoid being linted", () => { - walkSync(testRulesDir, (filename) => { - if (/\.ts$/.test(filename)) { - assert.match(filename, /\.test\.ts$/); - } - }); - }); - - const walkSync = (dir: string, cb: (filename: string) => void) => { - fs.readdirSync(dir).forEach((file) => { - const fullPath = path.join(dir, file); - if (fs.statSync(path.join(dir, file)).isDirectory()) { - walkSync(fullPath, cb); - } else { - cb(fullPath); - } - }); - }; }); From 6de44d9766263bb8ee5ec3bf9dda4b99c9f67b83 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 28 Feb 2017 08:42:05 -0800 Subject: [PATCH 3/4] Add "Symbol" --- src/configs/latest.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/configs/latest.ts b/src/configs/latest.ts index 76db94b0911..9e8e743f687 100644 --- a/src/configs/latest.ts +++ b/src/configs/latest.ts @@ -61,6 +61,7 @@ export const rules = { ["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"], ["Number", "Avoid using the `Number` type. Did you mean `number`?"], ["String", "Avoid using the `String` type. Did you mean `string`?"], + ["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"], ], }; // tslint:enable object-literal-sort-keys From b1726be836575321debd547002d579474fbcb5f0 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 28 Feb 2017 09:01:09 -0800 Subject: [PATCH 4/4] Undo changes to test file linting --- package.json | 4 ++-- test/ruleLoaderTests.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 382c14b033f..e301095a471 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "compile:test": "tsc -p test", "lint": "npm-run-all -p lint:core lint:test lint:from-bin", "lint:core": "tslint \"src/**/*.ts\"", - "lint:test": "tslint \"test/**/*.ts\" -e \"test/files/**\" -e \"test/rules/**\"", - "lint:from-bin": "node bin/tslint \"{src,test}/**/*.ts\" -e \"test/files/**\" -e \"test/rules/**\"", + "lint:test": "tslint \"test/**/*.ts\" -e \"test/**/*.test.ts\"", + "lint:from-bin": "node bin/tslint \"{src,test}/**/*.ts\" -e \"test/**/*.test.ts\"", "test": "npm-run-all test:pre -p test:mocha test:rules", "test:pre": "cd ./test/config && npm install", "test:mocha": "mocha --reporter spec --colors \"build/test/**/*Tests.js\" build/test/assert.js", diff --git a/test/ruleLoaderTests.ts b/test/ruleLoaderTests.ts index c40ad28b760..2c81f20b18f 100644 --- a/test/ruleLoaderTests.ts +++ b/test/ruleLoaderTests.ts @@ -110,4 +110,23 @@ describe("Rule Loader", () => { assert.isFalse(testFailed, "List of rules doesn't match list of tests"); }); + + it("ensures that `.ts` files in `rules/` end in `.test.ts` to avoid being linted", () => { + walkSync(testRulesDir, (filename) => { + if (/\.ts$/.test(filename)) { + assert.match(filename, /\.test\.ts$/); + } + }); + }); + + const walkSync = (dir: string, cb: (filename: string) => void) => { + fs.readdirSync(dir).forEach((file) => { + const fullPath = path.join(dir, file); + if (fs.statSync(path.join(dir, file)).isDirectory()) { + walkSync(fullPath, cb); + } else { + cb(fullPath); + } + }); + }; });