Skip to content

Commit

Permalink
Merge pull request #33390 from Swatinem/identifiers-nan
Browse files Browse the repository at this point in the history
Fix `Identifiers: NaN` diagnostic when having JSON SourceFiles
  • Loading branch information
sheetalkamat authored Sep 12, 2019
2 parents 0cf00fa + a8d04b2 commit bc7bde3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/testRunner/unittests/programApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});
}

0 comments on commit bc7bde3

Please sign in to comment.