Skip to content

Commit 9d82dc5

Browse files
authored
🤖 Merge PR DefinitelyTyped#72112 Add types for the npm package "json-source-map". by @rehmsen
1 parent 0dc4a7d commit 9d82dc5

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Parses JSON string.
3+
*
4+
* @param source The JSON string to parse.
5+
* @param _ Unused - reveserved for reviver of JSON.parse.
6+
* @param options.bigint If true, parse large integers as BigInt
7+
* @returns Object with properties:
8+
* data: The parsed data.
9+
* pointers: An object where each key is a JSON pointer (RFC 6901), each
10+
* corresponding value is a Mapping object.
11+
*/
12+
export function parse(
13+
source: string,
14+
_?: unknown,
15+
options?: ParseOptions,
16+
): {
17+
data: unknown;
18+
pointers: Pointers;
19+
};
20+
21+
/**
22+
* Stringifies JavaScript data.
23+
*
24+
* @param data The data to convert to JSON.
25+
* @param _ Unused - reserved for replacer of JSON.stringify.
26+
* @param options If number or string, like the space parameter of
27+
* JSON.stringify, but if it is a string, it may only contain characters
28+
* space, tab ('\t'), caret return ('\r') and line feed ('\n') - using any
29+
* other character throws an exception.
30+
* @param options.space: Like above.
31+
* @param options.es6: If true, stringify ES6 Maps, Sets and Typed arrays as
32+
* JSON arrays.
33+
*/
34+
export function stringify(
35+
data: number | bigint | boolean | string | object,
36+
_?: unknown,
37+
options?: StringifyOptions | number | string,
38+
): {
39+
json: string;
40+
pointers: Pointers;
41+
};
42+
43+
export interface ParseOptions {
44+
/** Parse large integers as BigInt */
45+
bigint?: boolean;
46+
}
47+
48+
export interface StringifyOptions {
49+
/**
50+
* Like the space parameter of JSON.stringify, but if it is a string, it may
51+
* only contain characters space, tab ('\t'), caret return ('\r') and line
52+
* feed ('\n') - using any other character throws an exception.
53+
*/
54+
space?: number | string;
55+
56+
/** Stringify ES6 Maps, Sets and Typed arrays (as JSON arrays). */
57+
es6?: boolean;
58+
}
59+
60+
export interface Pointers {
61+
/**
62+
* Each key is a JSON pointer
63+
* ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)).
64+
*/
65+
[pointer: string]: Mapping;
66+
}
67+
68+
export interface Mapping {
69+
/**
70+
* Location of the beginning of the key in JSON string. This property is
71+
* only present if parent data is an object (rather than array).
72+
*/
73+
key?: Location;
74+
75+
/**
76+
* Location of the end of the key in JSON string. This property is only
77+
* present if parent data is an object.
78+
*/
79+
keyEnd?: Location;
80+
81+
/** Location of the beginning of the value in JSON string. */
82+
value: Location;
83+
84+
/** Location of the end of the value in JSON string. */
85+
valueEnd: Location;
86+
}
87+
88+
/** Location in the source string - zero-based numbers. */
89+
export interface Location {
90+
/** Line number in JSON file. */
91+
line: number;
92+
93+
/** Column number in JSON string (from the beginning of line. */
94+
column: number;
95+
96+
/** Column number in JSON string (from the beginning of line. */
97+
pos: number;
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { parse, stringify } from "json-source-map";
2+
3+
const stringifyResult = stringify({ foo: "bar" }, null, 2);
4+
// $ExpectType string
5+
const json = stringifyResult.json;
6+
// $ExpectType Pointers
7+
const stringifyPointers = stringifyResult.pointers;
8+
9+
const parseResult = parse("{ \"foo\": \"bar\" }");
10+
// $ExpectType unknown
11+
const data = parseResult.data;
12+
// $ExpectType Pointers
13+
const parsePointers = parseResult.pointers;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/json-source-map",
4+
"version": "0.6.9999",
5+
"projects": [
6+
"https://github.com/epoberezkin/json-source-map#readme"
7+
],
8+
"devDependencies": {
9+
"@types/json-source-map": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Ole Rehmsen",
14+
"githubUsername": "rehmsen"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"json-source-map-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)