From a8d04b2db98f47d38a2ed9346b156b584cccc2c7 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 12 Sep 2019 13:23:16 +0200 Subject: [PATCH] Fix `Identifiers: NaN` diagnostic when having JSON SourceFiles This makes sure that the `identifierCount` and `nodeCount` properties are always initialized for `SourceFile` objects. --- src/compiler/parser.ts | 4 ++++ src/testRunner/unittests/programApi.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c22717bc8e3..f437ca697a8 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -770,7 +770,11 @@ namespace ts { fixupParentReferences(sourceFile); } + sourceFile.nodeCount = nodeCount; + sourceFile.identifierCount = identifierCount; + sourceFile.identifiers = identifiers; sourceFile.parseDiagnostics = parseDiagnostics; + const result = sourceFile as JsonSourceFile; clearState(); return result; diff --git a/src/testRunner/unittests/programApi.ts b/src/testRunner/unittests/programApi.ts index 51e1b8ffa50..5f5251afaf2 100644 --- a/src/testRunner/unittests/programApi.ts +++ b/src/testRunner/unittests/programApi.ts @@ -160,4 +160,22 @@ namespace ts { } } }); + + describe("unittests:: Program.getNodeCount / Program.getIdentifierCount", () => { + it("works on projects that have .json files", () => { + const main = new documents.TextDocument("/main.ts", 'export { version } from "./package.json";'); + const pkg = new documents.TextDocument("/package.json", '{"version": "1.0.0"}'); + + const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, pkg], cwd: "/" }); + const program = createProgram(["/main.ts"], { resolveJsonModule: true }, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed })); + + const json = program.getSourceFile("/package.json")!; + assert.equal(json.scriptKind, ScriptKind.JSON); + assert.isNumber(json.nodeCount); + assert.isNumber(json.identifierCount); + + assert.isNotNaN(program.getNodeCount()); + assert.isNotNaN(program.getIdentifierCount()); + }); + }); }