@optique/core 1.0.0-dev.1895 → 1.0.0-dev.1899

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.
@@ -92,6 +92,46 @@ function isPendingNestedNormalizationAlias(originalValue, normalizedValue, seen)
92
92
  const entry = seen.get(originalValue);
93
93
  return entry != null && !entry.finalized && entry.clone === normalizedValue;
94
94
  }
95
+ function createPendingNestedNormalizationClone(source) {
96
+ return Array.isArray(source) ? [] : Object.create(Object.getPrototypeOf(source));
97
+ }
98
+ function normalizeNestedDelegatedStructuredState(source, seen) {
99
+ const clone = createPendingNestedNormalizationClone(source);
100
+ const entry = {
101
+ clone,
102
+ finalized: false,
103
+ result: clone
104
+ };
105
+ seen.set(source, entry);
106
+ const overrides = /* @__PURE__ */ new Map();
107
+ let changed = false;
108
+ for (const key of Reflect.ownKeys(source)) {
109
+ const descriptor = Object.getOwnPropertyDescriptor(source, key);
110
+ if (descriptor == null || !("value" in descriptor)) continue;
111
+ const nextValue = normalizeNestedDelegatedAnnotationState(descriptor.value, seen);
112
+ if (nextValue === descriptor.value) continue;
113
+ overrides.set(key, nextValue);
114
+ if (!isPendingNestedNormalizationAlias(descriptor.value, nextValue, seen)) changed = true;
115
+ }
116
+ if (!changed) {
117
+ entry.finalized = true;
118
+ entry.result = source;
119
+ return source;
120
+ }
121
+ const descriptors = Object.getOwnPropertyDescriptors(source);
122
+ for (const [key, nextValue] of overrides) {
123
+ const descriptor = descriptors[key];
124
+ if (descriptor == null || !("value" in descriptor)) continue;
125
+ descriptors[key] = {
126
+ ...descriptor,
127
+ value: nextValue
128
+ };
129
+ }
130
+ Object.defineProperties(clone, descriptors);
131
+ entry.finalized = true;
132
+ entry.result = clone;
133
+ return clone;
134
+ }
95
135
  /**
96
136
  * Recursively removes delegated annotation carriers from plain-object and array
97
137
  * structures.
@@ -114,55 +154,10 @@ function normalizeNestedDelegatedAnnotationState(value, seen = /* @__PURE__ */ n
114
154
  const source = normalized;
115
155
  const existing = seen.get(source);
116
156
  if (existing != null) return existing.finalized ? existing.result : existing.clone;
117
- if (Array.isArray(source)) {
118
- let changed$1 = false;
119
- const clone$1 = require_annotations.annotateFreshArray(source, source.slice());
120
- const entry$1 = {
121
- clone: clone$1,
122
- finalized: false,
123
- result: clone$1
124
- };
125
- seen.set(source, entry$1);
126
- for (let i = 0; i < source.length; i++) {
127
- const nextValue = normalizeNestedDelegatedAnnotationState(source[i], seen);
128
- if (nextValue !== source[i]) {
129
- clone$1[i] = nextValue;
130
- if (!isPendingNestedNormalizationAlias(source[i], nextValue, seen)) changed$1 = true;
131
- }
132
- }
133
- entry$1.finalized = true;
134
- entry$1.result = changed$1 ? clone$1 : source;
135
- return changed$1 ? clone$1 : source;
136
- }
157
+ if (Array.isArray(source)) return normalizeNestedDelegatedStructuredState(source, seen);
137
158
  const proto = Object.getPrototypeOf(source);
138
159
  if (proto !== Object.prototype && proto !== null) return normalized;
