Fix Identifiers: NaN diagnostic when having JSON SourceFiles

This makes sure that the `identifierCount` and `nodeCount` properties
are always initialized for `SourceFile` objects.
This commit is contained in:
Arpad Borsos 2019-09-12 13:23:16 +02:00
parent fd6fbdf7fe
commit a8d04b2db9
2 changed files with 22 additions and 0 deletions

View File

@ -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;

View File

@ -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());
});
});
}