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

Commit

Permalink
Skip linting JSON files entirely (#4001)
Browse files Browse the repository at this point in the history
This makes TSLint more friendly with the --resolveJsonModule compiler option.
  • Loading branch information
NaridaL authored and adidahiya committed Jan 10, 2019
1 parent c054cbb commit bb85d7d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
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"
]
}

0 comments on commit bb85d7d

Please sign in to comment.