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

Ignore .json files entirely to avoid errors due to tslint parsing json as typescript. #4001

Merged
merged 4 commits into from
Jan 10, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
11 changes: 11 additions & 0 deletions test/executable/executableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand Down Expand Up @@ -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", () => {
Expand Down
3 changes: 3 additions & 0 deletions test/files/tsconfig-resolve-json-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import settings from "./test.json";
// tslint:disable-next-line:no-console
console.log(settings.dry);
4 changes: 4 additions & 0 deletions test/files/tsconfig-resolve-json-module/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dry": false,
"debug": false
}
8 changes: 8 additions & 0 deletions test/files/tsconfig-resolve-json-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["index.ts"],
"compilerOptions": {
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
}
}
5 changes: 5 additions & 0 deletions test/files/tsconfig-resolve-json-module/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"tslint:latest"
]
}