Skip to content

Commit 38e8ed5

Browse files
committed
Add lib types for JSON.rawJSON, JSON.isRawJSON, and reviver context
Port proposal-json-parse-with-source (ES2025) lib typings onto the Go tree layout: - tsc/internal/bundled/source/es2025.json.d.ts (+ generated lib/embed) - reference from es2025 umbrella + libs.json + LibMap (enummaps.go) - compiler test and baselines Preserves RawJSON, JSON.rawJSON, JSON.isRawJSON, and optional context.source on JSON.parse reviver. Fixes #61330 Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
1 parent e9e4774 commit 38e8ed5

12 files changed

Lines changed: 694 additions & 133 deletions

File tree

tsc/internal/bundled/embed_generated.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsc/internal/bundled/libs/lib.es2025.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ and limitations under the License.
2121
/// <reference lib="es2025.iterator" />
2222
/// <reference lib="es2025.promise" />
2323
/// <reference lib="es2025.regexp" />
24+
/// <reference lib="es2025.json" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*! *****************************************************************************
2+
Copyright (c) Microsoft Corporation. All rights reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4+
this file except in compliance with the License. You may obtain a copy of the
5+
License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10+
MERCHANTABILITY OR NON-INFRINGEMENT.
11+
12+
See the Apache Version 2.0 License for specific language governing permissions
13+
and limitations under the License.
14+
***************************************************************************** */
15+
16+
17+
/**
18+
* Represents a "raw JSON" object created by `JSON.rawJSON()`.
19+
*
20+
* Raw JSON objects are frozen, null-prototype objects that carry pre-serialized
21+
* JSON text. When encountered by `JSON.stringify()`, the `rawJSON` property
22+
* value is emitted verbatim instead of the usual serialization.
23+
*
24+
* @see {@link https://tc39.es/proposal-json-parse-with-source/ TC39 proposal-json-parse-with-source}
25+
*/
26+
interface RawJSON {
27+
readonly rawJSON: string;
28+
}
29+
30+
interface JSON {
31+
/**
32+
* Converts a JavaScript Object Notation (JSON) string into an object.
33+
* @param text A valid JSON string.
34+
* @param reviver A function that transforms the results. This function is called for each member of the object.
35+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
36+
* For each value, the reviver also receives a `context` object. When the property is unmodified and its value is
37+
* primitive, `context` has a `source` property containing the original JSON text of that value.
38+
*/
39+
parse(text: string, reviver: (this: any, key: string, value: any, context: { source?: string }) => any): any;
40+
41+
/**
42+
* Creates a "raw JSON" object containing a piece of JSON text.
43+
* When serialized with `JSON.stringify()`, the raw text is emitted verbatim.
44+
* @param text A valid JSON string representing a primitive value (string, number, boolean, or null).
45+
* @throws {SyntaxError} If `text` is not valid JSON or represents an object or array.
46+
*/
47+
rawJSON(text: string): RawJSON;
48+
49+
/**
50+
* Returns whether the provided value is a raw JSON object created by `JSON.rawJSON()`.
51+
* @param value The value to test.
52+
*/
53+
isRawJSON(value: unknown): value is RawJSON;
54+
}

tsc/internal/bundled/libs_generated.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/// <reference lib="es2024" />
2-
/// <reference lib="es2025.collection" />
3-
/// <reference lib="es2025.float16" />
4-
/// <reference lib="es2025.intl" />
5-
/// <reference lib="es2025.iterator" />
6-
/// <reference lib="es2025.promise" />
7-
/// <reference lib="es2025.regexp" />
1+
/// <reference lib="es2024" />
2+
/// <reference lib="es2025.collection" />
3+
/// <reference lib="es2025.float16" />
4+
/// <reference lib="es2025.intl" />
5+
/// <reference lib="es2025.iterator" />
6+
/// <reference lib="es2025.promise" />
7+
/// <reference lib="es2025.regexp" />
8+
/// <reference lib="es2025.json" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Represents a "raw JSON" object created by `JSON.rawJSON()`.
3+
*
4+
* Raw JSON objects are frozen, null-prototype objects that carry pre-serialized
5+
* JSON text. When encountered by `JSON.stringify()`, the `rawJSON` property
6+
* value is emitted verbatim instead of the usual serialization.
7+
*
8+
* @see {@link https://tc39.es/proposal-json-parse-with-source/ TC39 proposal-json-parse-with-source}
9+
*/
10+
interface RawJSON {
11+
readonly rawJSON: string;
12+
}
13+
14+
interface JSON {
15+
/**
16+
* Converts a JavaScript Object Notation (JSON) string into an object.
17+
* @param text A valid JSON string.
18+
* @param reviver A function that transforms the results. This function is called for each member of the object.
19+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
20+
* For each value, the reviver also receives a `context` object. When the property is unmodified and its value is
21+
* primitive, `context` has a `source` property containing the original JSON text of that value.
22+
*/
23+
parse(text: string, reviver: (this: any, key: string, value: any, context: { source?: string }) => any): any;
24+
25+
/**
26+
* Creates a "raw JSON" object containing a piece of JSON text.
27+
* When serialized with `JSON.stringify()`, the raw text is emitted verbatim.
28+
* @param text A valid JSON string representing a primitive value (string, number, boolean, or null).
29+
* @throws {SyntaxError} If `text` is not valid JSON or represents an object or array.
30+
*/
31+
rawJSON(text: string): RawJSON;
32+
33+
/**
34+
* Returns whether the provided value is a raw JSON object created by `JSON.rawJSON()`.
35+
* @param value The value to test.
36+
*/
37+
isRawJSON(value: unknown): value is RawJSON;
38+
}

0 commit comments

Comments
 (0)