@marktiderman/genesis-cli 1.28.0 → 1.29.0

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/doctor.d.ts CHANGED
@@ -27,7 +27,7 @@ declare function parseRulesMarkdown(markdown: string): PatternRule[];
27
27
  declare function defineDoctorPlugin(plugin: DoctorPlugin): DoctorPlugin;
28
28
 
29
29
  /** Ids of the built-in rules, for `rules` overrides and `allow` entries. */
30
- declare const BUILTIN_RULES: readonly ["local-primitives", "figma-routes", "starter-leftover", "forbidden-import", "raw-tag", "palette-color", "arbitrary-color", "raw-color", "undefined-token", "shadowed-export", "version-skew", "duplicate-genesis-package", "peer-range-mismatch", "hand-rolled-sub-nav", "local-genesis-copy", "resource-workspace", "page-layout", "iso-date-slice", "resource-view-tree", "studio-server-auth-import", "removed-scope-name", "scope-practice-id", "deprecated-import", "beta-import", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "eas-placeholder", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit", "migration-view-rls", "exposed-schema-created-later", "package-app-import", "package-undeclared-dependency", "package-migration-test", "package-migration-number", "package-migration-edited", "package-unregistered", "package-unversioned-change", "package-export-missing", "package-block-invalid", "package-untagged-tests", "package-doctor-declaration", "overlay-unmarked", "overlay-invalid", "overlay-age", "overlay-version", "overlay-behind", "package-rules-invalid", "package-rule-override", "ratchet-skipped", "allow-grew", "rules-weakened", "extension-added", "stale-extension", "extension-invalid", "agent-guide-stale", "contracts-stale", "lock-files", "database-elsewhere", "genesis-eslint-config", "app-part-colors", "app-part-name", "custom-view-states", "client/layer", "client/one-view-per-resource"];
30
+ declare const BUILTIN_RULES: readonly ["local-primitives", "figma-routes", "starter-leftover", "forbidden-import", "raw-tag", "palette-color", "arbitrary-color", "raw-color", "undefined-token", "shadowed-export", "version-skew", "duplicate-genesis-package", "peer-range-mismatch", "hand-rolled-sub-nav", "local-genesis-copy", "resource-workspace", "page-layout", "iso-date-slice", "resource-view-tree", "studio-server-auth-import", "removed-scope-name", "scope-practice-id", "deprecated-import", "beta-import", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "eas-placeholder", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit", "migration-view-rls", "exposed-schema-created-later", "package-app-import", "package-undeclared-dependency", "package-migration-test", "package-migration-number", "package-migration-edited", "package-unregistered", "package-unversioned-change", "package-export-missing", "package-block-invalid", "package-untagged-tests", "package-doctor-declaration", "overlay-unmarked", "overlay-invalid", "overlay-age", "overlay-version", "overlay-behind", "package-rules-invalid", "package-rule-override", "ratchet-skipped", "allow-grew", "rules-weakened", "extension-added", "stale-extension", "extension-invalid", "agent-guide-stale", "contracts-stale", "lock-files", "database-elsewhere", "genesis-eslint-config", "app-part-colors", "app-part-name", "custom-view-states", "client/layer", "client/one-view-per-resource", "data/declared-access", "data/access-rule-tests"];
31
31
 
32
32
  /**
33
33
  * Minimal globs for config: `*` matches within a folder, `**` across folders,
package/dist/doctor.js CHANGED
@@ -1230,7 +1230,7 @@ function globToRegExp(glob) {
1230
1230
  }
1231
1231
  function globMatcher(globs) {
1232
1232
  const res = (globs ?? []).filter((g) => g.trim()).map(globToRegExp);
1233
- return (path50) => res.some((re) => re.test(path50));
1233
+ return (path51) => res.some((re) => re.test(path51));
1234
1234
  }
1235
1235
  var GENESIS_PACKAGE = /^@marktiderman\/genesis(-[a-z0-9-]+)?$/;
1236
1236
  var BLOCK_ID = /^[a-z][a-z0-9]*(?:[-_.][a-z0-9]+)*$/;
@@ -3195,7 +3195,10 @@ var BUILTIN_RULES = [
3195
3195
  "custom-view-states",
3196
3196
  // the app's client layer (client-layer.ts)
3197
3197
  "client/layer",
3198
- "client/one-view-per-resource"
3198
+ "client/one-view-per-resource",
3199
+ // how the app reaches its database (data-access.ts)
3200
+ "data/declared-access",
3201
+ "data/access-rule-tests"
3199
3202
  ];
3200
3203
  var DEFAULT_LOCAL_PRIMITIVE_DIRS = ["src/components/ui"];
3201
3204
  var STARTER_LEFTOVERS = ["components.json", "src/lib/utils.ts"];
@@ -6595,6 +6598,167 @@ function importedNames(filePath, code) {
6595
6598
  return out;
6596
6599
  }
6597
6600
  var SCRIPT_EXTS7 = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"]);
6601
+ var SKIP_DIRS4 = /* @__PURE__ */ new Set(["node_modules", "dist", "build", ".git", ".next", ".output", ".turbo", "coverage", ".expo"]);
6602
+ var TEST_FILE = /(^|\/)(__tests__\/|[^/]+\.(test|spec)\.[cm]?[jt]sx?$)/;
6603
+ var DOCS = "docs/DATA-ACCESS.md";
6604
+ var TABLE_WRITE = /\.from\(\s*(["'`])([\w.]+)\1\s*\)\s*\.\s*(insert|update|upsert|delete)\s*\(/g;
6605
+ var STORAGE_BEFORE = /\bstorage\s*\??\.?\s*$/;
6606
+ function balanced(code, start) {
6607
+ const open = code[start];
6608
+ const close = open === "{" ? "}" : open === "(" ? ")" : open === "[" ? "]" : "";
6609
+ if (!close) return "";
6610
+ const blank = blankStrings(code);
6611
+ let depth = 0;
6612
+ for (let i = start; i < blank.length; i++) {
6613
+ const ch = blank[i];
6614
+ if (ch === "{" || ch === "(" || ch === "[") depth++;
6615
+ else if (ch === "}" || ch === ")" || ch === "]") {
6616
+ depth--;
6617
+ if (depth === 0) return ch === close ? code.slice(start, i + 1) : "";
6618
+ }
6619
+ }
6620
+ return "";
6621
+ }
6622
+ function testFiles(root) {
6623
+ const out = [];
6624
+ const walk3 = (dir) => {
6625
+ let entries;
6626
+ try {
6627
+ entries = readdirSync(dir, { withFileTypes: true });
6628
+ } catch {
6629
+ return;
6630
+ }
6631
+ for (const entry of entries) {
6632
+ if (entry.isDirectory()) {
6633
+ if (!SKIP_DIRS4.has(entry.name) && !entry.name.startsWith(".")) walk3(path21.join(dir, entry.name));
6634
+ continue;
6635
+ }
6636
+ const abs = path21.join(dir, entry.name);
6637
+ const rel = path21.relative(root, abs).split(path21.sep).join("/");
6638
+ if (SCRIPT_EXTS7.has(path21.extname(entry.name)) && TEST_FILE.test(rel)) out.push(abs);
6639
+ }
6640
+ };
6641
+ walk3(root);
6642
+ return out;
6643
+ }
6644
+ function initializer(code, blank, name) {
6645
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6646
+ const decl = new RegExp(`\\b(const|let|var)\\s+${escaped}(?![\\w$])[^=;]*=\\s*\\{`).exec(blank);
6647
+ if (!decl) return null;
6648
+ const text = balanced(code, decl.index + decl[0].length - 1);
6649
+ return text ? { text, keyword: decl[1], at: decl.index } : null;
6650
+ }
6651
+ function objectDeclares(object) {
6652
+ const blank = blankStrings(object);
6653
+ let depth = 0;
6654
+ let declaredAt = -1;
6655
+ let lastAccess = -1;
6656
+ let lastSpread = -1;
6657
+ for (let i = 0; i < blank.length; i++) {
6658
+ const ch = blank[i];
6659
+ if (ch === "{" || ch === "(" || ch === "[") depth++;
6660
+ else if (ch === "}" || ch === ")" || ch === "]") depth--;
6661
+ else if (depth === 1 && blank.startsWith("...", i)) lastSpread = i;
6662
+ else if (depth === 1 && blank[i] === object[i] && /[\s{,]/.test(blank[i - 1] ?? " ") && /^(["'`]?)access\1\s*:/.test(object.slice(i))) {
6663
+ lastAccess = i;
6664
+ const value = /^(["'`]?)access\1\s*:\s*(["'`])([^"'`]*)\2/.exec(object.slice(i));
6665
+ declaredAt = value?.[3] === "declared" ? i : -1;
6666
+ }
6667
+ }
6668
+ return declaredAt !== -1 && declaredAt === lastAccess && lastSpread < declaredAt;
6669
+ }
6670
+ function inlineDeclares(call) {
6671
+ const blank = blankStrings(call);
6672
+ let depth = 0;
6673
+ for (let i = 0; i < blank.length; i++) {
6674
+ const ch = blank[i];
6675
+ if (ch === "{" && depth === 1 && objectDeclares(balanced(call, i))) return true;
6676
+ if (ch === "{" || ch === "(" || ch === "[") depth++;
6677
+ else if (ch === "}" || ch === ")" || ch === "]") depth--;
6678
+ }
6679
+ return false;
6680
+ }
6681
+ function optionsConstDeclares(code, blank, call) {
6682
+ const args = blankStrings(call).slice(1, -1);
6683
+ for (const [, name] of args.matchAll(/(?:^|,)\s*([A-Za-z_$][\w$]*)\s*(?=,|$)/g)) {
6684
+ const found = initializer(code, blank, name);
6685
+ if (!found || !objectDeclares(found.text)) continue;
6686
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6687
+ const reassigned = found.keyword !== "const" && new RegExp(`(?<![\\w$.])${escaped}\\s*=(?!=)`, "g").test(blank.slice(found.at + found.keyword.length + 1 + name.length));
6688
+ const changed = new RegExp(
6689
+ `(?<![\\w$.])${escaped}\\s*(?:\\.\\s*access|\\[[^\\]]*\\])\\s*=(?!=)|\\bdelete\\s+${escaped}\\s*(?:\\.\\s*access|\\[)|Object\\.assign\\(\\s*${escaped}(?![\\w$])`
6690
+ ).test(blank);
6691
+ if (!reassigned && !changed) return true;
6692
+ }
6693
+ return false;
6694
+ }
6695
+ function coverageTested(root) {
6696
+ for (const abs of testFiles(root)) {
6697
+ let code;
6698
+ try {
6699
+ code = stripBlockComments(readFileSync(abs, "utf8"), true);
6700
+ } catch {
6701
+ continue;
6702
+ }
6703
+ if (!code.includes("expectAccessRulesCover")) continue;
6704
+ if (/\bexpectAccessRulesCover\s*\(/.test(blankStrings(code))) return true;
6705
+ }
6706
+ return false;
6707
+ }
6708
+ function checkDataAccess(ctx) {
6709
+ for (const file of ctx.files) {
6710
+ if (!SCRIPT_EXTS7.has(file.ext)) continue;
6711
+ const blank = blankStrings(file.code);
6712
+ for (const m of blank.matchAll(/\bcreateSupabaseProvider\s*\(/g)) {
6713
+ const call = balanced(file.code, (m.index ?? 0) + m[0].length - 1);
6714
+ if (inlineDeclares(call) || optionsConstDeclares(file.code, blank, call)) continue;
6715
+ ctx.report({
6716
+ rule: "data/declared-access",
6717
+ level: "warn",
6718
+ file: file.path,
6719
+ line: lineAt(file.code, m.index ?? 0),
6720
+ subject: "createSupabaseProvider",
6721
+ message: `leaves its resources free to read and write their tables undeclared. Pass access: "declared", give each resource its read (source: a view or a function) and its writes (writes: named database functions), and opt a table in only with table: { \u2026, why } (${DOCS})`
6722
+ });
6723
+ }
6724
+ if (file.path.startsWith("supabase/") || file.path.includes("/supabase/functions/")) continue;
6725
+ for (const m of file.code.matchAll(TABLE_WRITE)) {
6726
+ const at = m.index ?? 0;
6727
+ if (blank[at] !== ".") continue;
6728
+ if (STORAGE_BEFORE.test(blank.slice(Math.max(0, at - 40), at))) continue;
6729
+ ctx.report({
6730
+ rule: "data/declared-access",
6731
+ level: "warn",
6732
+ file: file.path,
6733
+ line: lineAt(file.code, at),
6734
+ subject: `${m[2]}.${m[3]}`,
6735
+ message: `writes the ${m[2]} table directly (${m[3]}). Write through a named database function (the resource's writes, or a call to it), which holds the access rule; a write that must stay on the table is an allow entry for data/declared-access with its reason (${DOCS})`
6736
+ });
6737
+ }
6738
+ }
6739
+ const providers = providerCalls(ctx.files);
6740
+ if (providers.length === 0 || coverageTested(ctx.root)) return;
6741
+ const first = providers[0];
6742
+ ctx.report({
6743
+ rule: "data/access-rule-tests",
6744
+ level: "warn",
6745
+ file: first.file,
6746
+ line: first.line,
6747
+ subject: "expectAccessRulesCover",
6748
+ message: `no test checks that every database function the provider declares has an access-rule test. Add expectAccessRulesCover(providerOptions, [/* each accessRules spec */]) (@marktiderman/genesis-core/testing) to a test file: it fails for each declared function without one (${DOCS})`
6749
+ });
6750
+ }
6751
+ function providerCalls(files) {
6752
+ const out = [];
6753
+ for (const file of files) {
6754
+ if (!SCRIPT_EXTS7.has(file.ext)) continue;
6755
+ for (const m of blankStrings(file.code).matchAll(/\bcreateSupabaseProvider\s*\(/g)) {
6756
+ out.push({ file: file.path, line: lineAt(file.code, m.index ?? 0) });
6757
+ }
6758
+ }
6759
+ return out;
6760
+ }
6761
+ var SCRIPT_EXTS8 = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"]);
6598
6762
  var DECLARATION = /\/\*\*((?:(?!\*\/)[\s\S])*)\*\/\s*\n(?:export\s+)?(?:declare\s+)?(?:abstract\s+)?(?:function|const|let|class|interface|type|enum|namespace)\s+([A-Za-z_$][\w$]*)/g;
6599
6763
  var REEXPORT = /\/\*\*((?:(?!\*\/)[\s\S])*)\*\/\s*\nexport\s+(?:type\s+)?\{([^}]*)\}/g;
6600
6764
  function specifierParts(spec) {
@@ -6684,7 +6848,7 @@ function checkStabilityImports(ctx) {
6684
6848
  return cache2.get(pkg);
6685
6849
  };
6686
6850
  for (const file of ctx.files) {
6687
- if (!SCRIPT_EXTS7.has(file.ext)) continue;
6851
+ if (!SCRIPT_EXTS8.has(file.ext)) continue;
6688
6852
  for (const { pkg, specifier, names, line } of genesisNamedImports(file.code)) {
6689
6853
  const tags = tagsOf(pkg);
6690
6854
  if (!tags) continue;
@@ -7166,6 +7330,7 @@ var RULES = [
7166
7330
  "Never copy an older screen as a template. Old screens predate the primitives; the doctor flags what they did.",
7167
7331
  "A list screen is a view (`defineView`) rendered by `ResourceView`; a record view is `RecordView`; on the phone, `NativeResourceView`.",
7168
7332
  "Screens never call the database client (`supabase.from`, `.rpc`) directly: reads go through a view or a named data function, writes through the resource's provider rules.",
7333
+ `Data access is declared: the Supabase provider runs with \`access: "declared"\`; each resource reads through a view or a function (\`source\`) and writes through named database functions (\`writes\`, \`actions\`) that hold the access rule; a table is touched only with \`table: { \u2026, why }\`. Every declared function gets an access-rule test (\`accessRules\`), and one test (\`expectAccessRulesCover\`) fails for any declared function whose name no spec carries: it compares names only, so give it the same specs the cases are registered from. See ${GENESIS_DOCS}/DATA-ACCESS.md.`,
7169
7334
  "When a Genesis piece is close and does the same job, extend it: a prop, a variant, a slot, or your own component that wraps it. Never build a parallel copy beside it.",
7170
7335
  "When no Genesis piece does the job, invent it in the app: your own screens and components, on Genesis tokens and primitives. Genesis asks for no write-up or approval first (your app's own design review still applies); you may add it to `docs/genesis-upstream.md` if you think Genesis should have it.",
7171
7336
  "Run `genesis doctor` before you finish; it enforces these rules."
@@ -8414,6 +8579,7 @@ function builtinChecks(config, options = {}) {
8414
8579
  { name: "removed-scope-names", check: checkRemovedScopeNames },
8415
8580
  { name: "app-parts", check: checkAppParts },
8416
8581
  { name: "client-layer", check: checkClientLayer },
8582
+ { name: "data-access", check: checkDataAccess },
8417
8583
  { name: "stability-imports", check: checkStabilityImports },
8418
8584
  { name: "agent-guide", check: (ctx) => {
8419
8585
  checkAgentGuide(ctx);