@rohal12/spindle 0.59.29 → 0.59.31
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 +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +947 -351
- package/dist/pkg/macro-registry.js +19 -0
- package/dist/pkg/macro-registry.json +19 -0
- package/dist/pkg/story-variables.js +780 -107
- package/package.json +1 -1
- package/src/action-registry.ts +59 -5
- package/src/automation/load-yaml.ts +111 -20
- package/src/automation/runner.ts +19 -13
- package/src/automation/types.ts +7 -5
- package/src/class-registry.ts +8 -5
- package/src/code-check.ts +87 -20
- package/src/components/macros/Checkbox.tsx +5 -2
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/components/macros/For.tsx +1 -7
- package/src/components/macros/Listbox.tsx +1 -0
- package/src/components/macros/MenubarButtons.tsx +6 -1
- package/src/components/macros/Nobr.tsx +1 -0
- package/src/components/macros/PassageDisplay.tsx +136 -10
- package/src/components/macros/Radiobutton.tsx +1 -0
- package/src/components/macros/Repeat.tsx +4 -2
- package/src/components/macros/SaveManager.tsx +30 -8
- package/src/components/macros/SettingsControls.tsx +19 -4
- package/src/components/macros/Span.tsx +1 -0
- package/src/components/macros/StoryTitle.tsx +1 -0
- package/src/components/macros/Unset.tsx +26 -13
- package/src/components/macros/VarDisplay.tsx +2 -3
- package/src/components/macros/WidgetInvocation.tsx +5 -6
- package/src/components/macros/input-macro.ts +1 -0
- package/src/components/macros/locals-scope.tsx +19 -8
- package/src/components/macros/macro-args.ts +11 -3
- package/src/expression.ts +34 -18
- package/src/hooks/use-merged-locals.ts +17 -2
- package/src/hooks/use-modal-focus.ts +247 -133
- package/src/index.tsx +32 -21
- package/src/js-bindings.ts +594 -0
- package/src/js-lexer.ts +76 -14
- package/src/macro-discovery.ts +83 -32
- package/src/markup/ast.ts +22 -1
- package/src/markup/render.tsx +44 -15
- package/src/parser.ts +41 -10
- package/src/prng.ts +24 -1
- package/src/saves/save-manager.ts +36 -9
- package/src/static-literal.ts +87 -16
- package/src/store.ts +83 -6
- package/src/story-api.ts +23 -6
- package/src/story-variables.ts +91 -28
- package/src/structural.ts +26 -15
- package/src/tooling.ts +24 -10
- package/src/transform.ts +22 -7
- package/src/utils/macro-names.ts +4 -0
- package/src/utils/namespace.ts +15 -2
- package/src/utils/object-path.ts +2 -2
- package/src/utils/stable-key.ts +17 -6
- package/src/utils/value-kinds.ts +38 -1
- package/src/widgets/ast-scanner.ts +4 -1
- package/src/widgets/register-widget-def.ts +2 -3
- package/src/widgets/widget-def.ts +36 -6
- package/src/widgets/widget-registry.ts +6 -2
- package/types/index.d.ts +12 -5
- package/types/tooling.d.ts +12 -2
package/src/story-variables.ts
CHANGED
|
@@ -19,8 +19,10 @@ import {
|
|
|
19
19
|
sigilExpressions,
|
|
20
20
|
parseOrError,
|
|
21
21
|
pieceOffset,
|
|
22
|
+
widgetPieces,
|
|
22
23
|
withParseCache,
|
|
23
24
|
type ParametersOf,
|
|
25
|
+
type Piece,
|
|
24
26
|
} from './code-check';
|
|
25
27
|
import { getMacroParameters, type ParameterDef } from './registry';
|
|
26
28
|
import {
|
|
@@ -29,7 +31,11 @@ import {
|
|
|
29
31
|
type ArgSpans,
|
|
30
32
|
} from './components/macros/macro-args';
|
|
31
33
|
import { NOT_MARKUP } from './markup/validate';
|
|
32
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
createNamespace,
|
|
36
|
+
isArrayIndex,
|
|
37
|
+
variableNameError,
|
|
38
|
+
} from './utils/namespace';
|
|
33
39
|
import { staticLiteral, type LiteralShape } from './static-literal';
|
|
34
40
|
import { parse } from 'acorn';
|
|
35
41
|
|
|
@@ -437,6 +443,11 @@ function checkReference(
|
|
|
437
443
|
|
|
438
444
|
// Primitives expose their wrapper's built-ins (length, toUpperCase,
|
|
439
445
|
// toFixed, etc.). Keep validating past properties of primitive type.
|
|
446
|
+
// Strings also have the character at each index below their length,
|
|
447
|
+
// which the empty sample has none of (the default may not be the value)
|
|
448
|
+
if (current.type === 'string' && isArrayIndex(part)) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
440
451
|
const sample = PRIMITIVE_SAMPLES[current.type];
|
|
441
452
|
if (sample && part in sample) {
|
|
442
453
|
const member: unknown = sample[part as keyof typeof sample];
|
|
@@ -495,14 +506,41 @@ type Emit = (ref: VariableReference) => void;
|
|
|
495
506
|
|
|
496
507
|
const NO_STORE_VAR_MACROS: ReadonlySet<string> = new Set();
|
|
497
508
|
|
|
498
|
-
/** A
|
|
509
|
+
/** A `.field` path written right after a reference, which may be empty. */
|
|
510
|
+
const FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* The reference to variable `name`, which JavaScript reads at `[start, end)`
|
|
514
|
+
* of `code` (the sigil and the name as written, which may be spelled with
|
|
515
|
+
* escapes), with the dot path written after it.
|
|
516
|
+
*/
|
|
499
517
|
function referenceAt(
|
|
500
518
|
code: string,
|
|
519
|
+
sigil: string,
|
|
520
|
+
name: string,
|
|
521
|
+
start: number,
|
|
522
|
+
end: number,
|
|
523
|
+
at: At,
|
|
524
|
+
): VariableReference {
|
|
525
|
+
FIELD_PATH_RE.lastIndex = end;
|
|
526
|
+
const fields = FIELD_PATH_RE.exec(code)![0];
|
|
527
|
+
return {
|
|
528
|
+
sigil: sigil as '$' | '%',
|
|
529
|
+
name,
|
|
530
|
+
path: fields ? fields.slice(1).split('.') : [],
|
|
531
|
+
start: at(start),
|
|
532
|
+
end: at(end + fields.length),
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** A reference written at `index` of `term`, a `variable` parameter as written. */
|
|
537
|
+
function termReferenceAt(
|
|
538
|
+
term: string,
|
|
501
539
|
index: number,
|
|
502
540
|
at: At,
|
|
503
541
|
): VariableReference | undefined {
|
|
504
542
|
VAR_PATH_RE.lastIndex = index;
|
|
505
|
-
const match = VAR_PATH_RE.exec(
|
|
543
|
+
const match = VAR_PATH_RE.exec(term);
|
|
506
544
|
if (!match) return undefined;
|
|
507
545
|
const [name, ...path] = match[2]!.split('.');
|
|
508
546
|
return {
|
|
@@ -562,8 +600,7 @@ function scanCode(
|
|
|
562
600
|
}
|
|
563
601
|
for (const ref of parsed.refs) {
|
|
564
602
|
if (ref.sigil !== '$' && ref.sigil !== '%') continue;
|
|
565
|
-
|
|
566
|
-
if (found) emit(found);
|
|
603
|
+
emit(referenceAt(code, ref.sigil, ref.name, ref.start, ref.end, at));
|
|
567
604
|
}
|
|
568
605
|
}
|
|
569
606
|
|
|
@@ -577,10 +614,9 @@ function scanCodeLeniently(
|
|
|
577
614
|
lexJs(
|
|
578
615
|
code,
|
|
579
616
|
{
|
|
580
|
-
variable(sigil,
|
|
617
|
+
variable(sigil, name, index, _nesting, end) {
|
|
581
618
|
if (sigil !== '$' && sigil !== '%') return;
|
|
582
|
-
|
|
583
|
-
if (found) emit(found);
|
|
619
|
+
emit(referenceAt(code, sigil, name, index, end, at));
|
|
584
620
|
},
|
|
585
621
|
// Strings, regex literals and comments hold no references
|
|
586
622
|
literal() {},
|
|
@@ -589,21 +625,44 @@ function scanCodeLeniently(
|
|
|
589
625
|
);
|
|
590
626
|
}
|
|
591
627
|
|
|
628
|
+
/** Report the references in the pieces of code and markup text of arguments. */
|
|
629
|
+
function scanPieces(
|
|
630
|
+
pieces: Iterable<Piece>,
|
|
631
|
+
at: At,
|
|
632
|
+
emit: Emit,
|
|
633
|
+
options: ReferenceOptions,
|
|
634
|
+
): void {
|
|
635
|
+
for (const piece of pieces) {
|
|
636
|
+
const pieceAt: At = (i) => at(pieceOffset(piece, i));
|
|
637
|
+
if (piece.kind === 'code') {
|
|
638
|
+
scanCode(piece.code, pieceAt, emit, piece.goal);
|
|
639
|
+
} else if (piece.kind === 'text') {
|
|
640
|
+
scanInterpolations(piece.text, pieceAt, emit, {
|
|
641
|
+
...options,
|
|
642
|
+
storeVarMacros: NO_STORE_VAR_MACROS,
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
592
648
|
/**
|
|
593
649
|
* Report the references in the arguments of a macro by what its parameters
|
|
594
650
|
* say each holds (see ParameterDef.holds): code is scanned as code and
|
|
595
651
|
* markup as markup, but literal text (a placeholder, a passage name) holds
|
|
596
652
|
* none. A macro without declared parameters, or arguments that do not
|
|
597
|
-
* parse, are scanned as code.
|
|
653
|
+
* parse, are scanned as code. The arguments of a widget that declares what
|
|
654
|
+
* some parameters hold are also read by those (see widgetPieces).
|
|
598
655
|
*/
|
|
599
656
|
function scanArgs(
|
|
600
657
|
token: Extract<Token, { type: 'macro' }>,
|
|
601
|
-
|
|
658
|
+
parametersOf: ParametersOf,
|
|
602
659
|
at: At,
|
|
603
660
|
emit: Emit,
|
|
604
661
|
options: ReferenceOptions,
|
|
605
662
|
): void {
|
|
606
663
|
if (!token.rawArgs) return;
|
|
664
|
+
const params = parametersOf(token.name);
|
|
665
|
+
let byParameters = false;
|
|
607
666
|
if (params) {
|
|
608
667
|
if (options.all && !options.storeVarMacros.has(token.name.toLowerCase())) {
|
|
609
668
|
scanVariableParameters(token, params, at, emit);
|
|
@@ -611,22 +670,23 @@ function scanArgs(
|
|
|
611
670
|
const pieces = [
|
|
612
671
|
...argPieces(token.rawArgs, token.argsStart, params, token.name, ''),
|
|
613
672
|
];
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
673
|
+
byParameters = !pieces.some((piece) => piece.kind === 'argument-error');
|
|
674
|
+
if (byParameters) scanPieces(pieces, at, emit, options);
|
|
675
|
+
}
|
|
676
|
+
// The arguments of a widget are code: the strings among them hold none,
|
|
677
|
+
// but those that declare they hold code or markup are scanned as that
|
|
678
|
+
if (!byParameters) {
|
|
679
|
+
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
680
|
+
}
|
|
681
|
+
const widget = parametersOf.widget?.(token.name);
|
|
682
|
+
if (widget) {
|
|
683
|
+
scanPieces(
|
|
684
|
+
widgetPieces(token.rawArgs, token.argsStart, widget, token.name, ''),
|
|
685
|
+
at,
|
|
686
|
+
emit,
|
|
687
|
+
options,
|
|
688
|
+
);
|
|
628
689
|
}
|
|
629
|
-
scanCode(token.rawArgs, (i) => at(token.argsStart + i), emit);
|
|
630
690
|
}
|
|
631
691
|
|
|
632
692
|
/**
|
|
@@ -650,7 +710,7 @@ function scanVariableParameters(
|
|
|
650
710
|
for (const param of params) {
|
|
651
711
|
const span = spans.get(param);
|
|
652
712
|
if (param.type !== 'variable' || !span) continue;
|
|
653
|
-
const ref =
|
|
713
|
+
const ref = termReferenceAt(token.rawArgs.slice(span[0], span[1]), 0, (i) =>
|
|
654
714
|
at(token.argsStart + span[0] + i),
|
|
655
715
|
);
|
|
656
716
|
if (ref) emit(ref);
|
|
@@ -733,7 +793,7 @@ function collectTokenRefs(
|
|
|
733
793
|
else scanInterpolations(value, valueIn, emit, options);
|
|
734
794
|
}
|
|
735
795
|
} else if (token.type === 'macro' && !token.isClose) {
|
|
736
|
-
scanArgs(token, parametersOf
|
|
796
|
+
scanArgs(token, parametersOf, at, emit, options);
|
|
737
797
|
// Selectors (`{.{$cls} button}`) are interpolated when rendered
|
|
738
798
|
scanSelectors(content, token, at, emit, options);
|
|
739
799
|
|
|
@@ -804,7 +864,10 @@ export function collectVariableReferences(
|
|
|
804
864
|
options,
|
|
805
865
|
),
|
|
806
866
|
);
|
|
807
|
-
|
|
867
|
+
// The scan visits a macro's arguments before its selectors, and bindings
|
|
868
|
+
// after its arguments: the order of the source is the contract. (Stable,
|
|
869
|
+
// so references written at one place keep the order they were found in.)
|
|
870
|
+
return found.sort((a, b) => a.start - b.start);
|
|
808
871
|
}
|
|
809
872
|
|
|
810
873
|
/**
|
package/src/structural.ts
CHANGED
|
@@ -22,6 +22,8 @@ import {
|
|
|
22
22
|
mapEntries,
|
|
23
23
|
mapGet,
|
|
24
24
|
mapSet,
|
|
25
|
+
regExpParts,
|
|
26
|
+
regExpText,
|
|
25
27
|
setMembers,
|
|
26
28
|
} from './utils/value-kinds';
|
|
27
29
|
|
|
@@ -116,10 +118,7 @@ export function deepClone<T>(value: T, options: DeepCloneOptions = {}): T {
|
|
|
116
118
|
return builtin(val, new Date(Date.prototype.getTime.call(val)));
|
|
117
119
|
}
|
|
118
120
|
if (val instanceof RegExp) {
|
|
119
|
-
const copy = new RegExp(
|
|
120
|
-
Reflect.get(RegExp.prototype, 'source', val) as string,
|
|
121
|
-
Reflect.get(RegExp.prototype, 'flags', val) as string,
|
|
122
|
-
);
|
|
121
|
+
const copy = new RegExp(...regExpParts(val));
|
|
123
122
|
copy.lastIndex = val.lastIndex; // the scanning cursor of g/y patterns
|
|
124
123
|
return builtin(val, copy);
|
|
125
124
|
}
|
|
@@ -406,9 +405,8 @@ function equalBuiltin(
|
|
|
406
405
|
);
|
|
407
406
|
}
|
|
408
407
|
if (a instanceof RegExp) {
|
|
409
|
-
const text = RegExp.prototype.toString;
|
|
410
408
|
return (
|
|
411
|
-
|
|
409
|
+
regExpText(a) === regExpText(b as RegExp) &&
|
|
412
410
|
a.lastIndex === (b as RegExp).lastIndex &&
|
|
413
411
|
equalExtraKeys(a, b, assumed)
|
|
414
412
|
);
|
|
@@ -473,14 +471,18 @@ const BUILTIN_PROTOS: ReadonlySet<object | null> = new Set([
|
|
|
473
471
|
]);
|
|
474
472
|
|
|
475
473
|
/**
|
|
476
|
-
*
|
|
477
|
-
*
|
|
474
|
+
* The extra keys (see extraKeys) of a built-in that is an instance of a
|
|
475
|
+
* subclass: those are part of its value. A plain built-in has none.
|
|
478
476
|
*/
|
|
477
|
+
function subclassFields(obj: object): string[] {
|
|
478
|
+
return BUILTIN_PROTOS.has(Object.getPrototypeOf(obj) as object | null)
|
|
479
|
+
? []
|
|
480
|
+
: extraKeys(obj);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** Equality of the fields (see subclassFields) of two built-ins of one class. */
|
|
479
484
|
function equalExtraKeys(a: object, b: object, assumed: Pairs): boolean {
|
|
480
|
-
return (
|
|
481
|
-
BUILTIN_PROTOS.has(Object.getPrototypeOf(a) as object | null) ||
|
|
482
|
-
equalKeys(a, b, assumed, extraKeys)
|
|
483
|
-
);
|
|
485
|
+
return equalKeys(a, b, assumed, subclassFields);
|
|
484
486
|
}
|
|
485
487
|
|
|
486
488
|
/** `assumed` tracks the pairs of objects met (see Pairs). */
|
|
@@ -619,6 +621,8 @@ export function keyChanges(
|
|
|
619
621
|
}
|
|
620
622
|
};
|
|
621
623
|
|
|
624
|
+
// Elements are compared by index, then the fields of a subclass instance
|
|
625
|
+
// (#490) like the keys of an object
|
|
622
626
|
if (Array.isArray(after)) {
|
|
623
627
|
const b = before as unknown as unknown[];
|
|
624
628
|
const a = after as unknown[];
|
|
@@ -648,14 +652,14 @@ export function keyChanges(
|
|
|
648
652
|
if (a.length > next) {
|
|
649
653
|
changes.push({ key: 'length', kind: 'holes', after: a.length - next });
|
|
650
654
|
}
|
|
651
|
-
return changes;
|
|
652
655
|
}
|
|
653
656
|
|
|
654
|
-
|
|
657
|
+
const keysOf = Array.isArray(after) ? subclassFields : Object.keys;
|
|
658
|
+
for (const key of keysOf(after)) {
|
|
655
659
|
if (hasOwn(before, key)) compare(key, before[key], after[key]);
|
|
656
660
|
else changes.push({ key, kind: 'added', after: after[key] });
|
|
657
661
|
}
|
|
658
|
-
for (const key of
|
|
662
|
+
for (const key of keysOf(before)) {
|
|
659
663
|
if (!hasOwn(after, key)) changes.push({ key, kind: 'deleted' });
|
|
660
664
|
}
|
|
661
665
|
return changes;
|
|
@@ -677,6 +681,13 @@ export type PathChange =
|
|
|
677
681
|
* holding equal content there still takes the write.
|
|
678
682
|
*/
|
|
679
683
|
alias?: true;
|
|
684
|
+
/**
|
|
685
|
+
* Set for a write a caller made explicitly (a `Story.set` of a save
|
|
686
|
+
* hook), not found by a diff: the value is the one to hold at the
|
|
687
|
+
* path, also for an array's length and for an index past its end,
|
|
688
|
+
* which a diff's changes leave to the array they were found in.
|
|
689
|
+
*/
|
|
690
|
+
absolute?: true;
|
|
680
691
|
}
|
|
681
692
|
| {
|
|
682
693
|
path: string[];
|
package/src/tooling.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
} from './code-check';
|
|
22
22
|
import {
|
|
23
23
|
widgetDefinitions as readWidgetDefinitions,
|
|
24
|
+
effectiveWidgetDefinitions,
|
|
24
25
|
type WidgetDefinition,
|
|
25
26
|
} from './widgets/widget-def';
|
|
26
27
|
import {
|
|
@@ -186,13 +187,13 @@ export function validateStoryMarkup(
|
|
|
186
187
|
// The calls of widgets that declare what their arguments hold are checked
|
|
187
188
|
const widgets = readWidgetDefinitions(list, parameterLookup(all));
|
|
188
189
|
const blocks = new Set(
|
|
189
|
-
widgets
|
|
190
|
-
.filter((def) => def.block
|
|
190
|
+
effectiveWidgetDefinitions(widgets)
|
|
191
|
+
.filter((def) => def.block)
|
|
191
192
|
.map((def) => def.name.toLowerCase()),
|
|
192
193
|
);
|
|
193
194
|
const parametersOf = parameterLookup([
|
|
194
195
|
...all,
|
|
195
|
-
...widgets
|
|
196
|
+
...definedWidgetMacros(widgets),
|
|
196
197
|
]);
|
|
197
198
|
for (const macro of all) {
|
|
198
199
|
const name = macro.name.toLowerCase();
|
|
@@ -211,6 +212,11 @@ export function validateStoryMarkup(
|
|
|
211
212
|
});
|
|
212
213
|
}
|
|
213
214
|
|
|
215
|
+
/** The widgets as macros that declare what their parameters hold. */
|
|
216
|
+
function definedWidgetMacros(widgets: readonly WidgetDefinition[]) {
|
|
217
|
+
return widgets.map((widget) => ({ name: widget.name, widget }));
|
|
218
|
+
}
|
|
219
|
+
|
|
214
220
|
/** Whether a macro takes a body, among `macros` and the built-in ones. */
|
|
215
221
|
function blockLookup(macros: readonly ToolingMacro[]) {
|
|
216
222
|
const blocks = new Set(
|
|
@@ -274,15 +280,24 @@ export function widgetDefinitions(
|
|
|
274
280
|
/** Passages tagged so hold code or styles, not markup. */
|
|
275
281
|
const NOT_MARKUP_TAGS = ['script', 'stylesheet'];
|
|
276
282
|
|
|
277
|
-
/**
|
|
278
|
-
|
|
283
|
+
/**
|
|
284
|
+
* What reading the variable references of markup depends on: the `macros`,
|
|
285
|
+
* and the widgets of the `passages` that are given (the calls of those that
|
|
286
|
+
* declare what their parameters hold are read by them).
|
|
287
|
+
*/
|
|
288
|
+
function referenceOptions(
|
|
289
|
+
macros: Iterable<ToolingMacro>,
|
|
290
|
+
all = false,
|
|
291
|
+
passages: readonly MarkupPassage[] = [],
|
|
292
|
+
) {
|
|
279
293
|
const list = [...macros];
|
|
294
|
+
const widgets = readWidgetDefinitions(passages, parameterLookup(list));
|
|
280
295
|
return {
|
|
281
296
|
all,
|
|
282
297
|
storeVarMacros: new Set(
|
|
283
298
|
list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase()),
|
|
284
299
|
),
|
|
285
|
-
parametersOf: parameterLookup(list),
|
|
300
|
+
parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
|
|
286
301
|
tolerant: true,
|
|
287
302
|
};
|
|
288
303
|
}
|
|
@@ -326,11 +341,10 @@ export function validateVariableReferences(
|
|
|
326
341
|
declarations: VariableDeclarations,
|
|
327
342
|
macros: Iterable<ToolingMacro> = [],
|
|
328
343
|
): VariableDiagnostic[] {
|
|
344
|
+
const list = [...passages];
|
|
329
345
|
return checkVariableReferences(
|
|
330
|
-
|
|
331
|
-
(p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t)),
|
|
332
|
-
),
|
|
346
|
+
list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))),
|
|
333
347
|
declarations,
|
|
334
|
-
referenceOptions(macros),
|
|
348
|
+
referenceOptions(macros, false, list),
|
|
335
349
|
);
|
|
336
350
|
}
|
package/src/transform.ts
CHANGED
|
@@ -9,19 +9,34 @@ import { variableNameError } from './utils/namespace';
|
|
|
9
9
|
|
|
10
10
|
const TRANSFORM_CACHE_MAX = 500;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The identifiers generated code reads the namespaces through, and the
|
|
14
|
+
* compiled function takes them under (see src/expression.ts). Authored
|
|
15
|
+
* JavaScript may declare identifiers of its own (`const variables = {}` in
|
|
16
|
+
* a helper), so these are ones it cannot: an identifier that starts with
|
|
17
|
+
* `_` or `$` and has more word characters after it is a sigil reference,
|
|
18
|
+
* and the lexer refuses to declare one (`__variables` is the temporary
|
|
19
|
+
* variable `_variables`, rewritten like any other). No declaration or
|
|
20
|
+
* parameter of the author's can then capture a reference or clash with the
|
|
21
|
+
* generated bindings.
|
|
22
|
+
*/
|
|
23
|
+
export const BINDINGS: Record<Sigil, string> = {
|
|
24
|
+
$: '__variables',
|
|
25
|
+
_: '__temporary',
|
|
26
|
+
'@': '__locals',
|
|
27
|
+
'%': '__transient',
|
|
17
28
|
};
|
|
18
29
|
|
|
30
|
+
/** The identifier the functions that compiled code may call come under. */
|
|
31
|
+
export const FNS_BINDING = '__fns';
|
|
32
|
+
|
|
19
33
|
/** Ends with an identifier character. */
|
|
20
34
|
const IDENT_END_RE = /[\p{ID_Continue}$\u200c\u200d]$/u;
|
|
21
35
|
|
|
22
36
|
/**
|
|
23
37
|
* Transform expression: $var → variables["var"], _var → temporary["var"],
|
|
24
|
-
* @var → locals["var"], %var → transient["var"]
|
|
38
|
+
* @var → locals["var"], %var → transient["var"], where `variables` and the
|
|
39
|
+
* others stand for the generated names in BINDINGS.
|
|
25
40
|
* The lexer (`lexJs`) finds the references: only in code (string, template
|
|
26
41
|
* and regex literal text and comments are left untouched), only where an
|
|
27
42
|
* identifier starts (not in `a$b`, nor as the property name in `obj._x`),
|
|
@@ -45,7 +60,7 @@ export function transform(expr: string, goal: JsGoal = 'expression'): string {
|
|
|
45
60
|
if (ref.shorthand) result += `${ref.name}: `;
|
|
46
61
|
// `typeof%x` needs a space once `%x` turns into an identifier.
|
|
47
62
|
else if (IDENT_END_RE.test(result.slice(-2))) result += ' ';
|
|
48
|
-
result += `${
|
|
63
|
+
result += `${BINDINGS[ref.sigil]}["${ref.name}"]`;
|
|
49
64
|
at = ref.end;
|
|
50
65
|
}
|
|
51
66
|
result += expr.slice(at);
|
package/src/utils/macro-names.ts
CHANGED
package/src/utils/namespace.ts
CHANGED
|
@@ -38,6 +38,10 @@ export function checkVariableName(name: string, label = name): void {
|
|
|
38
38
|
if (error) throw new TypeError(`spindle: ${error}`);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/** Whether a property key is an array index (`"0"`, `"12"`; not `"01"`). */
|
|
42
|
+
export const isArrayIndex = (key: string): boolean =>
|
|
43
|
+
/^(?:0|[1-9]\d*)$/.test(key);
|
|
44
|
+
|
|
41
45
|
/** Whether `obj` holds `key` as an own property. */
|
|
42
46
|
export const hasOwn = (obj: object, key: PropertyKey): boolean =>
|
|
43
47
|
Object.prototype.hasOwnProperty.call(obj, key);
|
|
@@ -111,10 +115,19 @@ export function withEntry(
|
|
|
111
115
|
/** An empty namespace that cannot change. */
|
|
112
116
|
export const EMPTY_NAMESPACE: Namespace = Object.freeze(createNamespace());
|
|
113
117
|
|
|
114
|
-
/**
|
|
118
|
+
/**
|
|
119
|
+
* The names whose own entries differ between `a` and `b`, `a`'s first: an
|
|
120
|
+
* entry one holds and the other lacks (also one holding `undefined`), or
|
|
121
|
+
* values that are not the same by SameValue (#519): an unchanged NaN is no
|
|
122
|
+
* change, 0 and -0 differ.
|
|
123
|
+
*/
|
|
115
124
|
export function changedNames(a: object, b: object): string[] {
|
|
116
125
|
const names = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
117
|
-
return [...names].filter(
|
|
126
|
+
return [...names].filter(
|
|
127
|
+
(name) =>
|
|
128
|
+
hasOwn(a, name) !== hasOwn(b, name) ||
|
|
129
|
+
!Object.is(ownValue(a, name), ownValue(b, name)),
|
|
130
|
+
);
|
|
118
131
|
}
|
|
119
132
|
|
|
120
133
|
/**
|
package/src/utils/object-path.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { current as draftState, isDraft, original } from 'immer';
|
|
2
2
|
import { registeredClassName } from '../class-registry';
|
|
3
|
-
import { hasOwn } from './namespace';
|
|
3
|
+
import { hasOwn, isArrayIndex } from './namespace';
|
|
4
4
|
import { atomicName } from './value-kinds';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -36,7 +36,7 @@ export function getByPath(obj: object, segments: readonly string[]): unknown {
|
|
|
36
36
|
|
|
37
37
|
/** Array indices ("0", "1", …) and "length": the keys an array holds. */
|
|
38
38
|
const isArrayKey = (key: string): boolean =>
|
|
39
|
-
key === 'length' ||
|
|
39
|
+
key === 'length' || isArrayIndex(key);
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* Built-ins whose content is not their properties: a property written into
|
package/src/utils/stable-key.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
isBoxed,
|
|
7
7
|
isTemporal,
|
|
8
8
|
mapEntries,
|
|
9
|
+
regExpText,
|
|
9
10
|
setMembers,
|
|
10
11
|
toStringTag,
|
|
11
12
|
} from './value-kinds';
|
|
@@ -24,7 +25,9 @@ import {
|
|
|
24
25
|
* built-ins (see isAtomic) have their contents in the key: the bytes of a
|
|
25
26
|
* buffer, the place of a view in its buffer, the text of a URL or
|
|
26
27
|
* URLSearchParams, the class, message, cause and errors of an Error, the
|
|
27
|
-
* value of a boxed primitive (#418).
|
|
28
|
+
* value of a boxed primitive (#418). As for deepEqual and a save, -0 is not
|
|
29
|
+
* 0 (#498) and a hole in an array is not an undefined element (#497), so
|
|
30
|
+
* only the keys of -0 and of arrays with holes differ from JSON.stringify.
|
|
28
31
|
*
|
|
29
32
|
* An object met again, through a cycle or a second reference to it, is a
|
|
30
33
|
* marker naming where it was first met rather than another copy of its
|
|
@@ -39,6 +42,9 @@ export function stableKey(value: unknown): string {
|
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
/** The key of an array slot that holds no element. */
|
|
46
|
+
const HOLE = '<hole>';
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* The key of `val`. `seen` numbers the objects keyed so far, in the order
|
|
44
50
|
* they were first met.
|
|
@@ -48,6 +54,8 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
48
54
|
case 'string':
|
|
49
55
|
return JSON.stringify(val);
|
|
50
56
|
case 'number':
|
|
57
|
+
// JSON.stringify and String() read -0 as 0: the key does not (#498)
|
|
58
|
+
if (Object.is(val, -0)) return '-0';
|
|
51
59
|
return Number.isFinite(val) ? JSON.stringify(val) : String(val);
|
|
52
60
|
case 'boolean':
|
|
53
61
|
return String(val);
|
|
@@ -89,16 +97,19 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
89
97
|
return withFields(
|
|
90
98
|
val instanceof Date
|
|
91
99
|
? `Date(${Date.prototype.getTime.call(val)})`
|
|
92
|
-
: `RegExp(${
|
|
100
|
+
: `RegExp(${regExpText(val)}@${val.lastIndex})`,
|
|
93
101
|
);
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
seen.set(obj, seen.size);
|
|
97
105
|
if (Array.isArray(val)) {
|
|
98
|
-
//
|
|
99
|
-
// `[,]` the key of `[]`
|
|
100
|
-
|
|
101
|
-
|
|
106
|
+
// A hole is no element, not an undefined one (#497), as for deepEqual
|
|
107
|
+
// and a save; a plain `map` would skip it, giving `[,]` the key of `[]`
|
|
108
|
+
const items: string[] = [];
|
|
109
|
+
for (let i = 0; i < val.length; i++) {
|
|
110
|
+
items.push(i in val ? keyOf(val[i], seen) : HOLE);
|
|
111
|
+
}
|
|
112
|
+
return withFields(`[${items.join(',')}]`);
|
|
102
113
|
}
|
|
103
114
|
const atomic = atomicKey(obj, seen);
|
|
104
115
|
if (atomic !== undefined) return named(atomic);
|
package/src/utils/value-kinds.ts
CHANGED
|
@@ -50,7 +50,7 @@ export function atomicName(value: object): string | undefined {
|
|
|
50
50
|
export const ERROR_HIDDEN_KEYS = ['message', 'cause', 'errors'] as const;
|
|
51
51
|
|
|
52
52
|
/** Whether `key` is an index of an array element. */
|
|
53
|
-
const isIndexKey = (key: string): boolean =>
|
|
53
|
+
export const isIndexKey = (key: string): boolean =>
|
|
54
54
|
key !== '4294967295' && String(Number(key) >>> 0) === key;
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -63,6 +63,43 @@ export function extraKeys(collection: object): string[] {
|
|
|
63
63
|
return Array.isArray(collection) ? keys.filter((k) => !isIndexKey(k)) : keys;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// A subclass of RegExp may override `source`, `flags` and the getter of each
|
|
67
|
+
// flag (`global`, `sticky`, ...), and the built-in `flags` getter reads those
|
|
68
|
+
// getters, so a pattern is read with the built-in getters one by one (#391,
|
|
69
|
+
// #491). What a RegExp holds is its source and the flags in its slots.
|
|
70
|
+
|
|
71
|
+
/** The built-in getter of each flag, with the letter `flags` lists it by. */
|
|
72
|
+
const FLAG_GETTERS: [string, (this: RegExp) => boolean][] = (
|
|
73
|
+
[
|
|
74
|
+
['hasIndices', 'd'],
|
|
75
|
+
['global', 'g'],
|
|
76
|
+
['ignoreCase', 'i'],
|
|
77
|
+
['multiline', 'm'],
|
|
78
|
+
['dotAll', 's'],
|
|
79
|
+
['unicode', 'u'],
|
|
80
|
+
['unicodeSets', 'v'],
|
|
81
|
+
['sticky', 'y'],
|
|
82
|
+
] as const
|
|
83
|
+
).flatMap(([name, letter]) => {
|
|
84
|
+
const get = Object.getOwnPropertyDescriptor(RegExp.prototype, name)?.get;
|
|
85
|
+
return get ? [[letter, get] as [string, (this: RegExp) => boolean]] : [];
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/** The pattern of a RegExp and its flags, as its slots hold them. */
|
|
89
|
+
export function regExpParts(re: RegExp): [source: string, flags: string] {
|
|
90
|
+
const source = Reflect.get(RegExp.prototype, 'source', re) as string;
|
|
91
|
+
const flags = FLAG_GETTERS.map(([letter, get]) =>
|
|
92
|
+
get.call(re) ? letter : '',
|
|
93
|
+
).join('');
|
|
94
|
+
return [source, flags];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** `/source/flags` of a RegExp, as its slots hold them (see regExpParts). */
|
|
98
|
+
export function regExpText(re: RegExp): string {
|
|
99
|
+
const [source, flags] = regExpParts(re);
|
|
100
|
+
return `/${source}/${flags}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
66
103
|
// A subclass of Map or Set may override how it is iterated, read or written
|
|
67
104
|
// (an inventory that iterates only the items on hand). What it holds is in
|
|
68
105
|
// its built-in slots, so structural operations read and write those with the
|
|
@@ -53,7 +53,9 @@ export function tokenTextContainsChildren(
|
|
|
53
53
|
/**
|
|
54
54
|
* Recursively scan an AST node array for a VariableNode
|
|
55
55
|
* with scope 'local' and name 'children' (@children), in the content and in
|
|
56
|
-
* the attributes and labels that render markup.
|
|
56
|
+
* the attributes and labels that render markup. A nested `{widget}` is not
|
|
57
|
+
* entered: its slot belongs to the innermost widget (as widgetDefinitions
|
|
58
|
+
* reads it), and a definition renders nothing of its own.
|
|
57
59
|
*/
|
|
58
60
|
export function astContainsChildren(
|
|
59
61
|
nodes: ASTNode[],
|
|
@@ -87,6 +89,7 @@ export function astContainsChildren(
|
|
|
87
89
|
}
|
|
88
90
|
}
|
|
89
91
|
if (node.type === 'macro') {
|
|
92
|
+
if (node.name.toLowerCase() === 'widget') continue;
|
|
90
93
|
const tag: Token = {
|
|
91
94
|
isClose: false,
|
|
92
95
|
nameStart: 0,
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { registerWidget } from './widget-registry';
|
|
2
2
|
import { astContainsChildren } from './ast-scanner';
|
|
3
|
-
import {
|
|
3
|
+
import type { ASTNode } from '../markup/ast';
|
|
4
4
|
import { errorMessage } from '../utils/error-message';
|
|
5
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
|
-
* block macros too
|
|
10
|
+
* block macros too (see registerWidget). A parameter
|
|
11
11
|
* that no namespace can hold or declares what it holds wrongly throws (see
|
|
12
12
|
* registerWidget and checkWidgetHolds), registering nothing.
|
|
13
13
|
*/
|
|
@@ -16,7 +16,6 @@ export function registerWidgetDef(def: WidgetDef, body: ASTNode[]): void {
|
|
|
16
16
|
const { name, params, holds } = def;
|
|
17
17
|
const isBlock = astContainsChildren(body);
|
|
18
18
|
registerWidget(name, body, params, isBlock, holds);
|
|
19
|
-
if (isBlock) registerBlockMacro(name);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
/**
|