@rohal12/spindle 0.59.27 → 0.59.29

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.
@@ -2,21 +2,20 @@ import { registerWidget } from './widget-registry';
2
2
  import { astContainsChildren } from './ast-scanner';
3
3
  import { registerBlockMacro, type ASTNode } from '../markup/ast';
4
4
  import { errorMessage } from '../utils/error-message';
5
- import { parseWidgetDef, type WidgetDef } from './widget-def';
5
+ import { checkWidgetHolds, parseWidgetDef, type WidgetDef } from './widget-def';
6
6
 
7
7
  /**
8
8
  * Register the widget a definition declares, with its body. Widgets whose
9
9
  * body renders {@children} take a closing tag, so they are registered as
10
10
  * block macros too: passages parsed later nest their content. A parameter
11
- * that no namespace can hold throws (see registerWidget), registering
12
- * nothing.
11
+ * that no namespace can hold or declares what it holds wrongly throws (see
12
+ * registerWidget and checkWidgetHolds), registering nothing.
13
13
  */
14
- export function registerWidgetDef(
15
- { name, params }: WidgetDef,
16
- body: ASTNode[],
17
- ): void {
14
+ export function registerWidgetDef(def: WidgetDef, body: ASTNode[]): void {
15
+ checkWidgetHolds(def);
16
+ const { name, params, holds } = def;
18
17
  const isBlock = astContainsChildren(body);
19
- registerWidget(name, body, params, isBlock);
18
+ registerWidget(name, body, params, isBlock, holds);
20
19
  if (isBlock) registerBlockMacro(name);
21
20
  }
22
21
 
@@ -3,7 +3,7 @@
3
3
  * macro, startup and markup validation (also in tooling) share these.
4
4
  */
5
5
  import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
6
- import type { MacroArgs } from '../registry';
6
+ import { STRING_HOLDS, type MacroArgs, type StringHolds } from '../registry';
7
7
  import { tokenizeMarkupTolerant } from '../markup/parse';
8
8
  import type { MacroToken } from '../markup/tokens';
9
9
  import type { ParametersOf } from '../code-check';
@@ -17,25 +17,62 @@ export const WIDGET_PARAMETERS = [
17
17
 
18
18
  export interface WidgetDef {
19
19
  name: string;
20
+ /** The `@` parameters, without what they declare to hold. */
20
21
  params: string[];
22
+ /**
23
+ * What the parameters that declare it hold (`@target:passage`), by name
24
+ * with the `@`: the check of a call reads such an argument as it reads
25
+ * the same `holds` of a macro's `string` parameter. Absent when none does.
26
+ */
27
+ holds?: Record<string, StringHolds>;
28
+ /** The parameters that declare something no `holds` is (see checkWidgetHolds). */
29
+ badHolds?: { param: string; written: string }[];
21
30
  }
22
31
 
23
32
  /**
24
33
  * The widget a {widget} definition's arguments declare: its name and its
25
34
  * parameters, the words after it that start with `@` (docs/widgets.md),
26
- * separated by spaces or commas (`@a, @b`). Other words, such as `$name`, are
27
- * not parameters.
35
+ * separated by spaces or commas (`@a, @b`). A parameter may say what its
36
+ * argument holds after a colon (`@target:passage`, one of the `holds` of a
37
+ * macro parameter; another word is a `badHolds`). Other words, such as
38
+ * `$name`, are not parameters.
28
39
  */
29
40
  export function widgetDef({
30
41
  name = '',
31
42
  parameters = '',
32
43
  }: MacroArgs<typeof WIDGET_PARAMETERS>): WidgetDef {
44
+ const params: string[] = [];
45
+ const holds: Record<string, StringHolds> = {};
46
+ const badHolds: WidgetDef['badHolds'] = [];
47
+ for (const word of parameters.split(/[\s,]+/)) {
48
+ if (!word.startsWith('@')) continue;
49
+ const [param = word, written] = word.split(/:(.*)/s);
50
+ params.push(param);
51
+ if (written === undefined) continue;
52
+ if (STRING_HOLDS.includes(written as StringHolds)) {
53
+ holds[param] = written as StringHolds;
54
+ } else {
55
+ badHolds.push({ param, written });
56
+ }
57
+ }
33
58
  return {
34
59
  name,
35
- params: parameters.split(/[\s,]+/).filter((word) => word.startsWith('@')),
60
+ params,
61
+ ...(Object.keys(holds).length > 0 ? { holds } : {}),
62
+ ...(badHolds.length > 0 ? { badHolds } : {}),
36
63
  };
37
64
  }
38
65
 
66
+ /** Throw if a parameter of `def` declares what it holds wrongly. */
67
+ export function checkWidgetHolds({ name, badHolds = [] }: WidgetDef): void {
68
+ const [bad] = badHolds;
69
+ if (!bad) return;
70
+ throw new Error(
71
+ `spindle: The widget parameter ${bad.param} of {widget "${name}"} holds the unknown ${JSON.stringify(bad.written)}. ` +
72
+ `Write one of ${STRING_HOLDS.join(', ')} after the colon (see docs/widgets.md#what-an-argument-holds).`,
73
+ );
74
+ }
75
+
39
76
  /** Read the arguments of a {widget} definition (see widgetDef). */
40
77
  export function parseWidgetDef(rawArgs: string): WidgetDef {
41
78
  return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
@@ -1,10 +1,14 @@
1
1
  import type { ASTNode } from '../markup/ast';
2
2
  import { checkVariableName } from '../utils/namespace';
3
3
  import { NameMap } from '../utils/macro-names';
4
+ import type { StringHolds } from '../registry';
5
+ import type { MacroParameters } from '../code-check';
4
6
 
5
7
  interface WidgetEntry {
6
8
  body: ASTNode[];
7
9
  params: string[];
10
+ /** What the parameters declare to hold, by name with the `@`. */
11
+ holds: Record<string, StringHolds>;
8
12
  isBlock: boolean;
9
13
  }
10
14
 
@@ -20,6 +24,7 @@ export function registerWidget(
20
24
  bodyAST: ASTNode[],
21
25
  params: string[],
22
26
  isBlock = false,
27
+ holds: Record<string, StringHolds> = {},
23
28
  ): void {
24
29
  const filteredParams = params.filter((p) => p !== '@children');
25
30
  for (const param of filteredParams) {
@@ -28,6 +33,7 @@ export function registerWidget(
28
33
  widgets.set(name, {
29
34
  body: bodyAST,
30
35
  params: filteredParams,
36
+ holds,
31
37
  isBlock,
32
38
  });
33
39
  }
@@ -36,6 +42,14 @@ export function getWidget(name: string): WidgetEntry | undefined {
36
42
  return widgets.get(name);
37
43
  }
38
44
 
45
+ /** The widgets as `parameterLookup` reads them (see MacroParameters). */
46
+ export function widgetMacros(): MacroParameters[] {
47
+ return [...widgets].map(([name, { params, holds }]) => ({
48
+ name,
49
+ widget: { params, holds },
50
+ }));
51
+ }
52
+
39
53
  export function isBlockWidget(name: string): boolean {
40
54
  return widgets.get(name)?.isBlock ?? false;
41
55
  }