139
- const descriptors = Object.getOwnPropertyDescriptors(source);
140
- let changed = false;
141
- const clone = Object.create(proto);
142
- const entry = {
143
- clone,
144
- finalized: false,
145
- result: clone
146
- };
147
- seen.set(source, entry);
148
- for (const key of Reflect.ownKeys(descriptors)) {
149
- const descriptor = descriptors[key];
150
- if (descriptor != null && "value" in descriptor) {
151
- const nextValue = normalizeNestedDelegatedAnnotationState(descriptor.value, seen);
152
- if (nextValue !== descriptor.value) {
153
- descriptors[key] = {
154
- ...descriptor,
155
- value: nextValue
156
- };
157
- if (!isPendingNestedNormalizationAlias(descriptor.value, nextValue, seen)) changed = true;
158
- }
159
- }
160
- }
161
- entry.finalized = true;
162
- entry.result = changed ? clone : source;
163
- if (!changed) return source;
164
- Object.defineProperties(clone, descriptors);
165
- return clone;
160
+ return normalizeNestedDelegatedStructuredState(source, seen);
166
161
  }
167
162
  /**
168
163
  * Creates a short-lived delegated state that exposes the parent's annotations
@@ -1,4 +1,4 @@
1
- import { annotateFreshArray, annotationKey, getAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, 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
@@ -92,6 +92,46 @@ function isPendingNestedNormalizationAlias(originalValue, normalizedValue, seen)
92
92
  const entry = seen.get(originalValue);
93
93
  return entry != null && !entry.finalized && entry.clone === normalizedValue;
94
94
  }
95
+ function createPendingNestedNormalizationClone(source) {
96
+ return Array.isArray(source) ? [] : Object.create(Object.getPrototypeOf(source));
97
+ }
98
+ function normalizeNestedDelegatedStructuredState(source, seen) {
99
+ const clone = createPendingNestedNormalizationClone(source);
100
+ const entry = {
101
+ clone,
102
+ finalized: false,
103
+ result: clone
104
+ };
105
+ seen.set(source, entry);
106
+ const overrides = /* @__PURE__ */ new Map();
107
+ let changed = false;
108
+ for (const key of Reflect.ownKeys(source)) {
109
+ const descriptor = Object.getOwnPropertyDescriptor(source, key);
110
+ if (descriptor == null || !("value" in descriptor)) continue;
111
+ const nextValue = normalizeNestedDelegatedAnnotationState(descriptor.value, seen);
112
+ if (nextValue === descriptor.value) continue;
113
+ overrides.set(key, nextValue);
114
+ if (!isPendingNestedNormalizationAlias(descriptor.value, nextValue, seen)) changed = true;
115
+ }
116
+ if (!changed) {
117
+ entry.finalized = true;
118
+ entry.result = source;
119
+ return source;
120
+ }
121
+ const descriptors = Object.getOwnPropertyDescriptors(source);
122
+ for (const [key, nextValue] of overrides) {
123
+ const descriptor = descriptors[key];
124
+ if (descriptor == null || !("value" in descriptor)) continue;
125
+ descriptors[key] = {
126
+ ...descriptor,
127
+ value: nextValue
128
+ };
129
+ }
130
+ Object.defineProperties(clone, descriptors);
131
+ entry.finalized = true;
132
+ entry.result = clone;
133
+ return clone;
134
+ }
95
135
  /**
96
136
  * Recursively removes delegated annotation carriers from plain-object and array
97
137
  * structures.
@@ -114,55 +154,10 @@ function normalizeNestedDelegatedAnnotationState(value, seen = /* @__PURE__ */ n
114
154
  const source = normalized;
115
155
  const existing = seen.get(source);
116
156
  if (existing != null) return existing.finalized ? existing.result : existing.clone;
