@loomcli/core 0.6.0 → 0.7.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.
@@ -1,5 +1,7 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { CaptureFaults } from './capture.js';
2
3
  import type { Finding } from './diagnostic-text.js';
4
+ import { DeclarationError } from './errors.js';
3
5
  import type { InputSite } from './facts.js';
4
6
  import type { OptionValues } from './options.js';
5
7
  import type { ArgumentConfig, ArgumentValue, Host, OptionConfig, OptionValue } from './types.js';
@@ -75,22 +77,36 @@ declare class ValidatedInputs {
75
77
  read(input: InputDeclaration): unknown;
76
78
  }
77
79
  export type { ValidatedInputs };
80
+ /** The faults a config's capture raises: those of every capture, and a default nested too deep. */
81
+ export interface ConfigFaults extends CaptureFaults {
82
+ /** The default holds a path deeper than `defaultLevels`; the finding prints it elided. */
83
+ readonly tooDeep: () => DeclarationError;
84
+ }
78
85
  /**
79
- * The one check every `argument()`, `option()`, and `globalOption()` call makes before it reads its
80
- * config, so a call in JavaScript that supplies none, or a value of another kind, reports a
81
- * declaration fault and not a TypeError. `place` is where the call sits.
86
+ * Authoring's one read of a config, through `captureDeclaration`: the prototype verdict, the copy of
87
+ * every own string key, the copy of its `extensions` list, and the snapshot of its default, inside
88
+ * one try. Every later check, the registry entry, and every sentence read the copy and never the
89
+ * author's object again, so a getter runs once and the caller's later changes reach nothing. The
90
+ * default is copied and frozen to `defaultLevels` levels, cycles included, and that copy is the
91
+ * value the graph publishes and a run validates; every other property is captured as declared,
92
+ * because core clones no library object. A read that throws, from a getter or a proxy trap, is the
93
+ * unreadable fault, named by the key it threw in, a default nested deeper is the too-deep fault,
94
+ * and a value that is not a plain object is the not-an-object fault.
82
95
  */
83
- export declare function checkInputConfig(declared: {
84
- readonly config: unknown;
85
- readonly kind: InputDeclaration['kind'];
86
- readonly name: string;
87
- }, place: InputPlace): void;
96
+ export declare function captureConfig<Config extends ArgumentConfig | OptionConfig>(config: Config, faults: ConfigFaults): Config;
97
+ /** The fault of a default nested deeper than `defaultLevels`, declared by `subject`. */
98
+ export declare function defaultDepthFault(subject: string, findings: readonly Finding[]): DeclarationError;
88
99
  /**
89
- * Authoring's snapshot of one config. An array default is the one declared value core hands to an
90
- * action as its own value, so the declaration keeps a copy and the caller keeps its array. Every
91
- * other property is captured as declared, because core clones no library object.
100
+ * The config every `argument()`, `option()`, and `globalOption()` call, and every input a lifecycle
101
+ * hook declares, reads through `captureConfig`. A call in JavaScript that supplies none, or a value
102
+ * of another kind, reports a declaration fault and not a TypeError, and so does a config whose read
103
+ * throws. Each fault marks the config on the call at `place`.
92
104
  */
93
- export declare function captureConfig<Config extends ArgumentConfig | OptionConfig>(config: Config): Config;
105
+ export declare function captureInputConfig<Config extends ArgumentConfig | OptionConfig>(declared: {
106
+ readonly config: Config;
107
+ readonly kind: InputDeclaration['kind'];
108
+ readonly name: string;
109
+ }, place: InputPlace): Config;
94
110
  /**
95
111
  * A declaration error names the declaration, because the author reads the declaration to fix it.
96
112
  * An argument declares and reads under one name, so the two namings differ for options alone.
@@ -112,6 +128,11 @@ export declare function inputPlace(input: InputDeclaration, scope: {
112
128
  * is the sentence's name for the input.
113
129
  */
114
130
  export declare function declaringSite(input: InputDeclaration, place: InputPlace, subject?: string): InputSite;
