@nodii/dev 0.6.145 → 0.6.146

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.145";
7
+ export const VERSION = "0.6.146";
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 = 16;
35
+ export const GENERATOR_FORMAT = 17;
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). */
@@ -548,7 +548,7 @@ export function emitSurface(tree, row, ds) {
548
548
  used.add(state);
549
549
  const fn = `${pascal(state)}View`;
550
550
  const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
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 }) ?? "";
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(), shown: new Map(), optionals }) ?? "";
552
552
  // A view the generator can't lay out is no page at all: generation fails rather than ship
553
553
  // an empty view (NODII-DEV-CLI-81).
554
554
  if (emitted.trimStart().startsWith("{/*")) {
@@ -1112,27 +1112,98 @@ function nameHandlers(code, node, ctx, path, scope) {
1112
1112
  const GLYPH_WORDS = [[/^[‹«←⟨]+$/, "Previous"], [/^[›»→⟩]+$/, "Next"], [/^[↑]+$/, "Up"], [/^[↓]+$/, "Down"]];
1113
1113
 
1114
1114
  /**
1115
- * The prop that hides a button the page shows only sometimes (a permission, a
1116
- * state): `show<what it does>`, the words and the place of its own handler
1117
- * (`onStateEstablishmentSizeClick` → `showStateEstablishmentSize`), so two
1118
- * buttons never share one (hub-478). Optional: a page that passes nothing
1119
- * shows the button. Null for a part that is not a button, for a probe, and for
1120
- * a button a table row or another part's slot holds: a row's actions are the
1121
- * row's, and an Alert's action hides with its Alert.
1115
+ * When a part the page shows only sometimes (a permission, data being present,
1116
+ * a state) is drawn (hub-478), or null when it always is. A page that passes
1117
+ * nothing still draws it:
1118
+ * - a button, an Alert, a badge or the pager: `show<X>?: boolean`. A button's
1119
+ * X is its own handler's (`onStateEstablishmentSizeClick` →
1120
+ * `showStateEstablishmentSize`); the others' is their title or label, in at
1121
+ * most five words, with the place a button's handler would take, so two parts
1122
+ * never share one;
1123
+ * - a record's lone description row: its value is non-null, so a page passes
1124
+ * null for a field the record leaves out.
1125
+ * Null for a probe, and for a part a table row or another part's slot holds: a
1126
+ * row's parts are the row's, and an Alert's action hides with its Alert.
1127
+ * `code` is the part's code with its identifiers marked.
1122
1128
  */
1123
1129
  const SHOWN_TAGS = new Set(["Button", "IconButton", "DecisionButton"]);
1124
- function showFlag(code, node, ctx, path) {
1130
+ const FLAGGED_TAGS = new Set(["Alert", "Badge", "StatusPill", "Chip", "Pagination", "CursorPagination"]);
1131
+ const LONE_DESCRIPTION = /^<DescriptionList\b[^>]*>\s*<DescriptionItem\b[^>]*?\bvalue=\{(__s\d+_[a-z][A-Za-z0-9]*)\}[^>]*\/>\s*<\/DescriptionList>$/;
1132
+ function showCondition(code, node, ctx, path) {
1125
1133
  if (!ctx.scopes || ctx.tabProbe || ctx.inTable || ctx.inSlot) return null;
1126
1134
  const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
1127
- if (!SHOWN_TAGS.has(tag)) return null;
1135
+ const lone = LONE_DESCRIPTION.exec(code.trim());
1136
+ if (lone) return `${lone[1]} != null`;
1137
+ if (!SHOWN_TAGS.has(tag) && !FLAGGED_TAGS.has(tag)) return null;
1128
1138
  const end = openTagEnd(code);
1129
1139
  const attrs = code.slice(tag.length + 1, end);
1130
- const own = new RegExp(String.raw`\b${HANDLER_ATTR[tag]}=\{(__s\d+)_on([A-Z]\w*)Click\}`).exec(attrs);
1131
- if (own) return `${own[1]}_show${own[2]}`;
1132
- // A template that draws no handler: the words and place nameHandlers would give it.
1133
- const label = handlerLabel(tag, attrs, code.slice(end), node) ?? scopeName(node, code, ctx);
1134
- const { scope, name } = handlerScope(node, ctx, path, null, label, 0);
1135
- return mark(ctx, scope, name.replace(/^on(\w+)Click$/, "show$1"));
1140
+ const own = SHOWN_TAGS.has(tag) && new RegExp(String.raw`\b${HANDLER_ATTR[tag]}=\{(__s\d+)_on([A-Z]\w*)Click\}`).exec(attrs);
1141
+ if (own) return `${own[1]}_show${own[2]} !== false`;
1142
+ // A button whose template draws no handler takes the words and place nameHandlers would
1143
+ // give it; any other part, the title or label it draws as copy. A label lifted as the
1144
+ // record's (a status Badge's "Processed") is Figma's sample, never a name (hub-468): the
1145
+ // part is named by its kind. Numbered apart from the handlers.
1146
+ const props = node.instance?.props ?? {};
1147
+ const copy = [props.title, props.Title, props.label, props.Label].find((v) => typeof v === "string" && v.trim() && code.includes(v));
1148
+ const label = (SHOWN_TAGS.has(tag) ? handlerLabel(tag, attrs, code.slice(end), node) : handlerWords(copy)) ?? camelName(tag);
1149
+ const { scope, name } = handlerScope(node, { ...ctx, handlers: ctx.shown }, path, null, label, 0);
1150
+ return `${mark(ctx, scope, name.replace(/^on(\w+)Click$/, "show$1"))} !== false`;
1151
+ }
1152
+
1153
+ /**
1154
+ * What a template draws inside one part that the page may still hide (hub-478),
1155
+ * in its marked code:
1156
+ * - an Alert's AlertAction: shown while its button's `show<X>` allows, the
1157
+ * whole AlertAction, so an Alert never keeps an empty one;
1158
+ * - a lone Tab's one trigger (the template's own strip): gated by the strip's
1159
+ * `visibleTabs`, its value its label (`Positions` → `positions`), as a run's
1160
+ * are (tabStrip).
1161
+ */
1162
+ function hideable(code) {
1163
+ let out = code.replace(/<AlertAction>([\s\S]*?)<\/AlertAction>/g, (block, inner) => {
1164
+ const flagOf = (part) => {
1165
+ const own = /\bonClick=\{(__s\d+)_on([A-Z]\w*)Click\}/.exec(part);
1166
+ return own ? `${own[1]}_show${own[2]}` : null;
1167
+ };
1168
+ const buttons = buttonsIn(inner);
1169
+ if (buttons.length <= 1) {
1170
+ const flag = flagOf(inner);
1171
+ return flag ? `{${flag} !== false && (${block})}` : block;
1172
+ }
1173
+ // Two or more actions: each its own, inside the one AlertAction.
1174
+ let held = inner;
1175
+ for (const b of buttons.reverse()) {
1176
+ const flag = flagOf(b.code);
1177
+ if (flag) held = `${held.slice(0, b.at)}{${flag} !== false && (${b.code})}${held.slice(b.at + b.code.length)}`;
1178
+ }
1179
+ return `<AlertAction>${held}</AlertAction>`;
1180
+ });
1181
+ const strip = /<Tabs panelless value=\{(__s\d+)_tab\}/.exec(out);
1182
+ const triggers = out.match(/<TabsTrigger\b/g) ?? [];
1183
+ if (strip && triggers.length === 1) {
1184
+ out = out.replace(/^([ \t]*)(<TabsTrigger\b[\s\S]*?<\/TabsTrigger>)/m, (_, pad, trigger) => {
1185
+ const label = /\{("(?:[^"\\\n]|\\.)*")\}/.exec(trigger);
1186
+ const value = (label && slug(JSON.parse(label[1]))) || "tab";
1187
+ const body = trigger.replace(/\bvalue="tab"/, `value="${value}"`).split("\n").map((line, i) => (i === 0 ? `${pad} ${line}` : line ? ` ${line}` : line)).join("\n");
1188
+ return `${pad}{(${strip[1]}_visibleTabs?.includes(${JSON.stringify(value)}) ?? true) && (\n${body}\n${pad})}`;
1189
+ });
1190
+ }
1191
+ return out;
1192
+ }
1193
+
1194
+ /** Each Button or Icon Button element in `code`: [{ at, code }], its end found past braces (`icon={<X />}`). */
1195
+ function buttonsIn(code) {
1196
+ const found = [];
1197
+ for (const m of code.matchAll(/<(Button|IconButton)(?=[\s/>])/g)) {
1198
+ if (found.some((b) => m.index < b.at + b.code.length)) continue;
1199
+ // openTagEnd is the opening tag's `>`.
1200
+ const gt = m.index + openTagEnd(code.slice(m.index));
1201
+ const selfClosing = /\/\s*$/.test(code.slice(m.index, gt));
1202
+ const close = selfClosing ? gt : code.indexOf(`</${m[1]}>`, gt);
1203
+ if (close === -1) continue;
1204
+ found.push({ at: m.index, code: code.slice(m.index, selfClosing ? gt + 1 : close + m[1].length + 3) });
1205
+ }
1206
+ return found;
1136
1207
  }
1137
1208
 
1138
1209
  /** `drawn` (indented one deeper than `depth`) drawn only while `condition` holds. */
@@ -1971,7 +2042,7 @@ function emitNode(node, ctx) {
1971
2042
  const scope = control ? fieldScope(node, ctx, own) : toggle ? camelName(toggle) : scopeName(node, own, ctx);
1972
2043
  own = nameHandlers(own, node, ctx, path, scope);
1973
2044
  own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
1974
- const code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes, ctx.layer) : own;
2045
+ let code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes, ctx.layer) : own;
1975
2046
  if (ctx.optionals && ctx.scopes) {
1976
2047
  // Each identifier is its drawing tag's: the outer template's own list, or, for a part it
1977
2048
  // draws through executeTemplate (a Field's Input), that part's template's (DESIGN-SYSTEM-507).
@@ -1987,15 +2058,17 @@ function emitNode(node, ctx) {
1987
2058
  (listed.has(m[1]) ? ctx.optionals.optional : ctx.optionals.required).add(m[0]);
1988
2059
  }
1989
2060
  }
2061
+ // What the page may still hide inside this part, after the optional props are read (hub-478).
2062
+ if (ctx.scopes && !ctx.tabProbe) code = hideable(code);
1990
2063
  // A Table part is valid only inside the DS Table. One the generator could not assemble
1991
2064
  // into a Table stays reported, never passed off as mapped (MW8-17.3).
1992
2065
  const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
1993
2066
  if (!ctx.inTable && (tablePart(tag) || tag === SKELETON_ROWS)) note(path, "table", `${describe(node)} is a Table part outside any table the generator recognises`);
1994
- const flag = showFlag(own, node, ctx, path);
1995
- const at = flag ? depth + 1 : depth;
2067
+ const shown = showCondition(code, node, ctx, path);
2068
+ const at = shown ? depth + 1 : depth;
1996
2069
  // Layout around a DS component is the page's; the component itself takes no className (U07-19).
1997
2070
  const drawn = !sizing.length ? indent(code, at) : [indent(`<div className="${cls(["flex", "flex-col", ...sizing])}">`, at), indent(code, at + 1), indent("</div>", at)].join("\n");
1998
- return flag ? shownWhen(`${flag} !== false`, drawn, depth) : drawn;
2071
+ return shown ? shownWhen(shown, drawn, depth) : drawn;
1999
2072
  }
2000
2073
 
2001
2074
  if (node.type === "TEXT") {
@@ -2049,10 +2122,37 @@ function emitNode(node, ctx) {
2049
2122
  return indent(comment(`unmapped: ${node.type.toLowerCase()} ${JSON.stringify(node.name)} has no auto layout`), depth);
2050
2123
  }
2051
2124
  const classes = cls([...sizeClasses(node, parent, root), ...layoutClasses(node), node.clips ? "overflow-hidden" : null]);
2125
+ const shown = root ? null : sectionCondition(node, ctx, path);
2052
2126
  const kids = emitChildren(node.children ?? [], child, { ...ctx, depth: depth + 1, parent: node, grand: parent, path, root: false, chain });
2053
2127
  const open = `<div${classes ? ` className="${classes}"` : ""}`;
2054
- if (!kids.length) return indent(`${open} />`, depth);
2055
- return [indent(`${open}>`, depth), ...kids, indent("</div>", depth)].join("\n");
2128
+ const drawn = !kids.length ? indent(`${open} />`, depth) : [indent(`${open}>`, depth), ...kids, indent("</div>", depth)].join("\n");
2129
+ return shown ? shownWhen(shown, indent(drawn, 1), depth) : drawn;
2130
+ }
2131
+
2132
+ /**
2133
+ * When a section, card, rail or row the page shows only sometimes is drawn
2134
+ * (hub-478): a frame the Surfaces name as a place, `<Kind> · <Name>`
2135
+ * ("Card · Peer invitation", "Rail · Your decision", "Fact · Close note"),
2136
+ * takes `show<Name>?: boolean`, its name's words with the place a button's
2137
+ * handler would take, numbered apart from the handlers. So does a frame that
2138
+ * opens with its own heading ("Peer-feedback invitations" over its rows),
2139
+ * named by the heading; a heading with a figure is the record's, never a name.
2140
+ * A page that passes nothing draws it. Null for a frame of a table's own
2141
+ * anatomy, one a table row or another part's slot holds, and a probe.
2142
+ */
2143
+ const PLACE_FRAME = /^\s*([A-Za-z][A-Za-z ]*?)\s*·\s*(.*[A-Za-z].*)$/;
2144
+ const TABLE_ANATOMY = new Set(["table", "cell"]);
2145
+ function sectionCondition(node, ctx, path) {
2146
+ if (!ctx.scopes || ctx.tabProbe || ctx.inTable || ctx.inSlot || node.instance) return null;
2147
+ const m = PLACE_FRAME.exec(node.name ?? "");
2148
+ // A head of only text (a title and its subtitle) is not the section: its body sits beside it.
2149
+ const [first, ...rest] = (node.children ?? []).filter((c) => c.visible !== false);
2150
+ const body = rest.some((c) => c.type !== "TEXT");
2151
+ const heading = body && first?.type === "TEXT" && HEADING_STYLE.test(first.textStyle ?? "") && !RECORD_TEXT(first.text ?? "") ? first.text : null;
2152
+ const words = m ? (TABLE_ANATOMY.has(m[1].trim().toLowerCase()) ? null : handlerWords(m[2])) : handlerWords(heading);
2153
+ if (!words) return null;
2154
+ const { scope, name } = handlerScope(node, { ...ctx, handlers: ctx.shown }, path, null, words, 0);
2155
+ return `${mark(ctx, scope, name.replace(/^on(\w+)Click$/, "show$1"))} !== false`;
2056
2156
  }
2057
2157
 
2058
2158
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodii/dev",
3
- "version": "0.6.145",
3
+ "version": "0.6.146",
4
4
  "description": "Agent CLI for Nodii Dev, reached through the platform edge",
5
5
  "type": "module",
6
6
  "bin": {