117
- if (Array.isArray(source)) {
118
- let changed$1 = false;
119
- const clone$1 = annotateFreshArray(source, source.slice());
120
- const entry$1 = {
121
- clone: clone$1,
122
- finalized: false,
123
- result: clone$1
124
- };
125
- seen.set(source, entry$1);
126
- for (let i = 0; i < source.length; i++) {
127
- const nextValue = normalizeNestedDelegatedAnnotationState(source[i], seen);
128
- if (nextValue !== source[i]) {
129
- clone$1[i] = nextValue;
130
- if (!isPendingNestedNormalizationAlias(source[i], nextValue, seen)) changed$1 = true;
131
- }
132
- }
133
- entry$1.finalized = true;
134
- entry$1.result = changed$1 ? clone$1 : source;
135
- return changed$1 ? clone$1 : source;
136
- }
157
+ if (Array.isArray(source)) return normalizeNestedDelegatedStructuredState(source, seen);
137
158
  const proto = Object.getPrototypeOf(source);
138
159
  if (proto !== Object.prototype && proto !== null) return normalized;
139
- const descriptors = Object.getOwnPropertyDescriptors(source);
140
- let changed = false;
141
- const clone = Object.create(proto);
142
- const entry = {
143
- clone,
144
- finalized: false,
145
- result: clone
146
- };
147
- seen.set(source, entry);
148
- for (const key of Reflect.ownKeys(descriptors)) {
149
- const descriptor = descriptors[key];
150
- if (descriptor != null && "value" in descriptor) {
151
- const nextValue = normalizeNestedDelegatedAnnotationState(descriptor.value, seen);
152
- if (nextValue !== descriptor.value) {
153
- descriptors[key] = {
154
- ...descriptor,
155
- value: nextValue
156
- };
157
- if (!isPendingNestedNormalizationAlias(descriptor.value, nextValue, seen)) changed = true;
158
- }
159
- }
160
- }
161
- entry.finalized = true;
162
- entry.result = changed ? clone : source;
163
- if (!changed) return source;
164
- Object.defineProperties(clone, descriptors);
165
- return clone;
160
+ return normalizeNestedDelegatedStructuredState(source, seen);
166
161
  }
