@optique/core 1.0.0-dev.908 → 1.0.0
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/dist/annotation-state.cjs +425 -0
- package/dist/annotation-state.d.cts +24 -0
- package/dist/annotation-state.d.ts +24 -0
- package/dist/annotation-state.js +414 -0
- package/dist/annotations.cjs +2 -248
- package/dist/annotations.d.cts +2 -137
- package/dist/annotations.d.ts +2 -137
- package/dist/annotations.js +2 -238
- package/dist/completion.cjs +611 -100
- package/dist/completion.d.cts +1 -1
- package/dist/completion.d.ts +1 -1
- package/dist/completion.js +611 -100
- package/dist/constructs.cjs +3338 -827
- package/dist/constructs.d.cts +48 -7
- package/dist/constructs.d.ts +48 -7
- package/dist/constructs.js +3338 -827
- package/dist/context.cjs +0 -23
- package/dist/context.d.cts +119 -53
- package/dist/context.d.ts +119 -53
- package/dist/context.js +0 -22
- package/dist/dependency-metadata.cjs +139 -0
- package/dist/dependency-metadata.d.cts +112 -0
- package/dist/dependency-metadata.d.ts +112 -0
- package/dist/dependency-metadata.js +138 -0
- package/dist/dependency-runtime.cjs +698 -0
- package/dist/dependency-runtime.d.cts +149 -0
- package/dist/dependency-runtime.d.ts +149 -0
- package/dist/dependency-runtime.js +687 -0
- package/dist/dependency.cjs +7 -928
- package/dist/dependency.d.cts +2 -794
- package/dist/dependency.d.ts +2 -794
- package/dist/dependency.js +2 -899
- package/dist/displaywidth.cjs +44 -0
- package/dist/displaywidth.js +43 -0
- package/dist/doc.cjs +285 -23
- package/dist/doc.d.cts +57 -2
- package/dist/doc.d.ts +57 -2
- package/dist/doc.js +283 -25
- package/dist/execution-context.cjs +56 -0
- package/dist/execution-context.js +53 -0
- package/dist/extension.cjs +87 -0
- package/dist/extension.d.cts +97 -0
- package/dist/extension.d.ts +97 -0
- package/dist/extension.js +76 -0
- package/dist/facade.cjs +718 -523
- package/dist/facade.d.cts +87 -18
- package/dist/facade.d.ts +87 -18
- package/dist/facade.js +718 -523
- package/dist/index.cjs +14 -29
- package/dist/index.d.cts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +7 -7
- package/dist/input-trace.cjs +56 -0
- package/dist/input-trace.d.cts +77 -0
- package/dist/input-trace.d.ts +77 -0
- package/dist/input-trace.js +55 -0
- package/dist/internal/annotations.cjs +316 -0
- package/dist/internal/annotations.d.cts +140 -0
- package/dist/internal/annotations.d.ts +140 -0
- package/dist/internal/annotations.js +306 -0
- package/dist/internal/dependency.cjs +984 -0
- package/dist/internal/dependency.d.cts +539 -0
- package/dist/internal/dependency.d.ts +539 -0
- package/dist/internal/dependency.js +964 -0
- package/dist/{mode-dispatch.cjs → internal/mode-dispatch.cjs} +1 -3
- package/dist/{mode-dispatch.d.cts → internal/mode-dispatch.d.cts} +3 -7
- package/dist/{mode-dispatch.d.ts → internal/mode-dispatch.d.ts} +3 -7
- package/dist/{mode-dispatch.js → internal/mode-dispatch.js} +1 -3
- package/dist/internal/parser.cjs +728 -0
- package/dist/internal/parser.d.cts +947 -0
- package/dist/internal/parser.d.ts +947 -0
- package/dist/internal/parser.js +711 -0
- package/dist/message.cjs +84 -26
- package/dist/message.d.cts +49 -9
- package/dist/message.d.ts +49 -9
- package/dist/message.js +84 -27
- package/dist/modifiers.cjs +1023 -240
- package/dist/modifiers.d.cts +42 -1
- package/dist/modifiers.d.ts +42 -1
- package/dist/modifiers.js +1023 -240
- package/dist/parser.cjs +11 -463
- package/dist/parser.d.cts +3 -537
- package/dist/parser.d.ts +3 -537
- package/dist/parser.js +2 -433
- package/dist/phase2-seed.cjs +59 -0
- package/dist/phase2-seed.js +56 -0
- package/dist/primitives.cjs +557 -208
- package/dist/primitives.d.cts +10 -14
- package/dist/primitives.d.ts +10 -14
- package/dist/primitives.js +557 -208
- package/dist/program.cjs +5 -1
- package/dist/program.d.cts +5 -3
- package/dist/program.d.ts +5 -3
- package/dist/program.js +6 -1
- package/dist/suggestion.cjs +22 -8
- package/dist/suggestion.js +22 -8
- package/dist/usage-internals.cjs +3 -2
- package/dist/usage-internals.js +4 -2
- package/dist/usage.cjs +195 -40
- package/dist/usage.d.cts +92 -11
- package/dist/usage.d.ts +92 -11
- package/dist/usage.js +194 -41
- package/dist/validate.cjs +170 -0
- package/dist/validate.js +164 -0
- package/dist/valueparser.cjs +1278 -191
- package/dist/valueparser.d.cts +330 -20
- package/dist/valueparser.d.ts +330 -20
- package/dist/valueparser.js +1277 -192
- package/package.json +9 -9
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const require_dependency = require('./internal/dependency.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/dependency-metadata.ts
|
|
4
|
+
/**
|
|
5
|
+
* Extracts {@link ParserDependencyMetadata} from a value parser by reading
|
|
6
|
+
* old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
|
|
7
|
+
* etc.).
|
|
8
|
+
*
|
|
9
|
+
* Returns `undefined` if the parser has no dependency-related markers.
|
|
10
|
+
*
|
|
11
|
+
* @param valueParser The value parser to inspect.
|
|
12
|
+
* @returns Metadata, or `undefined` for plain parsers.
|
|
13
|
+
* @internal
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
*/
|
|
16
|
+
function extractDependencyMetadata(valueParser) {
|
|
17
|
+
if (require_dependency.isDependencySource(valueParser)) return { source: {
|
|
18
|
+
kind: "source",
|
|
19
|
+
sourceId: valueParser[require_dependency.dependencyId],
|
|
20
|
+
extractSourceValue: extractFromBareState,
|
|
21
|
+
preservesSourceValue: true
|
|
22
|
+
} };
|
|
23
|
+
if (require_dependency.isDerivedValueParser(valueParser)) {
|
|
24
|
+
const isMultiSource = require_dependency.dependencyIds in valueParser && valueParser[require_dependency.dependencyIds] != null;
|
|
25
|
+
const allIds = isMultiSource ? valueParser[require_dependency.dependencyIds] : [valueParser[require_dependency.dependencyId]];
|
|
26
|
+
let defaultValuesFn;
|
|
27
|
+
if (require_dependency.defaultValues in valueParser && valueParser[require_dependency.defaultValues] != null) defaultValuesFn = valueParser[require_dependency.defaultValues];
|
|
28
|
+
else if (require_dependency.singleDefaultValue in valueParser && valueParser[require_dependency.singleDefaultValue] != null) {
|
|
29
|
+
const singleFn = valueParser[require_dependency.singleDefaultValue];
|
|
30
|
+
defaultValuesFn = () => [singleFn()];
|
|
31
|
+
}
|
|
32
|
+
const parser = valueParser;
|
|
33
|
+
const replayParse = (rawInput, depValues) => {
|
|
34
|
+
const depArg = isMultiSource ? depValues : depValues[0];
|
|
35
|
+
return parser[require_dependency.parseWithDependency](rawInput, depArg);
|
|
36
|
+
};
|
|
37
|
+
const suggestFn = require_dependency.suggestWithDependency in parser ? parser[require_dependency.suggestWithDependency] : void 0;
|
|
38
|
+
const replaySuggest = suggestFn != null ? (prefix, depValues) => {
|
|
39
|
+
const depArg = isMultiSource ? depValues : depValues[0];
|
|
40
|
+
return suggestFn(prefix, depArg);
|
|
41
|
+
} : void 0;
|
|
42
|
+
return { derived: {
|
|
43
|
+
kind: "derived",
|
|
44
|
+
dependencyIds: allIds,
|
|
45
|
+
getDefaultDependencyValues: defaultValuesFn,
|
|
46
|
+
replayParse,
|
|
47
|
+
replaySuggest
|
|
48
|
+
} };
|
|
49
|
+
}
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extracts the source parse result from a plain source state.
|
|
54
|
+
*
|
|
55
|
+
* Accepts either a `DependencySourceState` or a bare
|
|
56
|
+
* `ValueParserResult`-shaped object, and returns `undefined` for
|
|
57
|
+
* unrelated states. Used as the base `extractSourceValue` for plain
|
|
58
|
+
* dependency sources.
|
|
59
|
+
*/
|
|
60
|
+
function extractFromBareState(state) {
|
|
61
|
+
if (require_dependency.isDependencySourceState(state)) return state.result;
|
|
62
|
+
if (state != null && typeof state === "object" && Object.hasOwn(state, "success") && typeof state.success === "boolean" && (state.success && Object.hasOwn(state, "value") || !state.success && Object.hasOwn(state, "error"))) return state;
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
|
|
67
|
+
* Used by `optional()` and `withDefault()` which wrap state in a
|
|
68
|
+
* single-element array.
|
|
69
|
+
*/
|
|
70
|
+
function unwrapArrayThenExtract(innerExtract) {
|
|
71
|
+
return (state) => {
|
|
72
|
+
if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
|
|
73
|
+
return innerExtract(state);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Composes dependency metadata through a modifier wrapper.
|
|
78
|
+
*
|
|
79
|
+
* - `"optional"`: composes `extractSourceValue` with array unwrapping.
|
|
80
|
+
* - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
|
|
81
|
+
* preserves a dependency source.
|
|
82
|
+
* - `"map"`: sets `transformsSourceValue` and clears
|
|
83
|
+
* `preservesSourceValue`.
|
|
84
|
+
*
|
|
85
|
+
* Returns `undefined` if `inner` is `undefined`.
|
|
86
|
+
*
|
|
87
|
+
* @param inner The inner parser's metadata.
|
|
88
|
+
* @param wrapperKind The type of modifier being applied.
|
|
89
|
+
* @param options Additional options for certain wrapper kinds.
|
|
90
|
+
* @returns Composed metadata, or `undefined`.
|
|
91
|
+
* @internal
|
|
92
|
+
* @since 1.0.0
|
|
93
|
+
*/
|
|
94
|
+
function composeDependencyMetadata(inner, wrapperKind, options) {
|
|
95
|
+
if (inner === void 0) return void 0;
|
|
96
|
+
switch (wrapperKind) {
|
|
97
|
+
case "optional": {
|
|
98
|
+
if (inner.source != null && inner.source.extractSourceValue != null) return {
|
|
99
|
+
...inner,
|
|
100
|
+
source: {
|
|
101
|
+
...inner.source,
|
|
102
|
+
extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
return inner;
|
|
106
|
+
}
|
|
107
|
+
case "withDefault": {
|
|
108
|
+
const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
|
|
109
|
+
const preservesSourceValue = inner.source?.preservesSourceValue !== false;
|
|
110
|
+
if (inner.source != null) return {
|
|
111
|
+
...inner,
|
|
112
|
+
source: {
|
|
113
|
+
...inner.source,
|
|
114
|
+
...wrappedExtract != null && { extractSourceValue: wrappedExtract },
|
|
115
|
+
...preservesSourceValue && options?.defaultValue != null && { getMissingSourceValue: options.defaultValue }
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return inner;
|
|
119
|
+
}
|
|
120
|
+
case "map": {
|
|
121
|
+
const result = {
|
|
122
|
+
...inner,
|
|
123
|
+
transform: { transformsSourceValue: true }
|
|
124
|
+
};
|
|
125
|
+
if (inner.source != null) return {
|
|
126
|
+
...result,
|
|
127
|
+
source: {
|
|
128
|
+
...inner.source,
|
|
129
|
+
preservesSourceValue: false
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
exports.composeDependencyMetadata = composeDependencyMetadata;
|
|
139
|
+
exports.extractDependencyMetadata = extractDependencyMetadata;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ValueParserResult } from "./valueparser.cjs";
|
|
2
|
+
import { Suggestion } from "./internal/parser.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/dependency-metadata.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Metadata for a parser that is a dependency source.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
interface DependencySourceCapability {
|
|
13
|
+
/** Discriminant tag. */
|
|
14
|
+
readonly kind: "source";
|
|
15
|
+
/** The unique dependency source identifier. */
|
|
16
|
+
readonly sourceId: symbol;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the dependency source parse result from the parser's state.
|
|
19
|
+
*
|
|
20
|
+
* The `state` argument is the current parser state for the source-owning
|
|
21
|
+
* parser. Each wrapper composes this method to handle its own state shape:
|
|
22
|
+
* - plain source: reads from `DependencySourceState`
|
|
23
|
+
* - `optional()` / `withDefault()`: unwraps `[innerState]` first
|
|
24
|
+
* - `map()`: reads the pre-transform value from inner state
|
|
25
|
+
*
|
|
26
|
+
* Returns the `ValueParserResult` (which may be successful with any
|
|
27
|
+
* value including `undefined`, or failed), a promise that resolves to the
|
|
28
|
+
* same shape when extraction needs async work, or `undefined` if the state
|
|
29
|
+
* does not contain a source result at all (unpopulated / wrong shape).
|
|
30
|
+
* Callers and wrapper authors must handle both direct and promise-wrapped
|
|
31
|
+
* results when composing `extractSourceValue`.
|
|
32
|
+
*/
|
|
33
|
+
readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown> | undefined> | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* When present, provides a missing-source value (e.g., from a
|
|
36
|
+
* `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
|
|
37
|
+
* phase of the dependency runtime.
|
|
38
|
+
*/
|
|
39
|
+
readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the parser's output value is the actual dependency source value.
|
|
42
|
+
* `false` when a transform like `map()` has been applied.
|
|
43
|
+
*/
|
|
44
|
+
readonly preservesSourceValue: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Metadata for a parser that depends on one or more dependency sources.
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
*/
|
|
52
|
+
interface DerivedDependencyCapability {
|
|
53
|
+
/** Discriminant tag. */
|
|
54
|
+
readonly kind: "derived";
|
|
55
|
+
/** The dependency source IDs this parser depends on. */
|
|
56
|
+
readonly dependencyIds: readonly symbol[];
|
|
57
|
+
/**
|
|
58
|
+
* Returns default values for each dependency, used when the
|
|
59
|
+
* corresponding sources are not provided.
|
|
60
|
+
*/
|
|
61
|
+
readonly getDefaultDependencyValues?: () => readonly unknown[];
|
|
62
|
+
/**
|
|
63
|
+
* Replays a parse with the given raw input and resolved dependency values.
|
|
64
|
+
*/
|
|
65
|
+
readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
|
|
66
|
+
/**
|
|
67
|
+
* Replays suggestions with the given prefix and resolved dependency values.
|
|
68
|
+
*/
|
|
69
|
+
readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Metadata indicating that a wrapper transforms the dependency source value.
|
|
73
|
+
*
|
|
74
|
+
* @internal
|
|
75
|
+
* @since 1.0.0
|
|
76
|
+
*/
|
|
77
|
+
interface DependencyTransformCapability {
|
|
78
|
+
/** Whether the wrapper transforms the source value. */
|
|
79
|
+
readonly transformsSourceValue: boolean;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Composed dependency metadata for a parser node.
|
|
83
|
+
*
|
|
84
|
+
* A parser may have any combination of source, derived, and transform
|
|
85
|
+
* capabilities. In practice, a parser is typically either a source or
|
|
86
|
+
* derived, never both.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
* @since 1.0.0
|
|
90
|
+
*/
|
|
91
|
+
interface ParserDependencyMetadata {
|
|
92
|
+
/** Present if the parser is (or wraps) a dependency source. */
|
|
93
|
+
readonly source?: DependencySourceCapability;
|
|
94
|
+
/** Present if the parser depends on one or more sources. */
|
|
95
|
+
readonly derived?: DerivedDependencyCapability;
|
|
96
|
+
/** Present if a transform has been applied. */
|
|
97
|
+
readonly transform?: DependencyTransformCapability;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extracts {@link ParserDependencyMetadata} from a value parser by reading
|
|
101
|
+
* old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
|
|
102
|
+
* etc.).
|
|
103
|
+
*
|
|
104
|
+
* Returns `undefined` if the parser has no dependency-related markers.
|
|
105
|
+
*
|
|
106
|
+
* @param valueParser The value parser to inspect.
|
|
107
|
+
* @returns Metadata, or `undefined` for plain parsers.
|
|
108
|
+
* @internal
|
|
109
|
+
* @since 1.0.0
|
|
110
|
+
*/
|
|
111
|
+
//#endregion
|
|
112
|
+
export { ParserDependencyMetadata };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ValueParserResult } from "./valueparser.js";
|
|
2
|
+
import { Suggestion } from "./internal/parser.js";
|
|
3
|
+
|
|
4
|
+
//#region src/dependency-metadata.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Metadata for a parser that is a dependency source.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
interface DependencySourceCapability {
|
|
13
|
+
/** Discriminant tag. */
|
|
14
|
+
readonly kind: "source";
|
|
15
|
+
/** The unique dependency source identifier. */
|
|
16
|
+
readonly sourceId: symbol;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the dependency source parse result from the parser's state.
|
|
19
|
+
*
|
|
20
|
+
* The `state` argument is the current parser state for the source-owning
|
|
21
|
+
* parser. Each wrapper composes this method to handle its own state shape:
|
|
22
|
+
* - plain source: reads from `DependencySourceState`
|
|
23
|
+
* - `optional()` / `withDefault()`: unwraps `[innerState]` first
|
|
24
|
+
* - `map()`: reads the pre-transform value from inner state
|
|
25
|
+
*
|
|
26
|
+
* Returns the `ValueParserResult` (which may be successful with any
|
|
27
|
+
* value including `undefined`, or failed), a promise that resolves to the
|
|
28
|
+
* same shape when extraction needs async work, or `undefined` if the state
|
|
29
|
+
* does not contain a source result at all (unpopulated / wrong shape).
|
|
30
|
+
* Callers and wrapper authors must handle both direct and promise-wrapped
|
|
31
|
+
* results when composing `extractSourceValue`.
|
|
32
|
+
*/
|
|
33
|
+
readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown> | undefined> | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* When present, provides a missing-source value (e.g., from a
|
|
36
|
+
* `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
|
|
37
|
+
* phase of the dependency runtime.
|
|
38
|
+
*/
|
|
39
|
+
readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the parser's output value is the actual dependency source value.
|
|
42
|
+
* `false` when a transform like `map()` has been applied.
|
|
43
|
+
*/
|
|
44
|
+
readonly preservesSourceValue: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Metadata for a parser that depends on one or more dependency sources.
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
*/
|
|
52
|
+
interface DerivedDependencyCapability {
|
|
53
|
+
/** Discriminant tag. */
|
|
54
|
+
readonly kind: "derived";
|
|
55
|
+
/** The dependency source IDs this parser depends on. */
|
|
56
|
+
readonly dependencyIds: readonly symbol[];
|
|
57
|
+
/**
|
|
58
|
+
* Returns default values for each dependency, used when the
|
|
59
|
+
* corresponding sources are not provided.
|
|
60
|
+
*/
|
|
61
|
+
readonly getDefaultDependencyValues?: () => readonly unknown[];
|
|
62
|
+
/**
|
|
63
|
+
* Replays a parse with the given raw input and resolved dependency values.
|
|
64
|
+
*/
|
|
65
|
+
readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
|
|
66
|
+
/**
|
|
67
|
+
* Replays suggestions with the given prefix and resolved dependency values.
|
|
68
|
+
*/
|
|
69
|
+
readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Metadata indicating that a wrapper transforms the dependency source value.
|
|
73
|
+
*
|
|
74
|
+
* @internal
|
|
75
|
+
* @since 1.0.0
|
|
76
|
+
*/
|
|
77
|
+
interface DependencyTransformCapability {
|
|
78
|
+
/** Whether the wrapper transforms the source value. */
|
|
79
|
+
readonly transformsSourceValue: boolean;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Composed dependency metadata for a parser node.
|
|
83
|
+
*
|
|
84
|
+
* A parser may have any combination of source, derived, and transform
|
|
85
|
+
* capabilities. In practice, a parser is typically either a source or
|
|
86
|
+
* derived, never both.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
* @since 1.0.0
|
|
90
|
+
*/
|
|
91
|
+
interface ParserDependencyMetadata {
|
|
92
|
+
/** Present if the parser is (or wraps) a dependency source. */
|
|
93
|
+
readonly source?: DependencySourceCapability;
|
|
94
|
+
/** Present if the parser depends on one or more sources. */
|
|
95
|
+
readonly derived?: DerivedDependencyCapability;
|
|
96
|
+
/** Present if a transform has been applied. */
|
|
97
|
+
readonly transform?: DependencyTransformCapability;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extracts {@link ParserDependencyMetadata} from a value parser by reading
|
|
101
|
+
* old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
|
|
102
|
+
* etc.).
|
|
103
|
+
*
|
|
104
|
+
* Returns `undefined` if the parser has no dependency-related markers.
|
|
105
|
+
*
|
|
106
|
+
* @param valueParser The value parser to inspect.
|
|
107
|
+
* @returns Metadata, or `undefined` for plain parsers.
|
|
108
|
+
* @internal
|
|
109
|
+
* @since 1.0.0
|
|
110
|
+
*/
|
|
111
|
+
//#endregion
|
|
112
|
+
export { ParserDependencyMetadata };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { defaultValues, dependencyId, dependencyIds, isDependencySource, isDependencySourceState, isDerivedValueParser, parseWithDependency, singleDefaultValue, suggestWithDependency } from "./internal/dependency.js";
|
|
2
|
+
|
|
3
|
+
//#region src/dependency-metadata.ts
|
|
4
|
+
/**
|
|
5
|
+
* Extracts {@link ParserDependencyMetadata} from a value parser by reading
|
|
6
|
+
* old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
|
|
7
|
+
* etc.).
|
|
8
|
+
*
|
|
9
|
+
* Returns `undefined` if the parser has no dependency-related markers.
|
|
10
|
+
*
|
|
11
|
+
* @param valueParser The value parser to inspect.
|
|
12
|
+
* @returns Metadata, or `undefined` for plain parsers.
|
|
13
|
+
* @internal
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
*/
|
|
16
|
+
function extractDependencyMetadata(valueParser) {
|
|
17
|
+
if (isDependencySource(valueParser)) return { source: {
|
|
18
|
+
kind: "source",
|
|
19
|
+
sourceId: valueParser[dependencyId],
|
|
20
|
+
extractSourceValue: extractFromBareState,
|
|
21
|
+
preservesSourceValue: true
|
|
22
|
+
} };
|
|
23
|
+
if (isDerivedValueParser(valueParser)) {
|
|
24
|
+
const isMultiSource = dependencyIds in valueParser && valueParser[dependencyIds] != null;
|
|
25
|
+
const allIds = isMultiSource ? valueParser[dependencyIds] : [valueParser[dependencyId]];
|
|
26
|
+
let defaultValuesFn;
|
|
27
|
+
if (defaultValues in valueParser && valueParser[defaultValues] != null) defaultValuesFn = valueParser[defaultValues];
|
|
28
|
+
else if (singleDefaultValue in valueParser && valueParser[singleDefaultValue] != null) {
|
|
29
|
+
const singleFn = valueParser[singleDefaultValue];
|
|
30
|
+
defaultValuesFn = () => [singleFn()];
|
|
31
|
+
}
|
|
32
|
+
const parser = valueParser;
|
|
33
|
+
const replayParse = (rawInput, depValues) => {
|
|
34
|
+
const depArg = isMultiSource ? depValues : depValues[0];
|
|
35
|
+
return parser[parseWithDependency](rawInput, depArg);
|
|
36
|
+
};
|
|
37
|
+
const suggestFn = suggestWithDependency in parser ? parser[suggestWithDependency] : void 0;
|
|
38
|
+
const replaySuggest = suggestFn != null ? (prefix, depValues) => {
|
|
39
|
+
const depArg = isMultiSource ? depValues : depValues[0];
|
|
40
|
+
return suggestFn(prefix, depArg);
|
|
41
|
+
} : void 0;
|
|
42
|
+
return { derived: {
|
|
43
|
+
kind: "derived",
|
|
44
|
+
dependencyIds: allIds,
|
|
45
|
+
getDefaultDependencyValues: defaultValuesFn,
|
|
46
|
+
replayParse,
|
|
47
|
+
replaySuggest
|
|
48
|
+
} };
|
|
49
|
+
}
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extracts the source parse result from a plain source state.
|
|
54
|
+
*
|
|
55
|
+
* Accepts either a `DependencySourceState` or a bare
|
|
56
|
+
* `ValueParserResult`-shaped object, and returns `undefined` for
|
|
57
|
+
* unrelated states. Used as the base `extractSourceValue` for plain
|
|
58
|
+
* dependency sources.
|
|
59
|
+
*/
|
|
60
|
+
function extractFromBareState(state) {
|
|
61
|
+
if (isDependencySourceState(state)) return state.result;
|
|
62
|
+
if (state != null && typeof state === "object" && Object.hasOwn(state, "success") && typeof state.success === "boolean" && (state.success && Object.hasOwn(state, "value") || !state.success && Object.hasOwn(state, "error"))) return state;
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
|
|
67
|
+
* Used by `optional()` and `withDefault()` which wrap state in a
|
|
68
|
+
* single-element array.
|
|
69
|
+
*/
|
|
70
|
+
function unwrapArrayThenExtract(innerExtract) {
|
|
71
|
+
return (state) => {
|
|
72
|
+
if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
|
|
73
|
+
return innerExtract(state);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Composes dependency metadata through a modifier wrapper.
|
|
78
|
+
*
|
|
79
|
+
* - `"optional"`: composes `extractSourceValue` with array unwrapping.
|
|
80
|
+
* - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
|
|
81
|
+
* preserves a dependency source.
|
|
82
|
+
* - `"map"`: sets `transformsSourceValue` and clears
|
|
83
|
+
* `preservesSourceValue`.
|
|
84
|
+
*
|
|
85
|
+
* Returns `undefined` if `inner` is `undefined`.
|
|
86
|
+
*
|
|
87
|
+
* @param inner The inner parser's metadata.
|
|
88
|
+
* @param wrapperKind The type of modifier being applied.
|
|
89
|
+
* @param options Additional options for certain wrapper kinds.
|
|
90
|
+
* @returns Composed metadata, or `undefined`.
|
|
91
|
+
* @internal
|
|
92
|
+
* @since 1.0.0
|
|
93
|
+
*/
|
|
94
|
+
function composeDependencyMetadata(inner, wrapperKind, options) {
|
|
95
|
+
if (inner === void 0) return void 0;
|
|
96
|
+
switch (wrapperKind) {
|
|
97
|
+
case "optional": {
|
|
98
|
+
if (inner.source != null && inner.source.extractSourceValue != null) return {
|
|
99
|
+
...inner,
|
|
100
|
+
source: {
|
|
101
|
+
...inner.source,
|
|
102
|
+
extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
return inner;
|
|
106
|
+
}
|
|
107
|
+
case "withDefault": {
|
|
108
|
+
const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
|
|
109
|
+
const preservesSourceValue = inner.source?.preservesSourceValue !== false;
|
|
110
|
+
if (inner.source != null) return {
|
|
111
|
+
...inner,
|
|
112
|
+
source: {
|
|
113
|
+
...inner.source,
|
|
114
|
+
...wrappedExtract != null && { extractSourceValue: wrappedExtract },
|
|
115
|
+
...preservesSourceValue && options?.defaultValue != null && { getMissingSourceValue: options.defaultValue }
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return inner;
|
|
119
|
+
}
|
|
120
|
+
case "map": {
|
|
121
|
+
const result = {
|
|
122
|
+
...inner,
|
|
123
|
+
transform: { transformsSourceValue: true }
|
|
124
|
+
};
|
|
125
|
+
if (inner.source != null) return {
|
|
126
|
+
...result,
|
|
127
|
+
source: {
|
|
128
|
+
...inner.source,
|
|
129
|
+
preservesSourceValue: false
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
export { composeDependencyMetadata, extractDependencyMetadata };
|