@optique/core 1.0.0-dev.1888 → 1.0.0-dev.1891

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.
@@ -58,6 +58,57 @@ function withAnnotationView(state, annotations) {
58
58
  function normalizeInjectedAnnotationState(state) {
59
59
  return require_annotations.unwrapInjectedAnnotationWrapper(state);
60
60
  }
61
+ function isNonPlainDelegatedObject(state) {
62
+ if (Array.isArray(state) || state instanceof Date || state instanceof Map || state instanceof Set || state instanceof RegExp) return false;
63
+ const proto = Object.getPrototypeOf(state);
64
+ return proto !== Object.prototype && proto !== null;
65
+ }
66
+ /**
67
+ * Removes Optique's internal annotation carriers from a delegated state.
68
+ *
69
+ * This unwraps both primitive-state annotation wrappers and annotation-view
70
+ * proxies used for non-plain object states.
71
+ *
72
+ * @param state The delegated state to normalize.
73
+ * @returns The original underlying state value.
74
+ * @internal
75
+ */
76
+ function normalizeDelegatedAnnotationState(state) {
77
+ return normalizeInjectedAnnotationState(unwrapAnnotationView(state));
78
+ }
79
+ /**
80
+ * Returns whether the given state uses an internal delegated annotation carrier.
81
+ *
82
+ * @param state The candidate state to inspect.
83
+ * @returns `true` when the state is an injected primitive wrapper or an
84
+ * annotation-view proxy.
85
+ * @internal
86
+ */
87
+ function hasDelegatedAnnotationCarrier(state) {
88
+ return state != null && typeof state === "object" && (require_annotations.isInjectedAnnotationWrapper(state) || annotationViewTargets.has(state));
89
+ }
90
+ /**
91
+ * Creates a short-lived delegated state that exposes the parent's annotations
92
+ * regardless of the child state's runtime shape.
93
+ *
94
+ * Primitive and nullish states use `injectAnnotations()`, plain objects and
95
+ * built-ins use `inheritAnnotations()`, and non-plain objects use an
96
+ * annotation-view proxy so class invariants such as private fields remain
97
+ * intact.
98
+ *
99
+ * @param parentState The state carrying the annotations to delegate.
100
+ * @param childState The child state that should observe those annotations.
101
+ * @returns A delegated child state that exposes the parent's annotations.
102
+ * @internal
103
+ */
104
+ function getDelegatedAnnotationState(parentState, childState) {
105
+ const annotations = require_annotations.getAnnotations(parentState);
106
+ if (annotations === void 0 || require_annotations.getAnnotations(childState) === annotations) return childState;
107
+ if (childState == null || typeof childState !== "object") return require_annotations.injectAnnotations(childState, annotations);
108
+ if (require_annotations.isInjectedAnnotationWrapper(childState)) return require_annotations.injectAnnotations(childState, annotations);
109
+ if (isNonPlainDelegatedObject(childState)) return withAnnotationView(childState, annotations);
110
+ return require_annotations.inheritAnnotations(parentState, childState);
111
+ }
61
112
  /**
62
113
  * Returns whether a state is still at the initial sentinel after normalizing
63
114
  * Optique's injected annotation wrapper.
@@ -143,9 +194,12 @@ function reconcileObjectChildState(parentState, childState) {
143
194
 
144
195
  //#endregion
145
196
  exports.annotationViewTargets = annotationViewTargets;
197
+ exports.getDelegatedAnnotationState = getDelegatedAnnotationState;
146
198
  exports.getWrappedChildParseState = getWrappedChildParseState;
147
199
  exports.getWrappedChildState = getWrappedChildState;
200
+ exports.hasDelegatedAnnotationCarrier = hasDelegatedAnnotationCarrier;
148
201
  exports.isAnnotationWrappedInitialState = isAnnotationWrappedInitialState;
202
+ exports.normalizeDelegatedAnnotationState = normalizeDelegatedAnnotationState;
149
203
  exports.normalizeInjectedAnnotationState = normalizeInjectedAnnotationState;
150
204
  exports.reconcileObjectChildState = reconcileObjectChildState;
151
205
  exports.unwrapAnnotationView = unwrapAnnotationView;
@@ -1,4 +1,4 @@
1
- import { annotationKey, getAnnotations, inheritAnnotations, injectAnnotations, unwrapInjectedAnnotationWrapper } from "./annotations.js";
1
+ import { annotationKey, getAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper } from "./annotations.js";
2
2
  import { inheritParentAnnotationsKey } from "./parser.js";
3
3
 
4
4
  //#region src/annotation-state.ts
@@ -58,6 +58,57 @@ function withAnnotationView(state, annotations) {
58
58
  function normalizeInjectedAnnotationState(state) {
59
59
  return unwrapInjectedAnnotationWrapper(state);
60
60
  }
61
+ function isNonPlainDelegatedObject(state) {
62
+ if (Array.isArray(state) || state instanceof Date || state instanceof Map || state instanceof Set || state instanceof RegExp) return false;
63
+ const proto = Object.getPrototypeOf(state);
64
+ return proto !== Object.prototype && proto !== null;
65
+ }
66
+ /**
67
+ * Removes Optique's internal annotation carriers from a delegated state.
68
+ *
69
+ * This unwraps both primitive-state annotation wrappers and annotation-view
70
+ * proxies used for non-plain object states.
71
+ *
72
+ * @param state The delegated state to normalize.
73
+ * @returns The original underlying state value.
74
+ * @internal
75
+ */
76
+ function normalizeDelegatedAnnotationState(state) {
77
+ return normalizeInjectedAnnotationState(unwrapAnnotationView(state));
78
+ }
79
+ /**
80
+ * Returns whether the given state uses an internal delegated annotation carrier.
81
+ *
82
+ * @param state The candidate state to inspect.
83
+ * @returns `true` when the state is an injected primitive wrapper or an
84
+ * annotation-view proxy.
85
+ * @internal
86
+ */
87
+ function hasDelegatedAnnotationCarrier(state) {
88
+ return state != null && typeof state === "object" && (isInjectedAnnotationWrapper(state) || annotationViewTargets.has(state));
89
+ }
90
+ /**
91
+ * Creates a short-lived delegated state that exposes the parent's annotations
92
+ * regardless of the child state's runtime shape.
93
+ *
94
+ * Primitive and nullish states use `injectAnnotations()`, plain objects and
95
+ * built-ins use `inheritAnnotations()`, and non-plain objects use an
96
+ * annotation-view proxy so class invariants such as private fields remain
97
+ * intact.
98
+ *
99
+ * @param parentState The state carrying the annotations to delegate.
100
+ * @param childState The child state that should observe those annotations.
101
+ * @returns A delegated child state that exposes the parent's annotations.
102
+ * @internal
103
+ */
104
+ function getDelegatedAnnotationState(parentState, childState) {
105
+ const annotations = getAnnotations(parentState);
106
+ if (annotations === void 0 || getAnnotations(childState) === annotations) return childState;
107
+ if (childState == null || typeof childState !== "object") return injectAnnotations(childState, annotations);
108
+ if (isInjectedAnnotationWrapper(childState)) return injectAnnotations(childState, annotations);
109
+ if (isNonPlainDelegatedObject(childState)) return withAnnotationView(childState, annotations);
110
+ return inheritAnnotations(parentState, childState);
111
+ }
61
112
  /**
62
113
  * Returns whether a state is still at the initial sentinel after normalizing
63
114
  * Optique's injected annotation wrapper.
@@ -142,4 +193,4 @@ function reconcileObjectChildState(parentState, childState) {
142
193
  }
143
194
 
144
195
  //#endregion
145
- export { annotationViewTargets, getWrappedChildParseState, getWrappedChildState, isAnnotationWrappedInitialState, normalizeInjectedAnnotationState, reconcileObjectChildState, unwrapAnnotationView };
196
+ export { annotationViewTargets, getDelegatedAnnotationState, getWrappedChildParseState, getWrappedChildState, hasDelegatedAnnotationCarrier, isAnnotationWrappedInitialState, normalizeDelegatedAnnotationState, normalizeInjectedAnnotationState, reconcileObjectChildState, unwrapAnnotationView };
@@ -4,6 +4,7 @@ const require_mode_dispatch = require('./mode-dispatch.cjs');
4
4
  const require_dependency_metadata = require('./dependency-metadata.cjs');
5
5
  const require_phase2_seed = require('./phase2-seed.cjs');
6
6
  const require_parser = require('./parser.cjs');
7
+ const require_annotation_state = require('./annotation-state.cjs');
7
8
 
8
9
  //#region src/modifiers.ts
9
10
  function withChildExecPath(exec, segment) {
@@ -81,9 +82,63 @@ function unwrapMultipleItemState(state) {
81
82
  function isPromiseLike(value) {
82
83
  return value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
83
84
  }
85
+ function normalizeOptionalLikeCompleteResult(result) {
86
+ return result.success ? {
87
+ ...result,
88
+ value: require_annotation_state.normalizeDelegatedAnnotationState(result.value)
89
+ } : result;
90
+ }
91
+ function completeOptionalLikeSync(parser, state, exec) {
92
+ const fallbackState = require_annotation_state.normalizeDelegatedAnnotationState(state);
93
+ try {
94
+ const result = parser.complete(state, exec);
95
+ if (!result.success && require_annotation_state.hasDelegatedAnnotationCarrier(state)) return normalizeOptionalLikeCompleteResult(parser.complete(fallbackState, exec));
96
+ return normalizeOptionalLikeCompleteResult(result);
97
+ } catch (error) {
98
+ if (!require_annotation_state.hasDelegatedAnnotationCarrier(state)) throw error;
99
+ return normalizeOptionalLikeCompleteResult(parser.complete(fallbackState, exec));
100
+ }
101
+ }
102
+ async function completeOptionalLikeAsync(parser, state, exec) {
103
+ const fallbackState = require_annotation_state.normalizeDelegatedAnnotationState(state);
104
+ try {
105
+ const result = await parser.complete(state, exec);
106
+ if (!result.success && require_annotation_state.hasDelegatedAnnotationCarrier(state)) return normalizeOptionalLikeCompleteResult(await parser.complete(fallbackState, exec));
107
+ return normalizeOptionalLikeCompleteResult(result);
108
+ } catch (error) {
109
+ if (!require_annotation_state.hasDelegatedAnnotationCarrier(state)) throw error;
110
+ return normalizeOptionalLikeCompleteResult(await parser.complete(fallbackState, exec));
111
+ }
112
+ }
113
+ function normalizeOptionalLikePhase2Seed(seed) {
114
+ return seed == null ? null : {
115
+ ...seed,
116
+ value: require_annotation_state.normalizeDelegatedAnnotationState(seed.value)
117
+ };
118
+ }
84
119
  function extractOptionalLikePhase2Seed(parser, state, exec) {
85
120
  if (!Array.isArray(state) && !(state != null && typeof state === "object")) return require_mode_dispatch.wrapForMode(parser.$mode, null);
86
- return require_phase2_seed.completeOrExtractPhase2Seed(parser, normalizeOptionalLikeInnerState(state, parser.initialState, parser), exec);
121
+ const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
122
+ const fallbackState = require_annotation_state.normalizeDelegatedAnnotationState(innerState);
123
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => {
124
+ try {
125
+ const seed = require_phase2_seed.completeOrExtractPhase2Seed(parser, innerState, exec);
126
+ if (seed == null && require_annotation_state.hasDelegatedAnnotationCarrier(innerState)) return normalizeOptionalLikePhase2Seed(require_phase2_seed.completeOrExtractPhase2Seed(parser, fallbackState, exec));
127
+ return normalizeOptionalLikePhase2Seed(seed);
128
+ } catch (error) {
129
+ if (!require_annotation_state.hasDelegatedAnnotationCarrier(innerState)) throw error;
130
+ return normalizeOptionalLikePhase2Seed(require_phase2_seed.completeOrExtractPhase2Seed(parser, fallbackState, exec));
131
+ }
132
+ }, async () => {
133
+ try {
134
+ const seed = await require_phase2_seed.completeOrExtractPhase2Seed(parser, innerState, exec);
135
+ if (seed == null && require_annotation_state.hasDelegatedAnnotationCarrier(innerState)) return normalizeOptionalLikePhase2Seed(await require_phase2_seed.completeOrExtractPhase2Seed(parser, fallbackState, exec));
136
+ return normalizeOptionalLikePhase2Seed(seed);
137
+ } catch (error) {
138
+ if (!require_annotation_state.hasDelegatedAnnotationCarrier(innerState)) throw error;
139
+ return normalizeOptionalLikePhase2Seed(await require_phase2_seed.completeOrExtractPhase2Seed(parser, fallbackState, exec));
140
+ }
141
+ });
87
142
  }
88
143
  /**
89
144
  * Computes the inner state to pass through to the wrapped parser inside
@@ -182,7 +237,15 @@ function processOptionalStyleResult(result, innerState, context) {
182
237
  */
183
238
  function adaptShouldDeferCompletion(innerCheck, parser) {
184
239
  return (state, exec) => {
185
- if (Array.isArray(state) || state != null && typeof state === "object") return innerCheck(normalizeOptionalLikeInnerState(state, parser.initialState, parser), exec);
240
+ if (Array.isArray(state) || state != null && typeof state === "object") {
241
+ const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
242
+ try {
243
+ return innerCheck(innerState, exec);
244
+ } catch (error) {
245
+ if (!require_annotation_state.hasDelegatedAnnotationCarrier(innerState)) throw error;
246
+ return innerCheck(require_annotation_state.normalizeDelegatedAnnotationState(innerState), exec);
247
+ }
248
+ }
186
249
  return false;
187
250
  };
188
251
  }
@@ -192,9 +255,9 @@ function isAnnotationOnlyObjectState(state) {
192
255
  return require_annotations.getAnnotations(state) != null && keys.length === 1 && keys[0] === require_annotations.annotationKey;
193
256
  }
194
257
  function normalizeOptionalLikeInnerState(state, initialState, parser) {
195
- if (Array.isArray(state)) return require_annotations.getAnnotations(state) != null && state[0] != null && typeof state[0] === "object" ? require_annotations.inheritAnnotations(state, state[0]) : state[0];
258
+ if (Array.isArray(state)) return require_annotation_state.getDelegatedAnnotationState(state, state[0]);
196
259
  if (isAnnotationOnlyObjectState(state)) {
197
- if (parser != null && (parser.dependencyMetadata?.source != null || typeof parser.shouldDeferCompletion === "function")) return require_annotations.inheritAnnotations(state, initialState);
260
+ if (parser != null && (parser.dependencyMetadata?.source != null || typeof parser.shouldDeferCompletion === "function")) return require_annotation_state.getDelegatedAnnotationState(state, initialState);
198
261
  return initialState;
199
262
  }
200
263
  if (state != null && typeof state === "object") return state;
@@ -266,7 +329,7 @@ function optional(parser) {
266
329
  complete(state, exec) {
267
330
  if (!Array.isArray(state)) {
268
331
  const delegateToInner = (resolvedInnerState) => {
269
- const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(resolvedInnerState, exec), async () => await parser.complete(resolvedInnerState, exec));
332
+ const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, resolvedInnerState, exec), async () => await completeOptionalLikeAsync(parser, resolvedInnerState, exec));
270
333
  return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, (result) => result.success ? result : {
271
334
  success: true,
272
335
  value: void 0
@@ -279,7 +342,7 @@ function optional(parser) {
279
342
  const sourceMetadata = parser.dependencyMetadata?.source;
280
343
  if (sourceMetadata?.preservesSourceValue !== false && sourceMetadata?.getMissingSourceValue != null) {
281
344
  const delegatedState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
282
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(delegatedState, exec), async () => await parser.complete(delegatedState, exec));
345
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, delegatedState, exec), async () => await completeOptionalLikeAsync(parser, delegatedState, exec));
283
346
  }
284
347
  if (parser[require_parser.unmatchedNonCliDependencySourceStateMarker] === true && state != null && typeof state === "object") {
285
348
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
@@ -291,7 +354,7 @@ function optional(parser) {
291
354
  };
292
355
  }
293
356
  const innerElement = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
294
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerElement, exec), async () => await parser.complete(innerElement, exec));
357
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerElement, exec), async () => await completeOptionalLikeAsync(parser, innerElement, exec));
295
358
  },
