Add lib types for JSON.rawJSON, JSON.isRawJSON, and reviver context - #63248
Add lib types for JSON.rawJSON, JSON.isRawJSON, and reviver context#63248Vedant Madane (VedantMadane) wants to merge 8 commits into
Conversation
Adds esnext.json.d.ts with type definitions for the Stage 4 TC39
proposal-json-parse-with-source (shipped in all major browsers and
Node.js 22+):
- JSON.rawJSON(text): creates a RawJSON object for lossless serialization
- JSON.isRawJSON(value): type guard narrowing to RawJSON
- JSON.parse reviver context: third parameter with { source: string }
Fixes microsoft#61330
The proposal-json-parse-with-source is Stage 4 and part of ES2025, so the lib should live in es2025.json.d.ts, not esnext.json.d.ts.
It isn't, MDN is wrong on this one. It has stage 4 approval, but is not yet in the latest draft: tc39/ecma262#3714 |
|
With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies). Next steps for PRs:
|
|
Reopening as this is still valid (minus commandlineParser.ts) |
RyanCavanaugh noted this PR is still valid minus commandlineParser.ts. Revert commandLineParser.ts to match upstream main.
There was a problem hiding this comment.
Pull request overview
Adds an ES2025-by-feature lib file to type the now-standard JSON.rawJSON, JSON.isRawJSON, and the JSON.parse reviver “source context” capability, and wires it into the es2025 lib plus compiler test coverage.
Changes:
- Introduces
src/lib/es2025.json.d.tsdefiningRawJSONand augmentingJSONwithrawJSON,isRawJSON, and aJSON.parseoverload with acontextparameter. - References the new lib from
src/lib/es2025.d.tsand registerses2025.jsoninsrc/lib/libs.json. - Adds a compiler test (
jsonParseWithSource.ts) and accepted baselines covering all three APIs.
Reviewed changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/lib/es2025.json.d.ts |
Adds RawJSON and JSON API typings for the ES2025 JSON additions. |
src/lib/es2025.d.ts |
Includes the new es2025.json lib in the es2025 umbrella lib. |
src/lib/libs.json |
Registers es2025.json as an available by-feature lib. |
tests/cases/compiler/jsonParseWithSource.ts |
Compiler test exercising rawJSON, isRawJSON, and reviver context. |
tests/baselines/reference/jsonParseWithSource.types |
Baseline for type printing of the new APIs. |
tests/baselines/reference/jsonParseWithSource.symbols |
Baseline for symbol resolution including the new lib. |
tests/baselines/reference/jsonParseWithSource.js |
Baseline for JS emit. |
Andrii Furmanets (afurm)
left a comment
There was a problem hiding this comment.
Approach / Design (nits):
-
JSON.rawJSON(text: string): RawJSON— The JSDoc documents the happy path but doesn't mention@throwsfor invalid input. Per the spec,JSON.rawJSONthrows aSyntaxErrorwhentextis not a valid JSON primitive (not a string, number, boolean, or null) or is an object/array. Adding@throws {SyntaxError}would align with the existingJSON.parseJSDoc and help consumers handle error cases. -
Test coverage for reviver context — The test file has one reviver test using a plain object. It might be worth adding cases for:
- Array elements (
"arr": [1, 2, 3]) — the context source for each element - Nested objects to verify the context is available at all levels
- Array elements (
These are minor suggestions; the type definitions themselves look correct and match the Stage 4 proposal. The infrastructure changes (libs.json, commandLineParser.ts, es2025.d.ts) all look routine and correct.
|
Andrii Furmanets (@afurm) future automated comments will result in a block; any automated activity we want to occur in this repo we will set up ourselves or already have |
|
Ryan Cavanaugh (@RyanCavanaugh) the |
- Add test cases for array elements and nested structures. - Ensure JSDoc aligns with existing patterns. Signed-off-by: Vedant Madane <vedantnm@gmail.com>
| * For primitive values the reviver also receives a `context` object whose `source` property is the original JSON | ||
| * text of that value. | ||
| */ | ||
| parse(text: string, reviver: (this: any, key: string, value: any, context: { source: string }) => any): any; |
There was a problem hiding this comment.
The source property should be optional.
For each value produced by the parse,
reviveris called with three arguments (the associated property key, the value, and a context object). If the property is unmodified and its value is primitive, the provided context object has a "source" property containing source text of the corresponding Parse Node.
There was a problem hiding this comment.
Good catch — updated. context.source is now optional (source?: string), matching the spec: it's only present when the property is unmodified and the value is primitive. JSDoc, compiler tests, and baselines updated accordingly.
Per the ECMAScript spec, context.source is only present when the property is unmodified and its value is primitive. Update the es2025.json typings and compiler tests/baselines accordingly. Also re-add es2025.json to libMap so /// <reference lib="es2025.json" /> resolves (required for the es2025 umbrella lib to include the definitions). Addresses review feedback on microsoft#63248.
Adds
es2025.json.d.tswith type definitions for the Stage 4 TC39 proposal-json-parse-with-source which is part of ES2025 and has shipped in all major browsers and Node.js 22+.Summary
JSON.rawJSON(text)- creates a frozenRawJSONobject for lossless JSON serialization (e.g. preserving large-integer precision)JSON.isRawJSON(value)- type-guard that narrows toRawJSONJSON.parserevivercontext- overload whose reviver receives a third parameter{ source: string }containing the original JSON source text of each primitive valueNew files
src/lib/es2025.json.d.tsRawJSONinterface +JSONaugmentationstests/cases/compiler/jsonParseWithSource.tstests/baselines/reference/jsonParseWithSource.*Modified files
src/lib/es2025.d.ts/// <reference lib="es2025.json" />src/lib/libs.jsones2025.jsonsrc/compiler/commandLineParser.tses2025.jsonto its.d.tsFixes #61330