@nodii/dev 0.6.121 → 0.6.122
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/dist/core.js +1 -1
- package/dist/surface.js +227 -26
- package/dist/vendor/render-templates.mjs +7 -4
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { homedir, platform } from "node:os";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
|
|
7
|
-
export const VERSION = "0.6.
|
|
7
|
+
export const VERSION = "0.6.122";
|
|
8
8
|
export const configPath = () =>
|
|
9
9
|
process.env.NODII_DEV_CONFIG_PATH ??
|
|
10
10
|
join(homedir(), ".config/nodii-dev/config.json");
|
package/dist/surface.js
CHANGED
|
@@ -32,7 +32,7 @@ import { CliError, VERSION, devRequest, readConfig } from "./core.js";
|
|
|
32
32
|
import { createRenderer } from "./vendor/render-templates.mjs";
|
|
33
33
|
|
|
34
34
|
/** Bumped whenever the emitted code changes shape for the same inputs. */
|
|
35
|
-
export const GENERATOR_FORMAT =
|
|
35
|
+
export const GENERATOR_FORMAT = 8;
|
|
36
36
|
export const TREE_DIR = "docs/figma-ux-ds/figma/surface-trees";
|
|
37
37
|
export const GENERATED_SUFFIX = ".surface.generated.tsx";
|
|
38
38
|
/** Where every generated product page lives, as a git glob (NODII-DEV-CLI-77). */
|
|
@@ -324,12 +324,33 @@ export function trimTree(entry, { row, version }) {
|
|
|
324
324
|
|
|
325
325
|
if (node.type === "INSTANCE") {
|
|
326
326
|
const main = components[node.componentId] ?? {};
|
|
327
|
-
|
|
328
|
-
|
|
327
|
+
// An instance's own layers, down to (not into) its slots, which are the Surface's.
|
|
328
|
+
const layersOf = function* (parent) {
|
|
329
|
+
for (const child of parent.children ?? []) {
|
|
330
|
+
if (child.type === "SLOT") continue;
|
|
331
|
+
yield child;
|
|
332
|
+
yield* layersOf(child);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
// A swap keeps the swapped instance's own property values: a Field's
|
|
336
|
+
// control is a swap, and its typed value is an override on that instance, not the default.
|
|
337
|
+
const propsOf = (instance) => {
|
|
338
|
+
const swapped = new Map();
|
|
339
|
+
for (const layer of layersOf(instance)) {
|
|
340
|
+
const ref = layer.type === "INSTANCE" ? layer.componentPropertyReferences?.mainComponent : null;
|
|
341
|
+
if (ref && !swapped.has(ref)) swapped.set(ref, layer);
|
|
342
|
+
}
|
|
343
|
+
return Object.fromEntries(
|
|
329
344
|
Object.entries(instance.componentProperties ?? {})
|
|
330
345
|
.filter(([, p]) => p.type !== "SLOT")
|
|
331
|
-
.map(([name, p]) =>
|
|
346
|
+
.map(([name, p]) => {
|
|
347
|
+
if (p.type !== "INSTANCE_SWAP") return [stripPropId(name), p.value];
|
|
348
|
+
const layer = swapped.get(name);
|
|
349
|
+
const own = layer?.componentProperties && keyOf(layer.componentId) === keyOf(p.value) ? propsOf(layer) : null;
|
|
350
|
+
return [stripPropId(name), own && Object.keys(own).length ? { swap: keyOf(p.value), props: own } : { swap: keyOf(p.value) }];
|
|
351
|
+
}),
|
|
332
352
|
);
|
|
353
|
+
};
|
|
333
354
|
out.instance = {
|
|
334
355
|
key: main.key ?? null,
|
|
335
356
|
component: main.name ?? null,
|
|
@@ -337,17 +358,18 @@ export function trimTree(entry, { row, version }) {
|
|
|
337
358
|
remote: Boolean(main.remote),
|
|
338
359
|
props: propsOf(node),
|
|
339
360
|
};
|
|
340
|
-
//
|
|
341
|
-
//
|
|
342
|
-
|
|
361
|
+
// A nested instance the Surface sets (an exposed one, or one whose properties it
|
|
362
|
+
// overrides, as a Tabs Group's tab1..tab6 labels) is read only by a template's
|
|
363
|
+
// findInstance, so its properties are kept by layer name (DESIGN-SYSTEM-324).
|
|
364
|
+
if (main.remote) {
|
|
365
|
+
const overridden = new Set((node.overrides ?? []).filter((o) => o.overriddenFields?.length).map((o) => o.id));
|
|
343
366
|
const exposed = {};
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
367
|
+
for (const layer of layersOf(node)) {
|
|
368
|
+
if (layer.type !== "INSTANCE" || exposed[layer.name]) continue;
|
|
369
|
+
if (layer.isExposedInstance || overridden.has(layer.id) || layer.overrides?.some((o) => o.overriddenFields?.length)) {
|
|
370
|
+
exposed[layer.name] = { key: keyOf(layer.componentId), props: propsOf(layer) };
|
|
348
371
|
}
|
|
349
|
-
}
|
|
350
|
-
walk(node);
|
|
372
|
+
}
|
|
351
373
|
if (Object.keys(exposed).length) out.instance.exposed = exposed;
|
|
352
374
|
}
|
|
353
375
|
// A library instance is the template's to draw: only its slots' content is the Surface's.
|
|
@@ -467,13 +489,14 @@ export function emitSurface(tree, row, ds) {
|
|
|
467
489
|
throw new CliError("SURFACE_TREE_STALE", `${row.id}: the committed tree names its variants by node id; refetch it with \`nodii-dev surface generate ${row.componentSetKey} --fetch --write\``, 1);
|
|
468
490
|
}
|
|
469
491
|
const used = new Set();
|
|
492
|
+
const scopes = new Map();
|
|
470
493
|
const views = tree.variants.map((variant) => {
|
|
471
494
|
let state = variant.state;
|
|
472
495
|
while (used.has(state)) state += "-x";
|
|
473
496
|
used.add(state);
|
|
474
497
|
const fn = `${pascal(state)}View`;
|
|
475
498
|
const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
|
|
476
|
-
const emitted = emitNode(variant.root, { depth: 2, parent: null, path: variant.name, ds, imports, note, root: true }) ?? "";
|
|
499
|
+
const emitted = emitNode(variant.root, { depth: 2, parent: null, path: variant.name, ds, imports, note, root: true, scopes }) ?? "";
|
|
477
500
|
// A refused root is only a comment, and `return ({/* … */})` is an object, not JSX.
|
|
478
501
|
const body = emitted.trimStart().startsWith("{/*") ? [" <>", indent(emitted.trim(), 3), " </>"].join("\n") : emitted;
|
|
479
502
|
return { state, fn, body };
|
|
@@ -481,8 +504,10 @@ export function emitSurface(tree, row, ds) {
|
|
|
481
504
|
const component = `${pascal(row.id)}Surface`;
|
|
482
505
|
const stateType = `${component}State`;
|
|
483
506
|
const propsType = `${component}Props`;
|
|
507
|
+
resolveScopes(views, scopes);
|
|
484
508
|
const data = dataProps(views);
|
|
485
509
|
const reactTypes = [data.size && "ComponentProps", [...data.values()].some((d) => d.child) && "ReactNode"].filter(Boolean);
|
|
510
|
+
const drawn = [...data.values()].some((d) => [...d.attrs.values(), ...[...d.members.values()].flatMap((refs) => [...refs.values()])].some((r) => r.keys));
|
|
486
511
|
const header = [
|
|
487
512
|
"// GENERATED by `nodii-dev surface generate` from Code Connect (MW8-17). Do not edit:",
|
|
488
513
|
`// regenerate with \`nodii-dev surface generate ${row.componentSetKey} --write\`.`,
|
|
@@ -495,6 +520,7 @@ export function emitSurface(tree, row, ds) {
|
|
|
495
520
|
"",
|
|
496
521
|
`export type ${stateType} = ${views.map((v) => JSON.stringify(v.state)).join(" | ")};`,
|
|
497
522
|
"",
|
|
523
|
+
...(drawn ? [...DRAWN_TYPE, ""] : []),
|
|
498
524
|
`export const ${pascal(row.id).replace(/^./, (c) => c.toLowerCase())}SurfaceStates: readonly ${stateType}[] = [${views.map((v) => JSON.stringify(v.state)).join(", ")}];`,
|
|
499
525
|
"",
|
|
500
526
|
...(data.size
|
|
@@ -527,20 +553,79 @@ export function emitSurface(tree, row, ds) {
|
|
|
527
553
|
*/
|
|
528
554
|
const JS_LITERALS = new Set(["true", "false", "null", "undefined", "this"]);
|
|
529
555
|
|
|
556
|
+
/** Strings blanked to spaces of the same length, so a position still points into `code`. */
|
|
557
|
+
const maskStrings = (code) => code.replace(/"(?:[^"\\\n]|\\.)*"|`[^`]*`/g, (s) => `"${" ".repeat(s.length - 2)}"`);
|
|
558
|
+
|
|
559
|
+
/** Every free identifier a template emits in `code`: [{ at, name }], in the three forms dataProps reads. */
|
|
560
|
+
function freeIdentifiers(code) {
|
|
561
|
+
const masked = maskStrings(code);
|
|
562
|
+
const found = new Map();
|
|
563
|
+
const add = (at, name) => {
|
|
564
|
+
if (!JS_LITERALS.has(name)) found.set(at, name);
|
|
565
|
+
};
|
|
566
|
+
for (const m of masked.matchAll(/([A-Za-z][\w-]*)=\{([a-z][A-Za-z0-9]*)(?:\.[A-Za-z][A-Za-z0-9]*)?\}/g)) add(m.index + m[1].length + 2, m[2]);
|
|
567
|
+
for (const m of masked.matchAll(/(^|>)(\s*)\{([a-z][A-Za-z0-9]*)\}/gm)) add(m.index + m[1].length + m[2].length + 1, m[3]);
|
|
568
|
+
for (const m of masked.matchAll(/\b[A-Za-z_]\w*:\s*([a-z][A-Za-z0-9]*)(?=\s*[,}\]])/g)) add(m.index + m[0].length - m[1].length, m[1]);
|
|
569
|
+
return [...found.entries()].map(([at, name]) => ({ at, name })).sort((a, b) => a.at - b.at);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* The name a DS instance's data is grouped under: its label
|
|
574
|
+
* or title property, else its layer name, in camelCase (`Subject kind` →
|
|
575
|
+
* `subjectKind`).
|
|
576
|
+
*/
|
|
577
|
+
function scopeName(node) {
|
|
578
|
+
const props = node.instance?.props ?? {};
|
|
579
|
+
const text = [props.label, props.title].find((v) => typeof v === "string" && /[A-Za-z0-9]/.test(v)) ?? node.name ?? "";
|
|
580
|
+
const words = text.match(/[A-Za-z0-9]+/g) ?? [];
|
|
581
|
+
const name = words.map((w, i) => (i === 0 ? w.toLowerCase() : w[0].toUpperCase() + w.slice(1).toLowerCase())).join("");
|
|
582
|
+
return /^[a-z]/.test(name) ? name : `field${name.replace(/^./, (c) => c.toUpperCase())}`;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Marks each free identifier in one instance's code with its instance's
|
|
587
|
+
* scope (`__s<n>_date`); resolveScopes later keeps the plain name or scopes it.
|
|
588
|
+
* An identifier already marked (an inner instance's, in a slot) keeps its own.
|
|
589
|
+
*/
|
|
590
|
+
function scopeIdentifiers(code, scope, scopes) {
|
|
591
|
+
if (!scopes.has(scope)) scopes.set(scope, scopes.size);
|
|
592
|
+
const n = scopes.get(scope);
|
|
593
|
+
let out = code;
|
|
594
|
+
for (const { at, name } of freeIdentifiers(code).reverse()) out = `${out.slice(0, at)}__s${n}_${name}${out.slice(at + name.length)}`;
|
|
595
|
+
return out;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Each identifier two or more instances emit becomes one per instance: a
|
|
600
|
+
* Date Picker's `date` and `setDate` under "Expires" become
|
|
601
|
+
* `expiresDate` and `setExpiresDate`, so two inputs never share a value, a list
|
|
602
|
+
* of options or a setter. An identifier one instance alone emits keeps its name.
|
|
603
|
+
*/
|
|
604
|
+
function resolveScopes(views, scopes) {
|
|
605
|
+
const names = [...scopes.keys()];
|
|
606
|
+
const marked = /__s(\d+)_([a-z][A-Za-z0-9]*)/g;
|
|
607
|
+
const users = new Map();
|
|
608
|
+
for (const view of views) for (const m of view.body.matchAll(marked)) (users.get(m[2]) ?? users.set(m[2], new Set()).get(m[2])).add(m[1]);
|
|
609
|
+
const scoped = (scope, name) => {
|
|
610
|
+
const cap = (s) => s[0].toUpperCase() + s.slice(1);
|
|
611
|
+
const m = /^(set|on)([A-Z]\w*)$/.exec(name);
|
|
612
|
+
return m ? `${m[1]}${cap(scope)}${m[2]}` : `${scope}${cap(name)}`;
|
|
613
|
+
};
|
|
614
|
+
for (const view of views) view.body = view.body.replace(marked, (_, n, name) => (users.get(name).size > 1 ? scoped(names[Number(n)], name) : name));
|
|
615
|
+
}
|
|
616
|
+
|
|
530
617
|
function dataProps(views) {
|
|
531
618
|
const data = new Map();
|
|
532
|
-
const entry = (name) => data.get(name) ?? data.set(name, { attrs: new
|
|
619
|
+
const entry = (name) => data.get(name) ?? data.set(name, { attrs: new Map(), child: false, nested: false, members: new Map() }).get(name);
|
|
533
620
|
for (const view of views) {
|
|
534
621
|
view.uses = new Set();
|
|
535
622
|
const body = view.body;
|
|
536
623
|
for (const m of body.matchAll(/([A-Za-z][\w-]*)=\{([a-z][A-Za-z0-9]*)(?:\.([A-Za-z][A-Za-z0-9]*))?\}/g)) {
|
|
537
624
|
if (JS_LITERALS.has(m[2])) continue;
|
|
538
|
-
const
|
|
539
|
-
if (
|
|
540
|
-
const ref = `ComponentProps<typeof ${tag}>[${JSON.stringify(m[1])}]`;
|
|
625
|
+
const start = openingTag(body, m.index);
|
|
626
|
+
if (start === null) continue;
|
|
541
627
|
const d = entry(m[2]);
|
|
542
|
-
|
|
543
|
-
else d.attrs.add(ref);
|
|
628
|
+
addDrawn(m[3] ? (d.members.get(m[3]) ?? d.members.set(m[3], new Map()).get(m[3])) : d.attrs, drawnProps(body, start), m[1]);
|
|
544
629
|
view.uses.add(m[2]);
|
|
545
630
|
}
|
|
546
631
|
for (const m of body.matchAll(/(^|>)\s*\{([a-z][A-Za-z0-9]*)\}/gm)) {
|
|
@@ -560,7 +645,7 @@ function dataProps(views) {
|
|
|
560
645
|
return data;
|
|
561
646
|
}
|
|
562
647
|
|
|
563
|
-
/**
|
|
648
|
+
/** Where the opening tag of the component holding position `at` starts, walking back over balanced braces. */
|
|
564
649
|
function openingTag(body, at) {
|
|
565
650
|
let depth = 0;
|
|
566
651
|
for (let i = at - 1; i >= 0; i--) {
|
|
@@ -568,15 +653,126 @@ function openingTag(body, at) {
|
|
|
568
653
|
if (c === "}") depth++;
|
|
569
654
|
else if (c === "{") depth--;
|
|
570
655
|
else if (depth === 0 && c === ">" && body[i - 1] !== "=") return null;
|
|
571
|
-
else if (depth === 0 && c === "<") return /^<
|
|
656
|
+
else if (depth === 0 && c === "<") return /^<[A-Z]/.test(body.slice(i, i + 2)) ? i : null;
|
|
657
|
+
}
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* The props of the component whose opening tag starts at `start`, narrowed to
|
|
663
|
+
* the member of its props union this tag draws: a DS
|
|
664
|
+
* component whose props are a union (DatePicker single | range) types a data
|
|
665
|
+
* prop as that member's, not the union's. The tag's attribute names (and
|
|
666
|
+
* `children` unless it closes itself) are what it passes; a literal attribute
|
|
667
|
+
* (`mode="range"`, a bare `showReject`, `={false}`) is what it picks. A tag that
|
|
668
|
+
* spreads props is not narrowed.
|
|
669
|
+
*/
|
|
670
|
+
function drawnProps(body, start) {
|
|
671
|
+
const tag = /^<([A-Z][\w.]*)/.exec(body.slice(start))[1];
|
|
672
|
+
const attrs = tagAttributes(body, start + tag.length + 1);
|
|
673
|
+
if (!attrs) return { tag, keys: null, literals: null };
|
|
674
|
+
return { tag, keys: new Set([...attrs.names, ...(attrs.selfClosing ? [] : ["children"])]), literals: attrs.literals };
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* One prop read through the same component's same attribute at several tags
|
|
679
|
+
* (a PiiText in every row, a page-size select in every view) is one reference:
|
|
680
|
+
* the keys every tag passes and the literals every tag gives alike.
|
|
681
|
+
*/
|
|
682
|
+
function addDrawn(refs, drawn, attr) {
|
|
683
|
+
const id = `${drawn.tag}\u0000${attr}`;
|
|
684
|
+
const had = refs.get(id);
|
|
685
|
+
if (!had) return refs.set(id, { ...drawn, attr });
|
|
686
|
+
if (!had.keys || !drawn.keys) return refs.set(id, { tag: drawn.tag, attr, keys: null, literals: null });
|
|
687
|
+
refs.set(id, {
|
|
688
|
+
tag: drawn.tag,
|
|
689
|
+
attr,
|
|
690
|
+
keys: new Set([...had.keys].filter((k) => drawn.keys.has(k))),
|
|
691
|
+
literals: new Map([...had.literals].filter(([k, v]) => drawn.literals.get(k) === v)),
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** A prop's type: `Drawn<ComponentProps<typeof Tag>, keys, literals>["attr"]`, or the plain props when unnarrowed. */
|
|
696
|
+
function drawnRef({ tag, attr, keys, literals }) {
|
|
697
|
+
const props = `ComponentProps<typeof ${tag}>`;
|
|
698
|
+
if (!keys) return `${props}[${JSON.stringify(attr)}]`;
|
|
699
|
+
const lits = [...literals.entries()].sort(([a], [b]) => a.localeCompare(b));
|
|
700
|
+
const lit = lits.length ? `, { ${lits.map(([k, v]) => `${/^[A-Za-z_$][\w$]*$/.test(k) ? k : JSON.stringify(k)}: ${v}`).join("; ")} }` : "";
|
|
701
|
+
return `Drawn<${props}, ${keys.size ? [...keys].sort().map((k) => JSON.stringify(k)).join(" | ") : "never"}${lit}>[${JSON.stringify(attr)}]`;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/** The type the generated file declares for drawnProps (see there). */
|
|
705
|
+
const DRAWN_TYPE = [
|
|
706
|
+
"/** A DS component's props, narrowed to the member of their union this call draws: every required prop passed, every literal one fitting. */",
|
|
707
|
+
"type Drawn<P, K extends PropertyKey, L = {}> = [DrawnOf<P, K, L>] extends [never] ? P : DrawnOf<P, K, L>;",
|
|
708
|
+
"type DrawnOf<P, K extends PropertyKey, L> = P extends unknown",
|
|
709
|
+
" ? [Exclude<{ [k in keyof P]-?: {} extends Pick<P, k> ? never : k }[keyof P], K>] extends [never]",
|
|
710
|
+
" ? [{ [k in keyof L]: k extends keyof P ? (L[k] extends P[k] ? never : k) : never }[keyof L]] extends [never]",
|
|
711
|
+
" ? P",
|
|
712
|
+
" : never",
|
|
713
|
+
" : never",
|
|
714
|
+
" : never;",
|
|
715
|
+
];
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* An opening tag's attributes from `at` (just after its name) to its `>`:
|
|
719
|
+
* every name, the literal ones' values as TS literal types, and whether it
|
|
720
|
+
* closes itself. Null for a spread or a tag it cannot read.
|
|
721
|
+
*/
|
|
722
|
+
function tagAttributes(body, at) {
|
|
723
|
+
const names = [];
|
|
724
|
+
const literals = new Map();
|
|
725
|
+
let i = at;
|
|
726
|
+
const skipString = (q) => {
|
|
727
|
+
for (i++; i < body.length && body[i] !== q; i++) if (body[i] === "\\") i++;
|
|
728
|
+
i++;
|
|
729
|
+
};
|
|
730
|
+
const braced = () => {
|
|
731
|
+
const from = i;
|
|
732
|
+
let depth = 0;
|
|
733
|
+
for (; i < body.length; i++) {
|
|
734
|
+
const c = body[i];
|
|
735
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
736
|
+
skipString(c);
|
|
737
|
+
i--;
|
|
738
|
+
} else if (c === "{") depth++;
|
|
739
|
+
else if (c === "}" && --depth === 0) return body.slice(from + 1, i++);
|
|
740
|
+
}
|
|
741
|
+
return null;
|
|
742
|
+
};
|
|
743
|
+
while (i < body.length) {
|
|
744
|
+
while (/\s/.test(body[i] ?? "")) i++;
|
|
745
|
+
if (body.startsWith("/>", i)) return { names, literals, selfClosing: true };
|
|
746
|
+
if (body[i] === ">") return { names, literals, selfClosing: false };
|
|
747
|
+
if (body[i] === "{") return null;
|
|
748
|
+
const name = /^[A-Za-z_][\w:-]*/.exec(body.slice(i))?.[0];
|
|
749
|
+
if (!name) return null;
|
|
750
|
+
names.push(name);
|
|
751
|
+
i += name.length;
|
|
752
|
+
if (body[i] !== "=") {
|
|
753
|
+
literals.set(name, "true");
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
i++;
|
|
757
|
+
if (body[i] === '"' || body[i] === "'") {
|
|
758
|
+
const from = i;
|
|
759
|
+
skipString(body[i]);
|
|
760
|
+
literals.set(name, JSON.stringify(body.slice(from + 1, i - 1)));
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
if (body[i] !== "{") return null;
|
|
764
|
+
const inner = braced();
|
|
765
|
+
if (inner === null) return null;
|
|
766
|
+
const value = inner.trim();
|
|
767
|
+
if (/^(true|false|-?\d+(\.\d+)?|"(?:[^"\\\n]|\\.)*")$/.test(value)) literals.set(name, value);
|
|
572
768
|
}
|
|
573
769
|
return null;
|
|
574
770
|
}
|
|
575
771
|
|
|
576
772
|
const propType = (d) => {
|
|
577
773
|
// A value nested in an object prop has no ComponentProps path the generator can name.
|
|
578
|
-
const parts = [...d.attrs, ...(d.child ? ["ReactNode"] : []), ...(d.nested ? ["any"] : [])];
|
|
579
|
-
if (d.members.size) parts.push(`{ ${[...d.members.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([k, refs]) => `${k}: ${[...refs].join(" & ")}`).join("; ")} }`);
|
|
774
|
+
const parts = [...[...d.attrs.values()].map(drawnRef), ...(d.child ? ["ReactNode"] : []), ...(d.nested ? ["any"] : [])];
|
|
775
|
+
if (d.members.size) parts.push(`{ ${[...d.members.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([k, refs]) => `${k}: ${[...refs.values()].map(drawnRef).join(" & ")}`).join("; ")} }`);
|
|
580
776
|
return parts.join(" & ");
|
|
581
777
|
};
|
|
582
778
|
|
|
@@ -691,8 +887,13 @@ function emitNode(node, ctx) {
|
|
|
691
887
|
// A DS library instance: exactly what its Code Connect template emits for this instance's properties.
|
|
692
888
|
if (node.instance?.remote) {
|
|
693
889
|
const dsNode = node.instance.key ? ds.byKey.get(node.instance.key) : null;
|
|
890
|
+
// A swap is the swapped component's node id, with its own values when the Surface set any.
|
|
891
|
+
const swapOf = (value) => {
|
|
892
|
+
const nodeId = ds.byKey.get(value.swap) ?? value.swap;
|
|
893
|
+
return value.props ? { nodeId, values: valuesOf(value.props) } : nodeId;
|
|
894
|
+
};
|
|
694
895
|
const valuesOf = (props) =>
|
|
695
|
-
Object.fromEntries(Object.entries(props).map(([name, value]) => [name, value && typeof value === "object" && "swap" in value ? (
|
|
896
|
+
Object.fromEntries(Object.entries(props).map(([name, value]) => [name, value && typeof value === "object" && "swap" in value ? swapOf(value) : value]));
|
|
696
897
|
const values = valuesOf(node.instance.props);
|
|
697
898
|
const nested = Object.fromEntries(
|
|
698
899
|
Object.entries(node.instance.exposed ?? {})
|
|
@@ -718,7 +919,7 @@ function emitNode(node, ctx) {
|
|
|
718
919
|
}
|
|
719
920
|
ctx.imports.push(...rendered.imports);
|
|
720
921
|
const sizing = ctx.bare ? [] : sizeClasses(node, parent, false);
|
|
721
|
-
const code = rendered.code.trim();
|
|
922
|
+
const code = ctx.scopes ? scopeIdentifiers(rendered.code.trim(), scopeName(node), ctx.scopes) : rendered.code.trim();
|
|
722
923
|
// A Table part is valid only inside the DS Table. One the generator could not assemble
|
|
723
924
|
// into a Table stays reported, never passed off as mapped (MW8-17.3).
|
|
724
925
|
const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
|
|
@@ -318,8 +318,11 @@ export function createRenderer(root, docs, options = {}) {
|
|
|
318
318
|
getEnum: (name, options) =>
|
|
319
319
|
mapValue(name, options, need(name, "VARIANT").value),
|
|
320
320
|
getInstanceSwap: (name) => {
|
|
321
|
-
|
|
322
|
-
|
|
321
|
+
// A swap is a node id, or { nodeId, values } when the caller has the
|
|
322
|
+
// swapped instance's own property values (renderInstance's `values`).
|
|
323
|
+
const value = need(name, "INSTANCE_SWAP").value;
|
|
324
|
+
const swap = value && typeof value === "object" ? value : { nodeId: value };
|
|
325
|
+
return swapHandle(context, String(swap.nodeId), depth, label, swap.values);
|
|
323
326
|
},
|
|
324
327
|
getSlot: (name) => {
|
|
325
328
|
need(name, "SLOT");
|
|
@@ -344,7 +347,7 @@ export function createRenderer(root, docs, options = {}) {
|
|
|
344
347
|
};
|
|
345
348
|
}
|
|
346
349
|
|
|
347
|
-
function swapHandle(context, targetId, depth, label) {
|
|
350
|
+
function swapHandle(context, targetId, depth, label, values = {}) {
|
|
348
351
|
const { byId } = context;
|
|
349
352
|
const found = docForComponent(context, targetId);
|
|
350
353
|
const node = byId.get(targetId);
|
|
@@ -376,7 +379,7 @@ export function createRenderer(root, docs, options = {}) {
|
|
|
376
379
|
makeInstance(
|
|
377
380
|
context,
|
|
378
381
|
setNode,
|
|
379
|
-
propsFor(setNode, found.node,
|
|
382
|
+
propsFor(setNode, found.node, values),
|
|
380
383
|
depth + 1,
|
|
381
384
|
`${label} > ${setNode.name}`
|
|
382
385
|
)
|