@optique/core 1.0.0-dev.1874 → 1.0.0-dev.1878

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.
@@ -78,6 +78,11 @@ function cacheProtectedMethod(cache, key, factory) {
78
78
  cache.set(key, created);
79
79
  return created;
80
80
  }
81
+ function getProtectedClonePropertyValue(cache, target, key, value, context) {
82
+ if (typeof value !== "function") return protectAnnotationValue(value, context);
83
+ if (key === "constructor") return value;
84
+ return cacheProtectedMethod(cache, key, () => value.bind(target));
85
+ }
81
86
  function defineProtectedDataProperty(context, target, key, descriptor) {
82
87
  const value = protectAnnotationValue(descriptor.value, context);
83
88
  Object.defineProperty(target, key, {
@@ -118,21 +123,27 @@ function hasBuiltInSubclassPrototype(target, basePrototype) {
118
123
  return Object.getPrototypeOf(target) !== basePrototype;
119
124
  }
120
125
  function tryCloneMapSubclass(source, entries) {
126
+ const entriesArray = [...entries];
121
127
  const cloneConstructor = resolveCloneConstructor(source);
122
128
  if (cloneConstructor == null) return void 0;
123
129
  try {
124
- const cloned = new cloneConstructor(entries);
125
- return cloned instanceof Map ? cloned : void 0;
130
+ const cloned = new cloneConstructor(entriesArray);
131
+ if (!(cloned instanceof Map) || cloned.size !== entriesArray.length) return void 0;
132
+ for (const [key, value] of entriesArray) if (!cloned.has(key) || !Object.is(cloned.get(key), value)) return void 0;
133
+ return cloned;
126
134
  } catch {
127
135
  return void 0;
128
136
  }
129
137
  }
130
138
  function tryCloneSetSubclass(source, values) {
139
+ const valuesArray = [...values];
131
140
  const cloneConstructor = resolveCloneConstructor(source);
132
141
  if (cloneConstructor == null) return void 0;
133
142
  try {
134
- const cloned = new cloneConstructor(values);
135
- return cloned instanceof Set ? cloned : void 0;
143
+ const cloned = new cloneConstructor(valuesArray);
144
+ if (!(cloned instanceof Set) || cloned.size !== valuesArray.length) return void 0;
145
+ for (const value of valuesArray) if (!cloned.has(value)) return void 0;
146
+ return cloned;
136
147
  } catch {
137
148
  return void 0;
138
149
  }
@@ -142,7 +153,7 @@ function tryCloneDateSubclass(source) {
142
153
  if (cloneConstructor == null) return void 0;
143
154
  try {
144
155
  const cloned = new cloneConstructor(source.getTime());
145
- return cloned instanceof Date ? cloned : void 0;
156
+ return cloned instanceof Date && cloned.getTime() === source.getTime() ? cloned : void 0;
146
157
  } catch {
147
158
  return void 0;
148
159
  }
@@ -152,17 +163,18 @@ function tryCloneRegExpSubclass(source) {
152
163
  if (cloneConstructor == null) return void 0;
153
164
  try {
154
165
  const cloned = new cloneConstructor(source.source, source.flags);
155
- return cloned instanceof RegExp ? cloned : void 0;
166
+ return cloned instanceof RegExp && cloned.source === source.source && cloned.flags === source.flags ? cloned : void 0;
156
167
  } catch {
157
168
  return void 0;
158
169
  }
159
170
  }
160
171
  function tryCloneArraySubclass(source) {
172
+ const entries = [...source];
161
173
  const cloneConstructor = resolveCloneConstructor(source);
162
174
  if (cloneConstructor == null) return void 0;
163
175
  try {
164
- const cloned = Reflect.apply(Array.from, cloneConstructor, [source]);
165
- return Array.isArray(cloned) ? cloned : void 0;
176
+ const cloned = Reflect.apply(Array.from, cloneConstructor, [entries]);
177
+ return Array.isArray(cloned) && cloned.length === entries.length && entries.every((value, index) => Object.is(cloned[index], value)) ? cloned : void 0;
166
178
  } catch {
167
179
  return void 0;
168
180
  }
@@ -172,7 +184,7 @@ function tryCloneURLSearchParamsSubclass(source) {
172
184
  if (cloneConstructor == null) return void 0;
173
185
  try {
174
186
  const cloned = new cloneConstructor(source);
175
- return cloned instanceof URLSearchParams ? cloned : void 0;
187
+ return cloned instanceof URLSearchParams && cloned.toString() === source.toString() ? cloned : void 0;
176
188
  } catch {
177
189
  return void 0;
178
190
  }
@@ -182,7 +194,7 @@ function tryCloneURLSubclass(source) {
182
194
  if (cloneConstructor == null) return void 0;
183
195
  try {
184
196
  const cloned = new cloneConstructor(source.href);
185
- return cloned instanceof URL ? cloned : void 0;
197
+ return cloned instanceof URL && cloned.href === source.href ? cloned : void 0;
186
198
  } catch {
187
199
  return void 0;
188
200
  }
@@ -328,7 +340,7 @@ function createProtectedMapView(target, context) {
328
340
  for (const [entryKey, entryValue] of clonedTarget.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
329
341
  });
330
342
  const value = Reflect.get(clonedTarget, key, clonedTarget);
331
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
343
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
332
344
  },
333
345
  set() {
334
346
  throwReadonlyAnnotationMutation();
@@ -377,7 +389,7 @@ function createProtectedSetView(target, context) {
377
389
  }
378
390
  });
379
391
  const value = Reflect.get(clonedTarget, key, clonedTarget);
380
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
392
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
381
393
  },
382
394
  set() {
383
395
  throwReadonlyAnnotationMutation();
@@ -409,7 +421,7 @@ function createProtectedDateView(target, context) {
409
421
  get(clonedTarget, key) {
410
422
  const value = Reflect.get(clonedTarget, key, clonedTarget);
411
423
  if (typeof key === "string" && key.startsWith("set")) return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
412
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
424
+ return typeof value === "function" ? getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context) : protectAnnotationValue(value, context);
413
425
  },
414
426
  set() {
415
427
  throwReadonlyAnnotationMutation();
@@ -444,7 +456,7 @@ function createProtectedRegExpView(target, context) {
444
456
  const ownDescriptor = Object.getOwnPropertyDescriptor(clonedTarget, key);
445
457
  if (ownDescriptor != null && "value" in ownDescriptor) return ownDescriptor.value;
446
458
  const value = Reflect.get(clonedTarget, key, clonedTarget);
447
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
459
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
448
460
  },
449
461
  set() {
450
462
  throwReadonlyAnnotationMutation();
@@ -486,7 +498,7 @@ function createProtectedURLSearchParamsView(target, context) {
486
498
  for (const entry of clonedTarget.entries()) yield entry;
487
499
  });
488
500
  const value = Reflect.get(clonedTarget, key, clonedTarget);
489
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
501
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
490
502
  },
491
503
  set() {
492
504
  throwReadonlyAnnotationMutation();
@@ -512,13 +524,14 @@ function createProtectedURLSearchParamsView(target, context) {
512
524
  }
513
525
  function createProtectedURLView(target, context) {
514
526
  const syncPrototype = !hasBuiltInSubclassPrototype(target, URL.prototype);
527
+ const methodCache = /* @__PURE__ */ new Map();
515
528
  const cloned = syncPrototype ? new URL(target.href) : tryCloneURLSubclass(target) ?? new URL(target.href);
516
529
  const view = new Proxy(cloned, {
517
530
  get(clonedTarget, key) {
518
531
  if (key === "valueOf") return () => view;
519
532
  if (key === "searchParams") return protectAnnotationValue(clonedTarget.searchParams, context);
520
533
  const value = Reflect.get(clonedTarget, key, clonedTarget);
521
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
534
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
522
535
  },
523
536
  set() {
524
537
  throwReadonlyAnnotationMutation();
@@ -77,6 +77,11 @@ function cacheProtectedMethod(cache, key, factory) {
77
77
  cache.set(key, created);
78
78
  return created;
79
79
  }
80
+ function getProtectedClonePropertyValue(cache, target, key, value, context) {
81
+ if (typeof value !== "function") return protectAnnotationValue(value, context);
82
+ if (key === "constructor") return value;
83
+ return cacheProtectedMethod(cache, key, () => value.bind(target));
84
+ }
80
85
  function defineProtectedDataProperty(context, target, key, descriptor) {
81
86
  const value = protectAnnotationValue(descriptor.value, context);
82
87
  Object.defineProperty(target, key, {
@@ -117,21 +122,27 @@ function hasBuiltInSubclassPrototype(target, basePrototype) {
117
122
  return Object.getPrototypeOf(target) !== basePrototype;
118
123
  }
119
124
  function tryCloneMapSubclass(source, entries) {
125
+ const entriesArray = [...entries];
120
126
  const cloneConstructor = resolveCloneConstructor(source);
121
127
  if (cloneConstructor == null) return void 0;
122
128
  try {
123
- const cloned = new cloneConstructor(entries);
124
- return cloned instanceof Map ? cloned : void 0;
129
+ const cloned = new cloneConstructor(entriesArray);
130
+ if (!(cloned instanceof Map) || cloned.size !== entriesArray.length) return void 0;
131
+ for (const [key, value] of entriesArray) if (!cloned.has(key) || !Object.is(cloned.get(key), value)) return void 0;
132
+ return cloned;
125
133
  } catch {
126
134
  return void 0;
127
135
  }
128
136
  }
129
137
  function tryCloneSetSubclass(source, values) {
138
+ const valuesArray = [...values];
130
139
  const cloneConstructor = resolveCloneConstructor(source);
131
140
  if (cloneConstructor == null) return void 0;
132
141
  try {
133
- const cloned = new cloneConstructor(values);
134
- return cloned instanceof Set ? cloned : void 0;
142
+ const cloned = new cloneConstructor(valuesArray);
143
+ if (!(cloned instanceof Set) || cloned.size !== valuesArray.length) return void 0;
144
+ for (const value of valuesArray) if (!cloned.has(value)) return void 0;
145
+ return cloned;
135
146
  } catch {
136
147
  return void 0;
137
148
  }
@@ -141,7 +152,7 @@ function tryCloneDateSubclass(source) {
141
152
  if (cloneConstructor == null) return void 0;
142
153
  try {
143
154
  const cloned = new cloneConstructor(source.getTime());
144
- return cloned instanceof Date ? cloned : void 0;
155
+ return cloned instanceof Date && cloned.getTime() === source.getTime() ? cloned : void 0;
145
156
  } catch {
146
157
  return void 0;
147
158
  }
@@ -151,17 +162,18 @@ function tryCloneRegExpSubclass(source) {
151
162
  if (cloneConstructor == null) return void 0;
152
163
  try {
153
164
  const cloned = new cloneConstructor(source.source, source.flags);
154
- return cloned instanceof RegExp ? cloned : void 0;
165
+ return cloned instanceof RegExp && cloned.source === source.source && cloned.flags === source.flags ? cloned : void 0;
155
166
  } catch {
156
167
  return void 0;
157
168
  }
158
169
  }
159
170
  function tryCloneArraySubclass(source) {
171
+ const entries = [...source];
160
172
  const cloneConstructor = resolveCloneConstructor(source);
161
173
  if (cloneConstructor == null) return void 0;
162
174
  try {
163
- const cloned = Reflect.apply(Array.from, cloneConstructor, [source]);
164
- return Array.isArray(cloned) ? cloned : void 0;
175
+ const cloned = Reflect.apply(Array.from, cloneConstructor, [entries]);
176
+ return Array.isArray(cloned) && cloned.length === entries.length && entries.every((value, index) => Object.is(cloned[index], value)) ? cloned : void 0;
165
177
  } catch {
166
178
  return void 0;
167
179
  }
@@ -171,7 +183,7 @@ function tryCloneURLSearchParamsSubclass(source) {
171
183
  if (cloneConstructor == null) return void 0;
172
184
  try {
173
185
  const cloned = new cloneConstructor(source);
174
- return cloned instanceof URLSearchParams ? cloned : void 0;
186
+ return cloned instanceof URLSearchParams && cloned.toString() === source.toString() ? cloned : void 0;
175
187
  } catch {
176
188
  return void 0;
177
189
  }
@@ -181,7 +193,7 @@ function tryCloneURLSubclass(source) {
181
193
  if (cloneConstructor == null) return void 0;
182
194
  try {
183
195
  const cloned = new cloneConstructor(source.href);
184
- return cloned instanceof URL ? cloned : void 0;
196
+ return cloned instanceof URL && cloned.href === source.href ? cloned : void 0;
185
197
  } catch {
186
198
  return void 0;
187
199
  }
@@ -327,7 +339,7 @@ function createProtectedMapView(target, context) {
327
339
  for (const [entryKey, entryValue] of clonedTarget.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
328
340
  });
329
341
  const value = Reflect.get(clonedTarget, key, clonedTarget);
330
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
342
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
331
343
  },
332
344
  set() {
333
345
  throwReadonlyAnnotationMutation();
@@ -376,7 +388,7 @@ function createProtectedSetView(target, context) {
376
388
  }
377
389
  });
378
390
  const value = Reflect.get(clonedTarget, key, clonedTarget);
379
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
391
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
380
392
  },
381
393
  set() {
382
394
  throwReadonlyAnnotationMutation();
@@ -408,7 +420,7 @@ function createProtectedDateView(target, context) {
408
420
  get(clonedTarget, key) {
409
421
  const value = Reflect.get(clonedTarget, key, clonedTarget);
410
422
  if (typeof key === "string" && key.startsWith("set")) return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
411
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
423
+ return typeof value === "function" ? getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context) : protectAnnotationValue(value, context);
412
424
  },
413
425
  set() {
414
426
  throwReadonlyAnnotationMutation();
@@ -443,7 +455,7 @@ function createProtectedRegExpView(target, context) {
443
455
  const ownDescriptor = Object.getOwnPropertyDescriptor(clonedTarget, key);
444
456
  if (ownDescriptor != null && "value" in ownDescriptor) return ownDescriptor.value;
445
457
  const value = Reflect.get(clonedTarget, key, clonedTarget);
446
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
458
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
447
459
  },
448
460
  set() {
449
461
  throwReadonlyAnnotationMutation();
@@ -485,7 +497,7 @@ function createProtectedURLSearchParamsView(target, context) {
485
497
  for (const entry of clonedTarget.entries()) yield entry;
486
498
  });
487
499
  const value = Reflect.get(clonedTarget, key, clonedTarget);
488
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
500
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
489
501
  },
490
502
  set() {
491
503
  throwReadonlyAnnotationMutation();
@@ -511,13 +523,14 @@ function createProtectedURLSearchParamsView(target, context) {
511
523
  }
512
524
  function createProtectedURLView(target, context) {
513
525
  const syncPrototype = !hasBuiltInSubclassPrototype(target, URL.prototype);
526
+ const methodCache = /* @__PURE__ */ new Map();
514
527
  const cloned = syncPrototype ? new URL(target.href) : tryCloneURLSubclass(target) ?? new URL(target.href);
515
528
  const view = new Proxy(cloned, {
516
529
  get(clonedTarget, key) {
517
530
  if (key === "valueOf") return () => view;
518
531
  if (key === "searchParams") return protectAnnotationValue(clonedTarget.searchParams, context);
519
532
  const value = Reflect.get(clonedTarget, key, clonedTarget);
520
- return typeof value === "function" ? value.bind(clonedTarget) : protectAnnotationValue(value, context);
533
+ return getProtectedClonePropertyValue(methodCache, clonedTarget, key, value, context);
521
534
  },
522
535
  set() {
523
536
  throwReadonlyAnnotationMutation();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1874+39daa8c1",
3
+ "version": "1.0.0-dev.1878+1ab0e08e",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",