@nodii/dev 0.6.144 → 0.6.145

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 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.144";
7
+ export const VERSION = "0.6.145";
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
@@ -496,7 +496,15 @@ export function loadDesignSystem(root, { exec = execFileSync, docs } = {}) {
496
496
  const colours = colourCatalog.map((role) => ({ name: role.name, cls: role.name.replace(/\//g, "-"), light: String(role.light ?? "").toLowerCase(), scopes: role.scopes ?? [] }));
497
497
  const edges = existsSync(join(root, TYPE_EDGES_CATALOG)) ? readJson(join(root, TYPE_EDGES_CATALOG)) : {};
498
498
  const textStyles = new Map((edges.type ?? []).filter((t) => t.figma?.style).map((t) => [t.figma.style, t.utility]));
499
- return { dsFileKey, renderer, byKey, colours, textStyles };
499
+ // Each component's optional props, from its templates' `metadata.optionalProps` (DESIGN-SYSTEM-507).
500
+ const optionalProps = new Map();
501
+ for (const doc of Array.isArray(parsed) ? parsed : []) {
502
+ const listed = optionalPropsOf(doc);
503
+ if (!doc.component || !listed.size) continue;
504
+ const set = optionalProps.get(doc.component) ?? optionalProps.set(doc.component, new Set()).get(doc.component);
505
+ for (const name of listed) set.add(name);
506
+ }
507
+ return { dsFileKey, renderer, byKey, colours, textStyles, optionalProps };
500
508
  }
501
509
 
502
510
  /** `figma connect parse`, the repository's own Code Connect compile, into memory. */
@@ -532,13 +540,15 @@ export function emitSurface(tree, row, ds) {
532
540
  const layers = new Map();
533
541
  // Each list's columns by name, across views (listScope).
534
542
  const shapes = new Map();
543
+ // The marked identifiers a template lists as optional, and the ones it doesn't (DESIGN-SYSTEM-507).
544
+ const optionals = { optional: new Set(), required: new Set() };
535
545
  const views = tree.variants.map((variant) => {
536
546
  let state = variant.state;
537
547
  while (used.has(state)) state += "-x";
538
548
  used.add(state);
539
549
  const fn = `${pascal(state)}View`;
540
550
  const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
541
- const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, row, ds, imports, note, root: true, scopes, layers, layer: "", state, lists: new Map(), shapes, handlers: new Map() }) ?? "";
551
+ const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, row, ds, imports, note, root: true, scopes, layers, layer: "", state, lists: new Map(), shapes, handlers: new Map(), optionals }) ?? "";
542
552
  // A view the generator can't lay out is no page at all: generation fails rather than ship
543
553
  // an empty view (NODII-DEV-CLI-81).
544
554
  if (emitted.trimStart().startsWith("{/*")) {
@@ -549,8 +559,15 @@ export function emitSurface(tree, row, ds) {
549
559
  const component = `${pascal(row.id)}Surface`;
550
560
  const stateType = `${component}State`;
551
561
  const propsType = `${component}Props`;
552
- resolveScopes(views, scopes, layers);
562
+ const named = resolveScopes(views, scopes, layers);
553
563
  const data = dataProps(views, propsType);
564
+ // A prop is optional when every template that draws it lists it in `metadata.optionalProps`.
565
+ const optionalNames = new Map();
566
+ for (const [token, name] of named) {
567
+ if (optionals.optional.has(token)) optionalNames.set(name, optionalNames.get(name) ?? true);
568
+ else if (optionals.required.has(token)) optionalNames.set(name, false);
569
+ }
570
+ for (const [name, optional] of optionalNames) if (optional && data.has(name)) data.get(name).optional = true;
554
571
  const reactTypes = [data.size && "ComponentProps", [...data.values()].some((d) => /\bReactNode\b/.test(propType(d))) && "ReactNode"].filter(Boolean);
555
572
  const drawn = [...data.values()].some((d) => [...d.attrs.values(), ...[...d.members.values()].flatMap((refs) => [...refs.values()])].some((r) => r.keys));
556
573
  const header = [
@@ -1216,6 +1233,18 @@ function titleBefore(target, chain) {
1216
1233
  return null;
1217
1234
  }
1218
1235
 
1236
+ /**
1237
+ * The identifiers a template lists as optional (`metadata: { optionalProps: ["type", "error"] }`):
1238
+ * the props a page need not pass, so a page already adopted still typechecks when a template
1239
+ * gains one (DESIGN-SYSTEM-507). Read from the template's own source; empty when it lists none.
1240
+ */
1241
+ function optionalPropsOf(template) {
1242
+ // `figma connect parse` keeps a template's metadata on its doc; its source is the fallback.
1243
+ if (Array.isArray(template?.metadata?.optionalProps)) return new Set(template.metadata.optionalProps.filter((n) => typeof n === "string"));
1244
+ const list = /\boptionalProps\s*:\s*\[([^\]]*)\]/.exec(template?.template ?? "")?.[1];
1245
+ return new Set(list ? [...list.matchAll(/["'`]([A-Za-z_$][\w$]*)["'`]/g)].map((m) => m[1]) : []);
1246
+ }
1247
+
1219
1248
  /** A Checkbox or Switch: on or off is the page's, never Figma's sample (DESIGN-SYSTEM-508, hub-468). */
1220
1249
  const TOGGLE = /^<(?:Checkbox|Switch)\b/;
1221
1250
 
@@ -1443,6 +1472,7 @@ function resolveScopes(views, scopes, layers = new Map()) {
1443
1472
  });
1444
1473
  }
1445
1474
  for (const view of views) view.body = view.body.replace(marked, (token) => named.get(token));
1475
+ return named;
1446
1476
  }
1447
1477
 
1448
1478
  /** `scope` then `name` in camelCase, a word they share at the join said once (`recordSwitcherScope` + `ScopeValue`). */
@@ -1942,6 +1972,21 @@ function emitNode(node, ctx) {
1942
1972
  own = nameHandlers(own, node, ctx, path, scope);
1943
1973
  own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
1944
1974
  const code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes, ctx.layer) : own;
1975
+ if (ctx.optionals && ctx.scopes) {
1976
+ // Each identifier is its drawing tag's: the outer template's own list, or, for a part it
1977
+ // draws through executeTemplate (a Field's Input), that part's template's (DESIGN-SYSTEM-507).
1978
+ const outer = optionalPropsOf(rendered.template);
1979
+ const outerTag = /^<([A-Z][\w.]*)/.exec(own)?.[1];
1980
+ const mine = new Set([...own.matchAll(/__s\d+_[a-z][A-Za-z0-9]*/g)].map((m) => m[0]));
1981
+ for (const m of code.matchAll(/__s\d+_([a-z][A-Za-z0-9]*)/g)) {
1982
+ if (mine.has(m[0])) continue;
1983
+ // From before the `{` that holds it (`type={…}`), so that brace isn't counted.
1984
+ const start = code[m.index - 1] === "{" ? openingTag(code, m.index - 1) : null;
1985
+ const tag = start === null ? outerTag : /^<([A-Z][\w.]*)/.exec(code.slice(start))?.[1];
1986
+ const listed = tag && tag !== outerTag ? (ds.optionalProps?.get(tag) ?? new Set()) : outer;
1987
+ (listed.has(m[1]) ? ctx.optionals.optional : ctx.optionals.required).add(m[0]);
1988
+ }
1989
+ }
1945
1990
  // A Table part is valid only inside the DS Table. One the generator could not assemble
1946
1991
  // into a Table stays reported, never passed off as mapped (MW8-17.3).
1947
1992
  const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodii/dev",
3
- "version": "0.6.144",
3
+ "version": "0.6.145",
4
4
  "description": "Agent CLI for Nodii Dev, reached through the platform edge",
5
5
  "type": "module",
6
6
  "bin": {