@optique/core 1.1.3 → 1.1.4

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.
@@ -1528,6 +1528,33 @@ function createLongestMatch(...args) {
1528
1528
  return multiResult;
1529
1529
  }
1530
1530
  /**
1531
+ * Creates the initial parse error shared by object-like combinators.
1532
+ * @param context The current parser context.
1533
+ * @param noMatchContext The kinds of input accepted by the combinator.
1534
+ * @param errors Optional custom error formatters.
1535
+ * @returns A zero-consumption parse error.
1536
+ */
1537
+ function createObjectLikeInitialError(context, noMatchContext, errors) {
1538
+ if (context.buffer.length < 1) {
1539
+ const customEndOfInput = errors?.endOfInput;
1540
+ return {
1541
+ consumed: 0,
1542
+ error: customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext)
1543
+ };
1544
+ }
1545
+ const token = context.buffer[0];
1546
+ const customMessage = errors?.unexpectedInput;
1547
+ if (customMessage) return {
1548
+ consumed: 0,
1549
+ error: typeof customMessage === "function" ? customMessage(token) : customMessage
1550
+ };
1551
+ const baseError = require_message.message`Unexpected option or argument: ${token}.`;
1552
+ return {
1553
+ consumed: 0,
1554
+ error: require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", errors?.suggestions)
1555
+ };
1556
+ }
1557
+ /**
1531
1558
  * Internal sync helper for object suggest functionality.
1532
1559
  * @internal
1533
1560
  */
@@ -1920,19 +1947,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1920
1947
  checkDuplicateReachableLeadingCommandNames(parserPairs.map(([field, parser]) => [field, parser]));
1921
1948
  const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
1922
1949
  const combinedMode = parserKeys.some((k) => parsers[k].mode === "async") ? "async" : "sync";
1923
- const getInitialError = (context) => ({
1924
- consumed: 0,
1925
- error: context.buffer.length > 0 ? (() => {
1926
- const token = context.buffer[0];
1927
- const customMessage = options.errors?.unexpectedInput;
1928
- if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
1929
- const baseError = require_message.message`Unexpected option or argument: ${token}.`;
1930
- return require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
1931
- })() : (() => {
1932
- const customEndOfInput = options.errors?.endOfInput;
1933
- return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
1934
- })()
1935
- });
1950
+ const getInitialError = (context) => createObjectLikeInitialError(context, noMatchContext, options.errors);
1936
1951
  const parseSync = (context) => {
1937
1952
  let error = getInitialError(context);
1938
1953
  let currentContext = context;
@@ -3802,6 +3817,7 @@ function merge(...args) {
3802
3817
  const syncParsers = syncSorted.map(([p]) => p);
3803
3818
  if (!options.allowDuplicates) checkDuplicateOptionNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser.usage]));
3804
3819
  checkDuplicateReachableLeadingCommandNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser]));
3820
+ const noMatchContext = analyzeNoMatchContext(rawParsers);
3805
3821
  const mergedFieldParsers = collectChildFieldParsers(parsers);
3806
3822
  const duplicateOutputFieldNames = collectDuplicateFieldNames(mergedFieldParsers);
3807
3823
  const parserStateKey = (index) => `__parser_${index}`;
@@ -3895,8 +3911,7 @@ function merge(...args) {
3895
3911
  };
3896
3912
  return {
3897
3913
  success: false,
3898
- consumed: 0,
3899
- error: require_message.message`No matching option or argument found.`
3914
+ ...createObjectLikeInitialError(context, noMatchContext)
3900
3915
  };
3901
3916
  };
3902
3917
  const parseAsync = async (context) => {
@@ -3941,8 +3956,7 @@ function merge(...args) {
3941
3956
  };
3942
3957
  return {
3943
3958
  success: false,
3944
- consumed: 0,
3945
- error: require_message.message`No matching option or argument found.`
3959
+ ...createObjectLikeInitialError(context, noMatchContext)
3946
3960
  };
3947
3961
  };
