diff --git a/package.json b/package.json index d878300f45a..b8871aac121 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "commander": "^2.9.0", "diff": "^3.2.0", "glob": "^7.1.1", + "minimatch": "^3.0.4", "resolve": "^1.3.2", "semver": "^5.3.0", "tslib": "^1.7.1", @@ -57,6 +58,7 @@ "@types/diff": "^3.2.0", "@types/glob": "^5.0.30", "@types/js-yaml": "^3.5.29", + "@types/minimatch": "^2.0.29", "@types/mocha": "^2.2.35", "@types/node": "^7.0.16", "@types/resolve": "^0.0.4", diff --git a/src/runner.ts b/src/runner.ts index f4c44d3be9b..5ae114e9d6b 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -19,6 +19,7 @@ import * as fs from "fs"; import * as glob from "glob"; +import { Minimatch } from "minimatch"; import * as path from "path"; import * as ts from "typescript"; @@ -171,9 +172,11 @@ async function runLinter(options: Options, logger: Logger): Promise } function resolveFilesAndProgram({ files, project, exclude, outputAbsolutePaths }: Options): { files: string[]; program?: ts.Program } { - // if both files and tsconfig are present, use files + // remove single quotes which break matching on Windows when glob is passed in single quotes + const ignore = arrayify(exclude).map(trimSingleQuotes); + if (project === undefined) { - return { files: resolveFiles() }; + return { files: resolveGlobs(files, ignore, outputAbsolutePaths) }; } const projectPath = findTsconfig(project); @@ -182,17 +185,21 @@ function resolveFilesAndProgram({ files, project, exclude, outputAbsolutePaths } } const program = Linter.createProgram(projectPath); - return { files: files === undefined || files.length === 0 ? Linter.getFileNames(program) : resolveFiles(), program }; - - function resolveFiles(): string[] { - return resolveGlobs(files, exclude, outputAbsolutePaths === true); + let filesFound: string[]; + if (files === undefined || files.length === 0) { + filesFound = Linter.getFileNames(program); + if (ignore.length !== 0) { + const mm = ignore.map((pattern) => new Minimatch(path.resolve(pattern))); + filesFound = filesFound.filter((file) => !mm.some((matcher) => matcher.match(file))); + } + } else { + filesFound = resolveGlobs(files, ignore, outputAbsolutePaths); } + return { files: filesFound, program }; } -function resolveGlobs(files: string[] | undefined, exclude: Options["exclude"], outputAbsolutePaths: boolean): string[] { - const ignore = arrayify(exclude).map(trimSingleQuotes); +function resolveGlobs(files: string[] | undefined, ignore: string[], outputAbsolutePaths?: boolean): string[] { return flatMap(arrayify(files), (file) => - // remove single quotes which break matching on Windows when glob is passed in single quotes glob.sync(trimSingleQuotes(file), { ignore, nodir: true })) .map((file) => outputAbsolutePaths ? path.resolve(file) : path.relative(process.cwd(), file)); } diff --git a/test/executable/executableTests.ts b/test/executable/executableTests.ts index f7f890ef993..2ecd8cf9f3c 100644 --- a/test/executable/executableTests.ts +++ b/test/executable/executableTests.ts @@ -339,6 +339,15 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) { done(); }); }); + + it("works with '--exclude'", (done) => { + execCli( + [ "-p", "test/files/tsconfig-allow-js/tsconfig.json", "-e", "'test/files/tsconfig-allow-js/testfile.test.js'"], + (err) => { + assert.isNull(err, "process should exit without an error"); + done(); + }); + }); }); describe("--type-check", () => { diff --git a/test/files/tsconfig-allow-js/tslint.json b/test/files/tsconfig-allow-js/tslint.json index 8b3ddc16690..9f90c6f0336 100644 --- a/test/files/tsconfig-allow-js/tslint.json +++ b/test/files/tsconfig-allow-js/tslint.json @@ -1,4 +1,10 @@ { + "rules": { + "eofline": true, + "semicolon": [ + true, "always" + ] + }, "jsRules": { "eofline": true, "semicolon": [ diff --git a/test/files/tsconfig-allow-js/valid.test.ts b/test/files/tsconfig-allow-js/valid.test.ts new file mode 100644 index 00000000000..cb0ff5c3b54 --- /dev/null +++ b/test/files/tsconfig-allow-js/valid.test.ts @@ -0,0 +1 @@ +export {}; diff --git a/yarn.lock b/yarn.lock index 7a1cdee20f5..618624a3c14 100644 --- a/yarn.lock +++ b/yarn.lock @@ -35,7 +35,7 @@ version "3.5.30" resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.5.30.tgz#f555118c022318e57e36d803379cb8ee38ee20a7" -"@types/minimatch@*": +"@types/minimatch@*", "@types/minimatch@^2.0.29": version "2.0.29" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a" @@ -1475,7 +1475,7 @@ tslint@latest: tslib "^1.6.0" tsutils "^2.0.0" -tsutils@2, tsutils@^2.0.0: +tsutils@^2.0.0, tsutils@^2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.3.0.tgz#96e661d7c2363f31adc8992ac67bbe7b7fc175e5"