@rohal12/spindle 0.59.22 → 0.59.24
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/README.md +3 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +1707 -1273
- package/dist/pkg/macro-registry.js +575 -0
- package/dist/pkg/story-variables.js +1596 -1239
- package/dist/pkg/tooling.js +24 -16
- package/package.json +2 -1
- package/src/code-check.ts +293 -56
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- package/src/components/macros/SettingsControls.tsx +10 -6
- package/src/components/macros/Type.tsx +1 -1
- package/src/components/macros/detached-body.tsx +62 -3
- package/src/hooks/use-modal-focus.ts +62 -19
- package/src/markup/ast.ts +88 -1
- package/src/markup/line-column.ts +14 -0
- package/src/markup/pair.ts +331 -0
- package/src/markup/parse.ts +166 -36
- package/src/markup/render.tsx +15 -4
- package/src/markup/spindle.d.peggy.ts +3 -3
- package/src/markup/spindle.peggy +81 -180
- package/src/markup/tokens.ts +49 -9
- package/src/markup/validate.ts +167 -79
- package/src/settings.ts +9 -0
- package/src/static-literal.ts +292 -0
- package/src/story-variables.ts +148 -10
- package/src/tooling.ts +111 -9
- package/src/types-drift-check.ts +52 -0
- package/src/widgets/ast-scanner.ts +16 -4
- package/src/widgets/widget-def.ts +93 -22
- package/types/tooling.d.ts +498 -7
package/src/story-variables.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Passage } from './parser';
|
|
2
|
-
import type { Token } from './markup/tokens';
|
|
2
|
+
import type { Spans, Token } from './markup/tokens';
|
|
3
3
|
import { MarkupError, tokenizeMarkup } from './markup/parse';
|
|
4
4
|
import { isCodeAttribute, splitSigilTemplate } from './markup/code-attributes';
|
|
5
5
|
import { errorMessage } from './utils/error-message';
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
import { getMacroParameters, type ParameterDef } from './registry';
|
|
19
19
|
import { NOT_MARKUP } from './markup/validate';
|
|
20
20
|
import { createNamespace, variableNameError } from './utils/namespace';
|
|
21
|
+
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* The tokens of markup, or none if it is malformed: validateMarkup reports
|
|
@@ -102,32 +103,169 @@ function inferSchema(
|
|
|
102
103
|
return { type: jsType as VarType };
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
/** What is wrong with a declaration (see DeclarationError). */
|
|
107
|
+
export type DeclarationErrorCode =
|
|
108
|
+
| 'invalid-declaration'
|
|
109
|
+
| 'invalid-name'
|
|
110
|
+
| 'duplicate-declaration'
|
|
111
|
+
| 'unsupported-value';
|
|
112
|
+
|
|
105
113
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
114
|
+
* A declaration of a StoryVariables or StoryTransients passage: the name
|
|
115
|
+
* (without the sigil) and the initializer expression (without surrounding
|
|
116
|
+
* space) are written from `nameStart` to `nameEnd` and from `valueStart` to
|
|
117
|
+
* `valueEnd` (UTF-16 offsets).
|
|
108
118
|
*/
|
|
109
|
-
export
|
|
119
|
+
export interface Declaration extends Spans<'name'>, Spans<'value'> {
|
|
120
|
+
name: string;
|
|
121
|
+
/**
|
|
122
|
+
* The shape of the initializer when it is static: a literal, or an array or
|
|
123
|
+
* object literal of them (an object has the members that are static).
|
|
124
|
+
* Absent when it is not.
|
|
125
|
+
*/
|
|
126
|
+
schema?: FieldSchema;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* An invalid line of a StoryVariables or StoryTransients passage, from
|
|
131
|
+
* `offset` up to `end` (UTF-16 offsets into the content).
|
|
132
|
+
*/
|
|
133
|
+
export interface DeclarationError extends Record<'offset' | 'end', number> {
|
|
134
|
+
code: DeclarationErrorCode;
|
|
135
|
+
/** What is wrong, as parseStoryVariables throws it after the passage name. */
|
|
136
|
+
message: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const UNSUPPORTED_TYPE = 'Expected number, string, boolean, array, or object.';
|
|
140
|
+
|
|
141
|
+
/** The schema of a literal shape (see staticLiteral). */
|
|
142
|
+
function shapeSchema(shape: LiteralShape): FieldSchema {
|
|
143
|
+
if (!shape.fields) return { type: shape.type };
|
|
144
|
+
const fields = new Map<string, FieldSchema>();
|
|
145
|
+
for (const [key, member] of shape.fields)
|
|
146
|
+
fields.set(key, shapeSchema(member));
|
|
147
|
+
return { type: shape.type, fields };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Read the declarations of a StoryVariables (`$name = value`) or
|
|
152
|
+
* StoryTransients (`%name = value`) passage, without evaluating them and
|
|
153
|
+
* without throwing: every valid declaration, and every invalid line as an
|
|
154
|
+
* error. It is the grammar parseStoryVariables reads with.
|
|
155
|
+
*
|
|
156
|
+
* A declaration is a line of the form `$name = expression`; blank lines are
|
|
157
|
+
* skipped. A line that is not, and a name that cannot be a variable, are
|
|
158
|
+
* errors. So is an initializer that no variable can hold (a function,
|
|
159
|
+
* `undefined`), when it is a literal; whether any other initializer can only
|
|
160
|
+
* be known by running it. A name declared again is an error too, but both
|
|
161
|
+
* declarations are returned (the later wins when evaluated).
|
|
162
|
+
*/
|
|
163
|
+
export function parseDeclarations(
|
|
110
164
|
content: string,
|
|
111
165
|
sigil: '$' | '%' = '$',
|
|
112
|
-
):
|
|
113
|
-
const
|
|
166
|
+
): { declarations: Declaration[]; errors: DeclarationError[] } {
|
|
167
|
+
const declarations: Declaration[] = [];
|
|
168
|
+
const errors: DeclarationError[] = [];
|
|
114
169
|
const DECLARATION_RE = declarationRegex(sigil);
|
|
115
|
-
const
|
|
170
|
+
const seen = new Set<string>();
|
|
116
171
|
|
|
172
|
+
let lineStart = 0;
|
|
117
173
|
for (const rawLine of content.split('\n')) {
|
|
174
|
+
const at = lineStart + rawLine.length - rawLine.trimStart().length;
|
|
175
|
+
lineStart += rawLine.length + 1;
|
|
118
176
|
const line = rawLine.trim();
|
|
119
177
|
if (!line) continue;
|
|
120
178
|
|
|
121
179
|
const match = line.match(DECLARATION_RE);
|
|
180
|
+
const error = (
|
|
181
|
+
code: DeclarationErrorCode,
|
|
182
|
+
start: number,
|
|
183
|
+
end: number,
|
|
184
|
+
message: string,
|
|
185
|
+
) => errors.push({ code, offset: start, end, message });
|
|
122
186
|
if (!match) {
|
|
123
|
-
|
|
124
|
-
|
|
187
|
+
error(
|
|
188
|
+
'invalid-declaration',
|
|
189
|
+
at,
|
|
190
|
+
at + line.length,
|
|
191
|
+
`Invalid declaration: "${line}". Expected: ${sigil}name = value`,
|
|
125
192
|
);
|
|
193
|
+
continue;
|
|
126
194
|
}
|
|
127
195
|
|
|
128
196
|
const [, name, expr] = match as [string, string, string];
|
|
197
|
+
const nameStart = at + sigil.length;
|
|
198
|
+
const nameEnd = nameStart + name.length;
|
|
129
199
|
const nameError = variableNameError(name, sigil + name);
|
|
130
|
-
if (nameError)
|
|
200
|
+
if (nameError) {
|
|
201
|
+
error('invalid-name', nameStart, nameEnd, nameError);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const valueEnd = at + line.length;
|
|
205
|
+
const declaration: Declaration = {
|
|
206
|
+
name,
|
|
207
|
+
nameStart,
|
|
208
|
+
nameEnd,
|
|
209
|
+
valueStart: valueEnd - expr.length,
|
|
210
|
+
valueEnd,
|
|
211
|
+
};
|
|
212
|
+
if (seen.has(name)) {
|
|
213
|
+
error(
|
|
214
|
+
'duplicate-declaration',
|
|
215
|
+
nameStart,
|
|
216
|
+
nameEnd,
|
|
217
|
+
`Duplicate declaration of ${sigil}${name}`,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
seen.add(name);
|
|
221
|
+
const literal = staticLiteral(expr);
|
|
222
|
+
if (literal && 'shape' in literal) {
|
|
223
|
+
declaration.schema = shapeSchema(literal.shape);
|
|
224
|
+
} else if (literal) {
|
|
225
|
+
error(
|
|
226
|
+
'unsupported-value',
|
|
227
|
+
declaration.valueStart,
|
|
228
|
+
valueEnd,
|
|
229
|
+
`Unsupported type "${literal.unsupported}" for value ${expr}. ${UNSUPPORTED_TYPE}`,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
declarations.push(declaration);
|
|
233
|
+
}
|
|
234
|
+
return { declarations, errors };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Parse a StoryVariables or StoryTransients passage content into a schema map.
|
|
239
|
+
* Each line: `$varName = expression` (or `%varName = expression` for transients)
|
|
240
|
+
* The expressions are evaluated; parseDeclarations reads the same lines
|
|
241
|
+
* without.
|
|
242
|
+
*/
|
|
243
|
+
export function parseStoryVariables(
|
|
244
|
+
content: string,
|
|
245
|
+
sigil: '$' | '%' = '$',
|
|
246
|
+
): Map<string, VariableSchema> {
|
|
247
|
+
const schema = new Map<string, VariableSchema>();
|
|
248
|
+
const passageName = sigil === '%' ? 'StoryTransients' : 'StoryVariables';
|
|
249
|
+
const { declarations, errors } = parseDeclarations(content, sigil);
|
|
250
|
+
|
|
251
|
+
// Lines in order: the first that is invalid, or that cannot be evaluated,
|
|
252
|
+
// is the error. What parseDeclarations reports about a value that is
|
|
253
|
+
// evaluated here is left to the evaluation (its wording is the runtime's).
|
|
254
|
+
const lines = [
|
|
255
|
+
...errors
|
|
256
|
+
.filter(
|
|
257
|
+
(e) => e.code === 'invalid-declaration' || e.code === 'invalid-name',
|
|
258
|
+
)
|
|
259
|
+
.map((e) => ({ at: e.offset, error: e })),
|
|
260
|
+
...declarations.map((d) => ({ at: d.nameStart, declaration: d })),
|
|
261
|
+
].sort((a, b) => a.at - b.at);
|
|
262
|
+
|
|
263
|
+
for (const line of lines) {
|
|
264
|
+
if ('error' in line) {
|
|
265
|
+
throw new Error(`${passageName}: ${line.error.message}`);
|
|
266
|
+
}
|
|
267
|
+
const { name, valueStart, valueEnd } = line.declaration;
|
|
268
|
+
const expr = content.slice(valueStart, valueEnd);
|
|
131
269
|
let value: unknown;
|
|
132
270
|
try {
|
|
133
271
|
value = new Function('return (' + expr + ')')();
|
package/src/tooling.ts
CHANGED
|
@@ -12,16 +12,33 @@ import {
|
|
|
12
12
|
import { isBlockMacro } from './markup/ast';
|
|
13
13
|
import { tokenizeMarkupTolerant } from './markup/parse';
|
|
14
14
|
import {
|
|
15
|
-
collectPassageReferences,
|
|
16
15
|
parameterLookup,
|
|
16
|
+
passagePiecesOf,
|
|
17
|
+
pieceOffset,
|
|
18
|
+
referencesOf,
|
|
17
19
|
type PassageReference,
|
|
20
|
+
type Piece,
|
|
18
21
|
} from './code-check';
|
|
19
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
blockWidgetNames,
|
|
24
|
+
widgetDefinitions as readWidgetDefinitions,
|
|
25
|
+
type WidgetDefinition,
|
|
26
|
+
} from './widgets/widget-def';
|
|
20
27
|
import type { ParameterDef } from './registry';
|
|
21
28
|
|
|
22
|
-
export { parseStoryVariables } from './story-variables';
|
|
29
|
+
export { parseDeclarations, parseStoryVariables } from './story-variables';
|
|
30
|
+
export type {
|
|
31
|
+
Declaration,
|
|
32
|
+
DeclarationError,
|
|
33
|
+
DeclarationErrorCode,
|
|
34
|
+
FieldSchema,
|
|
35
|
+
VarType,
|
|
36
|
+
VariableSchema,
|
|
37
|
+
} from './story-variables';
|
|
23
38
|
export { checkParameterTypes } from './registry';
|
|
24
39
|
export { formatDiagnostic } from './markup/validate';
|
|
40
|
+
export { parseWidgetDef } from './widgets/widget-def';
|
|
41
|
+
export type { WidgetDef, WidgetDefinition } from './widgets/widget-def';
|
|
25
42
|
|
|
26
43
|
// The parsing rules (see "Tooling API" in docs/tooling.md): the leaf rules
|
|
27
44
|
// the runtime parses with, so editor tooling needn't mirror them.
|
|
@@ -40,7 +57,32 @@ export {
|
|
|
40
57
|
} from './markup/parse';
|
|
41
58
|
export type { ParseMarkupOptions, TolerantTokens } from './markup/parse';
|
|
42
59
|
export { SIGIL_SCOPES, isSigil } from './markup/tokens';
|
|
43
|
-
export type {
|
|
60
|
+
export type {
|
|
61
|
+
AttributeSpan,
|
|
62
|
+
ExpressionToken,
|
|
63
|
+
HtmlToken,
|
|
64
|
+
LinkToken,
|
|
65
|
+
MacroToken,
|
|
66
|
+
SelectorSpan,
|
|
67
|
+
Selectors,
|
|
68
|
+
TextToken,
|
|
69
|
+
Token,
|
|
70
|
+
VariableScope,
|
|
71
|
+
VariableToken,
|
|
72
|
+
} from './markup/tokens';
|
|
73
|
+
export { pairMarkup } from './markup/pair';
|
|
74
|
+
export { pieceOffset };
|
|
75
|
+
export type {
|
|
76
|
+
PairedBody,
|
|
77
|
+
PairedBranch,
|
|
78
|
+
PairedMarkup,
|
|
79
|
+
PairedNode,
|
|
80
|
+
PairingError,
|
|
81
|
+
PairingErrorCode,
|
|
82
|
+
PairMarkupOptions,
|
|
83
|
+
} from './markup/pair';
|
|
84
|
+
export { isBlockMacro } from './markup/ast';
|
|
85
|
+
export type { MarkupErrorCode } from './markup/parse';
|
|
44
86
|
export {
|
|
45
87
|
endsWithOperator,
|
|
46
88
|
readQuoted,
|
|
@@ -56,8 +98,21 @@ export {
|
|
|
56
98
|
} from './components/macros/macro-args';
|
|
57
99
|
export type { PassageTarget } from './components/macros/macro-args';
|
|
58
100
|
export { transform } from './transform';
|
|
59
|
-
export type {
|
|
60
|
-
|
|
101
|
+
export type {
|
|
102
|
+
ArgumentErrorPiece,
|
|
103
|
+
CodePiece,
|
|
104
|
+
PassagePiece,
|
|
105
|
+
PassageReference,
|
|
106
|
+
Piece,
|
|
107
|
+
PieceBase,
|
|
108
|
+
TextPiece,
|
|
109
|
+
} from './code-check';
|
|
110
|
+
export type {
|
|
111
|
+
MarkupDiagnostic,
|
|
112
|
+
MarkupDiagnosticCode,
|
|
113
|
+
MarkupDiagnosticData,
|
|
114
|
+
MarkupPassage,
|
|
115
|
+
} from './markup/validate';
|
|
61
116
|
|
|
62
117
|
/** What tooling knows about a macro (see MacroMetadata). */
|
|
63
118
|
/** Options for validating markup from tooling. */
|
|
@@ -117,15 +172,62 @@ export function validateStoryMarkup(
|
|
|
117
172
|
});
|
|
118
173
|
}
|
|
119
174
|
|
|
175
|
+
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
176
|
+
function blockLookup(macros: readonly ToolingMacro[]) {
|
|
177
|
+
const blocks = new Set(
|
|
178
|
+
macros.filter((m) => m.block).map((m) => m.name.toLowerCase()),
|
|
179
|
+
);
|
|
180
|
+
return (name: string) => blocks.has(name.toLowerCase()) || isBlockMacro(name);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* What a passage's markup runs and names, and where: the pieces of code
|
|
185
|
+
* (`{$expr}` displays, `{do}` bodies, conditions, macro arguments by their
|
|
186
|
+
* declared parameter types, strings that hold code), the passage names written
|
|
187
|
+
* out (links, `passage` arguments), the texts that hold markup of their own
|
|
188
|
+
* (labels, attribute values) and the arguments that don't have their
|
|
189
|
+
* parameters' forms, in source order with offsets (UTF-16) into `source`. The
|
|
190
|
+
* markup in a text follows it, as pieces marked `nested` with offsets into
|
|
191
|
+
* `source` too. Malformed tags are skipped, so it reads half-typed markup.
|
|
192
|
+
* The macros are those the markup may use, built-in and user-defined.
|
|
193
|
+
*/
|
|
194
|
+
export function passagePieces(
|
|
195
|
+
source: string,
|
|
196
|
+
macros: Iterable<ToolingMacro> = [],
|
|
197
|
+
): Piece[] {
|
|
198
|
+
const all = [...macros];
|
|
199
|
+
const { tokens } = tokenizeMarkupTolerant(source);
|
|
200
|
+
return [
|
|
201
|
+
...passagePiecesOf(source, tokens, parameterLookup(all), {
|
|
202
|
+
isBlock: blockLookup(all),
|
|
203
|
+
}),
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
|
|
120
207
|
/**
|
|
121
208
|
* The passages the markup of `source` names (see PassageReference), against
|
|
122
209
|
* the given macros: what the story-start check looks up. Malformed tags are
|
|
123
|
-
* skipped, so it reads half-typed markup.
|
|
210
|
+
* skipped, so it reads half-typed markup. It is `passagePieces`, less what
|
|
211
|
+
* names no passage.
|
|
124
212
|
*/
|
|
125
213
|
export function collectStoryPassageReferences(
|
|
126
214
|
source: string,
|
|
127
215
|
macros: Iterable<ToolingMacro>,
|
|
128
216
|
): PassageReference[] {
|
|
129
|
-
|
|
130
|
-
|
|
217
|
+
return referencesOf(passagePieces(source, macros));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The widgets the story defines, as the runtime reads them: those of
|
|
222
|
+
* StoryInit and of the passages tagged `widget`, with their parameters,
|
|
223
|
+
* whether their body renders `{@children}` (a block widget) and where they
|
|
224
|
+
* are written, in source order. Half-typed definitions are reported without a
|
|
225
|
+
* `closeStart`. The macros are those the markup may use, built-in and
|
|
226
|
+
* user-defined (a `{@children}` in an attribute or label counts).
|
|
227
|
+
*/
|
|
228
|
+
export function widgetDefinitions(
|
|
229
|
+
passages: Iterable<MarkupPassage>,
|
|
230
|
+
macros: Iterable<ToolingMacro> = [],
|
|
231
|
+
): WidgetDefinition[] {
|
|
232
|
+
return readWidgetDefinitions(passages, parameterLookup([...macros]));
|
|
131
233
|
}
|
package/src/types-drift-check.ts
CHANGED
|
@@ -35,6 +35,8 @@ import type {
|
|
|
35
35
|
validateStoryMarkup as SourceValidateStoryMarkup,
|
|
36
36
|
} from './tooling';
|
|
37
37
|
import type { bootStory as PublishedBootStory } from '../types/headless';
|
|
38
|
+
import type * as SourceTooling from './tooling';
|
|
39
|
+
import type * as PublishedTooling from '../types/tooling';
|
|
38
40
|
|
|
39
41
|
/** What the source declares, by published name. */
|
|
40
42
|
interface Source {
|
|
@@ -77,6 +79,56 @@ const _sourceToPublished: Published = {} as Source;
|
|
|
77
79
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
78
80
|
const _publishedToSource: Source = {} as Published;
|
|
79
81
|
|
|
82
|
+
/** `true` if `A` and `B` are assignable to each other, else `false`. */
|
|
83
|
+
type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
|
84
|
+
|
|
85
|
+
// The rest of the tooling entry point: what it parses and returns (tokens,
|
|
86
|
+
// pieces, pairing, declarations) and the stateless checks. Each must be the
|
|
87
|
+
// same type in the source and in types/tooling.d.ts, or `false` here is no
|
|
88
|
+
// `true`.
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
90
|
+
const _toolingTypes: [
|
|
91
|
+
Same<SourceTooling.MarkupError, PublishedTooling.MarkupError>,
|
|
92
|
+
Same<SourceTooling.Token, PublishedTooling.Token>,
|
|
93
|
+
Same<SourceTooling.Piece, PublishedTooling.Piece>,
|
|
94
|
+
Same<SourceTooling.PairedNode, PublishedTooling.PairedNode>,
|
|
95
|
+
Same<SourceTooling.PairingError, PublishedTooling.PairingError>,
|
|
96
|
+
Same<SourceTooling.Declaration, PublishedTooling.Declaration>,
|
|
97
|
+
Same<SourceTooling.DeclarationError, PublishedTooling.DeclarationError>,
|
|
98
|
+
Same<SourceTooling.ToolingMacro, PublishedTooling.ToolingMacro>,
|
|
99
|
+
Same<typeof SourceTooling.pairMarkup, typeof PublishedTooling.pairMarkup>,
|
|
100
|
+
Same<
|
|
101
|
+
typeof SourceTooling.passagePieces,
|
|
102
|
+
typeof PublishedTooling.passagePieces
|
|
103
|
+
>,
|
|
104
|
+
Same<
|
|
105
|
+
typeof SourceTooling.parseDeclarations,
|
|
106
|
+
typeof PublishedTooling.parseDeclarations
|
|
107
|
+
>,
|
|
108
|
+
Same<
|
|
109
|
+
typeof SourceTooling.validateStoryMarkup,
|
|
110
|
+
typeof PublishedTooling.validateStoryMarkup
|
|
111
|
+
>,
|
|
112
|
+
Same<
|
|
113
|
+
typeof SourceTooling.collectStoryPassageReferences,
|
|
114
|
+
typeof PublishedTooling.collectStoryPassageReferences
|
|
115
|
+
>,
|
|
116
|
+
] = [
|
|
117
|
+
true,
|
|
118
|
+
true,
|
|
119
|
+
true,
|
|
120
|
+
true,
|
|
121
|
+
true,
|
|
122
|
+
true,
|
|
123
|
+
true,
|
|
124
|
+
true,
|
|
125
|
+
true,
|
|
126
|
+
true,
|
|
127
|
+
true,
|
|
128
|
+
true,
|
|
129
|
+
true,
|
|
130
|
+
];
|
|
131
|
+
|
|
80
132
|
// A typical custom macro written against the published types must type-check,
|
|
81
133
|
// and misuse of the hooks must not (no `any` leaking through).
|
|
82
134
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -65,12 +65,16 @@ export function astContainsChildren(
|
|
|
65
65
|
return true;
|
|
66
66
|
}
|
|
67
67
|
if (node.type === 'html') {
|
|
68
|
-
|
|
68
|
+
// A token in name only: the scan reads its attribute values
|
|
69
69
|
const tag: Token = {
|
|
70
70
|
isClose: false,
|
|
71
71
|
isSelfClose: false,
|
|
72
|
+
tagNameStart: 0,
|
|
73
|
+
tagNameEnd: 0,
|
|
74
|
+
attributeSpans: [],
|
|
72
75
|
...node,
|
|
73
|
-
|
|
76
|
+
start: 0,
|
|
77
|
+
end: 0,
|
|
74
78
|
};
|
|
75
79
|
if (
|
|
76
80
|
tokenTextContainsChildren(tag, parametersOf) ||
|
|
@@ -80,8 +84,16 @@ export function astContainsChildren(
|
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
86
|
if (node.type === 'macro') {
|
|
83
|
-
const
|
|
84
|
-
|
|
87
|
+
const tag: Token = {
|
|
88
|
+
isClose: false,
|
|
89
|
+
nameStart: 0,
|
|
90
|
+
nameEnd: 0,
|
|
91
|
+
argsStart: 0,
|
|
92
|
+
argsEnd: 0,
|
|
93
|
+
...node,
|
|
94
|
+
start: 0,
|
|
95
|
+
end: 0,
|
|
96
|
+
};
|
|
85
97
|
if (
|
|
86
98
|
tokenTextContainsChildren(tag, parametersOf) ||
|
|
87
99
|
astContainsChildren(node.children, parametersOf)
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Reading widget definitions without rendering anything: the `{widget}`
|
|
3
3
|
* macro, startup and markup validation (also in tooling) share these.
|
|
4
4
|
*/
|
|
5
|
-
import { parseMacroArgs } from '../components/macros/macro-args';
|
|
5
|
+
import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
|
|
6
6
|
import type { MacroArgs } from '../registry';
|
|
7
7
|
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
8
|
+
import type { MacroToken } from '../markup/tokens';
|
|
8
9
|
import type { ParametersOf } from '../code-check';
|
|
9
10
|
import { registeredParameters, tokenTextContainsChildren } from './ast-scanner';
|
|
10
11
|
|
|
@@ -39,39 +40,74 @@ export function parseWidgetDef(rawArgs: string): WidgetDef {
|
|
|
39
40
|
return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/** A passage that may define widgets, as the tooling reads it. */
|
|
44
|
+
export interface WidgetPassage {
|
|
45
|
+
name: string;
|
|
46
|
+
tags?: string[];
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
* A widget defined by a `{widget}` macro, with where it is written (UTF-16
|
|
52
|
+
* offsets into the passage content).
|
|
53
|
+
*/
|
|
54
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
55
|
+
/** Whether its body renders `{@children}`. */
|
|
56
|
+
block: boolean;
|
|
57
|
+
/** The passage defining it. */
|
|
58
|
+
passage: string;
|
|
59
|
+
/** The opening `{widget …}` tag. */
|
|
60
|
+
start: number;
|
|
61
|
+
end: number;
|
|
62
|
+
/** The name as written, without quotes. */
|
|
63
|
+
nameStart: number;
|
|
64
|
+
nameEnd: number;
|
|
65
|
+
/** The `{/widget}` closer, absent while the definition is not closed. */
|
|
66
|
+
closeStart?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where the name is written in the arguments, without its quotes. */
|
|
70
|
+
function nameSpan(rawArgs: string): [number, number] {
|
|
71
|
+
const spans: ArgSpans = new Map();
|
|
72
|
+
parseMacroArgs(rawArgs, WIDGET_PARAMETERS, spans);
|
|
73
|
+
const [start, end] = spans.get(WIDGET_PARAMETERS[0]) ?? [0, 0];
|
|
74
|
+
const quote = rawArgs[start];
|
|
75
|
+
return (quote === '"' || quote === "'") && end - start >= 2
|
|
76
|
+
? [start + 1, rawArgs[end - 1] === quote ? end - 1 : end]
|
|
77
|
+
: [start, end];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The widgets that StoryInit and the passages tagged `widget` define, in
|
|
82
|
+
* source order: the ones the runtime registers, read tolerantly (a half-typed
|
|
83
|
+
* definition is still reported, without a `closeStart`). Definitions whose
|
|
84
|
+
* arguments cannot be read are left out.
|
|
47
85
|
*
|
|
48
86
|
* The passages are read as flat tokens, which need no block macros to be
|
|
49
87
|
* known, so a `{@children}` in an HTML comment or in a `{do}` body is text
|
|
50
|
-
* here as it is to the parser, and does not make a block widget (#387).
|
|
88
|
+
* here as it is to the parser, and does not make a block widget (#387). In
|
|
89
|
+
* nested definitions, it belongs to the innermost.
|
|
51
90
|
*/
|
|
52
|
-
export function
|
|
53
|
-
passages: Iterable<
|
|
91
|
+
export function widgetDefinitions(
|
|
92
|
+
passages: Iterable<WidgetPassage>,
|
|
54
93
|
parametersOf: ParametersOf = registeredParameters,
|
|
55
|
-
):
|
|
56
|
-
const
|
|
94
|
+
): WidgetDefinition[] {
|
|
95
|
+
const found: WidgetDefinition[] = [];
|
|
57
96
|
for (const passage of passages) {
|
|
58
97
|
if (passage.name !== 'StoryInit' && !passage.tags?.includes('widget')) {
|
|
59
98
|
continue;
|
|
60
99
|
}
|
|
61
|
-
// The open definitions: their
|
|
62
|
-
const open: {
|
|
100
|
+
// The open definitions: their tags, and whether a slot was seen
|
|
101
|
+
const open: { token: MacroToken; isBlock: boolean; closeStart?: number }[] =
|
|
102
|
+
[];
|
|
103
|
+
const done: typeof open = [];
|
|
63
104
|
for (const token of tokenizeMarkupTolerant(passage.content).tokens) {
|
|
64
105
|
if (token.type === 'macro' && token.name.toLowerCase() === 'widget') {
|
|
65
106
|
if (!token.isClose) {
|
|
66
|
-
open.push({
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (!def?.isBlock) continue;
|
|
71
|
-
try {
|
|
72
|
-
names.push(parseWidgetDef(def.rawArgs).name);
|
|
73
|
-
} catch {
|
|
74
|
-
// The {widget} macro reports a definition it cannot read
|
|
107
|
+
open.push({ token, isBlock: false });
|
|
108
|
+
} else {
|
|
109
|
+
const def = open.pop();
|
|
110
|
+
if (def) done.push({ ...def, closeStart: token.start });
|
|
75
111
|
}
|
|
76
112
|
} else if (
|
|
77
113
|
open.length > 0 &&
|
|
@@ -83,6 +119,41 @@ export function blockWidgetNames(
|
|
|
83
119
|
open[open.length - 1]!.isBlock = true;
|
|
84
120
|
}
|
|
85
121
|
}
|
|
122
|
+
done.push(...open);
|
|
123
|
+
const defs: WidgetDefinition[] = [];
|
|
124
|
+
for (const { token, isBlock, closeStart } of done) {
|
|
125
|
+
try {
|
|
126
|
+
const [from, to] = nameSpan(token.rawArgs);
|
|
127
|
+
defs.push({
|
|
128
|
+
...parseWidgetDef(token.rawArgs),
|
|
129
|
+
block: isBlock,
|
|
130
|
+
passage: passage.name,
|
|
131
|
+
start: token.start,
|
|
132
|
+
end: token.end,
|
|
133
|
+
nameStart: token.argsStart + from,
|
|
134
|
+
nameEnd: token.argsStart + to,
|
|
135
|
+
...(closeStart === undefined ? {} : { closeStart }),
|
|
136
|
+
});
|
|
137
|
+
} catch {
|
|
138
|
+
// The {widget} macro reports a definition it cannot read
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
found.push(...defs.sort((a, b) => a.start - b.start));
|
|
86
142
|
}
|
|
87
|
-
return
|
|
143
|
+
return found;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The names of the block widgets (those whose body renders `{@children}`)
|
|
148
|
+
* that StoryInit and the passages tagged `widget` define, closed. They must
|
|
149
|
+
* be known as block macros before any passage is parsed, so that passages
|
|
150
|
+
* invoking them nest their content whatever the passage order.
|
|
151
|
+
*/
|
|
152
|
+
export function blockWidgetNames(
|
|
153
|
+
passages: Iterable<WidgetPassage>,
|
|
154
|
+
parametersOf: ParametersOf = registeredParameters,
|
|
155
|
+
): string[] {
|
|
156
|
+
return widgetDefinitions(passages, parametersOf)
|
|
157
|
+
.filter((def) => def.block && def.closeStart !== undefined)
|
|
158
|
+
.map((def) => def.name);
|
|
88
159
|
}
|