@optique/core 1.0.0-dev.1856 → 1.0.0-dev.1860
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/annotations.cjs +168 -111
- package/dist/annotations.d.cts +14 -2
- package/dist/annotations.d.ts +14 -2
- package/dist/annotations.js +168 -112
- package/dist/parser.cjs +1 -1
- package/dist/parser.js +2 -2
- package/package.json +1 -1
package/dist/annotations.cjs
CHANGED
|
@@ -50,14 +50,20 @@ const protectedAnnotationStateViews = /* @__PURE__ */ new WeakMap();
|
|
|
50
50
|
function throwReadonlyAnnotationMutation() {
|
|
51
51
|
throw new TypeError("Cannot mutate read-only annotation data.");
|
|
52
52
|
}
|
|
53
|
-
function createAnnotationProtectionContext() {
|
|
54
|
-
return {
|
|
53
|
+
function createAnnotationProtectionContext(rewrapProtectedViews = false) {
|
|
54
|
+
return {
|
|
55
|
+
cache: /* @__PURE__ */ new WeakMap(),
|
|
56
|
+
rewrapProtectedViews
|
|
57
|
+
};
|
|
55
58
|
}
|
|
56
59
|
function registerProtectedAnnotationView(context, target, view) {
|
|
57
60
|
context.cache.set(target, view);
|
|
58
61
|
protectedAnnotationTargets.set(view, target);
|
|
59
62
|
return view;
|
|
60
63
|
}
|
|
64
|
+
function cacheProtectedAnnotationViewAlias(context, alias, view) {
|
|
65
|
+
context.cache.set(alias, view);
|
|
66
|
+
}
|
|
61
67
|
function isProtectedAnnotationView(value) {
|
|
62
68
|
return value != null && typeof value === "object" && protectedAnnotationTargets.has(value);
|
|
63
69
|
}
|
|
@@ -81,11 +87,11 @@ function defineProtectedDataProperty(context, target, key, descriptor) {
|
|
|
81
87
|
set: () => throwReadonlyAnnotationMutation()
|
|
82
88
|
});
|
|
83
89
|
}
|
|
84
|
-
function
|
|
90
|
+
function copyOwnProperties(source, target, transformValue, excludedKeys) {
|
|
85
91
|
const sourcePrototype = Object.getPrototypeOf(source);
|
|
86
92
|
if (Object.getPrototypeOf(target) !== sourcePrototype) Object.setPrototypeOf(target, sourcePrototype);
|
|
87
93
|
for (const key of Reflect.ownKeys(source)) {
|
|
88
|
-
if (key ===
|
|
94
|
+
if (excludedKeys?.has(key) === true) continue;
|
|
89
95
|
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
90
96
|
if (descriptor == null) continue;
|
|
91
97
|
if ("value" in descriptor && transformValue != null) {
|
|
@@ -97,6 +103,13 @@ function copyRegExpMetadata(source, target, transformValue) {
|
|
|
97
103
|
}
|
|
98
104
|
Object.defineProperty(target, key, descriptor);
|
|
99
105
|
}
|
|
106
|
+
}
|
|
107
|
+
function normalizeProtectedCollectionItem(value, context) {
|
|
108
|
+
return context.rewrapProtectedViews && isProtectedAnnotationView(value) ? unwrapProtectedAnnotationTarget(value) : value;
|
|
109
|
+
}
|
|
110
|
+
const regExpExcludedKeys = new Set(["lastIndex"]);
|
|
111
|
+
function copyRegExpMetadata(source, target, transformValue) {
|
|
112
|
+
copyOwnProperties(source, target, transformValue, regExpExcludedKeys);
|
|
100
113
|
target.lastIndex = source.lastIndex;
|
|
101
114
|
}
|
|
102
115
|
function cloneRegExpShape(source) {
|
|
@@ -106,7 +119,8 @@ function cloneRegExpShape(source) {
|
|
|
106
119
|
}
|
|
107
120
|
function createProtectedObjectView(target, context) {
|
|
108
121
|
if (Array.isArray(target)) {
|
|
109
|
-
const view$1 =
|
|
122
|
+
const view$1 = Object.setPrototypeOf([], Object.getPrototypeOf(target));
|
|
123
|
+
view$1.length = target.length;
|
|
110
124
|
registerProtectedAnnotationView(context, target, view$1);
|
|
111
125
|
for (const key of Reflect.ownKeys(target)) {
|
|
112
126
|
if (key === "length") continue;
|
|
@@ -145,26 +159,28 @@ function createProtectedObjectView(target, context) {
|
|
|
145
159
|
}
|
|
146
160
|
function createProtectedMapView(target, context) {
|
|
147
161
|
const methodCache = /* @__PURE__ */ new Map();
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
162
|
+
const cloned = /* @__PURE__ */ new Map();
|
|
163
|
+
for (const [entryKey, entryValue] of target.entries()) cloned.set(normalizeProtectedCollectionItem(entryKey, context), normalizeProtectedCollectionItem(entryValue, context));
|
|
164
|
+
const view = new Proxy(cloned, {
|
|
165
|
+
get(clonedTarget, key) {
|
|
166
|
+
if (key === "size") return clonedTarget.size;
|
|
151
167
|
if (key === "set" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
152
|
-
if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(
|
|
153
|
-
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) =>
|
|
154
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
168
|
+
if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(clonedTarget.get(unwrapProtectedAnnotationTarget(lookup)), context));
|
|
169
|
+
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => clonedTarget.has(unwrapProtectedAnnotationTarget(lookup)));
|
|
170
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1, mapKey) => {
|
|
155
171
|
callback.call(thisArg, protectAnnotationValue(value$1, context), protectAnnotationValue(mapKey, context), view);
|
|
156
172
|
}));
|
|
157
173
|
if (key === "keys") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
158
|
-
for (const value$1 of
|
|
174
|
+
for (const value$1 of clonedTarget.keys()) yield protectAnnotationValue(value$1, context);
|
|
159
175
|
});
|
|
160
176
|
if (key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
161
|
-
for (const value$1 of
|
|
177
|
+
for (const value$1 of clonedTarget.values()) yield protectAnnotationValue(value$1, context);
|
|
162
178
|
});
|
|
163
179
|
if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
164
|
-
for (const [entryKey, entryValue] of
|
|
180
|
+
for (const [entryKey, entryValue] of clonedTarget.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
|
|
165
181
|
});
|
|
166
|
-
const value = Reflect.get(
|
|
167
|
-
return typeof value === "function" ? value.bind(
|
|
182
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
183
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
168
184
|
},
|
|
169
185
|
set() {
|
|
170
186
|
throwReadonlyAnnotationMutation();
|
|
@@ -182,30 +198,35 @@ function createProtectedMapView(target, context) {
|
|
|
182
198
|
throwReadonlyAnnotationMutation();
|
|
183
199
|
}
|
|
184
200
|
});
|
|
185
|
-
|
|
201
|
+
registerProtectedAnnotationView(context, target, view);
|
|
202
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
203
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
204
|
+
return view;
|
|
186
205
|
}
|
|
187
206
|
function createProtectedSetView(target, context) {
|
|
188
207
|
const methodCache = /* @__PURE__ */ new Map();
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
208
|
+
const cloned = /* @__PURE__ */ new Set();
|
|
209
|
+
for (const value of target.values()) cloned.add(normalizeProtectedCollectionItem(value, context));
|
|
210
|
+
const view = new Proxy(cloned, {
|
|
211
|
+
get(clonedTarget, key) {
|
|
212
|
+
if (key === "size") return clonedTarget.size;
|
|
192
213
|
if (key === "add" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
193
|
-
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) =>
|
|
194
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
214
|
+
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => clonedTarget.has(unwrapProtectedAnnotationTarget(lookup)));
|
|
215
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1) => {
|
|
195
216
|
const protectedValue = protectAnnotationValue(value$1, context);
|
|
196
217
|
callback.call(thisArg, protectedValue, protectedValue, view);
|
|
197
218
|
}));
|
|
198
219
|
if (key === "keys" || key === "values" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
199
|
-
for (const value$1 of
|
|
220
|
+
for (const value$1 of clonedTarget.values()) yield protectAnnotationValue(value$1, context);
|
|
200
221
|
});
|
|
201
222
|
if (key === "entries") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
202
|
-
for (const value$1 of
|
|
223
|
+
for (const value$1 of clonedTarget.values()) {
|
|
203
224
|
const protectedValue = protectAnnotationValue(value$1, context);
|
|
204
225
|
yield [protectedValue, protectedValue];
|
|
205
226
|
}
|
|
206
227
|
});
|
|
207
|
-
const value = Reflect.get(
|
|
208
|
-
return typeof value === "function" ? value.bind(
|
|
228
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
229
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
209
230
|
},
|
|
210
231
|
set() {
|
|
211
232
|
throwReadonlyAnnotationMutation();
|
|
@@ -223,15 +244,19 @@ function createProtectedSetView(target, context) {
|
|
|
223
244
|
throwReadonlyAnnotationMutation();
|
|
224
245
|
}
|
|
225
246
|
});
|
|
226
|
-
|
|
247
|
+
registerProtectedAnnotationView(context, target, view);
|
|
248
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
249
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
250
|
+
return view;
|
|
227
251
|
}
|
|
228
252
|
function createProtectedDateView(target, context) {
|
|
229
253
|
const methodCache = /* @__PURE__ */ new Map();
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
254
|
+
const cloned = new Date(target.getTime());
|
|
255
|
+
const view = new Proxy(cloned, {
|
|
256
|
+
get(clonedTarget, key) {
|
|
257
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
233
258
|
if (typeof key === "string" && key.startsWith("set")) return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
234
|
-
return typeof value === "function" ? value.bind(
|
|
259
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
235
260
|
},
|
|
236
261
|
set() {
|
|
237
262
|
throwReadonlyAnnotationMutation();
|
|
@@ -249,7 +274,10 @@ function createProtectedDateView(target, context) {
|
|
|
249
274
|
throwReadonlyAnnotationMutation();
|
|
250
275
|
}
|
|
251
276
|
});
|
|
252
|
-
|
|
277
|
+
registerProtectedAnnotationView(context, target, view);
|
|
278
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
279
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
280
|
+
return view;
|
|
253
281
|
}
|
|
254
282
|
function createProtectedRegExpView(target, context) {
|
|
255
283
|
const methodCache = /* @__PURE__ */ new Map();
|
|
@@ -279,26 +307,28 @@ function createProtectedRegExpView(target, context) {
|
|
|
279
307
|
}
|
|
280
308
|
});
|
|
281
309
|
registerProtectedAnnotationView(context, target, view);
|
|
310
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
282
311
|
copyRegExpMetadata(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
283
312
|
return view;
|
|
284
313
|
}
|
|
285
314
|
function createProtectedURLSearchParamsView(target, context) {
|
|
286
315
|
const methodCache = /* @__PURE__ */ new Map();
|
|
287
|
-
const
|
|
288
|
-
|
|
316
|
+
const cloned = new URLSearchParams(target);
|
|
317
|
+
const view = new Proxy(cloned, {
|
|
318
|
+
get(clonedTarget, key) {
|
|
289
319
|
if (key === "append" || key === "delete" || key === "set" || key === "sort") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
290
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
320
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1, name) => {
|
|
291
321
|
callback.call(thisArg, value$1, name, view);
|
|
292
322
|
}));
|
|
293
323
|
if (key === "keys" || key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
294
|
-
const iterator = key === "keys" ?
|
|
324
|
+
const iterator = key === "keys" ? clonedTarget.keys() : clonedTarget.values();
|
|
295
325
|
for (const value$1 of iterator) yield value$1;
|
|
296
326
|
});
|
|
297
327
|
if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
298
|
-
for (const entry of
|
|
328
|
+
for (const entry of clonedTarget.entries()) yield entry;
|
|
299
329
|
});
|
|
300
|
-
const value = Reflect.get(
|
|
301
|
-
return typeof value === "function" ? value.bind(
|
|
330
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
331
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
302
332
|
},
|
|
303
333
|
set() {
|
|
304
334
|
throwReadonlyAnnotationMutation();
|
|
@@ -316,14 +346,18 @@ function createProtectedURLSearchParamsView(target, context) {
|
|
|
316
346
|
throwReadonlyAnnotationMutation();
|
|
317
347
|
}
|
|
318
348
|
});
|
|
319
|
-
|
|
349
|
+
registerProtectedAnnotationView(context, target, view);
|
|
350
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
351
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
352
|
+
return view;
|
|
320
353
|
}
|
|
321
354
|
function createProtectedURLView(target, context) {
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
355
|
+
const cloned = new URL(target.href);
|
|
356
|
+
const view = new Proxy(cloned, {
|
|
357
|
+
get(clonedTarget, key) {
|
|
358
|
+
if (key === "searchParams") return protectAnnotationValue(clonedTarget.searchParams, context);
|
|
359
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
360
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
327
361
|
},
|
|
328
362
|
set() {
|
|
329
363
|
throwReadonlyAnnotationMutation();
|
|
@@ -341,12 +375,16 @@ function createProtectedURLView(target, context) {
|
|
|
341
375
|
throwReadonlyAnnotationMutation();
|
|
342
376
|
}
|
|
343
377
|
});
|
|
344
|
-
|
|
378
|
+
registerProtectedAnnotationView(context, target, view);
|
|
379
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
380
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
381
|
+
return view;
|
|
345
382
|
}
|
|
346
383
|
function protectAnnotationValue(value, context) {
|
|
347
384
|
if (value == null || typeof value !== "object") return value;
|
|
348
|
-
const
|
|
349
|
-
if (
|
|
385
|
+
const isProtectedView = isProtectedAnnotationView(value);
|
|
386
|
+
if (isProtectedView && !context.rewrapProtectedViews) return value;
|
|
387
|
+
const target = isProtectedView ? unwrapProtectedAnnotationTarget(value) : value;
|
|
350
388
|
const cached = context.cache.get(target);
|
|
351
389
|
if (cached !== void 0) return cached;
|
|
352
390
|
if (target instanceof Map) return createProtectedMapView(target, context);
|
|
@@ -374,6 +412,71 @@ function protectAnnotationValue(value, context) {
|
|
|
374
412
|
function normalizeRunAnnotationInput(annotations) {
|
|
375
413
|
return isProtectedAnnotationView(annotations) ? unwrapProtectedAnnotationTarget(annotations) : annotations;
|
|
376
414
|
}
|
|
415
|
+
function injectAnnotationsWithContext(state, annotations, context) {
|
|
416
|
+
const protectedAnnotations = protectAnnotationValue(annotations, context);
|
|
417
|
+
if (state == null || typeof state !== "object") {
|
|
418
|
+
const wrapper = {};
|
|
419
|
+
Object.defineProperties(wrapper, {
|
|
420
|
+
[annotationKey]: {
|
|
421
|
+
value: protectedAnnotations,
|
|
422
|
+
enumerable: true,
|
|
423
|
+
writable: true,
|
|
424
|
+
configurable: true
|
|
425
|
+
},
|
|
426
|
+
[annotationStateValueKey]: {
|
|
427
|
+
value: state,
|
|
428
|
+
enumerable: false,
|
|
429
|
+
writable: true,
|
|
430
|
+
configurable: true
|
|
431
|
+
},
|
|
432
|
+
[annotationWrapperKey]: {
|
|
433
|
+
value: true,
|
|
434
|
+
enumerable: false,
|
|
435
|
+
writable: true,
|
|
436
|
+
configurable: true
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
injectedAnnotationWrappers.add(wrapper);
|
|
440
|
+
return wrapper;
|
|
441
|
+
}
|
|
442
|
+
if (Array.isArray(state)) {
|
|
443
|
+
const cloned$1 = [...state];
|
|
444
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
445
|
+
return cloned$1;
|
|
446
|
+
}
|
|
447
|
+
if (isInjectedAnnotationWrapper(state)) {
|
|
448
|
+
state[annotationKey] = protectedAnnotations;
|
|
449
|
+
return state;
|
|
450
|
+
}
|
|
451
|
+
if (state instanceof Date) {
|
|
452
|
+
const cloned$1 = new Date(state.getTime());
|
|
453
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
454
|
+
return cloned$1;
|
|
455
|
+
}
|
|
456
|
+
if (state instanceof Map) {
|
|
457
|
+
const cloned$1 = new Map(state);
|
|
458
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
459
|
+
return cloned$1;
|
|
460
|
+
}
|
|
461
|
+
if (state instanceof Set) {
|
|
462
|
+
const cloned$1 = new Set(state);
|
|
463
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
464
|
+
return cloned$1;
|
|
465
|
+
}
|
|
466
|
+
if (state instanceof RegExp) {
|
|
467
|
+
const cloned$1 = cloneRegExpShape(state);
|
|
468
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
469
|
+
return cloned$1;
|
|
470
|
+
}
|
|
471
|
+
const proto = Object.getPrototypeOf(state);
|
|
472
|
+
if (proto === Object.prototype || proto === null) return {
|
|
473
|
+
...state,
|
|
474
|
+
[annotationKey]: protectedAnnotations
|
|
475
|
+
};
|
|
476
|
+
const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
|
|
477
|
+
cloned[annotationKey] = protectedAnnotations;
|
|
478
|
+
return cloned;
|
|
479
|
+
}
|
|
377
480
|
/**
|
|
378
481
|
* Extracts annotations from parser state.
|
|
379
482
|
*
|
|
@@ -505,69 +608,22 @@ function hasMeaningfulAnnotations(annotations) {
|
|
|
505
608
|
*/
|
|
506
609
|
function injectAnnotations(state, annotations) {
|
|
507
610
|
if (!hasMeaningfulAnnotations(annotations)) return state;
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
[annotationWrapperKey]: {
|
|
525
|
-
value: true,
|
|
526
|
-
enumerable: false,
|
|
527
|
-
writable: true,
|
|
528
|
-
configurable: true
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
injectedAnnotationWrappers.add(wrapper);
|
|
532
|
-
return wrapper;
|
|
533
|
-
}
|
|
534
|
-
if (Array.isArray(state)) {
|
|
535
|
-
const cloned$1 = [...state];
|
|
536
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
537
|
-
return cloned$1;
|
|
538
|
-
}
|
|
539
|
-
if (isInjectedAnnotationWrapper(state)) {
|
|
540
|
-
state[annotationKey] = protectedAnnotations;
|
|
541
|
-
return state;
|
|
542
|
-
}
|
|
543
|
-
if (state instanceof Date) {
|
|
544
|
-
const cloned$1 = new Date(state.getTime());
|
|
545
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
546
|
-
return cloned$1;
|
|
547
|
-
}
|
|
548
|
-
if (state instanceof Map) {
|
|
549
|
-
const cloned$1 = new Map(state);
|
|
550
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
551
|
-
return cloned$1;
|
|
552
|
-
}
|
|
553
|
-
if (state instanceof Set) {
|
|
554
|
-
const cloned$1 = new Set(state);
|
|
555
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
556
|
-
return cloned$1;
|
|
557
|
-
}
|
|
558
|
-
if (state instanceof RegExp) {
|
|
559
|
-
const cloned$1 = cloneRegExpShape(state);
|
|
560
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
561
|
-
return cloned$1;
|
|
562
|
-
}
|
|
563
|
-
const proto = Object.getPrototypeOf(state);
|
|
564
|
-
if (proto === Object.prototype || proto === null) return {
|
|
565
|
-
...state,
|
|
566
|
-
[annotationKey]: protectedAnnotations
|
|
567
|
-
};
|
|
568
|
-
const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
|
|
569
|
-
cloned[annotationKey] = protectedAnnotations;
|
|
570
|
-
return cloned;
|
|
611
|
+
return injectAnnotationsWithContext(state, annotations, createAnnotationProtectionContext());
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Injects annotations for a fresh parse run.
|
|
615
|
+
*
|
|
616
|
+
* This path re-wraps protected views from previous runs so stateful container
|
|
617
|
+
* internals remain isolated across parse entrypoints.
|
|
618
|
+
*
|
|
619
|
+
* @param state The parser state to annotate.
|
|
620
|
+
* @param annotations The annotations to inject.
|
|
621
|
+
* @returns Annotated state for the fresh run.
|
|
622
|
+
* @internal
|
|
623
|
+
*/
|
|
624
|
+
function injectFreshRunAnnotations(state, annotations) {
|
|
625
|
+
if (!hasMeaningfulAnnotations(annotations)) return state;
|
|
626
|
+
return injectAnnotationsWithContext(state, normalizeRunAnnotationInput(annotations), createAnnotationProtectionContext(true));
|
|
571
627
|
}
|
|
572
628
|
/**
|
|
573
629
|
* Unwraps a primitive-state annotation wrapper injected by Optique internals.
|
|
@@ -607,6 +663,7 @@ exports.getAnnotations = getAnnotations;
|
|
|
607
663
|
exports.hasMeaningfulAnnotations = hasMeaningfulAnnotations;
|
|
608
664
|
exports.inheritAnnotations = inheritAnnotations;
|
|
609
665
|
exports.injectAnnotations = injectAnnotations;
|
|
666
|
+
exports.injectFreshRunAnnotations = injectFreshRunAnnotations;
|
|
610
667
|
exports.isInjectedAnnotationWrapper = isInjectedAnnotationWrapper;
|
|
611
668
|
exports.normalizeRunAnnotationInput = normalizeRunAnnotationInput;
|
|
612
669
|
exports.unwrapInjectedAnnotationWrapper = unwrapInjectedAnnotationWrapper;
|
package/dist/annotations.d.cts
CHANGED
|
@@ -88,7 +88,7 @@ interface ParseOptions {
|
|
|
88
88
|
* Optique treats these values as immutable input and exposes them back to
|
|
89
89
|
* parsers only through protected read-only views.
|
|
90
90
|
*/
|
|
91
|
-
annotations?: Annotations | ReadonlyAnnotations;
|
|
91
|
+
readonly annotations?: Annotations | ReadonlyAnnotations;
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* Extracts annotations from parser state.
|
|
@@ -162,6 +162,18 @@ declare function hasMeaningfulAnnotations(annotations: AnnotationInput | null |
|
|
|
162
162
|
* @internal
|
|
163
163
|
*/
|
|
164
164
|
declare function injectAnnotations<TState>(state: TState, annotations: AnnotationInput): TState;
|
|
165
|
+
/**
|
|
166
|
+
* Injects annotations for a fresh parse run.
|
|
167
|
+
*
|
|
168
|
+
* This path re-wraps protected views from previous runs so stateful container
|
|
169
|
+
* internals remain isolated across parse entrypoints.
|
|
170
|
+
*
|
|
171
|
+
* @param state The parser state to annotate.
|
|
172
|
+
* @param annotations The annotations to inject.
|
|
173
|
+
* @returns Annotated state for the fresh run.
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
declare function injectFreshRunAnnotations<TState>(state: TState, annotations: AnnotationInput): TState;
|
|
165
177
|
/**
|
|
166
178
|
* Unwraps a primitive-state annotation wrapper injected by Optique internals.
|
|
167
179
|
*
|
|
@@ -181,4 +193,4 @@ declare function unwrapInjectedAnnotationWrapper<T>(value: T): T;
|
|
|
181
193
|
*/
|
|
182
194
|
declare function isInjectedAnnotationWrapper(value: unknown): boolean;
|
|
183
195
|
//#endregion
|
|
184
|
-
export { Annotations, ParseOptions, ReadonlyAnnotations, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
|
196
|
+
export { Annotations, ParseOptions, ReadonlyAnnotations, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, injectFreshRunAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
package/dist/annotations.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ interface ParseOptions {
|
|
|
88
88
|
* Optique treats these values as immutable input and exposes them back to
|
|
89
89
|
* parsers only through protected read-only views.
|
|
90
90
|
*/
|
|
91
|
-
annotations?: Annotations | ReadonlyAnnotations;
|
|
91
|
+
readonly annotations?: Annotations | ReadonlyAnnotations;
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* Extracts annotations from parser state.
|
|
@@ -162,6 +162,18 @@ declare function hasMeaningfulAnnotations(annotations: AnnotationInput | null |
|
|
|
162
162
|
* @internal
|
|
163
163
|
*/
|
|
164
164
|
declare function injectAnnotations<TState>(state: TState, annotations: AnnotationInput): TState;
|
|
165
|
+
/**
|
|
166
|
+
* Injects annotations for a fresh parse run.
|
|
167
|
+
*
|
|
168
|
+
* This path re-wraps protected views from previous runs so stateful container
|
|
169
|
+
* internals remain isolated across parse entrypoints.
|
|
170
|
+
*
|
|
171
|
+
* @param state The parser state to annotate.
|
|
172
|
+
* @param annotations The annotations to inject.
|
|
173
|
+
* @returns Annotated state for the fresh run.
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
declare function injectFreshRunAnnotations<TState>(state: TState, annotations: AnnotationInput): TState;
|
|
165
177
|
/**
|
|
166
178
|
* Unwraps a primitive-state annotation wrapper injected by Optique internals.
|
|
167
179
|
*
|
|
@@ -181,4 +193,4 @@ declare function unwrapInjectedAnnotationWrapper<T>(value: T): T;
|
|
|
181
193
|
*/
|
|
182
194
|
declare function isInjectedAnnotationWrapper(value: unknown): boolean;
|
|
183
195
|
//#endregion
|
|
184
|
-
export { Annotations, ParseOptions, ReadonlyAnnotations, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
|
196
|
+
export { Annotations, ParseOptions, ReadonlyAnnotations, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, injectFreshRunAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
package/dist/annotations.js
CHANGED
|
@@ -49,14 +49,20 @@ const protectedAnnotationStateViews = /* @__PURE__ */ new WeakMap();
|
|
|
49
49
|
function throwReadonlyAnnotationMutation() {
|
|
50
50
|
throw new TypeError("Cannot mutate read-only annotation data.");
|
|
51
51
|
}
|
|
52
|
-
function createAnnotationProtectionContext() {
|
|
53
|
-
return {
|
|
52
|
+
function createAnnotationProtectionContext(rewrapProtectedViews = false) {
|
|
53
|
+
return {
|
|
54
|
+
cache: /* @__PURE__ */ new WeakMap(),
|
|
55
|
+
rewrapProtectedViews
|
|
56
|
+
};
|
|
54
57
|
}
|
|
55
58
|
function registerProtectedAnnotationView(context, target, view) {
|
|
56
59
|
context.cache.set(target, view);
|
|
57
60
|
protectedAnnotationTargets.set(view, target);
|
|
58
61
|
return view;
|
|
59
62
|
}
|
|
63
|
+
function cacheProtectedAnnotationViewAlias(context, alias, view) {
|
|
64
|
+
context.cache.set(alias, view);
|
|
65
|
+
}
|
|
60
66
|
function isProtectedAnnotationView(value) {
|
|
61
67
|
return value != null && typeof value === "object" && protectedAnnotationTargets.has(value);
|
|
62
68
|
}
|
|
@@ -80,11 +86,11 @@ function defineProtectedDataProperty(context, target, key, descriptor) {
|
|
|
80
86
|
set: () => throwReadonlyAnnotationMutation()
|
|
81
87
|
});
|
|
82
88
|
}
|
|
83
|
-
function
|
|
89
|
+
function copyOwnProperties(source, target, transformValue, excludedKeys) {
|
|
84
90
|
const sourcePrototype = Object.getPrototypeOf(source);
|
|
85
91
|
if (Object.getPrototypeOf(target) !== sourcePrototype) Object.setPrototypeOf(target, sourcePrototype);
|
|
86
92
|
for (const key of Reflect.ownKeys(source)) {
|
|
87
|
-
if (key ===
|
|
93
|
+
if (excludedKeys?.has(key) === true) continue;
|
|
88
94
|
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
89
95
|
if (descriptor == null) continue;
|
|
90
96
|
if ("value" in descriptor && transformValue != null) {
|
|
@@ -96,6 +102,13 @@ function copyRegExpMetadata(source, target, transformValue) {
|
|
|
96
102
|
}
|
|
97
103
|
Object.defineProperty(target, key, descriptor);
|
|
98
104
|
}
|
|
105
|
+
}
|
|
106
|
+
function normalizeProtectedCollectionItem(value, context) {
|
|
107
|
+
return context.rewrapProtectedViews && isProtectedAnnotationView(value) ? unwrapProtectedAnnotationTarget(value) : value;
|
|
108
|
+
}
|
|
109
|
+
const regExpExcludedKeys = new Set(["lastIndex"]);
|
|
110
|
+
function copyRegExpMetadata(source, target, transformValue) {
|
|
111
|
+
copyOwnProperties(source, target, transformValue, regExpExcludedKeys);
|
|
99
112
|
target.lastIndex = source.lastIndex;
|
|
100
113
|
}
|
|
101
114
|
function cloneRegExpShape(source) {
|
|
@@ -105,7 +118,8 @@ function cloneRegExpShape(source) {
|
|
|
105
118
|
}
|
|
106
119
|
function createProtectedObjectView(target, context) {
|
|
107
120
|
if (Array.isArray(target)) {
|
|
108
|
-
const view$1 =
|
|
121
|
+
const view$1 = Object.setPrototypeOf([], Object.getPrototypeOf(target));
|
|
122
|
+
view$1.length = target.length;
|
|
109
123
|
registerProtectedAnnotationView(context, target, view$1);
|
|
110
124
|
for (const key of Reflect.ownKeys(target)) {
|
|
111
125
|
if (key === "length") continue;
|
|
@@ -144,26 +158,28 @@ function createProtectedObjectView(target, context) {
|
|
|
144
158
|
}
|
|
145
159
|
function createProtectedMapView(target, context) {
|
|
146
160
|
const methodCache = /* @__PURE__ */ new Map();
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
161
|
+
const cloned = /* @__PURE__ */ new Map();
|
|
162
|
+
for (const [entryKey, entryValue] of target.entries()) cloned.set(normalizeProtectedCollectionItem(entryKey, context), normalizeProtectedCollectionItem(entryValue, context));
|
|
163
|
+
const view = new Proxy(cloned, {
|
|
164
|
+
get(clonedTarget, key) {
|
|
165
|
+
if (key === "size") return clonedTarget.size;
|
|
150
166
|
if (key === "set" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
151
|
-
if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(
|
|
152
|
-
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) =>
|
|
153
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
167
|
+
if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(clonedTarget.get(unwrapProtectedAnnotationTarget(lookup)), context));
|
|
168
|
+
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => clonedTarget.has(unwrapProtectedAnnotationTarget(lookup)));
|
|
169
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1, mapKey) => {
|
|
154
170
|
callback.call(thisArg, protectAnnotationValue(value$1, context), protectAnnotationValue(mapKey, context), view);
|
|
155
171
|
}));
|
|
156
172
|
if (key === "keys") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
157
|
-
for (const value$1 of
|
|
173
|
+
for (const value$1 of clonedTarget.keys()) yield protectAnnotationValue(value$1, context);
|
|
158
174
|
});
|
|
159
175
|
if (key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
160
|
-
for (const value$1 of
|
|
176
|
+
for (const value$1 of clonedTarget.values()) yield protectAnnotationValue(value$1, context);
|
|
161
177
|
});
|
|
162
178
|
if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
163
|
-
for (const [entryKey, entryValue] of
|
|
179
|
+
for (const [entryKey, entryValue] of clonedTarget.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
|
|
164
180
|
});
|
|
165
|
-
const value = Reflect.get(
|
|
166
|
-
return typeof value === "function" ? value.bind(
|
|
181
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
182
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
167
183
|
},
|
|
168
184
|
set() {
|
|
169
185
|
throwReadonlyAnnotationMutation();
|
|
@@ -181,30 +197,35 @@ function createProtectedMapView(target, context) {
|
|
|
181
197
|
throwReadonlyAnnotationMutation();
|
|
182
198
|
}
|
|
183
199
|
});
|
|
184
|
-
|
|
200
|
+
registerProtectedAnnotationView(context, target, view);
|
|
201
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
202
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
203
|
+
return view;
|
|
185
204
|
}
|
|
186
205
|
function createProtectedSetView(target, context) {
|
|
187
206
|
const methodCache = /* @__PURE__ */ new Map();
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
207
|
+
const cloned = /* @__PURE__ */ new Set();
|
|
208
|
+
for (const value of target.values()) cloned.add(normalizeProtectedCollectionItem(value, context));
|
|
209
|
+
const view = new Proxy(cloned, {
|
|
210
|
+
get(clonedTarget, key) {
|
|
211
|
+
if (key === "size") return clonedTarget.size;
|
|
191
212
|
if (key === "add" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
192
|
-
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) =>
|
|
193
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
213
|
+
if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => clonedTarget.has(unwrapProtectedAnnotationTarget(lookup)));
|
|
214
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1) => {
|
|
194
215
|
const protectedValue = protectAnnotationValue(value$1, context);
|
|
195
216
|
callback.call(thisArg, protectedValue, protectedValue, view);
|
|
196
217
|
}));
|
|
197
218
|
if (key === "keys" || key === "values" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
198
|
-
for (const value$1 of
|
|
219
|
+
for (const value$1 of clonedTarget.values()) yield protectAnnotationValue(value$1, context);
|
|
199
220
|
});
|
|
200
221
|
if (key === "entries") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
201
|
-
for (const value$1 of
|
|
222
|
+
for (const value$1 of clonedTarget.values()) {
|
|
202
223
|
const protectedValue = protectAnnotationValue(value$1, context);
|
|
203
224
|
yield [protectedValue, protectedValue];
|
|
204
225
|
}
|
|
205
226
|
});
|
|
206
|
-
const value = Reflect.get(
|
|
207
|
-
return typeof value === "function" ? value.bind(
|
|
227
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
228
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
208
229
|
},
|
|
209
230
|
set() {
|
|
210
231
|
throwReadonlyAnnotationMutation();
|
|
@@ -222,15 +243,19 @@ function createProtectedSetView(target, context) {
|
|
|
222
243
|
throwReadonlyAnnotationMutation();
|
|
223
244
|
}
|
|
224
245
|
});
|
|
225
|
-
|
|
246
|
+
registerProtectedAnnotationView(context, target, view);
|
|
247
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
248
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
249
|
+
return view;
|
|
226
250
|
}
|
|
227
251
|
function createProtectedDateView(target, context) {
|
|
228
252
|
const methodCache = /* @__PURE__ */ new Map();
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
253
|
+
const cloned = new Date(target.getTime());
|
|
254
|
+
const view = new Proxy(cloned, {
|
|
255
|
+
get(clonedTarget, key) {
|
|
256
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
232
257
|
if (typeof key === "string" && key.startsWith("set")) return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
233
|
-
return typeof value === "function" ? value.bind(
|
|
258
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
234
259
|
},
|
|
235
260
|
set() {
|
|
236
261
|
throwReadonlyAnnotationMutation();
|
|
@@ -248,7 +273,10 @@ function createProtectedDateView(target, context) {
|
|
|
248
273
|
throwReadonlyAnnotationMutation();
|
|
249
274
|
}
|
|
250
275
|
});
|
|
251
|
-
|
|
276
|
+
registerProtectedAnnotationView(context, target, view);
|
|
277
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
278
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
279
|
+
return view;
|
|
252
280
|
}
|
|
253
281
|
function createProtectedRegExpView(target, context) {
|
|
254
282
|
const methodCache = /* @__PURE__ */ new Map();
|
|
@@ -278,26 +306,28 @@ function createProtectedRegExpView(target, context) {
|
|
|
278
306
|
}
|
|
279
307
|
});
|
|
280
308
|
registerProtectedAnnotationView(context, target, view);
|
|
309
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
281
310
|
copyRegExpMetadata(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
282
311
|
return view;
|
|
283
312
|
}
|
|
284
313
|
function createProtectedURLSearchParamsView(target, context) {
|
|
285
314
|
const methodCache = /* @__PURE__ */ new Map();
|
|
286
|
-
const
|
|
287
|
-
|
|
315
|
+
const cloned = new URLSearchParams(target);
|
|
316
|
+
const view = new Proxy(cloned, {
|
|
317
|
+
get(clonedTarget, key) {
|
|
288
318
|
if (key === "append" || key === "delete" || key === "set" || key === "sort") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
|
|
289
|
-
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) =>
|
|
319
|
+
if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => clonedTarget.forEach((value$1, name) => {
|
|
290
320
|
callback.call(thisArg, value$1, name, view);
|
|
291
321
|
}));
|
|
292
322
|
if (key === "keys" || key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
293
|
-
const iterator = key === "keys" ?
|
|
323
|
+
const iterator = key === "keys" ? clonedTarget.keys() : clonedTarget.values();
|
|
294
324
|
for (const value$1 of iterator) yield value$1;
|
|
295
325
|
});
|
|
296
326
|
if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
|
|
297
|
-
for (const entry of
|
|
327
|
+
for (const entry of clonedTarget.entries()) yield entry;
|
|
298
328
|
});
|
|
299
|
-
const value = Reflect.get(
|
|
300
|
-
return typeof value === "function" ? value.bind(
|
|
329
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
330
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
301
331
|
},
|
|
302
332
|
set() {
|
|
303
333
|
throwReadonlyAnnotationMutation();
|
|
@@ -315,14 +345,18 @@ function createProtectedURLSearchParamsView(target, context) {
|
|
|
315
345
|
throwReadonlyAnnotationMutation();
|
|
316
346
|
}
|
|
317
347
|
});
|
|
318
|
-
|
|
348
|
+
registerProtectedAnnotationView(context, target, view);
|
|
349
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
350
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
351
|
+
return view;
|
|
319
352
|
}
|
|
320
353
|
function createProtectedURLView(target, context) {
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
354
|
+
const cloned = new URL(target.href);
|
|
355
|
+
const view = new Proxy(cloned, {
|
|
356
|
+
get(clonedTarget, key) {
|
|
357
|
+
if (key === "searchParams") return protectAnnotationValue(clonedTarget.searchParams, context);
|
|
358
|
+
const value = Reflect.get(clonedTarget, key, clonedTarget);
|
|
359
|
+
return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
|
|
326
360
|
},
|
|
327
361
|
set() {
|
|
328
362
|
throwReadonlyAnnotationMutation();
|
|
@@ -340,12 +374,16 @@ function createProtectedURLView(target, context) {
|
|
|
340
374
|
throwReadonlyAnnotationMutation();
|
|
341
375
|
}
|
|
342
376
|
});
|
|
343
|
-
|
|
377
|
+
registerProtectedAnnotationView(context, target, view);
|
|
378
|
+
cacheProtectedAnnotationViewAlias(context, cloned, view);
|
|
379
|
+
copyOwnProperties(target, cloned, (value) => protectAnnotationValue(value, context));
|
|
380
|
+
return view;
|
|
344
381
|
}
|
|
345
382
|
function protectAnnotationValue(value, context) {
|
|
346
383
|
if (value == null || typeof value !== "object") return value;
|
|
347
|
-
const
|
|
348
|
-
if (
|
|
384
|
+
const isProtectedView = isProtectedAnnotationView(value);
|
|
385
|
+
if (isProtectedView && !context.rewrapProtectedViews) return value;
|
|
386
|
+
const target = isProtectedView ? unwrapProtectedAnnotationTarget(value) : value;
|
|
349
387
|
const cached = context.cache.get(target);
|
|
350
388
|
if (cached !== void 0) return cached;
|
|
351
389
|
if (target instanceof Map) return createProtectedMapView(target, context);
|
|
@@ -373,6 +411,71 @@ function protectAnnotationValue(value, context) {
|
|
|
373
411
|
function normalizeRunAnnotationInput(annotations) {
|
|
374
412
|
return isProtectedAnnotationView(annotations) ? unwrapProtectedAnnotationTarget(annotations) : annotations;
|
|
375
413
|
}
|
|
414
|
+
function injectAnnotationsWithContext(state, annotations, context) {
|
|
415
|
+
const protectedAnnotations = protectAnnotationValue(annotations, context);
|
|
416
|
+
if (state == null || typeof state !== "object") {
|
|
417
|
+
const wrapper = {};
|
|
418
|
+
Object.defineProperties(wrapper, {
|
|
419
|
+
[annotationKey]: {
|
|
420
|
+
value: protectedAnnotations,
|
|
421
|
+
enumerable: true,
|
|
422
|
+
writable: true,
|
|
423
|
+
configurable: true
|
|
424
|
+
},
|
|
425
|
+
[annotationStateValueKey]: {
|
|
426
|
+
value: state,
|
|
427
|
+
enumerable: false,
|
|
428
|
+
writable: true,
|
|
429
|
+
configurable: true
|
|
430
|
+
},
|
|
431
|
+
[annotationWrapperKey]: {
|
|
432
|
+
value: true,
|
|
433
|
+
enumerable: false,
|
|
434
|
+
writable: true,
|
|
435
|
+
configurable: true
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
injectedAnnotationWrappers.add(wrapper);
|
|
439
|
+
return wrapper;
|
|
440
|
+
}
|
|
441
|
+
if (Array.isArray(state)) {
|
|
442
|
+
const cloned$1 = [...state];
|
|
443
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
444
|
+
return cloned$1;
|
|
445
|
+
}
|
|
446
|
+
if (isInjectedAnnotationWrapper(state)) {
|
|
447
|
+
state[annotationKey] = protectedAnnotations;
|
|
448
|
+
return state;
|
|
449
|
+
}
|
|
450
|
+
if (state instanceof Date) {
|
|
451
|
+
const cloned$1 = new Date(state.getTime());
|
|
452
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
453
|
+
return cloned$1;
|
|
454
|
+
}
|
|
455
|
+
if (state instanceof Map) {
|
|
456
|
+
const cloned$1 = new Map(state);
|
|
457
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
458
|
+
return cloned$1;
|
|
459
|
+
}
|
|
460
|
+
if (state instanceof Set) {
|
|
461
|
+
const cloned$1 = new Set(state);
|
|
462
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
463
|
+
return cloned$1;
|
|
464
|
+
}
|
|
465
|
+
if (state instanceof RegExp) {
|
|
466
|
+
const cloned$1 = cloneRegExpShape(state);
|
|
467
|
+
cloned$1[annotationKey] = protectedAnnotations;
|
|
468
|
+
return cloned$1;
|
|
469
|
+
}
|
|
470
|
+
const proto = Object.getPrototypeOf(state);
|
|
471
|
+
if (proto === Object.prototype || proto === null) return {
|
|
472
|
+
...state,
|
|
473
|
+
[annotationKey]: protectedAnnotations
|
|
474
|
+
};
|
|
475
|
+
const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
|
|
476
|
+
cloned[annotationKey] = protectedAnnotations;
|
|
477
|
+
return cloned;
|
|
478
|
+
}
|
|
376
479
|
/**
|
|
377
480
|
* Extracts annotations from parser state.
|
|
378
481
|
*
|
|
@@ -504,69 +607,22 @@ function hasMeaningfulAnnotations(annotations) {
|
|
|
504
607
|
*/
|
|
505
608
|
function injectAnnotations(state, annotations) {
|
|
506
609
|
if (!hasMeaningfulAnnotations(annotations)) return state;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
[annotationWrapperKey]: {
|
|
524
|
-
value: true,
|
|
525
|
-
enumerable: false,
|
|
526
|
-
writable: true,
|
|
527
|
-
configurable: true
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
injectedAnnotationWrappers.add(wrapper);
|
|
531
|
-
return wrapper;
|
|
532
|
-
}
|
|
533
|
-
if (Array.isArray(state)) {
|
|
534
|
-
const cloned$1 = [...state];
|
|
535
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
536
|
-
return cloned$1;
|
|
537
|
-
}
|
|
538
|
-
if (isInjectedAnnotationWrapper(state)) {
|
|
539
|
-
state[annotationKey] = protectedAnnotations;
|
|
540
|
-
return state;
|
|
541
|
-
}
|
|
542
|
-
if (state instanceof Date) {
|
|
543
|
-
const cloned$1 = new Date(state.getTime());
|
|
544
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
545
|
-
return cloned$1;
|
|
546
|
-
}
|
|
547
|
-
if (state instanceof Map) {
|
|
548
|
-
const cloned$1 = new Map(state);
|
|
549
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
550
|
-
return cloned$1;
|
|
551
|
-
}
|
|
552
|
-
if (state instanceof Set) {
|
|
553
|
-
const cloned$1 = new Set(state);
|
|
554
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
555
|
-
return cloned$1;
|
|
556
|
-
}
|
|
557
|
-
if (state instanceof RegExp) {
|
|
558
|
-
const cloned$1 = cloneRegExpShape(state);
|
|
559
|
-
cloned$1[annotationKey] = protectedAnnotations;
|
|
560
|
-
return cloned$1;
|
|
561
|
-
}
|
|
562
|
-
const proto = Object.getPrototypeOf(state);
|
|
563
|
-
if (proto === Object.prototype || proto === null) return {
|
|
564
|
-
...state,
|
|
565
|
-
[annotationKey]: protectedAnnotations
|
|
566
|
-
};
|
|
567
|
-
const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
|
|
568
|
-
cloned[annotationKey] = protectedAnnotations;
|
|
569
|
-
return cloned;
|
|
610
|
+
return injectAnnotationsWithContext(state, annotations, createAnnotationProtectionContext());
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Injects annotations for a fresh parse run.
|
|
614
|
+
*
|
|
615
|
+
* This path re-wraps protected views from previous runs so stateful container
|
|
616
|
+
* internals remain isolated across parse entrypoints.
|
|
617
|
+
*
|
|
618
|
+
* @param state The parser state to annotate.
|
|
619
|
+
* @param annotations The annotations to inject.
|
|
620
|
+
* @returns Annotated state for the fresh run.
|
|
621
|
+
* @internal
|
|
622
|
+
*/
|
|
623
|
+
function injectFreshRunAnnotations(state, annotations) {
|
|
624
|
+
if (!hasMeaningfulAnnotations(annotations)) return state;
|
|
625
|
+
return injectAnnotationsWithContext(state, normalizeRunAnnotationInput(annotations), createAnnotationProtectionContext(true));
|
|
570
626
|
}
|
|
571
627
|
/**
|
|
572
628
|
* Unwraps a primitive-state annotation wrapper injected by Optique internals.
|
|
@@ -597,4 +653,4 @@ function isInjectedAnnotationWrapper(value) {
|
|
|
597
653
|
}
|
|
598
654
|
|
|
599
655
|
//#endregion
|
|
600
|
-
export { annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
|
656
|
+
export { annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, hasMeaningfulAnnotations, inheritAnnotations, injectAnnotations, injectFreshRunAnnotations, isInjectedAnnotationWrapper, normalizeRunAnnotationInput, unwrapInjectedAnnotationWrapper };
|
package/dist/parser.cjs
CHANGED
|
@@ -69,7 +69,7 @@ function createParserContext(frame, exec) {
|
|
|
69
69
|
function injectAnnotationsIntoState(state, options) {
|
|
70
70
|
const annotations = options?.annotations;
|
|
71
71
|
if (!require_annotations.hasMeaningfulAnnotations(annotations)) return state;
|
|
72
|
-
return require_annotations.
|
|
72
|
+
return require_annotations.injectFreshRunAnnotations(state, annotations);
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Parses an array of command-line arguments using the provided combined parser.
|
package/dist/parser.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { hasMeaningfulAnnotations,
|
|
1
|
+
import { hasMeaningfulAnnotations, injectFreshRunAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper } from "./annotations.js";
|
|
2
2
|
import { cloneMessage, message } from "./message.js";
|
|
3
3
|
import { cloneUsage, normalizeUsage } from "./usage.js";
|
|
4
4
|
import { cloneDocEntry, isDocEntryHidden } from "./doc.js";
|
|
@@ -69,7 +69,7 @@ function createParserContext(frame, exec) {
|
|
|
69
69
|
function injectAnnotationsIntoState(state, options) {
|
|
70
70
|
const annotations = options?.annotations;
|
|
71
71
|
if (!hasMeaningfulAnnotations(annotations)) return state;
|
|
72
|
-
return
|
|
72
|
+
return injectFreshRunAnnotations(state, annotations);
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Parses an array of command-line arguments using the provided combined parser.
|