From 1d8d9283a5db921fe8b346f783627d2e2a3781cb Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Thu, 1 Jun 2017 19:49:39 +0200 Subject: [PATCH] Fix multiple --exclude arguments (#2858) --- src/tslint-cli.ts | 28 ++++++++++++++++--- test/executable/executableTests.ts | 13 +++++++++ test/files/multiple-excludes/invalid.test.ts | 1 + test/files/multiple-excludes/invalid2.test.ts | 1 + test/files/multiple-excludes/tslint.json | 5 ++++ test/files/multiple-excludes/valid.test.ts | 1 + 6 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 test/files/multiple-excludes/invalid.test.ts create mode 100644 test/files/multiple-excludes/invalid2.test.ts create mode 100644 test/files/multiple-excludes/tslint.json create mode 100644 test/files/multiple-excludes/valid.test.ts diff --git a/src/tslint-cli.ts b/src/tslint-cli.ts index f28d8ea8831..c9bf6a1e492 100644 --- a/src/tslint-cli.ts +++ b/src/tslint-cli.ts @@ -45,7 +45,7 @@ interface Option { short?: string; // Commander will camelCase option names. name: keyof Argv | "rules-dir" | "formatters-dir" | "type-check"; - type: "string" | "boolean"; + type: "string" | "boolean" | "array"; describe: string; // Short, used for usage message description: string; // Long, used for `--help` } @@ -74,7 +74,7 @@ const options: Option[] = [ { short: "e", name: "exclude", - type: "string", + type: "array", describe: "exclude globs from path expansion", description: dedent` A filename or glob which indicates files to exclude from linting. @@ -187,9 +187,18 @@ const options: Option[] = [ }, ]; +function collect(val: string, memo: string[]) { + memo.push(val); + return memo; +} + for (const option of options) { - const commanderStr = optionUsageTag(option) + (option.type === "string" ? ` [${option.name}]` : ""); - commander.option(commanderStr, option.describe); + const commanderStr = optionUsageTag(option) + optionParam(option); + if (option.type === "array") { + commander.option(commanderStr, option.describe, collect, []); + } else { + commander.option(commanderStr, option.describe); + } } commander.on("--help", () => { @@ -249,3 +258,14 @@ run({ function optionUsageTag({short, name}: Option) { return short !== undefined ? `-${short}, --${name}` : `--${name}`; } + +function optionParam(option: Option) { + switch (option.type) { + case "string": + return ` [${option.name}]`; + case "array": + return ` <${option.name}>`; + case "boolean": + return ""; + } +} diff --git a/test/executable/executableTests.ts b/test/executable/executableTests.ts index 2ecd8cf9f3c..d8a707a9c3a 100644 --- a/test/executable/executableTests.ts +++ b/test/executable/executableTests.ts @@ -348,6 +348,19 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) { done(); }); }); + + it("can handle multiple '--exclude' globs", (done) => { + execCli( + [ + "-c", "test/files/multiple-excludes/tslint.json", + "--exclude", "'test/files/multiple-excludes/invalid.test.ts'", + "--exclude", "'test/files/multiple-excludes/invalid2*'", + "'test/files/multiple-excludes/**.ts'", + ], (err) => { + assert.isNull(err, "process should exit without an error"); + done(); + }); + }); }); describe("--type-check", () => { diff --git a/test/files/multiple-excludes/invalid.test.ts b/test/files/multiple-excludes/invalid.test.ts new file mode 100644 index 00000000000..827e83153cb --- /dev/null +++ b/test/files/multiple-excludes/invalid.test.ts @@ -0,0 +1 @@ +var foo = 42 \ No newline at end of file diff --git a/test/files/multiple-excludes/invalid2.test.ts b/test/files/multiple-excludes/invalid2.test.ts new file mode 100644 index 00000000000..7c6d6c73d3d --- /dev/null +++ b/test/files/multiple-excludes/invalid2.test.ts @@ -0,0 +1 @@ +module.exports = {} \ No newline at end of file diff --git a/test/files/multiple-excludes/tslint.json b/test/files/multiple-excludes/tslint.json new file mode 100644 index 00000000000..9b43fb139eb --- /dev/null +++ b/test/files/multiple-excludes/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "semicolon": [true, "always"] + } +} \ No newline at end of file diff --git a/test/files/multiple-excludes/valid.test.ts b/test/files/multiple-excludes/valid.test.ts new file mode 100644 index 00000000000..ead34d5767e --- /dev/null +++ b/test/files/multiple-excludes/valid.test.ts @@ -0,0 +1 @@ +var foo = 42; \ No newline at end of file