@nodii/dev 0.6.143 → 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.143";
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 = [
@@ -1091,6 +1108,9 @@ function nameHandlers(code, node, ctx, path, scope) {
1091
1108
  });
1092
1109
  }
1093
1110
 
1111
+ /** A label that is only an arrow glyph names the step it takes ("‹" is Previous, "›" is Next; hub-464). */
1112
+ const GLYPH_WORDS = [[/^[‹«←⟨]+$/, "Previous"], [/^[›»→⟩]+$/, "Next"], [/^[↑]+$/, "Up"], [/^[↓]+$/, "Down"]];
1113
+
1094
1114
  /**
1095
1115
  * The prop that hides a button the page shows only sometimes (a permission, a
1096
1116
  * state): `show<what it does>`, the words and the place of its own handler
@@ -1120,7 +1140,8 @@ const shownWhen = (condition, drawn, depth) => [indent(`{${condition} && (`, dep
1120
1140
 
1121
1141
  /** Text as a handler's words: no figure, at most five words, in camelCase; null when nothing is left. */
1122
1142
  function handlerWords(text) {
1123
- const words = (String(text ?? "").match(/[A-Za-z0-9]+/g) ?? []).filter((w) => /[A-Za-z]/.test(w)).slice(0, 5);
1143
+ const glyph = GLYPH_WORDS.find(([re]) => re.test(String(text ?? "").trim()))?.[1];
1144
+ const words = (String(glyph ?? text ?? "").match(/[A-Za-z0-9]+/g) ?? []).filter((w) => /[A-Za-z]/.test(w)).slice(0, 5);
1124
1145
  return words.length ? camelName(words.join(" ")) : null;
1125
1146
  }
1126
1147
 
@@ -1149,7 +1170,8 @@ function handlerLabel(tag, attrs, after, node) {
1149
1170
  }
1150
1171
  if (/\/\s*$/.test(attrs)) return null;
1151
1172
  const close = after.indexOf(`</${tag}>`);
1152
- const inner = close === -1 ? "" : after.slice(0, close);
1173
+ // `after` starts with the `>` that closes the opening tag; it is not the button's text.
1174
+ const inner = close === -1 ? "" : after.slice(after.startsWith(">") ? 1 : 0, close);
1153
1175
  const strings = [...inner.matchAll(/\{("(?:[^"\\\n]|\\.)*")\}/g)].map((m) => JSON.parse(m[1]));
1154
1176
  const bare = inner.replace(/<[^>]*>/g, " ").replace(/\{[^{}]*\}/g, " ").trim();
1155
1177
  return handlerWords(strings.length ? strings.join(" ") : bare) ?? (node ? handlerWords(node.name.replace(/^\s*button\b[\s·:\-–]*/i, "")) : null);
@@ -1211,6 +1233,18 @@ function titleBefore(target, chain) {
1211
1233
  return null;
1212
1234
  }
1213
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
+
1214
1248
  /** A Checkbox or Switch: on or off is the page's, never Figma's sample (DESIGN-SYSTEM-508, hub-468). */
1215
1249
  const TOGGLE = /^<(?:Checkbox|Switch)\b/;
1216
1250
 
@@ -1438,6 +1472,7 @@ function resolveScopes(views, scopes, layers = new Map()) {
1438
1472
  });
1439
1473
  }
1440
1474
  for (const view of views) view.body = view.body.replace(marked, (token) => named.get(token));
1475
+ return named;
1441
1476
  }
1442
1477
 
1443
1478
  /** `scope` then `name` in camelCase, a word they share at the join said once (`recordSwitcherScope` + `ScopeValue`). */
@@ -1937,6 +1972,21 @@ function emitNode(node, ctx) {
1937
1972
  own = nameHandlers(own, node, ctx, path, scope);
1938
1973
  own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
1939
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
+ }
1940
1990
  // A Table part is valid only inside the DS Table. One the generator could not assemble
1941
1991
  // into a Table stays reported, never passed off as mapped (MW8-17.3).
1942
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.143",
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": {