@optique/core 1.4.0-dev.2607 → 1.4.0-dev.2608

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.
@@ -1,186 +0,0 @@
1
- import { inheritAnnotations } from "./internal/annotations.js";
2
- import { extractOptionNames } from "./usage.js";
3
- import { collectOptionNamesAtCurrentCommandDepth } from "./usage-internals.js";
4
-
5
- //#region src/short-option.ts
6
- const optionScopeKey = Symbol("optionScope");
7
- const scopeSourceKey = Symbol("optionScopeSource");
8
- const primitiveScopes = /* @__PURE__ */ new WeakMap();
9
- function parserScope(parser) {
10
- const declared = parser[scopeSourceKey];
11
- if (declared != null) return declared;
12
- let source = primitiveScopes.get(parser);
13
- if (source == null) {
14
- source = (_selections, arities) => {
15
- if (arities != null) collectOptionNamesAtCurrentCommandDepth(parser.usage, arities, false);
16
- return parser.leadingNames;
17
- };
18
- primitiveScopes.set(parser, source);
19
- }
20
- return source;
21
- }
22
- /** Combines declarations without flattening conditional ownership. @internal */
23
- function combinedOptionScope(parsers) {
24
- return (selections, arities, context) => new Set(parsers.flatMap((parser) => [...parserScope(parser)(selections, arities, context)]));
25
- }
26
- /** Tracks declarations after a command or discriminator routes input. @internal */
27
- function selectableOptionScope(initial) {
28
- const source = (selections, arities, context) => (selections.get(source) ?? initial)(selections, arities, context);
29
- return {
30
- source,
31
- select(context, selected) {
32
- context[optionScopeKey]?.selections.set(source, selected);
33
- }
34
- };
35
- }
36
- /** Keeps selected conditional names distinct from sibling declarations. @internal */
37
- function conditionalOptionScope(discriminator, fallback, candidates = []) {
38
- const scope = selectableOptionScope((selections, arities, context) => {
39
- const canSkip = discriminator.canSkip?.(context == null ? discriminator.initialState : inheritAnnotations(context.state, discriminator.initialState), context?.exec) === true;
40
- return combinedOptionScope([
41
- discriminator,
42
- ...canSkip ? candidates : [],
43
- ...fallback == null ? [] : [fallback]
44
- ])(selections, arities, context);
45
- });
46
- return {
47
- source: scope.source,
48
- select(context, branch) {
49
- scope.select(context, combinedOptionScope([discriminator, branch]));
50
- }
51
- };
52
- }
53
- /** Returns the names reachable in this parse scope. @internal */
54
- function getOptionScope(context) {
55
- const scoped = context[optionScopeKey];
56
- const names = scoped == null ? extractOptionNames(context.usage, true) : new Set([...scoped.names, ...[...scoped.sources].flatMap((source) => [...source(scoped.selections, void 0, context)])]);
57
- return names;
58
- }
59
- /** Adds reachable names without consulting the flattened help tree. @internal */
60
- function withOptionScope(context, names) {
61
- const inherited = context[optionScopeKey];
62
- return {
63
- ...context,
64
- [optionScopeKey]: {
65
- names: new Set([...inherited?.names ?? [], ...names]),
66
- sources: inherited?.sources ?? /* @__PURE__ */ new Set(),
67
- selections: inherited?.selections ?? /* @__PURE__ */ new Map()
68
- }
69
- };
70
- }
71
- /** Adds a parser's owned declarations, retaining modifier metadata. @internal */
72
- function withParserOptionScope(context, parser) {
73
- const inherited = context[optionScopeKey];
74
- return {
75
- ...context,
76
- [optionScopeKey]: {
77
- names: inherited?.names ?? /* @__PURE__ */ new Set(),
78
- sources: new Set([...inherited?.sources ?? [], parserScope(parser)]),
79
- selections: inherited?.selections ?? /* @__PURE__ */ new Map()
80
- }
81
- };
82
- }
83
- /** Isolates route selections while probing a competing branch. @internal */
84
- function forkOptionScope(context) {
85
- const scoped = context[optionScopeKey];
86
- return scoped == null ? context : {
87
- ...context,
88
- [optionScopeKey]: {
89
- ...scoped,
90
- selections: new Map(scoped.selections)
91
- }
92
- };
93
- }
94
- /** Publishes winning routes without persisting temporary candidate names. @internal */
95
- function adoptOptionScope(context, next) {
96
- const own = context[optionScopeKey];
97
- const child = next[optionScopeKey];
98
- if (own != null && child != null) for (const [source, selected] of child.selections) own.selections.set(source, selected);
99
- return context;
100
- }
101
- /** Removes names reserved only for a speculative probe. @internal */
102
- function restoreOptionScope(next, parent) {
103
- adoptOptionScope(parent, next);
104
- const own = parent[optionScopeKey];
105
- const child = next[optionScopeKey];
106
- return own == null || child == null ? next : {
107
- ...next,
108
- [optionScopeKey]: {
109
- ...child,
110
- names: own.names,
111
- selections: own.selections
112
- }
113
- };
114
- }
115
- /** Decorates construct entry points while retaining internal metadata. @internal */
116
- function scopeParser(parser, source = () => parser.leadingNames, prepare) {
117
- const parse = parser.parse;
118
- const suggest = parser.suggest;
119
- Object.defineProperties(parser, {
120
- [scopeSourceKey]: {
121
- value: source,
122
- enumerable: true
123
- },
124
- parse: {
125
- enumerable: true,
126
- configurable: true,
127
- writable: true,
128
- value: (context) => {
129
- const scoped = withParserOptionScope(context, parser);
130
- prepare?.(scoped);
131
- return parse.call(parser, scoped);
132
- }
133
- },
134
- suggest: {
135
- enumerable: true,
136
- configurable: true,
137
- writable: true,
138
- value: (context, prefix) => {
139
- const scoped = withParserOptionScope(context, parser);
140
- prepare?.(scoped);
141
- return suggest.call(parser, scoped, prefix);
142
- }
143
- }
144
- });
145
- return parser;
146
- }
147
- /** Tests full-name ownership before splitting a short prefix. @internal */
148
- function fullNameOwnsToken(context, token) {
149
- if (!/^-[^-]/.test(token)) return false;
150
- for (const name of getOptionScope(context)) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (token === name || token.startsWith(`${name}=`))) return true;
151
- return false;
152
- }
153
- /** Finds the value part of an attached short-option completion. @internal */
154
- function attachedValuePrefix(context, prefix, optionNames) {
155
- if (!/^-[^-]/.test(prefix)) return void 0;
156
- const scope = getOptionScope(context);
157
- const names = {
158
- value: /* @__PURE__ */ new Set(),
159
- flag: /* @__PURE__ */ new Set()
160
- };
161
- const scoped = context[optionScopeKey];
162
- if (scoped == null) collectOptionNamesAtCurrentCommandDepth(context.usage, names, false);
163
- else for (const source of scoped.sources) source(scoped.selections, names, context);
164
- for (let index = 1; index < prefix.length; index++) {
165
- const remainder = `-${prefix.slice(index)}`;
166
- for (const name of scope) if (name.startsWith("-") && !name.startsWith("--") && name.length > 2 && (name.startsWith(remainder) || remainder.startsWith(`${name}=`))) return void 0;
167
- const short = `-${prefix[index]}`;
168
- if (optionNames.includes(short)) return {
169
- head: prefix.slice(0, index + 1),
170
- value: prefix.slice(index + 1)
171
- };
172
- if (!scope.has(short) || !names.flag.has(short) || names.value.has(short)) return void 0;
173
- }
174
- return void 0;
175
- }
176
- /** Preserves the prefix when embedding value suggestions in one token. @internal */
177
- function prefixSuggestion(head, suggestion) {
178
- return {
179
- kind: "literal",
180
- text: head + (suggestion.kind === "literal" ? suggestion.text : suggestion.pattern ?? ""),
181
- ...suggestion.description != null ? { description: suggestion.description } : {}
182
- };
183
- }
184
-
185
- //#endregion
186
- export { adoptOptionScope, attachedValuePrefix, combinedOptionScope, conditionalOptionScope, forkOptionScope, fullNameOwnsToken, prefixSuggestion, restoreOptionScope, scopeParser, selectableOptionScope, withOptionScope, withParserOptionScope };