167
162
  /**
168
163
  * Creates a short-lived delegated state that exposes the parent's annotations
@@ -277,6 +277,13 @@ function normalizeOptionalLikeInnerState(state, initialState, parser) {
277
277
  if (state != null && typeof state === "object") return state;
278
278
  return initialState;
279
279
  }
280
+ function normalizeOptionalLikeSuggestState(state, initialState, parser) {
281
+ if (Array.isArray(state)) {
282
+ const innerState = state[0];
283
+ return innerState != null && typeof innerState === "object" ? require_annotation_state.getDelegatedAnnotationState(state, innerState) : innerState;
284
+ }
285
+ return normalizeOptionalLikeInnerState(state, initialState, parser);
286
+ }
280
287
  /**
281
288
  * Creates a parser that makes another parser optional, allowing it to succeed
282
289
  * without consuming input if the wrapped parser fails to match.
@@ -292,14 +299,14 @@ function normalizeOptionalLikeInnerState(state, initialState, parser) {
292
299
  function optional(parser) {
293
300
  const syncParser = parser;
294
301
  function* suggestSync(context, prefix) {
295
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
302
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
296
303
  yield* syncParser.suggest({
297
304
  ...context,
298
305
  state: innerState
299
306
  }, prefix);
300
307
  }
301
308
  async function* suggestAsync(context, prefix) {
302
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
309
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
303
310
  const suggestions = parser.suggest({
304
311
  ...context,
305
312
  state: innerState
@@ -327,7 +334,7 @@ function optional(parser) {
327
334
  parser: optionalParser,
328
335
  state
329
336
  }];
330
- const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
337
+ const innerState = normalizeOptionalLikeSuggestState(state, parser.initialState, parser);
331
338
  return parser.getSuggestRuntimeNodes?.(innerState, path) ?? (parser.dependencyMetadata?.source != null ? [{
332
339
  path,
333
340
  parser,
@@ -461,14 +468,14 @@ function withDefault(parser, defaultValue, options) {
461
468
  }
462
469
  };
463
470
  function* suggestSync(context, prefix) {
464
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
471
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
465
472
  yield* syncParser.suggest({
466
473
  ...context,
467
474
  state: innerState
468
475
  }, prefix);
469
476
  }
470
477
  async function* suggestAsync(context, prefix) {
471
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
478
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
472
479
  const suggestions = parser.suggest({
473
480
  ...context,
474
481
  state: innerState
@@ -495,7 +502,7 @@ function withDefault(parser, defaultValue, options) {
495
502
  parser: withDefaultParser,
496
503
  state
497
504
  }];
498
- const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
505
+ const innerState = normalizeOptionalLikeSuggestState(state, parser.initialState, parser);
499
506
  return parser.getSuggestRuntimeNodes?.(innerState, path) ?? (parser.dependencyMetadata?.source != null ? [{
500
507
  path,
501
508
  parser,
package/dist/modifiers.js CHANGED
@@ -277,6 +277,13 @@ function normalizeOptionalLikeInnerState(state, initialState, parser) {
277
277
  if (state != null && typeof state === "object") return state;
278
278
  return initialState;
279
279
  }
280
+ function normalizeOptionalLikeSuggestState(state, initialState, parser) {
281
+ if (Array.isArray(state)) {
282
+ const innerState = state[0];
283
+ return innerState != null && typeof innerState === "object" ? getDelegatedAnnotationState(state, innerState) : innerState;
284
+ }
285
+ return normalizeOptionalLikeInnerState(state, initialState, parser);
286
+ }
280
287
  /**
281
288
  * Creates a parser that makes another parser optional, allowing it to succeed
282
289
  * without consuming input if the wrapped parser fails to match.
@@ -292,14 +299,14 @@ function normalizeOptionalLikeInnerState(state, initialState, parser) {
292
299
  function optional(parser) {
293
300
  const syncParser = parser;
294
301
  function* suggestSync(context, prefix) {
295
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
302
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
296
303
  yield* syncParser.suggest({
297
304
  ...context,
298
305
  state: innerState
299
306
  }, prefix);
300
307
  }
301
308
  async function* suggestAsync(context, prefix) {
302
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
309
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
303
310
  const suggestions = parser.suggest({
304
311
  ...context,
305
312
  state: innerState
@@ -327,7 +334,7 @@ function optional(parser) {
327
334
  parser: optionalParser,
328
335
  state
329
336
  }];
330
- const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
337
+ const innerState = normalizeOptionalLikeSuggestState(state, parser.initialState, parser);
331
338
  return parser.getSuggestRuntimeNodes?.(innerState, path) ?? (parser.dependencyMetadata?.source != null ? [{
332
339
  path,
333
340
  parser,
@@ -461,14 +468,14 @@ function withDefault(parser, defaultValue, options) {
461
468
  }
462
469
  };
463
470
  function* suggestSync(context, prefix) {
464
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
471
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
465
472
  yield* syncParser.suggest({
466
473
  ...context,
467
474
  state: innerState
468
475
  }, prefix);
469
476
  }
470
477
  async function* suggestAsync(context, prefix) {
471
- const innerState = normalizeOptionalLikeInnerState(context.state, syncParser.initialState, parser);
478
+ const innerState = normalizeOptionalLikeSuggestState(context.state, syncParser.initialState, parser);
472
479
  const suggestions = parser.suggest({
473
480
  ...context,
474
481
  state: innerState
@@ -495,7 +502,7 @@ function withDefault(parser, defaultValue, options) {
495
502
  parser: withDefaultParser,
496
503
  state
497
504
  }];
498
- const innerState = normalizeOptionalLikeInnerState(state, parser.initialState, parser);
505
+ const innerState = normalizeOptionalLikeSuggestState(state, parser.initialState, parser);
499
506
  return parser.getSuggestRuntimeNodes?.(innerState, path) ?? (parser.dependencyMetadata?.source != null ? [{
500
507
  path,
501
508
  parser,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1895+42aa2942",
3
+ "version": "1.0.0-dev.1899+f70a2468",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",