131
+ /**
132
+ * One input's site with its config printed elided, for a fault judged before the config is read,
133
+ * such as an invalid name, so the fault reads none of the config.
134
+ */
135
+ export declare function configUnread(site: InputSite): InputSite;
115
136
  /**
116
137
  * The declaration flag that sends an omitted value to its own validator. Every declaration reads it
117
138
  * here, and the declaration rules below reject it wherever another rule already decides absence.
@@ -1,10 +1,12 @@
1
+ import { captureDeclaration, elidedRead, unreadableFault } from './capture.js';
1
2
  import { schemaOptions } from './context.js';
2
3
  import { escapeControlCharacters } from './controls.js';
4
+ import { elided, spelled } from './diagnostic-text.js';
3
5
  import { asSentence, DeclarationError, InputError, quoted, reasonOf } from './errors.js';
4
6
  import { callSite, factFault, flagFault, partOf, siteFinding } from './facts.js';
5
- import { booleanOptionValueRule, defaultShape, invalidDefault, notAValidator, omissionAlreadyDecided, omissionWithoutValidator, requiredWithDefault, } from './input-rules.js';
7
+ import { booleanOptionValueRule, defaultDepth, defaultLevels, defaultShape, invalidDefault, notAValidator, omissionAlreadyDecided, omissionWithoutValidator, requiredWithDefault, } from './input-rules.js';
6
8
  import { booleanValue } from './options.js';
7
- import { isPlainObject } from './plain.js';
9
+ import { boundedSnapshot, NestedTooDeepError, shallowList } from './plain.js';
8
10
  import { notAnObject } from './plugin-rules.js';
9
11
  import { validatorFailed } from './rules.js';
10
12
  /** Every declaration in validation order: the globals first, then the reading Command's own. */
@@ -53,34 +55,65 @@ class ValidatedInputs {
53
55
  }
54
56
  }
55
57
  /**
56
- * The one check every `argument()`, `option()`, and `globalOption()` call makes before it reads its
57
- * config, so a call in JavaScript that supplies none, or a value of another kind, reports a
58
- * declaration fault and not a TypeError. `place` is where the call sits.
58
+ * Authoring's one read of a config, through `captureDeclaration`: the prototype verdict, the copy of
59
+ * every own string key, the copy of its `extensions` list, and the snapshot of its default, inside
60
+ * one try. Every later check, the registry entry, and every sentence read the copy and never the
61
+ * author's object again, so a getter runs once and the caller's later changes reach nothing. The
62
+ * default is copied and frozen to `defaultLevels` levels, cycles included, and that copy is the
63
+ * value the graph publishes and a run validates; every other property is captured as declared,
64
+ * because core clones no library object. A read that throws, from a getter or a proxy trap, is the
65
+ * unreadable fault, named by the key it threw in, a default nested deeper is the too-deep fault,
66
+ * and a value that is not a plain object is the not-an-object fault.
59
67
  */
