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

CLI: handle directories passed with -p #2756

Merged
merged 2 commits into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ export class Runner {
let program: ts.Program | undefined;

if (this.options.project != null) {
if (!fs.existsSync(this.options.project)) {
const project = findTsconfig(this.options.project);
if (project === undefined) {
console.error("Invalid option for project: " + this.options.project);
return onComplete(1);
}
program = Linter.createProgram(this.options.project);
program = Linter.createProgram(project);
if (files.length === 0) {
files = Linter.getFileNames(program);
}
Expand All @@ -175,9 +176,6 @@ export class Runner {
// if not type checking, we don't need to pass in a program object
program = undefined;
}
} else if (this.options.typeCheck) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all static option validation is done in tslint-cli.ts
runner.ts should only be responsible to catch errors that occur at runtime

console.error("--project must be specified in order to enable type checking.");
return onComplete(1);
}

let ignorePatterns: string[] = [];
Expand Down Expand Up @@ -257,3 +255,16 @@ export class Runner {
});
}
}

function findTsconfig(project: string): string | undefined {
try {
const stats = fs.statSync(project); // throws if file does not exist
if (stats.isDirectory()) {
project = path.join(project, "tsconfig.json");
fs.accessSync(project); // throws if file does not exist
}
} catch (e) {
return undefined;
}
return project;
}
8 changes: 7 additions & 1 deletion src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ const processed = optimist
throw "Missing files";
}

// tslint:disable-next-line strict-boolean-expressions
if (argv["type-check"] && !argv.project) {
// tslint:disable-next-line:no-string-throw
throw "--project must be specified in order to enable type checking.";
}

// tslint:disable-next-line strict-boolean-expressions
if (argv.f) {
// throw a string, otherwise a call stack is printed for this message
Expand Down Expand Up @@ -214,7 +220,7 @@ tslint accepts the following commandline options:
this can be used to test custom rules.

-p, --project:
The location of a tsconfig.json file that will be used to determine which
The path or directory containing a tsconfig.json file that will be used to determine which
files will be linted.

--type-check
Expand Down
23 changes: 23 additions & 0 deletions test/executable/executableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});
});

it("can be passed a directory and defaults to tsconfig.json", (done) => {
execCli(["-c", "test/files/tsconfig-test/tslint.json", "--project", "test/files/tsconfig-test"], (err) => {
assert.isNull(err, "process should exit without an error");
done();
});
});

it("exits with error if passed a directory and there is not tsconfig.json", (done) => {
execCli(["-c", "test/files/tsconfig-test/tslint.json", "--project", "test/files"], (err) => {
assert.isNotNull(err, "process should exit with an error");
assert.strictEqual(err.code, 1, "error code should be 1");
done();
});
});

it("exits with error if passed directory does not exist", (done) => {
execCli(["-c", "test/files/tsconfig-test/tslint.json", "--project", "test/files/non-existant"], (err) => {
assert.isNotNull(err, "process should exit with an error");
assert.strictEqual(err.code, 1, "error code should be 1");
done();
});
});

it("exits with code 2 if both `tsconfig.json` and files arguments are passed and files contain lint errors", (done) => {
execCli(
[
Expand Down