diff --git a/src/linter.ts b/src/linter.ts index ab4ad44291a..7dd4e55d3dc 100644 --- a/src/linter.ts +++ b/src/linter.ts @@ -104,13 +104,16 @@ export class Linter { /** * Returns a list of source file names from a TypeScript program. This includes all referenced - * files and excludes declaration (".d.ts") files. + * files and excludes declaration (".d.ts") files, as well as JSON files, to avoid problems with + * `resolveJsonModule`. */ public static getFileNames(program: ts.Program): string[] { return mapDefined( program.getSourceFiles(), file => - file.fileName.endsWith(".d.ts") || program.isSourceFileFromExternalLibrary(file) + file.fileName.endsWith(".d.ts") || + file.fileName.endsWith(".json") || + program.isSourceFileFromExternalLibrary(file) ? undefined : file.fileName, ); diff --git a/test/executable/executableTests.ts b/test/executable/executableTests.ts index 64ea241b654..b4f89b89382 100644 --- a/test/executable/executableTests.ts +++ b/test/executable/executableTests.ts @@ -19,8 +19,10 @@ import * as cp from "child_process"; import * as fs from "fs"; import * as os from "os"; import * as path from "path"; +import * as semver from "semver"; import { Logger, Options, run, Status } from "../../src/runner"; import { denormalizeWinPath } from "../../src/utils"; +import { getNormalizedTypescriptVersion } from "../../src/verify/parse"; import { createTempFile } from "../utils"; // when tests are run with mocha from npm scripts CWD points to project root @@ -41,6 +43,8 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) { this.slow(3000); // the executable is JIT-ed each time it runs; avoid showing slowness warnings this.timeout(10000); + const tsVersion = getNormalizedTypescriptVersion(); + describe("Files", () => { it("exits with code 1 if no arguments passed", done => { execCli([], (err, stdout, stderr) => { @@ -596,6 +600,13 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) { fs.readFileSync("test/files/project-multiple-fixes/after.test.ts", "utf-8"), ); }).timeout(8000); + + if (semver.satisfies(tsVersion, ">=2.9")) { + it("does not try to parse JSON files with --resolveJsonModule with TS >= 2.9", async () => { + const status = await execRunner({project: "test/files/tsconfig-resolve-json-module/tsconfig.json"}); + assert.equal(status, Status.Ok, "process should exit without an error"); + }); + } }); describe("--type-check", () => { diff --git a/test/files/tsconfig-resolve-json-module/index.ts b/test/files/tsconfig-resolve-json-module/index.ts new file mode 100644 index 00000000000..8a3502284d4 --- /dev/null +++ b/test/files/tsconfig-resolve-json-module/index.ts @@ -0,0 +1,3 @@ +import settings from "./test.json"; +// tslint:disable-next-line:no-console +console.log(settings.dry); diff --git a/test/files/tsconfig-resolve-json-module/test.json b/test/files/tsconfig-resolve-json-module/test.json new file mode 100644 index 00000000000..a6696097cc2 --- /dev/null +++ b/test/files/tsconfig-resolve-json-module/test.json @@ -0,0 +1,4 @@ +{ + "dry": false, + "debug": false +} \ No newline at end of file diff --git a/test/files/tsconfig-resolve-json-module/tsconfig.json b/test/files/tsconfig-resolve-json-module/tsconfig.json new file mode 100644 index 00000000000..708459a0a56 --- /dev/null +++ b/test/files/tsconfig-resolve-json-module/tsconfig.json @@ -0,0 +1,8 @@ +{ + "include": ["index.ts"], + "compilerOptions": { + "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true + } +} diff --git a/test/files/tsconfig-resolve-json-module/tslint.json b/test/files/tsconfig-resolve-json-module/tslint.json new file mode 100644 index 00000000000..d3216dbe2ea --- /dev/null +++ b/test/files/tsconfig-resolve-json-module/tslint.json @@ -0,0 +1,5 @@ +{ + "extends": [ + "tslint:latest" + ] +}