60
- export function checkInputConfig(declared, place) {
61
- const { config, kind, name } = declared;
62
- if (isPlainObject(config)) {
63
- return;
64
- }
65
- const subject = kind === 'argument' ? 'Argument' : 'Option';
66
- const site = callSite(`${subject} ${quoted(name)}`, {
67
- arguments: [name, config],
68
- ...place,
68
+ export function captureConfig(config, faults) {
69
+ const { notAnObject: notAnObjectFault, tooDeep, unreadable } = faults;
70
+ return captureDeclaration(config, (copy, read) => {
71
+ // Presence is the key, so a declared `default: undefined` stays a default.
72
+ read.nested(copy, 'default', (value) => boundedSnapshot(value, defaultLevels));
73
+ read.nested(copy, 'extensions', shallowList);
74
+ }, {
75
+ notAnObject: notAnObjectFault,
76
+ unreadable: (thrown, slot) => thrown instanceof NestedTooDeepError ? tooDeep() : unreadable(thrown, slot),
69
77
  });
70
- throw new DeclarationError(notAnObject, {
71
- correction: `Supply ${kind === 'argument' ? 'an argument' : 'an option'} config object, such as ${kind === 'argument' ? '{}' : "{ type: 'string' }"}.`,
72
- findings: [siteFinding(site, '1')],
73
- sentence: `${site.subject} declares a config that is not an object.`,
78
+ }
79
+ /** The fault of a default nested deeper than `defaultLevels`, declared by `subject`. */
80
+ export function defaultDepthFault(subject, findings) {
81
+ return new DeclarationError(defaultDepth, {
82
+ correction: `Nest a default at most ${String(defaultLevels)} levels deep.`,
83
+ findings,
84
+ sentence: `${subject} default nests deeper than ${String(defaultLevels)} levels.`,
74
85
  });
75
86
  }
76
87
  /**
77
- * Authoring's snapshot of one config. An array default is the one declared value core hands to an
78
- * action as its own value, so the declaration keeps a copy and the caller keeps its array. Every
79
- * other property is captured as declared, because core clones no library object.
88
+ * The config every `argument()`, `option()`, and `globalOption()` call, and every input a lifecycle
89
+ * hook declares, reads through `captureConfig`. A call in JavaScript that supplies none, or a value
90
+ * of another kind, reports a declaration fault and not a TypeError, and so does a config whose read
91
+ * throws. Each fault marks the config on the call at `place`.
80
92
  */
81
- export function captureConfig(config) {
82
- const value = config.default;
83
- return Array.isArray(value) ? { ...config, default: [...value] } : { ...config };
93
+ export function captureInputConfig(declared, place) {
94
+ const { config, kind, name } = declared;
95
+ const subject = `${kind === 'argument' ? 'Argument' : 'Option'} ${quoted(name)}`;
96
+ // The finding marks the config, or one key inside it, which `shown` stands for when the call prints.
97
+ const findings = (shown, keys = []) => [
98
+ siteFinding(callSite(subject, { arguments: [name, shown], ...place }), ['1', ...keys].join('.')),
99
+ ];
100
+ return captureConfig(config, {
101
+ notAnObject: () => new DeclarationError(notAnObject, {
102
+ correction: `Supply ${kind === 'argument' ? 'an argument' : 'an option'} config object, such as ${kind === 'argument' ? '{}' : "{ type: 'string' }"}.`,
103
+ findings: findings(config),
104
+ sentence: `${subject} declares a config that is not an object.`,
105
+ }),
106
+ // The default's walk stopped at the limit, so the finding prints it elided.
107
+ tooDeep: () => {
108
+ const { keys, shown } = elidedRead('default');
109
+ return defaultDepthFault(subject, findings(shown, keys));
110
+ },
111
+ // A part whose read threw is never read again, so the finding prints it elided.
112
+ unreadable: (thrown, slot) => {
113
+ const { keys, shown } = elidedRead(slot);
114
+ return unreadableFault({ declared: 'config', findings: findings(shown, keys), subject }, thrown);
115
+ },
116
+ });
84
117
  }
85
118
  /**
86
119
  * A declaration error names the declaration, because the author reads the declaration to fix it.
@@ -106,6 +139,15 @@ export function inputPlace(input, scope) {
106
139
  export function declaringSite(input, place, subject = declarationSubject(input)) {
107
140
  return callSite(subject, { arguments: [input.name, input.config], ...place });
108
141
  }
142
+ /**
143
+ * One input's site with its config printed elided, for a fault judged before the config is read,
144
+ * such as an invalid name, so the fault reads none of the config.
145
+ */
146
+ export function configUnread(site) {
147
+ const [name, config] = site.declaration.arguments;
148
+ const shown = config === undefined ? [name] : [name, spelled(elided)];
149
+ return { ...site, declaration: { ...site.declaration, arguments: shown } };
150
+ }
109
151
  /**
110
152
  * The token an operator would type for one declaration: `--file` for an option, `-F` when the
111
153
  * option declares `shortOnly`, and the declared name for an argument. Every input diagnostic and
@@ -463,13 +505,16 @@ export async function prepareInputs(inputs, host, places) {
463
505
  return defaults;
464
506
  }
465
507
  /**
466
- * An array default reaches the action as its own copy, so an action that mutates its array
508
+ * An array default reaches the action as its own mutable copy, so an action that mutates its array
467
509
  * rewrites neither the declaration nor the next invocation. One prepared default serves every
468
- * invocation of a run, and an unvalidated default is the declared array itself, so each read
469
- * copies it. Every other output passes through unchanged.
510
+ * invocation of a run, and an unvalidated default is the frozen snapshot the declaring call took,
511
+ * so each read copies it. The copy keeps a hole where the default has one, as the graph's does.
512
+ * Every other output passes through unchanged.
470
513
  */
471
514
  function freshDefault(value) {
472
- return Array.isArray(value) ? [...value] : value;
515
+ // A spread reads a hole as `undefined`, and `slice` keeps it.
516
+ // oxlint-disable-next-line unicorn/prefer-spread
517
+ return Array.isArray(value) ? value.slice() : value;
473
518
  }
474
519
  /**
475
520
  * The raw tokens of one invocation, keyed by declared name. Every declared input of the routed
package/dist/view.d.ts CHANGED
@@ -56,7 +56,7 @@ interface FailureViewContext extends ViewContext {
56
56
  interface FailureView<Failure extends LoomError> {
57
57
  render: (failure: Readonly<Failure>, context: FailureViewContext) => string;
58
58
  /** A failure view has one shape, as a view does. */
59
- row?: never;
59
+ row?: undefined;
60
60
  }
61
61
  /**
62
62
  * One stored view function, with the data type and the context erased. A registry holds one entry
@@ -125,17 +125,22 @@ declare class OverrideDeclaration {
125
125
  type ViewOverride = Pick<OverrideDeclaration, typeof viewOverride>;
126
126
  /** What a plugin's own list holds: the views it declares and the overrides it makes. */
127
127
  type ViewContribution = AnyDeclaredView | ViewOverride;
128
+ /** Every value an override can key on: a declared view of either shape or a failure class. */
129
+ type AnyOverrideKey = AnyDeclaredView | FailureClass<LoomError>;
128
130
  /**
129
- * One override pairing a key with a replacement view. Under a declared view the replacement is
130
- * typed from the view's data; under a failure class it is typed from the class's instances, which
131
- * is the typed path for a class-keyed list, because an array literal cannot carry a different type
132
- * parameter per element.
131
+ * The replacement view one key takes, derived from the key alone. The declared-view branches come
132
+ * first, so a declared view is never read as a failure class. Each check is wrapped in a tuple so a
133
+ * union key is not split into a union of replacements that each answer only one of its members.
133
134
  */
134
- declare function override<Data>(key: DeclaredView<Data>, replacement: NoInfer<View<Data>>): ViewOverride;
135
- declare function override<Row>(key: DeclaredRowView<Row>, replacement: NoInfer<RowView<Row>>): ViewOverride;
136
- declare function override<Failure extends LoomError>(key: FailureClass<Failure> & {
137
- readonly [declaredView]?: never;
138
- }, replacement: NoInfer<FailureView<Failure>>): ViewOverride;
135
+ type ReplacementView<Key> = [Key] extends [DeclaredView<infer Data>] ? View<Data> : [Key] extends [DeclaredRowView<infer Row>] ? RowView<Row> : [Key] extends [FailureClass<infer Failure>] ? FailureView<Failure> : never;
136
+ /**
137
+ * One override pairing a key with a replacement view. The replacement's type is derived from the
138
+ * key: under a declared view it is typed from the view's data, and under a failure class from the
139
+ * class's instances, which is the typed path for a class-keyed list, because an array literal cannot
140
+ * carry a different type parameter per element. One signature serves every key, so a mismatch
141
+ * names the replacement's type against the one the key expects.
142
+ */
143
+ declare function override<Key extends AnyOverrideKey>(key: Key, replacement: NoInfer<ReplacementView<Key>>): ViewOverride;
139
144
  /** One contributor's overrides, read once per build and consulted in contributor order. */
140
145
  interface ViewContributions {
141
146
  failures: Map<unknown, StoredView>;
@@ -210,5 +215,5 @@ type FailureReport = {
210
215
  * carries the same default text without hints, which the plain fallback path writes.
211
216
  */
212
217
  declare function describeFailure(registry: ViewRegistry, failure: LoomError, context: FailureViewContext): FailureReport;
213
- export type { AnyDeclaredView, DeclaredRowView, DeclaredView, DeclaredViewBrand, FailureClass, FailureReport, FailureView, FailureViewContext, ResolvedRowView, ViewContribution, ViewContributions, ViewSubject, ViewIdentities, ViewOverride, ViewRegistry, ViewShape, };
218
+ export type { AnyDeclaredView, AnyOverrideKey, DeclaredRowView, DeclaredView, DeclaredViewBrand, FailureClass, FailureReport, FailureView, FailureViewContext, ReplacementView, ResolvedRowView, ViewContribution, ViewContributions, ViewSubject, ViewIdentities, ViewOverride, ViewRegistry, ViewShape, };
214
219
  export { buildViews, describeFailure, override, resolveRowView, resolveView, shapeOf, view, viewIdentities, };
package/dist/view.js CHANGED
@@ -84,12 +84,21 @@ class OverrideDeclaration {
84
84
  Object.freeze(this);
85
85
  }
86
86
  }
87
+ /**
88
+ * One override pairing a key with a replacement view. The replacement's type is derived from the
89
+ * key: under a declared view it is typed from the view's data, and under a failure class from the
90
+ * class's instances, which is the typed path for a class-keyed list, because an array literal cannot
91
+ * carry a different type parameter per element. One signature serves every key, so a mismatch
92
+ * names the replacement's type against the one the key expects.
93
+ */
87
94
  function override(key, replacement) {
95
+ // Every replacement a key derives is a stored view, so it widens here with no check.
96
+ const supplied = replacement;
88
97
  const stored = {
89
- head: replacement.head,
90
- render: replacement.render,
91
- row: replacement.row,
92
- tail: replacement.tail,
98
+ head: supplied.head,
99
+ render: supplied.render,
100
+ row: supplied.row,
101
+ tail: supplied.tail,
93
102
  };
94
103
  const declared = declarations.get(key);
95
104
  if (declared) {
@@ -0,0 +1,41 @@
1
+ https://www.unicode.org/license.txt
2
+
3
+ UNICODE LICENSE V3
4
+
5
+ COPYRIGHT AND PERMISSION NOTICE
6
+
7
+ Copyright © 1991-2025 Unicode, Inc.
8
+
9
+ NOTICE TO USER: Carefully read the following legal agreement. BY
10
+ DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
11
+ SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
12
+ TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
13
+ DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a
16
+ copy of data files and any associated documentation (the "Data Files") or
17
+ software and any associated documentation (the "Software") to deal in the
18
+ Data Files or Software without restriction, including without limitation
19
+ the rights to use, copy, modify, merge, publish, distribute, and/or sell
20
+ copies of the Data Files or Software, and to permit persons to whom the
21
+ Data Files or Software are furnished to do so, provided that either (a)
22
+ this copyright and permission notice appear with all copies of the Data
23
+ Files or Software, or (b) this copyright and permission notice appear in
24
+ associated Documentation.
25
+
26
+ THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
29
+ THIRD PARTY RIGHTS.
30
+
31
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
32
+ BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
33
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
34
+ WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
35
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
36
+ FILES OR SOFTWARE.
37
+
38
+ Except as contained in this notice, the name of a copyright holder shall
39
+ not be used in advertising or otherwise to promote the sale, use or other
40
+ dealings in these Data Files or Software without prior written
41
+ authorization of the copyright holder.
@@ -0,0 +1,35 @@
1
+ # uucode license
2
+
3
+ MIT License
4
+
5
+ Copyright (c) 2026 Tim Culverhouse
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+ this software and associated documentation files (the "Software"), to deal in
9
+ the Software without restriction, including without limitation the rights to
10
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11
+ of the Software, and to permit persons to whom the Software is furnished to do
12
+ so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
24
+
25
+ ## Attribution
26
+
27
+ This TypeScript implementation is based on the design and table strategy of
28
+ Jacob Sandlund's `uucode`:
29
+
30
+ - https://github.com/jacobsandlund/uucode
31
+
32
+ The bundled Unicode Character Database files are provided by Unicode, Inc. and
33
+ are subject to the Unicode terms of use:
34
+
35
+ - https://www.unicode.org/terms_of_use.html
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@loomcli/core",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "The Loom CLI core package. Provides the scaffolding for creating new Loom CLI applications.",
5
- "license": "MIT",
5
+ "license": "MIT AND Unicode-3.0",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/dbtlr/loomcli.git"
9
9
  },
10
10
  "files": [
11
11
  "dist",
12
- "LICENSE"
12
+ "LICENSE",
13
+ "licenses",
14
+ "NOTICE"
13
15
  ],
14
16
  "type": "module",
15
17
  "exports": {
@@ -19,9 +21,11 @@
19
21
  }
20
22
  },
21
23
  "dependencies": {
22
- "@rockorager/uucode": "2.2.1",
23
24
  "@standard-schema/spec": "^1.1.0",
24
- "@types/node": "22.20.1"
25
+ "@types/node": "22.20.5"
26
+ },
27
+ "devDependencies": {
28
+ "@rockorager/uucode": "2.2.1"
25
29
  },
26
30
  "engines": {
27
31
  "bun": ">=1.4.0",