Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Fix --project with --exclude (#2853)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and adidahiya committed Jun 1, 2017
1 parent 44b803f commit 88649b8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
25 changes: 16 additions & 9 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -171,9 +172,11 @@ async function runLinter(options: Options, logger: Logger): Promise<LintResult>
}

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);
Expand All @@ -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));
}
Expand Down
9 changes: 9 additions & 0 deletions test/executable/executableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 6 additions & 0 deletions test/files/tsconfig-allow-js/tslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"rules": {
"eofline": true,
"semicolon": [
true, "always"
]
},
"jsRules": {
"eofline": true,
"semicolon": [
Expand Down
1 change: 1 addition & 0 deletions test/files/tsconfig-allow-js/valid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 88649b8

Please sign in to comment.