296
359
  suggest(context, prefix) {
297
360
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
@@ -442,12 +505,12 @@ function withDefault(parser, defaultValue, options) {
442
505
  if (!Array.isArray(state)) {
443
506
  if (typeof parser.shouldDeferCompletion === "function" && state != null && typeof state === "object") {
444
507
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
445
- const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerState, exec), async () => await parser.complete(innerState, exec));
508
+ const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerState, exec), async () => await completeOptionalLikeAsync(parser, innerState, exec));
446
509
  return innerResult;
447
510
  }
448
511
  if (parser[require_parser.unmatchedNonCliDependencySourceStateMarker] === true && state != null && typeof state === "object") {
449
512
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
450
- const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerState, exec), async () => await parser.complete(innerState, exec));
513
+ const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerState, exec), async () => await completeOptionalLikeAsync(parser, innerState, exec));
451
514
  const handleInnerResult = (result) => {
452
515
  if (result.success && result.value !== void 0) return result;
453
516
  try {
@@ -478,7 +541,7 @@ function withDefault(parser, defaultValue, options) {
478
541
  }
479
542
  }
480
543
  const innerElement = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
481
- return require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(innerElement, exec), async () => await parser.complete(innerElement, exec));
544
+ return require_mode_dispatch.dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerElement, exec), async () => await completeOptionalLikeAsync(parser, innerElement, exec));
482
545
  },
