@openrewrite/rewrite 8.100.0 → 8.100.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/java/rpc.d.ts.map +1 -1
- package/dist/java/rpc.js +6 -1
- package/dist/java/rpc.js.map +1 -1
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +3 -0
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/binding.d.ts.map +1 -1
- package/dist/javascript/binding.js +8 -1
- package/dist/javascript/binding.js.map +1 -1
- package/dist/javascript/comparator.js +2 -1
- package/dist/javascript/comparator.js.map +1 -1
- package/dist/javascript/format/format.d.ts.map +1 -1
- package/dist/javascript/format/format.js +3 -2
- package/dist/javascript/format/format.js.map +1 -1
- package/dist/javascript/format/minimum-viable-spacing-visitor.js +3 -3
- package/dist/javascript/format/minimum-viable-spacing-visitor.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +11 -17
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/print.d.ts +4 -0
- package/dist/javascript/print.d.ts.map +1 -1
- package/dist/javascript/print.js +82 -20
- package/dist/javascript/print.js.map +1 -1
- package/dist/javascript/rpc.js +2 -0
- package/dist/javascript/rpc.js.map +1 -1
- package/dist/javascript/scope.d.ts.map +1 -1
- package/dist/javascript/scope.js +4 -1
- package/dist/javascript/scope.js.map +1 -1
- package/dist/javascript/templating/comparator.d.ts +9 -0
- package/dist/javascript/templating/comparator.d.ts.map +1 -1
- package/dist/javascript/templating/comparator.js +21 -0
- package/dist/javascript/templating/comparator.js.map +1 -1
- package/dist/javascript/templating/debug.d.ts.map +1 -1
- package/dist/javascript/templating/debug.js +2 -0
- package/dist/javascript/templating/debug.js.map +1 -1
- package/dist/javascript/templating/precedence.d.ts.map +1 -1
- package/dist/javascript/templating/precedence.js +36 -39
- package/dist/javascript/templating/precedence.js.map +1 -1
- package/dist/javascript/templating/types.d.ts +4 -4
- package/dist/javascript/tree-debug.d.ts.map +1 -1
- package/dist/javascript/tree-debug.js +6 -3
- package/dist/javascript/tree-debug.js.map +1 -1
- package/dist/javascript/tree.d.ts +4 -1
- package/dist/javascript/tree.d.ts.map +1 -1
- package/dist/javascript/tree.js +2 -0
- package/dist/javascript/tree.js.map +1 -1
- package/dist/javascript/type-mapping.d.ts +1 -0
- package/dist/javascript/type-mapping.d.ts.map +1 -1
- package/dist/javascript/type-mapping.js +12 -1
- package/dist/javascript/type-mapping.js.map +1 -1
- package/dist/json/edit.d.ts +150 -0
- package/dist/json/edit.d.ts.map +1 -0
- package/dist/json/edit.js +667 -0
- package/dist/json/edit.js.map +1 -0
- package/dist/json/index.d.ts +1 -0
- package/dist/json/index.d.ts.map +1 -1
- package/dist/json/index.js +1 -0
- package/dist/json/index.js.map +1 -1
- package/dist/parser.d.ts +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/print.d.ts +16 -0
- package/dist/print.d.ts.map +1 -1
- package/dist/print.js +33 -0
- package/dist/print.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/rpc/request/get-languages.d.ts.map +1 -1
- package/dist/rpc/request/get-languages.js +1 -0
- package/dist/rpc/request/get-languages.js.map +1 -1
- package/dist/rpc/request/parse.d.ts.map +1 -1
- package/dist/rpc/request/parse.js +4 -0
- package/dist/rpc/request/parse.js.map +1 -1
- package/dist/rpc/server.d.ts +1 -0
- package/dist/rpc/server.d.ts.map +1 -1
- package/dist/rpc/server.js +1 -0
- package/dist/rpc/server.js.map +1 -1
- package/dist/xml/assertions.d.ts +4 -0
- package/dist/xml/assertions.d.ts.map +1 -0
- package/dist/xml/assertions.js +31 -0
- package/dist/xml/assertions.js.map +1 -0
- package/dist/xml/index.d.ts +8 -0
- package/dist/xml/index.d.ts.map +1 -0
- package/dist/xml/index.js +39 -0
- package/dist/xml/index.js.map +1 -0
- package/dist/xml/namespaces.d.ts +36 -0
- package/dist/xml/namespaces.d.ts.map +1 -0
- package/dist/xml/namespaces.js +98 -0
- package/dist/xml/namespaces.js.map +1 -0
- package/dist/xml/parser.d.ts +15 -0
- package/dist/xml/parser.d.ts.map +1 -0
- package/dist/xml/parser.js +677 -0
- package/dist/xml/parser.js.map +1 -0
- package/dist/xml/print.d.ts +2 -0
- package/dist/xml/print.d.ts.map +1 -0
- package/dist/xml/print.js +209 -0
- package/dist/xml/print.js.map +1 -0
- package/dist/xml/rpc.d.ts +2 -0
- package/dist/xml/rpc.d.ts.map +1 -0
- package/dist/xml/rpc.js +313 -0
- package/dist/xml/rpc.js.map +1 -0
- package/dist/xml/tree.d.ts +244 -0
- package/dist/xml/tree.d.ts.map +1 -0
- package/dist/xml/tree.js +265 -0
- package/dist/xml/tree.js.map +1 -0
- package/dist/xml/visitor.d.ts +35 -0
- package/dist/xml/visitor.d.ts.map +1 -0
- package/dist/xml/visitor.js +203 -0
- package/dist/xml/visitor.js.map +1 -0
- package/package.json +5 -1
- package/src/java/rpc.ts +6 -1
- package/src/javascript/add-import.ts +3 -0
- package/src/javascript/binding.ts +10 -1
- package/src/javascript/comparator.ts +2 -1
- package/src/javascript/format/format.ts +3 -2
- package/src/javascript/format/minimum-viable-spacing-visitor.ts +3 -3
- package/src/javascript/parser.ts +12 -17
- package/src/javascript/print.ts +85 -23
- package/src/javascript/rpc.ts +2 -0
- package/src/javascript/scope.ts +4 -1
- package/src/javascript/templating/comparator.ts +23 -0
- package/src/javascript/templating/debug.ts +1 -0
- package/src/javascript/templating/precedence.ts +17 -20
- package/src/javascript/templating/types.ts +4 -4
- package/src/javascript/tree-debug.ts +7 -3
- package/src/javascript/tree.ts +4 -1
- package/src/javascript/type-mapping.ts +15 -1
- package/src/json/edit.ts +750 -0
- package/src/json/index.ts +1 -0
- package/src/parser.ts +1 -1
- package/src/print.ts +42 -0
- package/src/rpc/request/get-languages.ts +1 -0
- package/src/rpc/request/parse.ts +3 -0
- package/src/rpc/server.ts +1 -0
- package/src/xml/assertions.ts +28 -0
- package/src/xml/index.ts +23 -0
- package/src/xml/namespaces.ts +109 -0
- package/src/xml/parser.ts +692 -0
- package/src/xml/print.ts +225 -0
- package/src/xml/rpc.ts +370 -0
- package/src/xml/tree.ts +455 -0
- package/src/xml/visitor.ts +227 -0
package/src/json/edit.ts
ADDED
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 the original author or authors.
|
|
3
|
+
* <p>
|
|
4
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
* <p>
|
|
8
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
9
|
+
* <p>
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import {emptyMarkers} from "../markers";
|
|
17
|
+
import {randomId} from "../uuid";
|
|
18
|
+
import {Cursor} from "../tree";
|
|
19
|
+
import {
|
|
20
|
+
detectIndent,
|
|
21
|
+
emptySpace,
|
|
22
|
+
getMemberKeyName,
|
|
23
|
+
isArray,
|
|
24
|
+
isLiteral,
|
|
25
|
+
isMember,
|
|
26
|
+
isObject,
|
|
27
|
+
Json,
|
|
28
|
+
rightPadded,
|
|
29
|
+
space
|
|
30
|
+
} from "./tree";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Pure, immutable helpers for reading and editing JSON objects while keeping a document's formatting.
|
|
34
|
+
* Every editing function returns its input (the same reference) when there is nothing to change.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
export type JsonRoot = Json.Document | Json.Value;
|
|
38
|
+
|
|
39
|
+
export interface LayoutOptions {
|
|
40
|
+
/**
|
|
41
|
+
* The indentation unit, e.g. `" "`, used to lay out a member or element added to an empty object or array,
|
|
42
|
+
* which has no sibling to copy the indentation from. Without it, the addition stays on the same line: `{"a": 1}`,
|
|
43
|
+
* `["a"]`. The `*At` functions default it to the document's {@link detectIndent} when given a document laid out
|
|
44
|
+
* over several lines.
|
|
45
|
+
*/
|
|
46
|
+
indent?: string;
|
|
47
|
+
/** How deep the object or array is nested, the root being 0. Used together with {@link indent}. */
|
|
48
|
+
depth?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface SetMemberOptions extends LayoutOptions {
|
|
52
|
+
/**
|
|
53
|
+
* Where a new member goes: `"last"` (the default), `"first"`, or `"sorted"`, which places it
|
|
54
|
+
* before the first member whose key sorts after it, the way npm keeps its dependency sections.
|
|
55
|
+
* A member that already exists keeps its place.
|
|
56
|
+
*/
|
|
57
|
+
position?: "first" | "last" | "sorted";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AppendElementOptions extends LayoutOptions {
|
|
61
|
+
/** Whether to leave the array unchanged if one of its elements already holds the same data as the value. */
|
|
62
|
+
unique?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SetAtOptions extends SetMemberOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Where the objects that {@link createMissing} creates go among their siblings: `"last"` (the default),
|
|
68
|
+
* `"first"`, or `"sorted"`. {@link SetMemberOptions.position} applies to the last key of the path only.
|
|
69
|
+
*/
|
|
70
|
+
parentPosition?: "first" | "last" | "sorted";
|
|
71
|
+
/** Whether to create the objects missing along the path. Otherwise a missing step leaves the root unchanged. */
|
|
72
|
+
createMissing?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RemoveAtOptions {
|
|
76
|
+
/** Whether to also remove the objects along the path that the removal leaves empty. The root object always stays. */
|
|
77
|
+
pruneEmptyParents?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether an object or array holds nothing, which the LST represents as a single `Json.Empty`.
|
|
82
|
+
*/
|
|
83
|
+
export function isEmptyContainer(container: Json.Object | Json.Array): boolean {
|
|
84
|
+
const elements: Json.RightPadded<Json>[] = isObject(container) ? container.members : container.values;
|
|
85
|
+
return elements.length === 0 || (elements.length === 1 && elements[0].element.kind === Json.Kind.Empty);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The members of an object, without the `Json.Empty` of an empty object or a trailing comma.
|
|
90
|
+
*/
|
|
91
|
+
export function membersOf(obj: Json.Object): Json.Member[] {
|
|
92
|
+
return obj.members.map(m => m.element).filter(isMember);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The first member of an object with the given key, matched by {@link getMemberKeyName}.
|
|
97
|
+
*/
|
|
98
|
+
export function findMember(obj: Json.Object, key: string): Json.Member | undefined {
|
|
99
|
+
return membersOf(obj).find(m => getMemberKeyName(m) === key);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The value at a path of member keys, or `undefined` if a step is missing or isn't an object.
|
|
104
|
+
*/
|
|
105
|
+
export function valueAt(root: JsonRoot, path: readonly string[]): Json.Value | undefined {
|
|
106
|
+
let current: Json.Value | undefined = root.kind === Json.Kind.Document ? (root as Json.Document).value : root as Json.Value;
|
|
107
|
+
for (const key of path) {
|
|
108
|
+
if (current === undefined || !isObject(current)) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
current = findMember(current, key)?.value;
|
|
112
|
+
}
|
|
113
|
+
return current;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The plain JavaScript value of a literal, array, object or document, or `undefined` where it isn't plain data.
|
|
118
|
+
* A literal is read from its source, so it doesn't depend on how a parser or RPC typed its `value`.
|
|
119
|
+
*/
|
|
120
|
+
export function toPlain(json: Json): unknown {
|
|
121
|
+
if (json.kind === Json.Kind.Document) {
|
|
122
|
+
return toPlain((json as Json.Document).value);
|
|
123
|
+
}
|
|
124
|
+
if (isLiteral(json)) {
|
|
125
|
+
if (!json.source.startsWith("'")) {
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(json.source);
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return json.value;
|
|
132
|
+
}
|
|
133
|
+
if (isArray(json)) {
|
|
134
|
+
const values = json.values.map(v => v.element).filter(v => v.kind !== Json.Kind.Empty).map(toPlain);
|
|
135
|
+
return values.includes(undefined) ? undefined : values;
|
|
136
|
+
}
|
|
137
|
+
if (isObject(json)) {
|
|
138
|
+
const entries: [string, unknown][] = [];
|
|
139
|
+
for (const m of membersOf(json)) {
|
|
140
|
+
const key = getMemberKeyName(m);
|
|
141
|
+
const value = toPlain(m.value);
|
|
142
|
+
if (key === undefined || value === undefined) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
entries.push([key, value]);
|
|
146
|
+
}
|
|
147
|
+
return Object.fromEntries(entries);
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The member keys from the document root down to the cursor, or `undefined` if a key on the way can't be read.
|
|
154
|
+
* Array elements on the way contribute no step.
|
|
155
|
+
*/
|
|
156
|
+
export function pathOf(cursor: Cursor): string[] | undefined {
|
|
157
|
+
const path: string[] = [];
|
|
158
|
+
for (const value of cursor.asArray()) {
|
|
159
|
+
if (value?.kind === Json.Kind.Member) {
|
|
160
|
+
const key = getMemberKeyName(value as Json.Member);
|
|
161
|
+
if (key === undefined) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
path.unshift(key);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return path;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Adds a member, or replaces the value of an existing one unless it already holds the same data.
|
|
172
|
+
*
|
|
173
|
+
* The value is placed after the colon with the spacing already there: the replaced value's, or a sibling's.
|
|
174
|
+
* A new member copies its indentation from the sibling it is placed next to; in an object without members,
|
|
175
|
+
* it is laid out by {@link SetMemberOptions.indent} and {@link SetMemberOptions.depth}.
|
|
176
|
+
* To change only the formatting or markers of a value, use {@link updateMember}, which keeps the prefix it is given.
|
|
177
|
+
*/
|
|
178
|
+
export function setMember(obj: Json.Object, key: string, value: Json.Value, options: SetMemberOptions = {}): Json.Object {
|
|
179
|
+
const index = indexOfMember(obj, key);
|
|
180
|
+
if (index >= 0) {
|
|
181
|
+
const existing = obj.members[index].element as Json.Member;
|
|
182
|
+
if (sameData(existing.value, value)) {
|
|
183
|
+
return obj;
|
|
184
|
+
}
|
|
185
|
+
return withElementAt(obj, index, {...existing, value: withPrefix(value, existing.value.prefix)});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const present = presentIndexes(obj.members);
|
|
189
|
+
const insertAt = options.position === "first" ? 0 :
|
|
190
|
+
options.position === "sorted" ? sortedIndex(obj.members, present, key) :
|
|
191
|
+
present.length;
|
|
192
|
+
const neighbour = present.length === 0 ? undefined :
|
|
193
|
+
obj.members[present[Math.min(insertAt, present.length - 1)]].element as Json.Member;
|
|
194
|
+
const added = newMember(key, value, emptySpace, neighbour?.key.after ?? emptySpace, space(neighbour?.value.prefix.whitespace ?? " "));
|
|
195
|
+
return {...obj, members: insertIntoList(obj.members, insertAt, added, options)};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Replaces the value of an existing member with what `fn` makes of it. A value that `fn` derives from the old one
|
|
200
|
+
* keeps the prefix `fn` gives it; a new value without a prefix is placed after the same space as the old value.
|
|
201
|
+
*/
|
|
202
|
+
export function updateMember(obj: Json.Object, key: string, fn: (old: Json.Value) => Json.Value): Json.Object {
|
|
203
|
+
const index = indexOfMember(obj, key);
|
|
204
|
+
if (index < 0) {
|
|
205
|
+
return obj;
|
|
206
|
+
}
|
|
207
|
+
const existing = obj.members[index].element as Json.Member;
|
|
208
|
+
const value = fn(existing.value);
|
|
209
|
+
if (value === existing.value) {
|
|
210
|
+
return obj;
|
|
211
|
+
}
|
|
212
|
+
return withElementAt(obj, index, {...existing, value: placedAs(value, existing.value)});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Removes a member. The space before the closing brace stays where it is, a removed first member's
|
|
217
|
+
* indentation passes to the next one, and an object left without members holds a single `Json.Empty`.
|
|
218
|
+
* A comment on the line before the member stays, and one on the member's own line goes with it.
|
|
219
|
+
* An object left without members prints as `{}` unless a comment remains in it.
|
|
220
|
+
*/
|
|
221
|
+
export function removeMember(obj: Json.Object, key: string): Json.Object {
|
|
222
|
+
const index = indexOfMember(obj, key);
|
|
223
|
+
if (index < 0) {
|
|
224
|
+
return obj;
|
|
225
|
+
}
|
|
226
|
+
return {...obj, members: removeFromList(obj.members, index)};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The elements of an array, without the `Json.Empty` of an empty array or a trailing comma.
|
|
231
|
+
*/
|
|
232
|
+
export function elementsOf(arr: Json.Array): Json.Value[] {
|
|
233
|
+
return arr.values.map(v => v.element).filter(v => v.kind !== Json.Kind.Empty);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Adds an element after the last one. It copies its indentation from a sibling; in an array without elements,
|
|
238
|
+
* it is laid out by {@link LayoutOptions.indent} and {@link LayoutOptions.depth}, or stays inline: `["a"]`.
|
|
239
|
+
*/
|
|
240
|
+
export function appendElement(arr: Json.Array, value: Json.Value, options: AppendElementOptions = {}): Json.Array {
|
|
241
|
+
const elements = elementsOf(arr);
|
|
242
|
+
if (options.unique && elements.some(e => sameData(e, value))) {
|
|
243
|
+
return arr;
|
|
244
|
+
}
|
|
245
|
+
return withValues(arr, insertIntoList(arr.values, elements.length, value, options));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Adds an element before the one at `index`, or after the last one if `index` is past the end.
|
|
250
|
+
*/
|
|
251
|
+
export function insertElement(arr: Json.Array, index: number, value: Json.Value, options: LayoutOptions = {}): Json.Array {
|
|
252
|
+
const count = elementsOf(arr).length;
|
|
253
|
+
return withValues(arr, insertIntoList(arr.values, Math.max(0, Math.min(index, count)), value, options));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Removes the elements that `pred` matches, with the same handling of whitespace and comments as {@link removeMember}.
|
|
258
|
+
* An array left without elements prints as `[]` unless a comment remains in it.
|
|
259
|
+
*/
|
|
260
|
+
export function removeElements(arr: Json.Array, pred: (value: Json.Value, index: number) => boolean): Json.Array {
|
|
261
|
+
return mapElements(arr, (v, i) => pred(v, i) ? undefined : v);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Replaces each element with what `fn` makes of it, or removes it where `fn` returns `undefined`.
|
|
266
|
+
* Replacing and removing in one pass covers e.g. replacing the first match and dropping the others.
|
|
267
|
+
* A value that `fn` derives from the old one keeps the prefix `fn` gives it; a new value without a prefix takes the
|
|
268
|
+
* old element's place. Elements are compared by reference: return the element itself to leave it unchanged.
|
|
269
|
+
*/
|
|
270
|
+
export function mapElements(arr: Json.Array, fn: (value: Json.Value, index: number) => Json.Value | undefined): Json.Array {
|
|
271
|
+
const removed: Json.Value[] = [];
|
|
272
|
+
let index = 0;
|
|
273
|
+
let values: Json.RightPadded<Json>[] = arr.values.map(padded => {
|
|
274
|
+
const old = padded.element;
|
|
275
|
+
if (old.kind === Json.Kind.Empty) {
|
|
276
|
+
return padded;
|
|
277
|
+
}
|
|
278
|
+
const value = fn(old, index++);
|
|
279
|
+
if (value === undefined) {
|
|
280
|
+
removed.push(old);
|
|
281
|
+
return padded;
|
|
282
|
+
}
|
|
283
|
+
return value === old ? padded : {...padded, element: placedAs(value, old)};
|
|
284
|
+
});
|
|
285
|
+
for (const old of removed) {
|
|
286
|
+
values = removeFromList(values, values.findIndex(p => p.element.id === old.id));
|
|
287
|
+
}
|
|
288
|
+
return values.every((p, i) => p === arr.values[i]) && values.length === arr.values.length ? arr : withValues(arr, values);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Sets the value at a path of member keys with {@link setMember}, rebuilding the objects along the path.
|
|
293
|
+
* Returns `root` unchanged if the path is empty, if a step isn't an object, or if a step is missing
|
|
294
|
+
* and {@link SetAtOptions.createMissing} isn't set.
|
|
295
|
+
*/
|
|
296
|
+
export function setAt<T extends JsonRoot>(root: T, path: readonly string[], value: Json.Value, options: SetAtOptions = {}): T {
|
|
297
|
+
return editRoot(root, path, options, (obj, opts) => setIn(obj, path, value, opts));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Replaces the value at a path of member keys with what `fn` makes of it, rebuilding the objects along the path.
|
|
302
|
+
* Returns `root` unchanged if the path doesn't resolve.
|
|
303
|
+
*/
|
|
304
|
+
export function updateAt<T extends JsonRoot>(root: T, path: readonly string[], fn: (old: Json.Value) => Json.Value): T {
|
|
305
|
+
return editRoot(root, path, {}, obj => updateIn(obj, path, fn));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Removes the member at a path of member keys with {@link removeMember}, rebuilding the objects along the path.
|
|
310
|
+
* Returns `root` unchanged if the path doesn't resolve.
|
|
311
|
+
*/
|
|
312
|
+
export function removeAt<T extends JsonRoot>(root: T, path: readonly string[], options: RemoveAtOptions = {}): T {
|
|
313
|
+
return editRoot(root, path, {}, obj => removeIn(obj, path, options.pruneEmptyParents ?? false));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* A literal with its `source` written as JSON and its `value` set.
|
|
318
|
+
*/
|
|
319
|
+
export function jsonLiteral(value: string | number | boolean | null, prefix: Json.Space = emptySpace): Json.Literal {
|
|
320
|
+
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
321
|
+
throw new RangeError(`${value} can't be written as JSON`);
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
kind: Json.Kind.Literal,
|
|
325
|
+
id: randomId(),
|
|
326
|
+
prefix,
|
|
327
|
+
markers: emptyMarkers,
|
|
328
|
+
source: JSON.stringify(value),
|
|
329
|
+
value
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* A JSON value for plain data, laid out one element per line like `JSON.stringify(value, null, indent)`.
|
|
335
|
+
* `depth` is how deep the value will be nested, so that its elements line up with the surrounding document.
|
|
336
|
+
* Object properties that are `undefined` are left out and a `toJSON` method is called, as `JSON.stringify` does.
|
|
337
|
+
*/
|
|
338
|
+
export function fromPlain(value: unknown, indent: string, depth: number = 0): Json.Value {
|
|
339
|
+
if (typeof (value as { toJSON?: unknown } | null | undefined)?.toJSON === "function") {
|
|
340
|
+
return fromPlain((value as { toJSON(): unknown }).toJSON(), indent, depth);
|
|
341
|
+
}
|
|
342
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
343
|
+
return jsonLiteral(value);
|
|
344
|
+
}
|
|
345
|
+
const inner = space("\n" + indent.repeat(depth + 1));
|
|
346
|
+
const closing = space("\n" + indent.repeat(depth));
|
|
347
|
+
if (Array.isArray(value)) {
|
|
348
|
+
if (value.length === 0) {
|
|
349
|
+
return emptyArray();
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
kind: Json.Kind.Array,
|
|
353
|
+
id: randomId(),
|
|
354
|
+
prefix: emptySpace,
|
|
355
|
+
markers: emptyMarkers,
|
|
356
|
+
values: value.map((v, i) => rightPadded(
|
|
357
|
+
withPrefix(fromPlain(v === undefined ? null : v, indent, depth + 1), inner),
|
|
358
|
+
i === value.length - 1 ? closing : emptySpace
|
|
359
|
+
))
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
if (typeof value === "object") {
|
|
363
|
+
const entries = Object.entries(value).filter(([, v]) => v !== undefined);
|
|
364
|
+
if (entries.length === 0) {
|
|
365
|
+
return emptyObject();
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
kind: Json.Kind.Object,
|
|
369
|
+
id: randomId(),
|
|
370
|
+
prefix: emptySpace,
|
|
371
|
+
markers: emptyMarkers,
|
|
372
|
+
members: entries.map(([k, v], i) => rightPadded<Json>(
|
|
373
|
+
newMember(k, fromPlain(v, indent, depth + 1), inner, emptySpace, space(" ")),
|
|
374
|
+
i === entries.length - 1 ? closing : emptySpace
|
|
375
|
+
))
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
throw new TypeError(`A ${typeof value} can't be written as JSON`);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* An object without members: `{}`.
|
|
383
|
+
*/
|
|
384
|
+
export function emptyObject(prefix: Json.Space = emptySpace): Json.Object {
|
|
385
|
+
return {
|
|
386
|
+
kind: Json.Kind.Object,
|
|
387
|
+
id: randomId(),
|
|
388
|
+
prefix,
|
|
389
|
+
markers: emptyMarkers,
|
|
390
|
+
members: [rightPadded<Json>(emptyElement(), emptySpace)]
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* An array without elements: `[]`.
|
|
396
|
+
*/
|
|
397
|
+
export function emptyArray(prefix: Json.Space = emptySpace): Json.Array {
|
|
398
|
+
return {
|
|
399
|
+
kind: Json.Kind.Array,
|
|
400
|
+
id: randomId(),
|
|
401
|
+
prefix,
|
|
402
|
+
markers: emptyMarkers,
|
|
403
|
+
values: [rightPadded<Json.Value>(emptyElement(), emptySpace)]
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function withValues(arr: Json.Array, values: Json.RightPadded<Json>[]): Json.Array {
|
|
408
|
+
return {...arr, values: values as Json.RightPadded<Json.Value>[]};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function presentIndexes(list: Json.RightPadded<Json>[]): number[] {
|
|
412
|
+
return list.map((_, i) => i).filter(i => list[i].element.kind !== Json.Kind.Empty);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Inserts an element into the members of an object or the values of an array, before the `insertAt`-th element
|
|
417
|
+
* that isn't `Json.Empty`, or after the last one. The element's leading space comes from where it is placed.
|
|
418
|
+
*/
|
|
419
|
+
function insertIntoList(list: Json.RightPadded<Json>[], insertAt: number, added: Json, options: LayoutOptions): Json.RightPadded<Json>[] {
|
|
420
|
+
const present = presentIndexes(list);
|
|
421
|
+
if (present.length === 0) {
|
|
422
|
+
const closing = list.length > 0 ? list[list.length - 1].after : emptySpace;
|
|
423
|
+
const layout = emptyLayout(closing, options);
|
|
424
|
+
return [rightPadded<Json>(withLeadingSpace(added, space(layout.element)), layout.closing)];
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const following = followingWhitespace(list, present);
|
|
428
|
+
const placed = withLeadingSpace(added, space(following));
|
|
429
|
+
const elements = [...list];
|
|
430
|
+
if (insertAt < present.length) {
|
|
431
|
+
const at = present[insertAt];
|
|
432
|
+
const next = elements[at].element;
|
|
433
|
+
const nextSpace = leadingSpace(next);
|
|
434
|
+
const {ending, rest} = splitAtLineEnd(nextSpace);
|
|
435
|
+
if (ending.length > 0) {
|
|
436
|
+
elements[at] = {...elements[at], element: withLeadingSpace(next, rest)};
|
|
437
|
+
elements.splice(at, 0, rightPadded<Json>(withLeadingSpace(placed, withLeadingComments(nextSpace.whitespace, ending, space(following))), emptySpace));
|
|
438
|
+
} else if (insertAt === 0) {
|
|
439
|
+
elements[at] = {...elements[at], element: withLeadingWhitespace(next, following)};
|
|
440
|
+
elements.splice(at, 0, rightPadded<Json>(withLeadingSpace(placed, space(leadingWhitespace(next))), emptySpace));
|
|
441
|
+
} else {
|
|
442
|
+
elements.splice(at, 0, rightPadded<Json>(placed, emptySpace));
|
|
443
|
+
}
|
|
444
|
+
} else {
|
|
445
|
+
const last = present[present.length - 1];
|
|
446
|
+
if (last === elements.length - 1) {
|
|
447
|
+
const {trailing, closing} = splitTrailingComment(elements[last].after, following);
|
|
448
|
+
elements.push(rightPadded<Json>(trailing ? withLeadingSpace(placed, trailing) : placed, closing));
|
|
449
|
+
elements[last] = {...elements[last], after: emptySpace};
|
|
450
|
+
} else {
|
|
451
|
+
elements.splice(last + 1, 0, rightPadded<Json>(placed, emptySpace));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return elements;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Removes the element at `index` from the members of an object or the values of an array. The space before the
|
|
459
|
+
* closing bracket stays where it is, a removed first element's indentation passes to the next one, a comment on the
|
|
460
|
+
* line before the element stays and one on the element's own line goes with it. A list left without elements holds
|
|
461
|
+
* a single `Json.Empty`, which prints without space unless a comment remains.
|
|
462
|
+
*/
|
|
463
|
+
function removeFromList(list: Json.RightPadded<Json>[], index: number): Json.RightPadded<Json>[] {
|
|
464
|
+
const removed = list[index];
|
|
465
|
+
const removedSpace = leadingSpace(removed.element);
|
|
466
|
+
const kept = commentsEndingPreviousLine(removedSpace);
|
|
467
|
+
const keep = (to: Json.Space) => withLeadingComments(removedSpace.whitespace, kept, splitAtLineEnd(to).rest);
|
|
468
|
+
const elements = list.filter((_, i) => i !== index);
|
|
469
|
+
|
|
470
|
+
if (presentIndexes(elements).length === 0) {
|
|
471
|
+
const closing = keep(elements.length > 0 ? elements[elements.length - 1].after : removed.after);
|
|
472
|
+
return [rightPadded<Json>(emptyElement(), closing.comments.length === 0 ? emptySpace : closing)];
|
|
473
|
+
}
|
|
474
|
+
if (index === elements.length) {
|
|
475
|
+
elements[index - 1] = {...elements[index - 1], after: keep(removed.after)};
|
|
476
|
+
} else if (elements[index].element.kind === Json.Kind.Empty) {
|
|
477
|
+
elements[index] = {...elements[index], after: keep(elements[index].after)};
|
|
478
|
+
} else {
|
|
479
|
+
const next = withLeadingSpace(elements[index].element, keep(leadingSpace(elements[index].element)));
|
|
480
|
+
const element = kept.length === 0 && index === 0 ? withLeadingWhitespace(next, leadingWhitespace(removed.element)) : next;
|
|
481
|
+
elements[index] = {...elements[index], element};
|
|
482
|
+
}
|
|
483
|
+
return elements;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* A replacement value as `fn` gave it if it derives from the old one, otherwise after the old value's prefix
|
|
488
|
+
* unless it brings its own.
|
|
489
|
+
*/
|
|
490
|
+
function placedAs(value: Json.Value, old: Json.Value): Json.Value {
|
|
491
|
+
return value.id !== old.id && isEmptySpace(value.prefix) ? withPrefix(value, old.prefix) : value;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function indexOfMember(obj: Json.Object, key: string): number {
|
|
495
|
+
return obj.members.findIndex(m => isMember(m.element) && getMemberKeyName(m.element) === key);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function withElementAt(obj: Json.Object, index: number, element: Json.Member): Json.Object {
|
|
499
|
+
const elements = [...obj.members];
|
|
500
|
+
elements[index] = {...elements[index], element};
|
|
501
|
+
return {...obj, members: elements};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function isEmptySpace(s: Json.Space): boolean {
|
|
505
|
+
return s.whitespace === "" && s.comments.length === 0;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function withPrefix<T extends Json>(json: T, prefix: Json.Space): T {
|
|
509
|
+
return json.prefix === prefix ? json : {...json, prefix};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function emptyElement(): Json.Empty {
|
|
513
|
+
return {kind: Json.Kind.Empty, id: randomId(), prefix: emptySpace, markers: emptyMarkers};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function newMember(key: string, value: Json.Value, prefix: Json.Space, keyAfter: Json.Space, valuePrefix: Json.Space): Json.Member {
|
|
517
|
+
return {
|
|
518
|
+
kind: Json.Kind.Member,
|
|
519
|
+
id: randomId(),
|
|
520
|
+
prefix,
|
|
521
|
+
markers: emptyMarkers,
|
|
522
|
+
key: rightPadded<Json.Key>(jsonLiteral(key), keyAfter),
|
|
523
|
+
value: withPrefix(value, valuePrefix)
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Whether two values hold the same data: literals that decode to the same value, arrays with the same elements
|
|
529
|
+
* in the same order, and objects with the same members in any order.
|
|
530
|
+
*/
|
|
531
|
+
function sameData(a: Json.Value, b: Json.Value): boolean {
|
|
532
|
+
if (a === b) {
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
if (isLiteral(a) && isLiteral(b)) {
|
|
536
|
+
const plain = toPlain(a);
|
|
537
|
+
return plain === toPlain(b) && (typeof plain !== "number" || sameInteger(a.source, b.source));
|
|
538
|
+
}
|
|
539
|
+
if (isArray(a) && isArray(b)) {
|
|
540
|
+
const as = a.values.map(v => v.element).filter(v => v.kind !== Json.Kind.Empty);
|
|
541
|
+
const bs = b.values.map(v => v.element).filter(v => v.kind !== Json.Kind.Empty);
|
|
542
|
+
return as.length === bs.length && as.every((v, i) => sameData(v, bs[i]));
|
|
543
|
+
}
|
|
544
|
+
if (isObject(a) && isObject(b)) {
|
|
545
|
+
const as = membersOf(a);
|
|
546
|
+
const bs = membersOf(b);
|
|
547
|
+
return as.length === bs.length && as.every(m => {
|
|
548
|
+
const key = getMemberKeyName(m);
|
|
549
|
+
const other = key === undefined ? undefined : findMember(b, key);
|
|
550
|
+
return other !== undefined && sameData(m.value, other.value);
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Integers beyond double precision decode to the same number, so their sources are compared exactly.
|
|
558
|
+
*/
|
|
559
|
+
function sameInteger(a: string, b: string): boolean {
|
|
560
|
+
const integer = /^-?\d+$/;
|
|
561
|
+
return !integer.test(a) || !integer.test(b) || BigInt(a) === BigInt(b);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* The indentation of a member that isn't the first, which may differ from the first one's in an object
|
|
566
|
+
* laid out on one line.
|
|
567
|
+
*/
|
|
568
|
+
function followingWhitespace(list: Json.RightPadded<Json>[], present: number[]): string {
|
|
569
|
+
if (present.length > 1) {
|
|
570
|
+
const second = leadingSpace(list[present[1]].element);
|
|
571
|
+
return lastLineBreak(second) ?? second.whitespace;
|
|
572
|
+
}
|
|
573
|
+
return lastLineBreak(leadingSpace(list[present[0]].element)) ?? " ";
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* The line break and indentation that the last line of a space starts with, looking past its comments.
|
|
578
|
+
*/
|
|
579
|
+
function lastLineBreak(s: Json.Space): string | undefined {
|
|
580
|
+
const text = [s.whitespace, ...s.comments.map(c => c.suffix)].filter(t => t.includes("\n")).pop();
|
|
581
|
+
return text === undefined ? undefined : "\n" + text.substring(text.lastIndexOf("\n") + 1);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function sortedIndex(list: Json.RightPadded<Json>[], present: number[], key: string): number {
|
|
585
|
+
const index = present.findIndex(i => {
|
|
586
|
+
const m = list[i].element;
|
|
587
|
+
const name = isMember(m) ? getMemberKeyName(m) : undefined;
|
|
588
|
+
return name !== undefined && name.localeCompare(key, "en") > 0;
|
|
589
|
+
});
|
|
590
|
+
return index < 0 ? present.length : index;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* A comment on the same line as the last member, which goes after the comma that a member appended to it adds,
|
|
595
|
+
* the way the parsers attach such a comment to the member after it.
|
|
596
|
+
*/
|
|
597
|
+
function splitTrailingComment(after: Json.Space, following: string): { trailing?: Json.Space, closing: Json.Space } {
|
|
598
|
+
if (after.comments.length === 0 || after.whitespace.includes("\n")) {
|
|
599
|
+
return {closing: after};
|
|
600
|
+
}
|
|
601
|
+
const [comment, ...rest] = after.comments;
|
|
602
|
+
return {
|
|
603
|
+
trailing: {kind: Json.Kind.Space, whitespace: after.whitespace, comments: [{...comment, suffix: following}]},
|
|
604
|
+
closing: rest.length === 0 ? space(comment.suffix) : {kind: Json.Kind.Space, whitespace: comment.suffix, comments: rest}
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* The comments at the start of a member's prefix that end the line before it, such as one after the previous
|
|
610
|
+
* member's comma. They describe what comes before the member, so they stay when it is removed.
|
|
611
|
+
*/
|
|
612
|
+
function commentsEndingPreviousLine(prefix: Json.Space): Json.Comment[] {
|
|
613
|
+
if (prefix.whitespace.includes("\n")) {
|
|
614
|
+
return [];
|
|
615
|
+
}
|
|
616
|
+
const end = prefix.comments.findIndex(c => c.suffix.includes("\n"));
|
|
617
|
+
return end < 0 ? [] : prefix.comments.slice(0, end + 1);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function splitAtLineEnd(s: Json.Space): { ending: Json.Comment[], rest: Json.Space } {
|
|
621
|
+
const ending = commentsEndingPreviousLine(s);
|
|
622
|
+
if (ending.length === 0) {
|
|
623
|
+
return {ending, rest: s};
|
|
624
|
+
}
|
|
625
|
+
return {
|
|
626
|
+
ending,
|
|
627
|
+
rest: {kind: Json.Kind.Space, whitespace: ending[ending.length - 1].suffix, comments: s.comments.slice(ending.length)}
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function withLeadingComments(whitespace: string, comments: Json.Comment[], to: Json.Space): Json.Space {
|
|
632
|
+
if (comments.length === 0) {
|
|
633
|
+
return to;
|
|
634
|
+
}
|
|
635
|
+
const last = comments.length - 1;
|
|
636
|
+
return {
|
|
637
|
+
kind: Json.Kind.Space,
|
|
638
|
+
whitespace,
|
|
639
|
+
comments: [...comments.slice(0, last), {...comments[last], suffix: to.whitespace}, ...to.comments]
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function emptyLayout(closing: Json.Space, options: LayoutOptions): { element: string, closing: Json.Space } {
|
|
644
|
+
const lineBreak = lastLineBreak(closing);
|
|
645
|
+
if (lineBreak !== undefined) {
|
|
646
|
+
return {element: lineBreak + (options.indent ?? " "), closing};
|
|
647
|
+
}
|
|
648
|
+
if (options.indent !== undefined) {
|
|
649
|
+
const base = "\n" + options.indent.repeat(options.depth ?? 0);
|
|
650
|
+
return {element: base + options.indent, closing: space(base)};
|
|
651
|
+
}
|
|
652
|
+
return {element: "", closing};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* LSTs built before the parsers put a member's indentation on the member (#8644) have it on the key.
|
|
657
|
+
*/
|
|
658
|
+
function indentationOnKey(m: Json.Member): boolean {
|
|
659
|
+
return m.prefix.whitespace === "" && m.prefix.comments.length === 0 && m.key.element.prefix.whitespace !== "";
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* The space an element starts with: its prefix, or a member's key's prefix where the indentation is on the key.
|
|
664
|
+
*/
|
|
665
|
+
function leadingSpace(element: Json): Json.Space {
|
|
666
|
+
return isMember(element) && indentationOnKey(element) ? element.key.element.prefix : element.prefix;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function withLeadingSpace<T extends Json>(element: T, prefix: Json.Space): T {
|
|
670
|
+
if (leadingSpace(element) === prefix) {
|
|
671
|
+
return element;
|
|
672
|
+
}
|
|
673
|
+
if (isMember(element) && indentationOnKey(element)) {
|
|
674
|
+
return {...element, key: {...element.key, element: {...element.key.element, prefix}}};
|
|
675
|
+
}
|
|
676
|
+
return {...element, prefix};
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function leadingWhitespace(element: Json): string {
|
|
680
|
+
return leadingSpace(element).whitespace;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function withLeadingWhitespace<T extends Json>(element: T, whitespace: string): T {
|
|
684
|
+
const prefix = leadingSpace(element);
|
|
685
|
+
return prefix.whitespace === whitespace ? element : withLeadingSpace(element, {...prefix, whitespace});
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function editRoot<T extends JsonRoot>(root: T, path: readonly string[], options: SetMemberOptions,
|
|
689
|
+
edit: (obj: Json.Object, options: SetMemberOptions) => Json.Object): T {
|
|
690
|
+
if (path.length === 0) {
|
|
691
|
+
return root;
|
|
692
|
+
}
|
|
693
|
+
if (root.kind === Json.Kind.Document) {
|
|
694
|
+
const doc = root as Json.Document;
|
|
695
|
+
if (!isObject(doc.value)) {
|
|
696
|
+
return root;
|
|
697
|
+
}
|
|
698
|
+
const value = edit(doc.value, {...options, indent: options.indent ?? documentIndent(doc, doc.value)});
|
|
699
|
+
return value === doc.value ? root : {...doc, value} as T;
|
|
700
|
+
}
|
|
701
|
+
return isObject(root as Json.Value) ? edit(root as Json.Object, options) as T : root;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function documentIndent(doc: Json.Document, obj: Json.Object): string | undefined {
|
|
705
|
+
const first = membersOf(obj)[0];
|
|
706
|
+
if (first === undefined) {
|
|
707
|
+
return detectIndent(doc);
|
|
708
|
+
}
|
|
709
|
+
const whitespace = leadingWhitespace(first);
|
|
710
|
+
return whitespace.includes("\n") ? whitespace.match(/\n([ \t]+)$/)?.[1] ?? detectIndent(doc) : undefined;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function setIn(obj: Json.Object, path: readonly string[], value: Json.Value, options: SetAtOptions): Json.Object {
|
|
714
|
+
const [key, ...rest] = path;
|
|
715
|
+
if (rest.length === 0) {
|
|
716
|
+
return setMember(obj, key, value, options);
|
|
717
|
+
}
|
|
718
|
+
const nested = {...options, depth: (options.depth ?? 0) + 1};
|
|
719
|
+
const child = findMember(obj, key)?.value;
|
|
720
|
+
if (child === undefined) {
|
|
721
|
+
return options.createMissing ?
|
|
722
|
+
setMember(obj, key, setIn(emptyObject(), rest, value, nested), {...options, position: options.parentPosition ?? "last"}) :
|
|
723
|
+
obj;
|
|
724
|
+
}
|
|
725
|
+
return isObject(child) ? updateMember(obj, key, () => setIn(child, rest, value, nested)) : obj;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function updateIn(obj: Json.Object, path: readonly string[], fn: (old: Json.Value) => Json.Value): Json.Object {
|
|
729
|
+
const [key, ...rest] = path;
|
|
730
|
+
if (rest.length === 0) {
|
|
731
|
+
return updateMember(obj, key, fn);
|
|
732
|
+
}
|
|
733
|
+
return updateMember(obj, key, child => isObject(child) ? updateIn(child, rest, fn) : child);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function removeIn(obj: Json.Object, path: readonly string[], pruneEmptyParents: boolean): Json.Object {
|
|
737
|
+
const [key, ...rest] = path;
|
|
738
|
+
if (rest.length === 0) {
|
|
739
|
+
return removeMember(obj, key);
|
|
740
|
+
}
|
|
741
|
+
const child = findMember(obj, key)?.value;
|
|
742
|
+
if (child === undefined || !isObject(child)) {
|
|
743
|
+
return obj;
|
|
744
|
+
}
|
|
745
|
+
const updated = removeIn(child, rest, pruneEmptyParents);
|
|
746
|
+
if (updated === child) {
|
|
747
|
+
return obj;
|
|
748
|
+
}
|
|
749
|
+
return pruneEmptyParents && isEmptyContainer(updated) ? removeMember(obj, key) : updateMember(obj, key, () => updated);
|
|
750
|
+
}
|