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

Commit

Permalink
report errors from parsing and validating tsconfig.json (#3410)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and adidahiya committed Oct 31, 2017
1 parent 2d0a49b commit 50f8286
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,32 @@ class Linter {
* Creates a TypeScript program object from a tsconfig.json file path and optional project directory.
*/
public static createProgram(configFile: string, projectDirectory: string = path.dirname(configFile)): ts.Program {
const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
const config = ts.readConfigFile(configFile, ts.sys.readFile);
if (config.error !== undefined) {
throw new FatalError(ts.formatDiagnostics([config.error], {
getCanonicalFileName: (f) => f,
getCurrentDirectory: process.cwd,
getNewLine: () => "\n",
}));
}
const parseConfigHost: ts.ParseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
readFile: (file) => fs.readFileSync(file, "utf8"),
useCaseSensitiveFileNames: true,
};
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, path.resolve(projectDirectory), {noEmit: true});
const parsed = ts.parseJsonConfigFileContent(config.config, parseConfigHost, path.resolve(projectDirectory), {noEmit: true});
if (parsed.errors !== undefined) {
// ignore warnings and 'TS18003: No inputs were found in config file ...'
const errors = parsed.errors.filter((d) => d.category === ts.DiagnosticCategory.Error && d.code !== 18003);
if (errors.length !== 0) {
throw new FatalError(ts.formatDiagnostics(errors, {
getCanonicalFileName: (f) => f,
getCurrentDirectory: process.cwd,
getNewLine: () => "\n",
}));
}
}
const host = ts.createCompilerHost(parsed.options, true);
const program = ts.createProgram(parsed.fileNames, parsed.options, host);

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

it("reports errors from parsing tsconfig.json", (done) => {
execCli(
["-p", "test/files/tsconfig-invalid/syntax-error.json"],
(err, _stdout, stderr) => {
assert.isNotNull(err, "process should exit with an error");
assert.equal(err.code, 1, "exit code should be 1");
assert.include(stderr, "error TS");
done();
});
});

it("reports errors from validating tsconfig.json", (done) => {
execCli(
["-p", "test/files/tsconfig-invalid/empty-files.json"],
(err, _stdout, stderr) => {
assert.isNotNull(err, "process should exit with an error");
assert.equal(err.code, 1, "exit code should be 1");
assert.include(stderr, "error TS");
done();
});
});

it("does not report an error if tsconfig.json matches no files", (done) => {
execCli(
["-p", "test/files/tsconfig-invalid/no-match.json"],
(err) => {
assert.isNull(err, "process should exit without an error");
done();
});
});

it("can extend `tsconfig.json` with relative path", (done) => {
execCli(
["-c", "test/files/tsconfig-extends-relative/tslint-ok.json", "-p",
Expand Down
3 changes: 3 additions & 0 deletions test/files/tsconfig-invalid/empty-files.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": []
}
5 changes: 5 additions & 0 deletions test/files/tsconfig-invalid/no-match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": [
"*.js"
]
}
1 change: 1 addition & 0 deletions test/files/tsconfig-invalid/syntax-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,

0 comments on commit 50f8286

Please sign in to comment.