483
546
  suggest(context, prefix) {
484
547
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
package/dist/modifiers.js CHANGED
@@ -4,6 +4,7 @@ import { dispatchByMode, dispatchIterableByMode, mapModeValue, wrapForMode } fro
4
4
  import { composeDependencyMetadata } from "./dependency-metadata.js";
5
5
  import { completeOrExtractPhase2Seed, extractPhase2Seed, extractPhase2SeedKey } from "./phase2-seed.js";
6
6
  import { defineInheritedAnnotationParser, defineSourceBindingOnlyAnnotationCompletionParser, unmatchedNonCliDependencySourceStateMarker } from "./parser.js";
7
+ import { getDelegatedAnnotationState, hasDelegatedAnnotationCarrier, normalizeDelegatedAnnotationState } from "./annotation-state.js";
7
8
 
8
9
  //#region src/modifiers.ts
9
10
  function withChildExecPath(exec, segment) {
@@ -81,9 +82,63 @@ function unwrapMultipleItemState(state) {
81
82
  function isPromiseLike(value) {
82
83
  return value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
83
84
  }
85
+ function normalizeOptionalLikeCompleteResult(result) {
86
+ return result.success ? {
87
+ ...result,
88
+ value: normalizeDelegatedAnnotationState(result.value)
89
+ } : result;
90
+ }
91
+ function completeOptionalLikeSync(parser, state, exec) {
92
+ const fallbackState = normalizeDelegatedAnnotationState(state);
93
+ try {
94
+ const result = parser.complete(state, exec);
95
+ if (!result.success && hasDelegatedAnnotationCarrier(state)) return normalizeOptionalLikeCompleteResult(parser.complete(fallbackState, exec));
96
+ return normalizeOptionalLikeCompleteResult(result);
97
+ } catch (error) {
98
+ if (!hasDelegatedAnnotationCarrier(state)) throw error;
99
+ return normalizeOptionalLikeCompleteResult(parser.complete(fallbackState, exec));
100
+ }
101
+ }
102
+ async function completeOptionalLikeAsync(parser, state, exec) {
103
+ const fallbackState = normalizeDelegatedAnnotationState(state);
104
+ try {
105
+ const result = await parser.complete(state, exec);
106
+ if (!result.success && hasDelegatedAnnotationCarrier(state)) return normalizeOptionalLikeCompleteResult(await parser.complete(fallbackState, exec));
107
+ return normalizeOptionalLikeCompleteResult(result);
108
+ } catch (error) {
109
+ if (!hasDelegatedAnnotationCarrier(state)) throw error;
110
+ return normalizeOptionalLikeCompleteResult(await parser.complete(fallbackState, exec));
111
+ }
112
+ }
113
+ function normalizeOptionalLikePhase2Seed(seed) {
114
+ return seed == null ? null : {
115
+ ...seed,
116
+ value: normalizeDelegatedAnnotationState(seed.value)
117
+ };
118
+ }
84
119
  function extractOptionalLikePhase2Seed(parser, state, exec) {
85
120
  if (!Array.isArray(state) && !(state != null && typeof state === "object")) return wrapForMode(parser.$mode, null);
86
- return completeOrExtractPhase2Seed(parser, normalizeOptionalLikeInnerState(state, parser.initialState, parser), exec);
121
+ const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
122
+ const fallbackState = normalizeDelegatedAnnotationState(innerState);
123
+ return dispatchByMode(parser.$mode, () => {
124
+ try {
125
+ const seed = completeOrExtractPhase2Seed(parser, innerState, exec);
126
+ if (seed == null && hasDelegatedAnnotationCarrier(innerState)) return normalizeOptionalLikePhase2Seed(completeOrExtractPhase2Seed(parser, fallbackState, exec));
127
+ return normalizeOptionalLikePhase2Seed(seed);
128
+ } catch (error) {
129
+ if (!hasDelegatedAnnotationCarrier(innerState)) throw error;
130
+ return normalizeOptionalLikePhase2Seed(completeOrExtractPhase2Seed(parser, fallbackState, exec));
131
+ }
132
+ }, async () => {
133
+ try {
134
+ const seed = await completeOrExtractPhase2Seed(parser, innerState, exec);
135
+ if (seed == null && hasDelegatedAnnotationCarrier(innerState)) return normalizeOptionalLikePhase2Seed(await completeOrExtractPhase2Seed(parser, fallbackState, exec));
136
+ return normalizeOptionalLikePhase2Seed(seed);
137
+ } catch (error) {
138
+ if (!hasDelegatedAnnotationCarrier(innerState)) throw error;
139
+ return normalizeOptionalLikePhase2Seed(await completeOrExtractPhase2Seed(parser, fallbackState, exec));
140
+ }
141
+ });
87
142
  }
88
143
  /**
89
144
  * Computes the inner state to pass through to the wrapped parser inside
@@ -182,7 +237,15 @@ function processOptionalStyleResult(result, innerState, context) {
182
237
  */
183
238
  function adaptShouldDeferCompletion(innerCheck, parser) {
184
239
  return (state, exec) => {
185
- if (Array.isArray(state) || state != null && typeof state === "object") return innerCheck(normalizeOptionalLikeInnerState(state, parser.initialState, parser), exec);
240
+ if (Array.isArray(state) || state != null && typeof state === "object") {
241
+ const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
242
+ try {
243
+ return innerCheck(innerState, exec);
244
+ } catch (error) {
245
+ if (!hasDelegatedAnnotationCarrier(innerState)) throw error;
246
+ return innerCheck(normalizeDelegatedAnnotationState(innerState), exec);
247
+ }
248
+ }
186
249
  return false;
187
250
  };
188
251
  }
@@ -192,9 +255,9 @@ function isAnnotationOnlyObjectState(state) {
192
255
  return getAnnotations(state) != null && keys.length === 1 && keys[0] === annotationKey;
193
256
  }
194
257
  function normalizeOptionalLikeInnerState(state, initialState, parser) {
195
- if (Array.isArray(state)) return getAnnotations(state) != null && state[0] != null && typeof state[0] === "object" ? inheritAnnotations(state, state[0]) : state[0];
258
+ if (Array.isArray(state)) return getDelegatedAnnotationState(state, state[0]);
196
259
  if (isAnnotationOnlyObjectState(state)) {
197
- if (parser != null && (parser.dependencyMetadata?.source != null || typeof parser.shouldDeferCompletion === "function")) return inheritAnnotations(state, initialState);
260
+ if (parser != null && (parser.dependencyMetadata?.source != null || typeof parser.shouldDeferCompletion === "function")) return getDelegatedAnnotationState(state, initialState);
198
261
  return initialState;
199
262
  }
200
263
  if (state != null && typeof state === "object") return state;
@@ -266,7 +329,7 @@ function optional(parser) {
266
329
  complete(state, exec) {
267
330
  if (!Array.isArray(state)) {
268
331
  const delegateToInner = (resolvedInnerState) => {
269
- const innerResult = dispatchByMode(parser.$mode, () => syncParser.complete(resolvedInnerState, exec), async () => await parser.complete(resolvedInnerState, exec));
332
+ const innerResult = dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, resolvedInnerState, exec), async () => await completeOptionalLikeAsync(parser, resolvedInnerState, exec));
270
333
  return mapModeValue(parser.$mode, innerResult, (result) => result.success ? result : {
271
334
  success: true,
272
335
  value: void 0
@@ -279,7 +342,7 @@ function optional(parser) {
279
342
  const sourceMetadata = parser.dependencyMetadata?.source;
280
343
  if (sourceMetadata?.preservesSourceValue !== false && sourceMetadata?.getMissingSourceValue != null) {
281
344
  const delegatedState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
282
- return dispatchByMode(parser.$mode, () => syncParser.complete(delegatedState, exec), async () => await parser.complete(delegatedState, exec));
345
+ return dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, delegatedState, exec), async () => await completeOptionalLikeAsync(parser, delegatedState, exec));
283
346
  }
284
347
  if (parser[unmatchedNonCliDependencySourceStateMarker] === true && state != null && typeof state === "object") {
285
348
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
@@ -291,7 +354,7 @@ function optional(parser) {
291
354
  };
292
355
  }
293
356
  const innerElement = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
294
- return dispatchByMode(parser.$mode, () => syncParser.complete(innerElement, exec), async () => await parser.complete(innerElement, exec));
357
+ return dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerElement, exec), async () => await completeOptionalLikeAsync(parser, innerElement, exec));
295
358
  },
296
359
  suggest(context, prefix) {
297
360
  return dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
@@ -442,12 +505,12 @@ function withDefault(parser, defaultValue, options) {
442
505
  if (!Array.isArray(state)) {
443
506
  if (typeof parser.shouldDeferCompletion === "function" && state != null && typeof state === "object") {
444
507
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
445
- const innerResult = dispatchByMode(parser.$mode, () => syncParser.complete(innerState, exec), async () => await parser.complete(innerState, exec));
508
+ const innerResult = dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerState, exec), async () => await completeOptionalLikeAsync(parser, innerState, exec));
446
509
  return innerResult;
447
510
  }
448
511
  if (parser[unmatchedNonCliDependencySourceStateMarker] === true && state != null && typeof state === "object") {
449
512
  const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
450
- const innerResult = dispatchByMode(parser.$mode, () => syncParser.complete(innerState, exec), async () => await parser.complete(innerState, exec));
513
+ const innerResult = dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerState, exec), async () => await completeOptionalLikeAsync(parser, innerState, exec));
451
514
  const handleInnerResult = (result) => {
452
515
  if (result.success && result.value !== void 0) return result;
453
516
  try {
@@ -478,7 +541,7 @@ function withDefault(parser, defaultValue, options) {
478
541
  }
479
542
  }
480
543
  const innerElement = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
481
- return dispatchByMode(parser.$mode, () => syncParser.complete(innerElement, exec), async () => await parser.complete(innerElement, exec));
544
+ return dispatchByMode(parser.$mode, () => completeOptionalLikeSync(syncParser, innerElement, exec), async () => await completeOptionalLikeAsync(parser, innerElement, exec));
482
545
  },
483
546
  suggest(context, prefix) {
484
547
  return dispatchIterableByMode(parser.$mode, () => suggestSync(context, prefix), () => suggestAsync(context, prefix));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1888+5c18aa3b",
3
+ "version": "1.0.0-dev.1891+f32e7ae2",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",