Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): cap every repeat the layout expander walks (#6565)

Joeyc802014e4da3
diff --git a/.changeset/cli-spec-layout-repeat-cap.md b/.changeset/cli-spec-layout-repeat-cap.md
new file mode 100644
index 0000000000..7ea6a7decf
--- /dev/null
+++ b/.changeset/cli-spec-layout-repeat-cap.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx layout expand` now caps every `*N` repeat at 10000 copies. Repeated table rows skipped the cap, and a huge count on any element was still walked copy by copy before the cap applied, so `B*999999999` could hang or run out of memory. `astryx layout grammar` now states the cap.
+
+@josephfarina
diff --git a/packages/cli/api/layout/grammar/grammar.mjs b/packages/cli/api/layout/grammar/grammar.mjs
index e9d1b7edda..b7bc5f4cee 100644
--- a/packages/cli/api/layout/grammar/grammar.mjs
+++ b/packages/cli/api/layout/grammar/grammar.mjs
@@ -10,6 +10,7 @@
*/
import {buildRegistry} from '../../../foundation/xle/registry.mjs';
+import {MAX_REPEAT} from '../../../foundation/xle/expand.mjs';
/**
* `astryx layout grammar` — the agent cheatsheet, with the alias table
@@ -50,7 +51,7 @@ NODE ANATOMY Name#id.enum"payload"[attrs]{hint}*N > children
.enum unique enum value of any prop: Bd.success Tx.lg B.primary
"payload" primary text prop (label/title/heading) or text child: TI"Email" B"Save"
{hint} kebab-case template/component reference (see TEMPLATE REFERENCING) — NEVER text
- *N / xN repeat (use $ for the counter: Tk"item-$"*3)
+ *N / xN repeat, at most ${MAX_REPEAT} copies (use $ for the counter: Tk"item-$"*3)
trailing ! initial selection for scaffolded state: Tab"Overview"!
ATTRS [...] (outline: bare tokens after the name, no brackets)
diff --git a/packages/cli/foundation/xle/expand.mjs b/packages/cli/foundation/xle/expand.mjs
index b51d9d5999..c1e064e885 100644
--- a/packages/cli/foundation/xle/expand.mjs
+++ b/packages/cli/foundation/xle/expand.mjs
@@ -20,7 +20,8 @@ import {PAYLOAD_PROPS} from './validate.mjs';
import {mergeImports, renderImport, prepareSpliceModule} from './splice.mjs';
const INDENT = ' ';
-const MAX_REPEAT = 10000;
+/** The most copies one `*N` repeat expands to. */
+export const MAX_REPEAT = 10000;
/** @param {string | null | undefined} text */
function slugify(text) {
@@ -642,7 +643,7 @@ class Emitter {
/** @type {import('./xle-ast').XLENode[]} */
const out = [];
for (const item of items) {
- const count = item.repeat || 1;
+ const count = Math.min(item.repeat || 1, MAX_REPEAT);
for (let i = 1; i <= count; i++) {
const clone = count > 1 ? cloneItem(item) : item;
if (count > 1) {
diff --git a/packages/cli/foundation/xle/expand.test.mjs b/packages/cli/foundation/xle/expand.test.mjs
new file mode 100644
index 0000000000..9c58f0b50c
--- /dev/null
+++ b/packages/cli/foundation/xle/expand.test.mjs
@@ -0,0 +1,54 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Expander tests for the repeat cap. Every path that expands a `*N`
+ * repeat — emitted children, table rows, the root count — honours MAX_REPEAT.
+ * Uses the real @astryxdesign/core registry.
+ */
+
+import {describe, it, expect, beforeAll} from 'vitest';
+import {parse} from './parse.mjs';
+import {validate} from './validate.mjs';
+import {buildRegistry} from './registry.mjs';
+import {expand, MAX_REPEAT} from './expand.mjs';
+
+/** @type {import('./xle-ast').Registry} */
+let registry;
+
+// The registry imports every component doc on first use; warm it once.
+beforeAll(async () => {
+ registry = /** @type {import('./xle-ast').Registry} */ (
+ /** @type {unknown} */ (await buildRegistry())
+ );
+}, 120_000);
+
+/**
+ * @param {string} expression
+ * @returns {string}
+ */
+function expandCode(expression) {
+ const doc = parse(expression);
+ const {errors} = validate(doc, registry, []);
+ expect(errors).toEqual([]);
+ return expand(doc, registry).code;
+}
+
+/**
+ * @param {string} code
+ * @param {string} component
+ */
+function countOpening(code, component) {
+ return (code.match(new RegExp(`<(?:XDS)?${component}\\b`, 'g')) || []).length;
+}
+
+describe('expand — repeat cap', () => {
+ it('caps a repeated child at MAX_REPEAT copies', () => {
+ expect(countOpening(expandCode(`V > B*${MAX_REPEAT + 5}`), 'Button')).toBe(MAX_REPEAT);
+ });
+
+ it('caps repeated table rows at MAX_REPEAT copies too', () => {
+ expect(countOpening(expandCode(`T > TR*${MAX_REPEAT + 5} > TC`), 'TableRow')).toBe(
+ MAX_REPEAT,
+ );
+ });
+});