@optique/core 0.8.5 → 0.8.7

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright 2025 Hong Minhee
3
+ Copyright 2025–2026 Hong Minhee
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
73
73
  */
74
74
  var DuplicateOptionError = class extends Error {
75
75
  constructor(optionName$1, sources) {
76
- super(`Duplicate option name "${optionName$1}" found in fields: ${sources.join(", ")}. Each option name must be unique within a parser combinator.`);
76
+ const sourceNames = sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s);
77
+ super(`Duplicate option name "${optionName$1}" found in fields: ${sourceNames.join(", ")}. Each option name must be unique within a parser combinator.`);
77
78
  this.optionName = optionName$1;
78
79
  this.sources = sources;
79
80
  this.name = "DuplicateOptionError";
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
367
368
  parsers = labelOrParsers;
368
369
  options = maybeParsersOrOptions ?? {};
369
370
  }
370
- const parserPairs = Object.entries(parsers);
371
+ const parserKeys = Reflect.ownKeys(parsers);
372
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
371
373
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
374
+ const initialState = {};
375
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
372
376
  if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
373
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
377
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
374
378
  return {
375
379
  $valueType: [],
376
380
  $stateType: [],
377
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
381
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
378
382
  usage: parserPairs.flatMap(([_, p]) => p.usage),
379
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
383
+ initialState,
380
384
  parse(context) {
385
+ if (!options.allowDuplicates) {
386
+ const optionNameSources = /* @__PURE__ */ new Map();
387
+ for (const [field, parser] of parserPairs) {
388
+ const names = require_usage.extractOptionNames(parser.usage);
389
+ for (const name of names) {
390
+ if (!optionNameSources.has(name)) optionNameSources.set(name, []);
391
+ optionNameSources.get(name).push(field);
392
+ }
393
+ }
394
+ for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
+ success: false,
396
+ consumed: 0,
397
+ error: require_message.message`Duplicate option name ${require_message.optionName(name)} found in fields: ${require_message.values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
398
+ };
399
+ }
381
400
  let error = {
382
401
  consumed: 0,
383
402
  error: context.buffer.length > 0 ? (() => {
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
447
466
  },
448
467
  complete(state) {
449
468
  const result = {};
450
- for (const field in state) {
451
- if (!(field in parsers)) continue;
469
+ for (const field of parserKeys) {
452
470
  const valueResult = parsers[field].complete(state[field]);
453
471
  if (valueResult.success) result[field] = valueResult.value;
454
472
  else return {
@@ -83,8 +83,8 @@ interface OrErrorOptions {
83
83
  */
84
84
  declare class DuplicateOptionError extends Error {
85
85
  readonly optionName: string;
86
- readonly sources: string[];
87
- constructor(optionName: string, sources: string[]);
86
+ readonly sources: readonly (string | symbol)[];
87
+ constructor(optionName: string, sources: readonly (string | symbol)[]);
88
88
  }
89
89
  /**
90
90
  * Creates a parser that combines two mutually exclusive parsers into one.
@@ -83,8 +83,8 @@ interface OrErrorOptions {
83
83
  */
84
84
  declare class DuplicateOptionError extends Error {
85
85
  readonly optionName: string;
86
- readonly sources: string[];
87
- constructor(optionName: string, sources: string[]);
86
+ readonly sources: readonly (string | symbol)[];
87
+ constructor(optionName: string, sources: readonly (string | symbol)[]);
88
88
  }
89
89
  /**
90
90
  * Creates a parser that combines two mutually exclusive parsers into one.
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
73
73
  */
74
74
  var DuplicateOptionError = class extends Error {
75
75
  constructor(optionName$1, sources) {
76
- super(`Duplicate option name "${optionName$1}" found in fields: ${sources.join(", ")}. Each option name must be unique within a parser combinator.`);
76
+ const sourceNames = sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s);
77
+ super(`Duplicate option name "${optionName$1}" found in fields: ${sourceNames.join(", ")}. Each option name must be unique within a parser combinator.`);
77
78
  this.optionName = optionName$1;
78
79
  this.sources = sources;
79
80
  this.name = "DuplicateOptionError";
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
367
368
  parsers = labelOrParsers;
368
369
  options = maybeParsersOrOptions ?? {};
369
370
  }
370
- const parserPairs = Object.entries(parsers);
371
+ const parserKeys = Reflect.ownKeys(parsers);
372
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
371
373
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
374
+ const initialState = {};
375
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
372
376
  if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
373
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
377
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
374
378
  return {
375
379
  $valueType: [],
376
380
  $stateType: [],
377
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
381
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
378
382
  usage: parserPairs.flatMap(([_, p]) => p.usage),
379
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
383
+ initialState,
380
384
  parse(context) {
385
+ if (!options.allowDuplicates) {
386
+ const optionNameSources = /* @__PURE__ */ new Map();
387
+ for (const [field, parser] of parserPairs) {
388
+ const names = extractOptionNames(parser.usage);
389
+ for (const name of names) {
390
+ if (!optionNameSources.has(name)) optionNameSources.set(name, []);
391
+ optionNameSources.get(name).push(field);
392
+ }
393
+ }
394
+ for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
+ success: false,
396
+ consumed: 0,
397
+ error: message`Duplicate option name ${optionName(name)} found in fields: ${values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
398
+ };
399
+ }
381
400
  let error = {
382
401
  consumed: 0,
383
402
  error: context.buffer.length > 0 ? (() => {
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
447
466
  },
448
467
  complete(state) {
449
468
  const result = {};
450
- for (const field in state) {
451
- if (!(field in parsers)) continue;
469
+ for (const field of parserKeys) {
452
470
  const valueResult = parsers[field].complete(state[field]);
453
471
  if (valueResult.success) result[field] = valueResult.value;
454
472
  else return {
@@ -316,10 +316,23 @@ function multiple(parser, options = {}) {
316
316
  },
317
317
  suggest(context, prefix) {
318
318
  const innerState = context.state.length > 0 ? context.state.at(-1) : parser.initialState;
319
- return parser.suggest({
319
+ const suggestions = parser.suggest({
320
320
  ...context,
321
321
  state: innerState
322
322
  }, prefix);
323
+ const selectedValues = /* @__PURE__ */ new Set();
324
+ for (const s of context.state) {
325
+ const completed = parser.complete(s);
326
+ if (completed.success) {
327
+ const valueStr = String(completed.value);
328
+ selectedValues.add(valueStr);
329
+ }
330
+ }
331
+ if (selectedValues.size > 0) return Array.from(suggestions).filter((suggestion) => {
332
+ if (suggestion.kind === "literal") return !selectedValues.has(suggestion.text);
333
+ return true;
334
+ });
335
+ return suggestions;
323
336
  },
324
337
  getDocFragments(state, defaultValue) {
325
338
  const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
package/dist/modifiers.js CHANGED
@@ -316,10 +316,23 @@ function multiple(parser, options = {}) {
316
316
  },
317
317
  suggest(context, prefix) {
318
318
  const innerState = context.state.length > 0 ? context.state.at(-1) : parser.initialState;
319
- return parser.suggest({
319
+ const suggestions = parser.suggest({
320
320
  ...context,
321
321
  state: innerState
322
322
  }, prefix);
323
+ const selectedValues = /* @__PURE__ */ new Set();
324
+ for (const s of context.state) {
325
+ const completed = parser.complete(s);
326
+ if (completed.success) {
327
+ const valueStr = String(completed.value);
328
+ selectedValues.add(valueStr);
329
+ }
330
+ }
331
+ if (selectedValues.size > 0) return Array.from(suggestions).filter((suggestion) => {
332
+ if (suggestion.kind === "literal") return !selectedValues.has(suggestion.text);
333
+ return true;
334
+ });
335
+ return suggestions;
323
336
  },
324
337
  getDocFragments(state, defaultValue) {
325
338
  const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",