@rohal12/spindle 0.59.25 → 0.59.27

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/src/tooling.ts CHANGED
@@ -34,6 +34,8 @@ import {
34
34
  import type { ParameterDef } from './registry';
35
35
 
36
36
  export { parseDeclarations, parseStoryVariables } from './story-variables';
37
+ export { discoverMacros } from './macro-discovery';
38
+ export type { DiscoveredMacro } from './macro-discovery';
37
39
  export type {
38
40
  VariableDeclarations,
39
41
  VariableDiagnostic,
@@ -136,6 +138,14 @@ export interface ValidateMarkupOptions {
136
138
  * validate only part of a story.
137
139
  */
138
140
  checkPassageNames?: boolean;
141
+ /**
142
+ * Report every problem of a passage, not only the first malformed or
143
+ * unpaired tag (default: false, which is the story-start check). The
144
+ * macros, code, arguments and passage names are checked in the markup
145
+ * that could be read around the malformed tags, so an unclosed `{if}` does
146
+ * not hide the unknown macro, broken link and bad expression after it.
147
+ */
148
+ tolerant?: boolean;
139
149
  }
140
150
 
141
151
  export interface ToolingMacro {
@@ -184,6 +194,7 @@ export function validateStoryMarkup(
184
194
  isBlockMacro: (name) =>
185
195
  blocks.has(name.toLowerCase()) || isBlockMacro(name),
186
196
  checkPassageNames: options.checkPassageNames,
197
+ tolerant: options.tolerant,
187
198
  });
188
199
  }
189
200
 
@@ -251,13 +262,14 @@ export function widgetDefinitions(
251
262
  const NOT_MARKUP_TAGS = ['script', 'stylesheet'];
252
263
 
253
264
  /** What reading the variable references of markup depends on. */
254
- function referenceOptions(macros: Iterable<ToolingMacro>) {
255
- const all = [...macros];
265
+ function referenceOptions(macros: Iterable<ToolingMacro>, all = false) {
266
+ const list = [...macros];
256
267
  return {
268
+ all,
257
269
  storeVarMacros: new Set(
258
- all.filter((m) => m.storeVar).map((m) => m.name.toLowerCase()),
270
+ list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase()),
259
271
  ),
260
- parametersOf: parameterLookup(all),
272
+ parametersOf: parameterLookup(list),
261
273
  tolerant: true,
262
274
  };
263
275
  }
@@ -270,12 +282,23 @@ function referenceOptions(macros: Iterable<ToolingMacro>) {
270
282
  * bound by input macros, and the markup in labels and HTML attributes. Prose
271
283
  * is literal text. Malformed tags are skipped, so it reads half-typed markup.
272
284
  * The macros are those the markup may use, built-in and user-defined.
285
+ *
286
+ * These are the references the story start checks. With `all: true`, the
287
+ * list is complete (the checked ones are among it, in source order): it also
288
+ * has the variable a macro names in a `variable` parameter, such as the
289
+ * target of `{unset}` and `{computed}`, and the `{$name}` in the selectors of
290
+ * a link, display or expression (`[[.c{$name} Go->T]]`). A rename or a find
291
+ * of references needs these too.
273
292
  */
274
293
  export function variableReferences(
275
294
  source: string,
276
295
  macros: Iterable<ToolingMacro> = [],
296
+ options: { all?: boolean } = {},
277
297
  ): VariableReference[] {
278
- return collectVariableReferences(source, referenceOptions(macros));
298
+ return collectVariableReferences(
299
+ source,
300
+ referenceOptions(macros, options.all),
301
+ );
279
302
  }
280
303
 
281
304
  /**
@@ -113,6 +113,19 @@ const _toolingTypes: [
113
113
  typeof SourceTooling.collectStoryPassageReferences,
114
114
  typeof PublishedTooling.collectStoryPassageReferences
115
115
  >,
116
+ Same<
117
+ typeof SourceTooling.variableReferences,
118
+ typeof PublishedTooling.variableReferences
119
+ >,
120
+ Same<SourceTooling.DiscoveredMacro, PublishedTooling.DiscoveredMacro>,
121
+ Same<
122
+ typeof SourceTooling.discoverMacros,
123
+ typeof PublishedTooling.discoverMacros
124
+ >,
125
+ Same<
126
+ SourceTooling.ValidateMarkupOptions,
127
+ PublishedTooling.ValidateMarkupOptions
128
+ >,
116
129
  ] = [
117
130
  true,
118
131
  true,
@@ -127,6 +140,10 @@ const _toolingTypes: [
127
140
  true,
128
141
  true,
129
142
  true,
143
+ true,
144
+ true,
145
+ true,
146
+ true,
130
147
  ];
131
148
 
132
149
  // A typical custom macro written against the published types must type-check,
@@ -31,12 +31,15 @@ function textContainsChildren(
31
31
  * Whether a tag renders a `{@children}` in an attribute value or a macro
32
32
  * argument that holds markup (`<span title="{@children}">`,
33
33
  * `{button "{@children}"}`). Literal arguments and code are not markup.
34
+ * `src` is the text a flat token's offsets index into (its attribute values
35
+ * are read from it); a token made from an AST has none.
34
36
  */
35
37
  export function tokenTextContainsChildren(
36
38
  token: Token,
37
39
  parametersOf: ParametersOf = registeredParameters,
40
+ src = '',
38
41
  ): boolean {
39
- for (const piece of codeAndText('', [token], parametersOf)) {
42
+ for (const piece of codeAndText(src, [token], parametersOf)) {
40
43
  if (
41
44
  piece.kind === 'text' &&
42
45
  textContainsChildren(piece.text, parametersOf)
@@ -22,8 +22,9 @@ export interface WidgetDef {
22
22
 
23
23
  /**
24
24
  * The widget a {widget} definition's arguments declare: its name and its
25
- * parameters, the words after it that start with `@` (docs/widgets.md).
26
- * Other words, such as `$name`, are not parameters.
25
+ * 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.
27
28
  */
28
29
  export function widgetDef({
29
30
  name = '',
@@ -31,7 +32,7 @@ export function widgetDef({
31
32
  }: MacroArgs<typeof WIDGET_PARAMETERS>): WidgetDef {
32
33
  return {
33
34
  name,
34
- params: parameters.split(/\s+/).filter((word) => word.startsWith('@')),
35
+ params: parameters.split(/[\s,]+/).filter((word) => word.startsWith('@')),
35
36
  };
36
37
  }
37
38
 
@@ -114,7 +115,7 @@ export function widgetDefinitions(
114
115
  ((token.type === 'variable' &&
115
116
  token.scope === 'local' &&
116
117
  token.name === 'children') ||
117
- tokenTextContainsChildren(token, parametersOf))
118
+ tokenTextContainsChildren(token, parametersOf, passage.content))
118
119
  ) {
119
120
  open[open.length - 1]!.isBlock = true;
120
121
  }
package/types/index.d.ts CHANGED
@@ -287,11 +287,7 @@ export type ParameterType =
287
287
  * @see {@link ../../src/registry.ts} for the implementation.
288
288
  */
289
289
  export type StringHolds =
290
- | 'markup'
291
- | 'text'
292
- | 'passage'
293
- | 'expression'
294
- | 'statements';
290
+ 'markup' | 'text' | 'passage' | 'expression' | 'statements';
295
291
 
296
292
  /**
297
293
  * Parameter metadata for a macro definition.
@@ -401,11 +397,7 @@ export interface HtmlNode {
401
397
  * @see {@link ../../src/markup/ast.ts} for the implementation.
402
398
  */
403
399
  export type ASTNode =
404
- | TextNode
405
- | VariableNode
406
- | ExpressionNode
407
- | MacroNode
408
- | HtmlNode;
400
+ TextNode | VariableNode | ExpressionNode | MacroNode | HtmlNode;
409
401
 
410
402
  /**
411
403
  * Props passed to a macro's render function.
@@ -44,11 +44,7 @@ export type ParameterType =
44
44
  * any other `text`.
45
45
  */
46
46
  export type StringHolds =
47
- | 'markup'
48
- | 'text'
49
- | 'passage'
50
- | 'expression'
51
- | 'statements';
47
+ 'markup' | 'text' | 'passage' | 'expression' | 'statements';
52
48
 
53
49
  export interface ParameterDef {
54
50
  name: string;
@@ -101,12 +97,7 @@ export declare function getMacroRegistry(): MacroMetadata[];
101
97
 
102
98
  /** Variable type inferred from a StoryVariables/StoryTransients default value. */
103
99
  export type VarType =
104
- | 'number'
105
- | 'string'
106
- | 'boolean'
107
- | 'array'
108
- | 'object'
109
- | 'null';
100
+ 'number' | 'string' | 'boolean' | 'array' | 'object' | 'null';
110
101
 
111
102
  /** Inferred shape of a declared variable (or one of its object fields). */
112
103
  export interface FieldSchema {
@@ -221,6 +212,14 @@ export interface ValidateMarkupOptions {
221
212
  * only part of a story.
222
213
  */
223
214
  checkPassageNames?: boolean;
215
+ /**
216
+ * Report every problem of a passage, not only the first malformed or
217
+ * unpaired tag (default: `false`, which is the story-start check). The
218
+ * macros, code, arguments and passage names are checked in the markup that
219
+ * could be read around the malformed tags, so an unclosed `{if}` does not
220
+ * hide the unknown macro, broken link and bad expression after it.
221
+ */
222
+ tolerant?: boolean;
224
223
  }
225
224
 
226
225
  /**
@@ -580,8 +579,7 @@ export declare function transform(expr: string, goal?: JsGoal): string;
580
579
 
581
580
  /** What a `passage` argument is: a quoted name, or an expression. */
582
581
  export type PassageTarget =
583
- | { kind: 'name'; name: string }
584
- | { kind: 'expression'; expression: string };
582
+ { kind: 'name'; name: string } | { kind: 'expression'; expression: string };
585
583
 
586
584
  /**
587
585
  * Read a `passage` argument (`{goto "Hall"}`, `{include $room}`) as written:
@@ -733,10 +731,17 @@ export interface VariableDiagnostic {
733
731
  * The variable references the markup of a passage evaluates, in source order,
734
732
  * with their positions. Malformed tags are skipped. The macros are those the
735
733
  * markup may use (`builtinMacros` and the user-defined ones).
734
+ *
735
+ * These are the references the story start checks. With `all: true` the list
736
+ * is complete (the checked ones are among it, in source order): it also has
737
+ * the variable a macro names in a `variable` parameter, such as the target of
738
+ * `{unset}` and `{computed}`, and the `{$name}` in the selectors of a link,
739
+ * display or expression (`[[.c{$name} Go->T]]`).
736
740
  */
737
741
  export declare function variableReferences(
738
742
  source: string,
739
743
  macros?: Iterable<ToolingMacro>,
744
+ options?: { all?: boolean },
740
745
  ): VariableReference[];
741
746
 
742
747
  /**
@@ -922,10 +927,7 @@ export declare function passagePieces(
922
927
 
923
928
  /** What is wrong with the pairing of tags (a subset of {@link MarkupErrorCode}). */
924
929
  export type PairingErrorCode =
925
- | 'unclosed-block'
926
- | 'mismatched-closer'
927
- | 'stray-closer'
928
- | 'misplaced-branch';
930
+ 'unclosed-block' | 'mismatched-closer' | 'stray-closer' | 'misplaced-branch';
929
931
 
930
932
  /** A problem in the pairing of the tokens. */
931
933
  export interface PairingError {
@@ -1023,7 +1025,8 @@ export type DeclarationErrorCode =
1023
1025
  | 'invalid-declaration'
1024
1026
  | 'invalid-name'
1025
1027
  | 'duplicate-declaration'
1026
- | 'unsupported-value';
1028
+ | 'unsupported-value'
1029
+ | 'syntax';
1027
1030
 
1028
1031
  /** A declaration of a `StoryVariables` or `StoryTransients` passage. */
1029
1032
  export interface Declaration {
@@ -1063,11 +1066,43 @@ export interface DeclarationError {
1063
1066
  * `null` literals (also signed numbers), arrays of them, and objects with
1064
1067
  * identifier, string or number keys and static values. The `schema` of an
1065
1068
  * array is `{ type: 'array' }`, as `parseStoryVariables` makes it. Anything
1066
- * else (`Math.PI`, `new Date()`, `1 + 2`, a reference, a spread, a computed
1067
- * key) has no `schema`, and is no error: only a value that no variable can
1068
- * hold is, a function (`function`, `=>`, `class`) or `undefined`.
1069
+ * else (`Math.PI`, `new Date()`, `1 + 2`, a reference) has no `schema`, and
1070
+ * is no error. An object with a spread, a computed key, a shorthand member or
1071
+ * an accessor has the fields written after it (an earlier one can be replaced).
1072
+ *
1073
+ * The errors are those of the runtime's evaluation: a syntax error, and a
1074
+ * literal that no variable can hold (a function, `undefined` or `void 0`, a
1075
+ * BigInt) or whose evaluation throws (`+1n`). A message names the value as
1076
+ * the runtime does.
1069
1077
  */
1070
1078
  export declare function parseDeclarations(
1071
1079
  content: string,
1072
1080
  sigil?: '$' | '%',
1073
1081
  ): { declarations: Declaration[]; errors: DeclarationError[] };
1082
+
1083
+ // ---------------------------------------------------------------------------
1084
+ // Macro definitions in JavaScript
1085
+ // ---------------------------------------------------------------------------
1086
+
1087
+ /** A macro that a `defineMacro` call declares, and where its name is written. */
1088
+ export interface DiscoveredMacro extends ToolingMacro {
1089
+ merged?: boolean;
1090
+ description?: string;
1091
+ /** The name as written, without quotes: `[nameStart, nameEnd)` UTF-16 offsets. */
1092
+ nameStart: number;
1093
+ nameEnd: number;
1094
+ }
1095
+
1096
+ /**
1097
+ * The macros the `defineMacro({ … })` and `Story.defineMacro({ … })` calls in
1098
+ * `source` (JavaScript: a script passage, the body of a `{do}`, a project
1099
+ * file) declare, in source order, read from the text without running it. A
1100
+ * call whose config is an object literal (or a variable declared as one) and
1101
+ * whose `name` is a string is found, wherever it is. Only what is written
1102
+ * out is read: `block`, `interpolate`, `storeVar` and `merged` as boolean
1103
+ * literals, the strings of `subMacros`, and `parameters` when every one is
1104
+ * an object literal with a string `name` and `type`; a list with a parameter
1105
+ * that is not, or that `defineMacro` would refuse, declares none. It never
1106
+ * throws.
1107
+ */
1108
+ export declare function discoverMacros(source: string): DiscoveredMacro[];