3948
3962
  const mergeParser = {
@@ -1528,6 +1528,33 @@ function createLongestMatch(...args) {
1528
1528
  return multiResult;
1529
1529
  }
1530
1530
  /**
1531
+ * Creates the initial parse error shared by object-like combinators.
1532
+ * @param context The current parser context.
1533
+ * @param noMatchContext The kinds of input accepted by the combinator.
1534
+ * @param errors Optional custom error formatters.
1535
+ * @returns A zero-consumption parse error.
1536
+ */
1537
+ function createObjectLikeInitialError(context, noMatchContext, errors) {
1538
+ if (context.buffer.length < 1) {
1539
+ const customEndOfInput = errors?.endOfInput;
1540
+ return {
1541
+ consumed: 0,
1542
+ error: customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext)
1543
+ };
1544
+ }
1545
+ const token = context.buffer[0];
1546
+ const customMessage = errors?.unexpectedInput;
1547
+ if (customMessage) return {
1548
+ consumed: 0,
1549
+ error: typeof customMessage === "function" ? customMessage(token) : customMessage
1550
+ };
1551
+ const baseError = message`Unexpected option or argument: ${token}.`;
1552
+ return {
1553
+ consumed: 0,
1554
+ error: createErrorWithSuggestions(baseError, token, context.usage, "both", errors?.suggestions)
1555
+ };
1556
+ }
1557
+ /**
1531
1558
  * Internal sync helper for object suggest functionality.
1532
1559
  * @internal
1533
1560
  */
@@ -1920,19 +1947,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1920
1947
  checkDuplicateReachableLeadingCommandNames(parserPairs.map(([field, parser]) => [field, parser]));
1921
1948
  const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
1922
1949
  const combinedMode = parserKeys.some((k) => parsers[k].mode === "async") ? "async" : "sync";
1923
- const getInitialError = (context) => ({
1924
- consumed: 0,
1925
- error: context.buffer.length > 0 ? (() => {
1926
- const token = context.buffer[0];
1927
- const customMessage = options.errors?.unexpectedInput;
1928
- if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
1929
- const baseError = message`Unexpected option or argument: ${token}.`;
1930
- return createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
1931
- })() : (() => {
1932
- const customEndOfInput = options.errors?.endOfInput;
1933
- return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
1934
- })()
1935
- });
1950
+ const getInitialError = (context) => createObjectLikeInitialError(context, noMatchContext, options.errors);
1936
1951
  const parseSync = (context) => {
1937
1952
  let error = getInitialError(context);
1938
1953
  let currentContext = context;
@@ -3802,6 +3817,7 @@ function merge(...args) {
3802
3817
  const syncParsers = syncSorted.map(([p]) => p);
3803
3818
  if (!options.allowDuplicates) checkDuplicateOptionNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser.usage]));
3804
3819
  checkDuplicateReachableLeadingCommandNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser]));
3820
+ const noMatchContext = analyzeNoMatchContext(rawParsers);
3805
3821
  const mergedFieldParsers = collectChildFieldParsers(parsers);
3806
3822
  const duplicateOutputFieldNames = collectDuplicateFieldNames(mergedFieldParsers);
3807
3823
  const parserStateKey = (index) => `__parser_${index}`;
@@ -3895,8 +3911,7 @@ function merge(...args) {
3895
3911
  };
3896
3912
  return {
3897
3913
  success: false,
3898
- consumed: 0,
3899
- error: message`No matching option or argument found.`
3914
+ ...createObjectLikeInitialError(context, noMatchContext)
3900
3915
  };
3901
3916
  };
3902
3917
  const parseAsync = async (context) => {
@@ -3941,8 +3956,7 @@ function merge(...args) {
3941
3956
  };
3942
3957
  return {
3943
3958
  success: false,
3944
- consumed: 0,
3945
- error: message`No matching option or argument found.`
3959
+ ...createObjectLikeInitialError(context, noMatchContext)
3946
3960
  };
3947
3961
  };
3948
3962
  const mergeParser = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",
@@ -200,7 +200,7 @@
200
200
  "fast-check": "^4.7.0",
201
201
  "tsdown": "^0.13.0",
202
202
  "typescript": "^5.8.3",
203
- "@optique/env": "1.1.3"
203
+ "@optique/env": "1.1.4"
204
204
  },
205
205
  "scripts": {
206
206
  "build": "tsdown",