Skip to content

Commit a8d04b2

Browse files
committed
Fix Identifiers: NaN diagnostic when having JSON SourceFiles
This makes sure that the `identifierCount` and `nodeCount` properties are always initialized for `SourceFile` objects.
1 parent fd6fbdf commit a8d04b2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/compiler/parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,11 @@ namespace ts {
770770
fixupParentReferences(sourceFile);
771771
}
772772

773+
sourceFile.nodeCount = nodeCount;
774+
sourceFile.identifierCount = identifierCount;
775+
sourceFile.identifiers = identifiers;
773776
sourceFile.parseDiagnostics = parseDiagnostics;
777+
774778
const result = sourceFile as JsonSourceFile;
775779
clearState();
776780
return result;

src/testRunner/unittests/programApi.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,22 @@ namespace ts {
160160
}
161161
}
162162
});
163+
164+
describe("unittests:: Program.getNodeCount / Program.getIdentifierCount", () => {
165+
it("works on projects that have .json files", () => {
166+
const main = new documents.TextDocument("/main.ts", 'export { version } from "./package.json";');
167+
const pkg = new documents.TextDocument("/package.json", '{"version": "1.0.0"}');
168+
169+
const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false, { documents: [main, pkg], cwd: "/" });
170+
const program = createProgram(["/main.ts"], { resolveJsonModule: true }, new fakes.CompilerHost(fs, { newLine: NewLineKind.LineFeed }));
171+
172+
const json = program.getSourceFile("/package.json")!;
173+
assert.equal(json.scriptKind, ScriptKind.JSON);
174+
assert.isNumber(json.nodeCount);
175+
assert.isNumber(json.identifierCount);
176+
177+
assert.isNotNaN(program.getNodeCount());
178+
assert.isNotNaN(program.getIdentifierCount());
179+
});
180+
});
163181
}

0 commit comments

Comments
 (0)