@rohal12/spindle 0.59.22 → 0.59.23
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 +1519 -1219
- package/dist/pkg/macro-registry.js +575 -0
- package/dist/pkg/story-variables.js +1566 -1253
- package/dist/pkg/tooling.js +22 -16
- package/package.json +2 -1
- package/src/code-check.ts +293 -56
- 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/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/static-literal.ts +292 -0
- package/src/story-variables.ts +148 -10
- package/src/tooling.ts +89 -8
- package/src/types-drift-check.ts +52 -0
- package/src/widgets/ast-scanner.ts +16 -4
- package/types/tooling.d.ts +449 -7
package/src/markup/validate.ts
CHANGED
|
@@ -5,13 +5,21 @@
|
|
|
5
5
|
* line and column they are at, instead of failing when the passage renders.
|
|
6
6
|
* Tooling runs it too (see src/tooling.ts).
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
lineColumn,
|
|
10
|
+
MarkupError,
|
|
11
|
+
parseMarkup,
|
|
12
|
+
tokenizeMarkup,
|
|
13
|
+
type MarkupErrorCode,
|
|
14
|
+
} from './parse';
|
|
15
|
+
import type { Span, Token } from './tokens';
|
|
10
16
|
import { parseWidgetDef } from '../widgets/widget-def';
|
|
11
17
|
import {
|
|
12
|
-
codeAndText,
|
|
13
18
|
parseOrError,
|
|
19
|
+
passagePiecesOf,
|
|
20
|
+
pieceOffset,
|
|
14
21
|
withParseCache,
|
|
22
|
+
type CodePiece,
|
|
15
23
|
type ParametersOf,
|
|
16
24
|
} from '../code-check';
|
|
17
25
|
import { CodeSyntaxError } from '../js-lexer';
|
|
@@ -28,14 +36,39 @@ export interface MarkupPassage {
|
|
|
28
36
|
metadata?: Record<string, string>;
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The kinds of diagnostic, which stay the same where the wording of the
|
|
41
|
+
* message changes. The kinds of malformed markup are those of MarkupError.
|
|
42
|
+
*/
|
|
43
|
+
export type MarkupDiagnosticCode =
|
|
44
|
+
| MarkupErrorCode
|
|
45
|
+
| 'unknown-macro'
|
|
46
|
+
| 'argument-error'
|
|
47
|
+
| 'code-syntax'
|
|
48
|
+
| 'unknown-passage'
|
|
49
|
+
| 'unquoted-passage-name';
|
|
50
|
+
|
|
51
|
+
/** What a diagnostic names, by code (see docs/tooling.md). */
|
|
52
|
+
export type MarkupDiagnosticData = Readonly<
|
|
53
|
+
Record<string, string | readonly string[]>
|
|
54
|
+
>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* An error in a passage's markup, at the offending text from `start` up to
|
|
58
|
+
* `end`: UTF-16 offsets into the passage's content, of which `line` and
|
|
59
|
+
* `column` are those of `start`.
|
|
60
|
+
*/
|
|
61
|
+
export interface MarkupDiagnostic extends Span {
|
|
33
62
|
passage: string;
|
|
34
63
|
/** 1-based line within the passage's content. */
|
|
35
64
|
line: number;
|
|
36
65
|
/** 1-based column (UTF-16 code units). */
|
|
37
66
|
column: number;
|
|
38
67
|
message: string;
|
|
68
|
+
/** The kind of error, which tools tell apart without reading `message`. */
|
|
69
|
+
code: MarkupDiagnosticCode;
|
|
70
|
+
/** What the diagnostic names, by `code`: the macro, the passage, what to try. */
|
|
71
|
+
data?: MarkupDiagnosticData;
|
|
39
72
|
/** The source file and line, when the passage says where it came from. */
|
|
40
73
|
file?: string;
|
|
41
74
|
fileLine?: number;
|
|
@@ -110,17 +143,13 @@ function distance(a: string, b: string): number {
|
|
|
110
143
|
return prev[b.length]!;
|
|
111
144
|
}
|
|
112
145
|
|
|
113
|
-
/**
|
|
114
|
-
|
|
115
|
-
* `quote`, if one is close.
|
|
116
|
-
*/
|
|
117
|
-
function didYouMean(
|
|
146
|
+
/** The known name closest to `name`, if one is close. */
|
|
147
|
+
function closestName(
|
|
118
148
|
name: string,
|
|
119
149
|
names: Iterable<string>,
|
|
120
|
-
|
|
121
|
-
): string {
|
|
150
|
+
): string | undefined {
|
|
122
151
|
const lower = name.toLowerCase();
|
|
123
|
-
let best
|
|
152
|
+
let best: string | undefined;
|
|
124
153
|
let bestDistance = Math.max(2, Math.floor(lower.length / 3)) + 1;
|
|
125
154
|
for (const candidate of names) {
|
|
126
155
|
const d = distance(lower, candidate.toLowerCase());
|
|
@@ -129,9 +158,15 @@ function didYouMean(
|
|
|
129
158
|
bestDistance = d;
|
|
130
159
|
}
|
|
131
160
|
}
|
|
132
|
-
return best
|
|
161
|
+
return best;
|
|
133
162
|
}
|
|
134
163
|
|
|
164
|
+
/** " Did you mean …?" with `best`, written by `quote`, if there is one. */
|
|
165
|
+
const didYouMean = (
|
|
166
|
+
best: string | undefined,
|
|
167
|
+
quote: (name: string) => string,
|
|
168
|
+
) => (best ? ` Did you mean ${quote(best)}?` : '');
|
|
169
|
+
|
|
135
170
|
/** What a passage argument may be, after an error in one. */
|
|
136
171
|
const PASSAGE_ARGUMENT_HINT =
|
|
137
172
|
' (a passage name is a quoted string or an expression)';
|
|
@@ -160,14 +195,25 @@ export function validateMarkup(
|
|
|
160
195
|
const hooks = options.isBlockMacro
|
|
161
196
|
? { isBlock: options.isBlockMacro }
|
|
162
197
|
: undefined;
|
|
163
|
-
const report = (
|
|
164
|
-
|
|
198
|
+
const report = (
|
|
199
|
+
passage: MarkupPassage,
|
|
200
|
+
start: number,
|
|
201
|
+
end: number,
|
|
202
|
+
code: MarkupDiagnosticCode,
|
|
203
|
+
message: string,
|
|
204
|
+
data?: MarkupDiagnosticData,
|
|
205
|
+
) => {
|
|
206
|
+
const { line, column } = lineColumn(passage.content, start);
|
|
165
207
|
const d: MarkupDiagnostic = {
|
|
166
208
|
passage: passage.name,
|
|
167
209
|
line,
|
|
168
210
|
column,
|
|
169
211
|
message,
|
|
212
|
+
code,
|
|
213
|
+
start,
|
|
214
|
+
end,
|
|
170
215
|
};
|
|
216
|
+
if (data) d.data = data;
|
|
171
217
|
const file = passage.metadata?.['data-source-file'];
|
|
172
218
|
if (file) {
|
|
173
219
|
d.file = file;
|
|
@@ -176,6 +222,20 @@ export function validateMarkup(
|
|
|
176
222
|
}
|
|
177
223
|
diagnostics.push(d);
|
|
178
224
|
};
|
|
225
|
+
/** Report malformed markup. */
|
|
226
|
+
const reportError = (
|
|
227
|
+
passage: MarkupPassage,
|
|
228
|
+
err: MarkupError,
|
|
229
|
+
where: string,
|
|
230
|
+
) =>
|
|
231
|
+
report(
|
|
232
|
+
passage,
|
|
233
|
+
err.offset,
|
|
234
|
+
err.end,
|
|
235
|
+
err.code,
|
|
236
|
+
where + err.reason,
|
|
237
|
+
err.data,
|
|
238
|
+
);
|
|
179
239
|
|
|
180
240
|
// Parse every passage first: the widgets they define are known macros.
|
|
181
241
|
const parsed: [MarkupPassage, Token[]][] = [];
|
|
@@ -200,18 +260,15 @@ export function validateMarkup(
|
|
|
200
260
|
parsed.push([passage, tokens]);
|
|
201
261
|
} catch (err) {
|
|
202
262
|
if (!(err instanceof MarkupError)) throw err;
|
|
203
|
-
if (reported)
|
|
204
|
-
report(passage, err.offset, err.reason);
|
|
205
|
-
}
|
|
263
|
+
if (reported) reportError(passage, err, '');
|
|
206
264
|
}
|
|
207
265
|
}
|
|
208
266
|
const names = [...(options.macroNames ?? []), ...widgets];
|
|
209
267
|
|
|
210
|
-
/** Report the unknown macros among `tokens`,
|
|
268
|
+
/** Report the unknown macros among `tokens`, with `where` in front. */
|
|
211
269
|
const checkMacros = (
|
|
212
270
|
passage: MarkupPassage,
|
|
213
|
-
tokens: Token[],
|
|
214
|
-
base: number,
|
|
271
|
+
tokens: readonly Token[],
|
|
215
272
|
where: string,
|
|
216
273
|
) => {
|
|
217
274
|
for (const token of tokens) {
|
|
@@ -224,10 +281,14 @@ export function validateMarkup(
|
|
|
224
281
|
) {
|
|
225
282
|
continue;
|
|
226
283
|
}
|
|
284
|
+
const best = closestName(token.name, names);
|
|
227
285
|
report(
|
|
228
286
|
passage,
|
|
229
|
-
|
|
230
|
-
|
|
287
|
+
token.start,
|
|
288
|
+
token.end,
|
|
289
|
+
'unknown-macro',
|
|
290
|
+
`${where}Unknown macro {${token.name}}.${didYouMean(best, (n) => `{${n}}`)}`,
|
|
291
|
+
{ name: token.name, suggestions: best ? [best] : [] },
|
|
231
292
|
);
|
|
232
293
|
}
|
|
233
294
|
};
|
|
@@ -235,71 +296,98 @@ export function validateMarkup(
|
|
|
235
296
|
const parametersOf: ParametersOf = options.parametersOf ?? (() => undefined);
|
|
236
297
|
|
|
237
298
|
/**
|
|
238
|
-
* Check
|
|
239
|
-
*
|
|
299
|
+
* Check the pieces of a passage: its macros, the code in them, the passage
|
|
300
|
+
* names they write out and the markup in their text.
|
|
240
301
|
*/
|
|
241
|
-
const
|
|
242
|
-
passage
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
continue;
|
|
254
|
-
}
|
|
255
|
-
if (piece.kind === 'passage') {
|
|
256
|
-
if (
|
|
257
|
-
options.checkPassageNames !== false &&
|
|
258
|
-
!passageNames.has(piece.name)
|
|
259
|
-
) {
|
|
260
|
-
const hint = didYouMean(piece.name, passageNames, JSON.stringify);
|
|
302
|
+
const checkPassage = (passage: MarkupPassage, tokens: readonly Token[]) => {
|
|
303
|
+
checkMacros(passage, tokens, '');
|
|
304
|
+
// The markup of a text with an error in it is not read further
|
|
305
|
+
let skipTo = 0;
|
|
306
|
+
for (const piece of passagePiecesOf(passage.content, tokens, parametersOf, {
|
|
307
|
+
isBlock: options.isBlockMacro,
|
|
308
|
+
})) {
|
|
309
|
+
if (piece.nested && piece.offset < skipTo) continue;
|
|
310
|
+
const at = piece.offset;
|
|
311
|
+
const where = piece.where && piece.nested ? piece.where : '';
|
|
312
|
+
switch (piece.kind) {
|
|
313
|
+
case 'argument-error':
|
|
261
314
|
report(
|
|
262
315
|
passage,
|
|
263
316
|
at,
|
|
264
|
-
|
|
317
|
+
at + piece.length,
|
|
318
|
+
'argument-error',
|
|
319
|
+
`${where}${piece.message} in ${piece.label}`,
|
|
320
|
+
{ macro: piece.macro },
|
|
265
321
|
);
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
(piece.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
322
|
+
break;
|
|
323
|
+
case 'passage':
|
|
324
|
+
if (
|
|
325
|
+
options.checkPassageNames !== false &&
|
|
326
|
+
!passageNames.has(piece.name)
|
|
327
|
+
) {
|
|
328
|
+
const best = closestName(piece.name, passageNames);
|
|
329
|
+
report(
|
|
330
|
+
passage,
|
|
331
|
+
at,
|
|
332
|
+
at + piece.length,
|
|
333
|
+
'unknown-passage',
|
|
334
|
+
`${where}No passage named ${JSON.stringify(piece.name)} in ${piece.label}.${didYouMean(best, JSON.stringify)}`,
|
|
335
|
+
{
|
|
336
|
+
name: piece.name,
|
|
337
|
+
macro: piece.macro,
|
|
338
|
+
suggestions: best ? [best] : [],
|
|
339
|
+
},
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
case 'code':
|
|
344
|
+
checkCode(passage, piece, where);
|
|
345
|
+
break;
|
|
346
|
+
case 'text':
|
|
347
|
+
if (piece.errors.length > 0) {
|
|
348
|
+
reportError(passage, piece.errors[0]!, piece.where);
|
|
349
|
+
skipTo = pieceOffset(piece, piece.text.length);
|
|
350
|
+
} else {
|
|
351
|
+
checkMacros(passage, piece.tokens, piece.where);
|
|
352
|
+
}
|
|
297
353
|
}
|
|
298
354
|
}
|
|
299
355
|
};
|
|
300
356
|
|
|
357
|
+
const checkCode = (
|
|
358
|
+
passage: MarkupPassage,
|
|
359
|
+
piece: CodePiece,
|
|
360
|
+
where: string,
|
|
361
|
+
) => {
|
|
362
|
+
const at = piece.offset;
|
|
363
|
+
const result = parseOrError(piece.code, piece.goal);
|
|
364
|
+
const code = piece.code.trim();
|
|
365
|
+
if (result instanceof CodeSyntaxError) {
|
|
366
|
+
const offending = /^(?:[\w$]+|\S)/.exec(piece.code.slice(result.pos));
|
|
367
|
+
report(
|
|
368
|
+
passage,
|
|
369
|
+
pieceOffset(piece, result.pos),
|
|
370
|
+
pieceOffset(piece, result.pos + (offending?.[0].length ?? 0)),
|
|
371
|
+
'code-syntax',
|
|
372
|
+
`${where}${result.reasonIn(passage.content, at)} in ${piece.label}` +
|
|
373
|
+
(piece.passage ? PASSAGE_ARGUMENT_HINT : ''),
|
|
374
|
+
piece.macro ? { macro: piece.macro } : undefined,
|
|
375
|
+
);
|
|
376
|
+
} else if (piece.passage && BARE_WORD_RE.test(code)) {
|
|
377
|
+
const lead = piece.code.length - piece.code.trimStart().length;
|
|
378
|
+
report(
|
|
379
|
+
passage,
|
|
380
|
+
pieceOffset(piece, lead),
|
|
381
|
+
pieceOffset(piece, lead + code.length),
|
|
382
|
+
'unquoted-passage-name',
|
|
383
|
+
`${where}Unquoted passage name in ${piece.label}: write ${JSON.stringify(code)}${PASSAGE_ARGUMENT_HINT}`,
|
|
384
|
+
{ name: code, macro: piece.macro ?? '' },
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
301
389
|
for (const [passage, tokens] of parsed) {
|
|
302
|
-
withParseCache(() =>
|
|
390
|
+
withParseCache(() => checkPassage(passage, tokens));
|
|
303
391
|
}
|
|
304
392
|
return diagnostics;
|
|
305
393
|
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of a JavaScript literal, read from its text without running it:
|
|
3
|
+
* what a StoryVariables initializer is when it is a literal, or an array or
|
|
4
|
+
* object literal of literals (see parseDeclarations in story-variables.ts).
|
|
5
|
+
*
|
|
6
|
+
* The reader is deliberately small. Anything it does not recognise (a call,
|
|
7
|
+
* an operator, a reference, a spread) is "not static", which is no error: the
|
|
8
|
+
* value is simply not known without running the code.
|
|
9
|
+
*/
|
|
10
|
+
import { scanStringLiteral } from './js-lexer';
|
|
11
|
+
|
|
12
|
+
/** The type of a variable by its default (see story-variables.ts VarType). */
|
|
13
|
+
type LiteralType =
|
|
14
|
+
| 'number'
|
|
15
|
+
| 'string'
|
|
16
|
+
| 'boolean'
|
|
17
|
+
| 'array'
|
|
18
|
+
| 'object'
|
|
19
|
+
| 'null';
|
|
20
|
+
|
|
21
|
+
/** The shape of a literal (see story-variables.ts FieldSchema). */
|
|
22
|
+
export interface LiteralShape {
|
|
23
|
+
type: LiteralType;
|
|
24
|
+
/** The members an object literal gives statically, by key. */
|
|
25
|
+
fields?: Map<string, LiteralShape>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a literal reads as. */
|
|
29
|
+
export type StaticLiteral =
|
|
30
|
+
/** A value of this shape. */
|
|
31
|
+
| { shape: LiteralShape }
|
|
32
|
+
/** A value no variable can hold: its `typeof`. */
|
|
33
|
+
| { unsupported: 'function' | 'undefined' | 'bigint' }
|
|
34
|
+
/** Not known without running it. */
|
|
35
|
+
| undefined;
|
|
36
|
+
|
|
37
|
+
const NUMBER_RE =
|
|
38
|
+
/(?:0[xX][\da-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?[\d_]*|\.\d[\d_]*)(?:[eE][+-]?\d+)?)(n?)/y;
|
|
39
|
+
const IDENT_RE = /[A-Za-z_$][\w$]*/y;
|
|
40
|
+
const ARROW_RE =
|
|
41
|
+
/(?:async\s+)?(?:\((?:[^()]|\([^()]*\))*\)|[A-Za-z_$][\w$]*)\s*=>/y;
|
|
42
|
+
|
|
43
|
+
/** Read `src`, one literal and nothing else. */
|
|
44
|
+
export function staticLiteral(src: string): StaticLiteral {
|
|
45
|
+
const reader = new Reader(src);
|
|
46
|
+
const value = reader.value();
|
|
47
|
+
reader.space();
|
|
48
|
+
return reader.i === src.length ? value : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class Reader {
|
|
52
|
+
i = 0;
|
|
53
|
+
constructor(private readonly src: string) {}
|
|
54
|
+
|
|
55
|
+
/** Skip whitespace and comments. */
|
|
56
|
+
space(): void {
|
|
57
|
+
const { src } = this;
|
|
58
|
+
for (;;) {
|
|
59
|
+
while (/\s/.test(src.charAt(this.i))) this.i++;
|
|
60
|
+
if (src.startsWith('//', this.i)) {
|
|
61
|
+
const end = src.indexOf('\n', this.i);
|
|
62
|
+
this.i = end < 0 ? src.length : end;
|
|
63
|
+
} else if (src.startsWith('/*', this.i)) {
|
|
64
|
+
const end = src.indexOf('*/', this.i + 2);
|
|
65
|
+
this.i = end < 0 ? src.length : end + 2;
|
|
66
|
+
} else return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The value at the cursor, which is left after it (or anywhere, if not static). */
|
|
71
|
+
value(): StaticLiteral {
|
|
72
|
+
const { src } = this;
|
|
73
|
+
this.space();
|
|
74
|
+
const c = src.charAt(this.i);
|
|
75
|
+
if (c === '"' || c === "'") {
|
|
76
|
+
return this.string().closed ? { shape: { type: 'string' } } : undefined;
|
|
77
|
+
}
|
|
78
|
+
if (c === '`') return this.template();
|
|
79
|
+
if (c === '[')
|
|
80
|
+
return this.skipBalanced() ? { shape: { type: 'array' } } : undefined;
|
|
81
|
+
if (c === '{') return this.object();
|
|
82
|
+
if (/[\d.+-]/.test(c)) return this.number();
|
|
83
|
+
if (/[A-Za-z_$(]/.test(c)) return this.word();
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Skip the string literal at the cursor: its body, and whether it is closed. */
|
|
88
|
+
private string(): { body: string; closed: boolean } {
|
|
89
|
+
const { end, closed } = scanStringLiteral(this.src, this.i);
|
|
90
|
+
const body = this.src.slice(this.i + 1, closed ? end - 1 : end);
|
|
91
|
+
this.i = end;
|
|
92
|
+
return { body, closed };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A template literal: a string, but for `${}`. */
|
|
96
|
+
private template(): StaticLiteral {
|
|
97
|
+
const { src } = this;
|
|
98
|
+
for (let i = this.i + 1; i < src.length; i++) {
|
|
99
|
+
const c = src.charAt(i);
|
|
100
|
+
if (c === '\\') i++;
|
|
101
|
+
else if (c === '`') {
|
|
102
|
+
this.i = i + 1;
|
|
103
|
+
return { shape: { type: 'string' } };
|
|
104
|
+
} else if (c === '$' && src.charAt(i + 1) === '{') return undefined;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A number, or a sign and one. */
|
|
110
|
+
private number(): StaticLiteral {
|
|
111
|
+
const { src } = this;
|
|
112
|
+
let i = this.i;
|
|
113
|
+
if (src.charAt(i) === '+' || src.charAt(i) === '-') {
|
|
114
|
+
i++;
|
|
115
|
+
while (/\s/.test(src.charAt(i))) i++;
|
|
116
|
+
}
|
|
117
|
+
NUMBER_RE.lastIndex = i;
|
|
118
|
+
const match = NUMBER_RE.exec(src);
|
|
119
|
+
if (match) {
|
|
120
|
+
this.i = NUMBER_RE.lastIndex;
|
|
121
|
+
return match[1]
|
|
122
|
+
? { unsupported: 'bigint' }
|
|
123
|
+
: { shape: { type: 'number' } };
|
|
124
|
+
}
|
|
125
|
+
IDENT_RE.lastIndex = i;
|
|
126
|
+
const word = IDENT_RE.exec(src)?.[0];
|
|
127
|
+
if (word === 'Infinity') {
|
|
128
|
+
this.i = IDENT_RE.lastIndex;
|
|
129
|
+
return { shape: { type: 'number' } };
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A keyword, a function, or a name (which is not static). */
|
|
135
|
+
private word(): StaticLiteral {
|
|
136
|
+
const { src } = this;
|
|
137
|
+
ARROW_RE.lastIndex = this.i;
|
|
138
|
+
if (ARROW_RE.test(src)) return this.function();
|
|
139
|
+
IDENT_RE.lastIndex = this.i;
|
|
140
|
+
const word = IDENT_RE.exec(src)?.[0];
|
|
141
|
+
if (word === undefined) return undefined;
|
|
142
|
+
const end = IDENT_RE.lastIndex;
|
|
143
|
+
switch (word) {
|
|
144
|
+
case 'true':
|
|
145
|
+
case 'false':
|
|
146
|
+
this.i = end;
|
|
147
|
+
return { shape: { type: 'boolean' } };
|
|
148
|
+
case 'null':
|
|
149
|
+
this.i = end;
|
|
150
|
+
return { shape: { type: 'null' } };
|
|
151
|
+
case 'NaN':
|
|
152
|
+
this.i = end;
|
|
153
|
+
return { shape: { type: 'number' } };
|
|
154
|
+
case 'undefined':
|
|
155
|
+
this.i = end;
|
|
156
|
+
return { unsupported: 'undefined' };
|
|
157
|
+
case 'function':
|
|
158
|
+
case 'class':
|
|
159
|
+
case 'async':
|
|
160
|
+
return this.function();
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A function (or class): the rest is its body, which is not read. */
|
|
166
|
+
private function(): StaticLiteral {
|
|
167
|
+
return this.unsupported({ unsupported: 'function' });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** `value`, which makes the whole literal unsupported: the rest is not read. */
|
|
171
|
+
private unsupported(value: {
|
|
172
|
+
unsupported: 'function' | 'undefined' | 'bigint';
|
|
173
|
+
}) {
|
|
174
|
+
this.i = this.src.length;
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** An object literal: the members that are static. */
|
|
179
|
+
private object(): StaticLiteral {
|
|
180
|
+
const { src } = this;
|
|
181
|
+
const fields = new Map<string, LiteralShape>();
|
|
182
|
+
this.i++;
|
|
183
|
+
for (;;) {
|
|
184
|
+
this.space();
|
|
185
|
+
const c = src.charAt(this.i);
|
|
186
|
+
if (c === '}') {
|
|
187
|
+
this.i++;
|
|
188
|
+
return { shape: { type: 'object', fields } };
|
|
189
|
+
}
|
|
190
|
+
if (c === ',') {
|
|
191
|
+
this.i++;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (this.i >= src.length) return undefined;
|
|
195
|
+
const key = this.key();
|
|
196
|
+
this.space();
|
|
197
|
+
if (key !== undefined && src.charAt(this.i) === ':') {
|
|
198
|
+
this.i++;
|
|
199
|
+
const at = this.i;
|
|
200
|
+
const value = this.value();
|
|
201
|
+
this.space();
|
|
202
|
+
if (value && 'unsupported' in value) return this.unsupported(value);
|
|
203
|
+
if (value && ',}'.includes(src.charAt(this.i))) {
|
|
204
|
+
fields.set(key, value.shape);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
// Not static: skip to the end of the member
|
|
208
|
+
this.i = at;
|
|
209
|
+
} else if (key !== undefined && src.charAt(this.i) === '(') {
|
|
210
|
+
return this.unsupported({ unsupported: 'function' });
|
|
211
|
+
}
|
|
212
|
+
if (!this.skipMember()) return undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The key of a member (a name, a string or a number), if it has one. */
|
|
217
|
+
private key(): string | undefined {
|
|
218
|
+
const { src } = this;
|
|
219
|
+
const c = src.charAt(this.i);
|
|
220
|
+
if (c === '"' || c === "'") {
|
|
221
|
+
const { body, closed } = this.string();
|
|
222
|
+
return closed ? body : undefined;
|
|
223
|
+
}
|
|
224
|
+
const word = /[\w$]+/y;
|
|
225
|
+
word.lastIndex = this.i;
|
|
226
|
+
const found = word.exec(src)?.[0];
|
|
227
|
+
if (found === undefined) return undefined;
|
|
228
|
+
this.i = word.lastIndex;
|
|
229
|
+
return found;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Skip to the `,` or `}` that ends a member, balancing what is inside. */
|
|
233
|
+
private skipMember(): boolean {
|
|
234
|
+
const { src } = this;
|
|
235
|
+
while (this.i < src.length) {
|
|
236
|
+
const c = src.charAt(this.i);
|
|
237
|
+
if (c === ',' || c === '}') return true;
|
|
238
|
+
if (!this.skipOne()) return false;
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Skip an array literal (or any bracketed group) at the cursor. */
|
|
244
|
+
private skipBalanced(): boolean {
|
|
245
|
+
const { src } = this;
|
|
246
|
+
const close = { '[': ']', '{': '}', '(': ')' }[src.charAt(this.i)];
|
|
247
|
+
if (!close) return false;
|
|
248
|
+
this.i++;
|
|
249
|
+
while (this.i < src.length) {
|
|
250
|
+
if (src.charAt(this.i) === close) {
|
|
251
|
+
this.i++;
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
if (!this.skipOne()) return false;
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Skip one character, or one string, template, comment or group. */
|
|
260
|
+
private skipOne(): boolean {
|
|
261
|
+
const { src } = this;
|
|
262
|
+
const c = src.charAt(this.i);
|
|
263
|
+
if (c === '"' || c === "'") return this.string().closed;
|
|
264
|
+
if (c === '`') return this.skipTemplate();
|
|
265
|
+
if (src.startsWith('//', this.i) || src.startsWith('/*', this.i)) {
|
|
266
|
+
this.space();
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
if (c === '[' || c === '{' || c === '(') return this.skipBalanced();
|
|
270
|
+
if (c === ']' || c === '}' || c === ')') return false;
|
|
271
|
+
this.i++;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Skip a template literal, with the expressions in it. */
|
|
276
|
+
private skipTemplate(): boolean {
|
|
277
|
+
const { src } = this;
|
|
278
|
+
this.i++;
|
|
279
|
+
while (this.i < src.length) {
|
|
280
|
+
const c = src.charAt(this.i);
|
|
281
|
+
if (c === '\\') this.i += 2;
|
|
282
|
+
else if (c === '`') {
|
|
283
|
+
this.i++;
|
|
284
|
+
return true;
|
|
285
|
+
} else if (c === '$' && src.charAt(this.i + 1) === '{') {
|
|
286
|
+
this.i++;
|
|
287
|
+
if (!this.skipBalanced()) return false;
|
|
288
|
+
} else this.i++;
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|