@marktiderman/genesis-cli 1.10.0 → 1.11.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
@@ -26,7 +26,7 @@ declare function parseRulesMarkdown(markdown: string): PatternRule[];
26
26
  declare function defineDoctorPlugin(plugin: DoctorPlugin): DoctorPlugin;
27
27
 
28
28
  /** Ids of the built-in rules, for `rules` overrides and `allow` entries. */
29
- declare const BUILTIN_RULES: readonly ["local-primitives", "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", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit"];
29
+ declare const BUILTIN_RULES: readonly ["local-primitives", "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", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit", "migration-view-rls"];
30
30
 
31
31
  /**
32
32
  * Minimal globs for config: `*` matches within a folder, `**` across folders,
@@ -89,12 +89,12 @@ interface GenesisInstall {
89
89
  * where a package's dependencies are its siblings in the store entry. Copies
90
90
  * nothing resolves to are left out: they never load.
91
91
  *
92
- * Each copy records which roots reach it: the app's declared runtime
93
- * dependencies (`runtime`), hoisted packages nobody declared (they could be
94
- * anyone's), and the dev roots (declared devDependencies, and the Genesis CLI
95
- * wherever it's listed). Only a copy reached from dev roots alone is `devOnly`.
96
- * An app with no runtime dependencies at all (everything in devDependencies, as
97
- * a bundled SPA may list them) has its devDependencies taken as runtime.
92
+ * Each copy records which roots reach it: the app's declared dependencies
93
+ * (`runtime`; devDependencies included, since a bundler ships what the app
94
+ * imports from them), hoisted packages nobody declared (they could be anyone's),
95
+ * and the dev roots: declared tooling (test runners, type packages, linters,
96
+ * bundlers, the Genesis CLI wherever it's listed), whose trees never ship. Only
97
+ * a copy reached from dev roots alone is `devOnly`.
98
98
  */
99
99
  declare function genesisInstalls(root: string): Map<string, GenesisInstall[]>;
100
100
 
package/dist/doctor.js CHANGED
@@ -966,7 +966,8 @@ var BUILTIN_RULES = [
966
966
  "migration-rls",
967
967
  "migration-definer-search-path",
968
968
  "migration-edited",
969
- "migration-testing-kit"
969
+ "migration-testing-kit",
970
+ "migration-view-rls"
970
971
  ];
971
972
  var DEFAULT_LOCAL_PRIMITIVE_DIRS = ["src/components/ui"];
972
973
  var STARTER_LEFTOVERS = ["components.json", "src/lib/utils.ts"];
@@ -1795,6 +1796,7 @@ function checkSupabasePack(ctx) {
1795
1796
  checkGrants(ctx, migrations.filter((m) => isNew(m.file)), exposed);
1796
1797
  }
1797
1798
  checkRls(ctx, migrations, exposed);
1799
+ checkViewInvoker(ctx, migrations, exposed);
1798
1800
  checkDefinerSearchPath(ctx, migrations);
1799
1801
  checkTestingKit(ctx, migrations);
1800
1802
  }
@@ -1924,6 +1926,50 @@ function checkRls(ctx, migrations, exposed) {
1924
1926
  });
1925
1927
  }
1926
1928
  }
1929
+ function checkViewInvoker(ctx, migrations, exposed) {
1930
+ const views = /* @__PURE__ */ new Map();
1931
+ const name = String.raw`([\w$.]+)`;
1932
+ const invokerOn = /\bsecurity_invoker\s*(?:=\s*(?:true|on|yes|1|'true'|'on'))?\s*(?:,|\))/;
1933
+ for (const { file, statements } of migrations) {
1934
+ for (const { sql, line } of statements) {
1935
+ const create = new RegExp(String.raw`^create (?:or replace )?(?:temp |temporary )?(?:recursive )?view ${name}`).exec(sql);
1936
+ if (create) {
1937
+ if (/^create (?:or replace )?(?:temp|temporary) /.test(sql)) continue;
1938
+ const options = /^[^(]*?\bwith\s*(\([^)]*\))\s*as\b/.exec(sql)?.[1] ?? "";
1939
+ views.set(qualify(create[1]), { invoker: invokerOn.test(options), file, line });
1940
+ continue;
1941
+ }
1942
+ const alter = new RegExp(String.raw`^alter view (?:if exists )?${name} (set|reset) (\([^)]*\))`).exec(sql);
1943
+ if (alter && /\bsecurity_invoker\b/.test(alter[3])) {
1944
+ const state = views.get(qualify(alter[1]));
1945
+ if (state) state.invoker = alter[2] === "set" && invokerOn.test(alter[3]);
1946
+ continue;
1947
+ }
1948
+ const rename = new RegExp(String.raw`^alter view (?:if exists )?${name} rename to ([\w$]+)$`).exec(sql);
1949
+ if (rename) {
1950
+ const from = qualify(rename[1]);
1951
+ const state = views.get(from);
1952
+ if (state) {
1953
+ views.delete(from);
1954
+ views.set(`${schemaOf(from)}.${rename[2]}`, state);
1955
+ }
1956
+ continue;
1957
+ }
1958
+ const drop = /^drop view (?:if exists )?(.+?)(?: cascade| restrict)?$/.exec(sql);
1959
+ if (drop) for (const n of drop[1].split(",")) views.delete(qualify(n));
1960
+ }
1961
+ }
1962
+ for (const [v, at] of views) {
1963
+ if (at.invoker || !exposed.has(schemaOf(v))) continue;
1964
+ ctx.report({
1965
+ rule: "migration-view-rls",
1966
+ file: at.file,
1967
+ line: at.line,
1968
+ subject: v,
1969
+ message: `${v} runs as its owner, so it reads past row level security; create it "with (security_invoker = true)" or add "alter view ${v} set (security_invoker = true);"`
1970
+ });
1971
+ }
1972
+ }
1927
1973
  function checkDefinerSearchPath(ctx, migrations) {
1928
1974
  const fns = /* @__PURE__ */ new Map();
1929
1975
  const keysFor = (sql, nameEnd, fnName) => {
@@ -2134,6 +2180,7 @@ function satisfies(version, range) {
2134
2180
  // src/doctor/installs.ts
2135
2181
  var GENESIS = /^@marktiderman\/genesis(?:-[\w.-]+)?$/;
2136
2182
  var DEV_TOOLS = /* @__PURE__ */ new Set(["@marktiderman/genesis-cli"]);
2183
+ var NON_SHIPPING = /^(?:@marktiderman\/genesis-cli|vitest|@vitest\/.+|jest|jest-.+|@jest\/.+|ts-jest|babel-jest|mocha|ava|c8|nyc|@playwright\/test|playwright|cypress|@testing-library\/.+|happy-dom|jsdom|msw|@types\/.+|typescript|tsx|ts-node|eslint|eslint-.+|@eslint\/.+|@typescript-eslint\/.+|typescript-eslint|prettier|prettier-.+|@biomejs\/.+|oxlint|vite|@vitejs\/.+|vite-plugin-.+|webpack|webpack-.+|rollup|@rollup\/.+|esbuild|tsup|turbo|@storybook\/.+|storybook|@changesets\/.+|husky|lint-staged|wrangler|knip|concurrently|npm-run-all2?|rimraf|cross-env|dotenv-cli)$/;
2137
2184
  var readManifest = (dir) => {
2138
2185
  try {
2139
2186
  return JSON.parse(readFileSync(path14.join(dir, "package.json"), "utf8"));
@@ -2208,12 +2255,14 @@ function genesisInstalls(root) {
2208
2255
  return dir ? [{ dir, by, ...range ? { range } : {} }] : [];
2209
2256
  });
2210
2257
  const app = readManifest(root);
2211
- let runtimeDeps = { ...app?.peerDependencies, ...app?.optionalDependencies, ...app?.dependencies };
2212
- let devDeps = Object.fromEntries(Object.entries(app?.devDependencies ?? {}).filter(([n]) => !(n in runtimeDeps)));
2213
- if (Object.keys(runtimeDeps).length === 0) {
2214
- runtimeDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => !DEV_TOOLS.has(n)));
2215
- devDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => DEV_TOOLS.has(n)));
2216
- }
2258
+ const declaredDev = Object.entries(app?.devDependencies ?? {});
2259
+ const runtimeDeps = {
2260
+ ...Object.fromEntries(declaredDev.filter(([n]) => !NON_SHIPPING.test(n))),
2261
+ ...app?.peerDependencies,
2262
+ ...app?.optionalDependencies,
2263
+ ...app?.dependencies
2264
+ };
2265
+ const devDeps = Object.fromEntries(declaredDev.filter(([n]) => NON_SHIPPING.test(n) && !(n in runtimeDeps)));
2217
2266
  walk("runtime", rootsOf(runtimeDeps, "the app"));
2218
2267
  const top = path14.join(root, "node_modules");
2219
2268
  walk(
@@ -2296,11 +2345,21 @@ function stripForImports(code) {
2296
2345
  const templates = [];
2297
2346
  let depth = 0;
2298
2347
  let i = 0;
2348
+ let sure = true;
2299
2349
  const isSpecifier = () => /(?:\bfrom|(?:^|[;{}\n])\s*import|\bimport\s*\(|\brequire\s*\()\s*$/.test(out.slice(-40).join(""));
2300
- const opensString = () => {
2301
- const before = out.slice(-12).join("");
2302
- if (!/[\w$]$/.test(before)) return true;
2303
- return /(?:^|[^\w$])(?:return|from|import|case|typeof|in|of|else|yield|await|export|default|void|require)$/.test(before);
2350
+ const before = () => out.slice(-24).join("");
2351
+ const KEYWORD = /(?:^|[^\w$.])(?:return|typeof|instanceof|case|do|else|in|of|new|delete|void|throw|yield|await|from|import|export|default|require)\s*$/;
2352
+ const startsExpression = (b) => {
2353
+ const t = b.replace(/\s+$/, "");
2354
+ if (!t) return true;
2355
+ if (KEYWORD.test(b)) return true;
2356
+ return /[([{,;:=!&|?+\-*%<>~^}]$/.test(t);
2357
+ };
2358
+ const exprStart = () => startsExpression(before());
2359
+ const taggedTemplate = () => {
2360
+ const b = out.slice(-48).join("");
2361
+ const m = /([A-Za-z_$][\w$.]*)$/.exec(b);
2362
+ return m !== null && startsExpression(b.slice(0, b.length - m[1].length));
2304
2363
  };
2305
2364
  const templateBody = () => {
2306
2365
  while (i < n) {
@@ -2323,8 +2382,9 @@ function stripForImports(code) {
2323
2382
  i++;
2324
2383
  }
2325
2384
  }
2385
+ sure = false;
2326
2386
  };
2327
- while (i < n) {
2387
+ while (i < n && sure) {
2328
2388
  const ch = code[i];
2329
2389
  const next = code[i + 1];
2330
2390
  if (ch === "/" && next === "/") {
@@ -2343,7 +2403,45 @@ function stripForImports(code) {
2343
2403
  out.push(" ");
2344
2404
  i += 2;
2345
2405
  }
2346
- } else if ((ch === '"' || ch === "'") && opensString()) {
2406
+ } else if (ch === "/" && next !== ">" && code[i - 1] !== "<" && exprStart()) {
2407
+ const afterBrace = /[}>]\s*$/.test(before());
2408
+ const start = i;
2409
+ const mark = out.length;
2410
+ out.push("/");
2411
+ i++;
2412
+ let inClass = false;
2413
+ for (; ; ) {
2414
+ const c = code[i];
2415
+ if (c === void 0 || c === "\n") {
2416
+ if (afterBrace) {
2417
+ out.length = mark;
2418
+ out.push("/");
2419
+ i = start + 1;
2420
+ } else sure = false;
2421
+ break;
2422
+ }
2423
+ if (c === "\\") {
2424
+ out.push(" ", blank(code[i + 1] ?? ""));
2425
+ i += 2;
2426
+ continue;
2427
+ }
2428
+ i++;
2429
+ if (c === "[") inClass = true;
2430
+ else if (c === "]") inClass = false;
2431
+ else if (c === "/" && !inClass) {
2432
+ if (afterBrace && /[<>]/.test(code.slice(start + 1, i - 1))) {
2433
+ out.length = mark;
2434
+ out.push("/");
2435
+ i = start + 1;
2436
+ break;
2437
+ }
2438
+ out.push("/");
2439
+ break;
2440
+ }
2441
+ out.push(" ");
2442
+ }
2443
+ if (out.length > mark + 1 || i > start + 1) while (i < n && /[a-z]/.test(code[i])) out.push(code[i++]);
2444
+ } else if ((ch === '"' || ch === "'") && exprStart()) {
2347
2445
  const keep = isSpecifier();
2348
2446
  out.push(ch);
2349
2447
  i++;
@@ -2356,11 +2454,12 @@ function stripForImports(code) {
2356
2454
  i++;
2357
2455
  }
2358
2456
  }
2359
- if (i < n) {
2457
+ if (code[i] !== ch) sure = false;
2458
+ else {
2360
2459
  out.push(code[i]);
2361
2460
  i++;
2362
2461
  }
2363
- } else if (ch === "`") {
2462
+ } else if (ch === "`" && (exprStart() || taggedTemplate())) {
2364
2463
  out.push("`");
2365
2464
  i++;
2366
2465
  templateBody();
@@ -2377,7 +2476,7 @@ function stripForImports(code) {
2377
2476
  i++;
2378
2477
  }
2379
2478
  }
2380
- return out.join("");
2479
+ return sure && templates.length === 0 ? out.join("") : null;
2381
2480
  }
2382
2481
  function bindings(clause) {
2383
2482
  const out = [];
@@ -2491,6 +2590,11 @@ function createTrail(ctx) {
2491
2590
  const files = [];
2492
2591
  let lost = false;
2493
2592
  const code = stripped(file);
2593
+ if (code === null) {
2594
+ const out2 = { files: [], lost: true };
2595
+ importsCache.set(file.absolutePath, out2);
2596
+ return out2;
2597
+ }
2494
2598
  const used = new Set(renderedElements(code).map((e) => e.split(".")[0]));
2495
2599
  for (const m of code.matchAll(/\b\w*[cC]omponent\s*:\s*([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
2496
2600
  for (const m of code.matchAll(/\bexport\s+default\s+([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
@@ -2527,20 +2631,23 @@ function createTrail(ctx) {
2527
2631
  };
2528
2632
  const strippedCache = /* @__PURE__ */ new Map();
2529
2633
  const stripped = (file) => {
2530
- let c = strippedCache.get(file.absolutePath);
2531
- if (c === void 0) strippedCache.set(file.absolutePath, c = stripForImports(file.code));
2532
- return c;
2634
+ if (!strippedCache.has(file.absolutePath)) strippedCache.set(file.absolutePath, stripForImports(file.code));
2635
+ return strippedCache.get(file.absolutePath);
2533
2636
  };
2534
2637
  const memo = /* @__PURE__ */ new WeakMap();
2535
2638
  const reach = (file, test2, depth, seen) => {
2536
2639
  let cache = memo.get(test2);
2537
- if (!cache) memo.set(test2, cache = /* @__PURE__ */ new Map());
2640
+ if (!cache) memo.set(test2, cache = { free: /* @__PURE__ */ new Map(), cut: /* @__PURE__ */ new Map() });
2538
2641
  const k = `${file.absolutePath}\0${depth}`;
2539
- const cached = cache.get(k);
2540
- if (cached) return [cached, false];
2642
+ const free = cache.free.get(k);
2643
+ if (free) return [free, /* @__PURE__ */ new Set()];
2644
+ const reusable = cache.cut.get(k)?.find((e) => e.cut.every((m) => seen.has(m)));
2645
+ if (reusable) return [reusable.result, new Set(reusable.cut)];
2541
2646
  let result;
2542
- let cut = false;
2543
- if (test2(stripped(file))) result = "yes";
2647
+ const cut = /* @__PURE__ */ new Set();
2648
+ const code = stripped(file);
2649
+ if (code === null) result = "unknown";
2650
+ else if (test2(code)) result = "yes";
2544
2651
  else {
2545
2652
  const { files, lost } = importsOf(file);
2546
2653
  let unknown = lost && depth > 0;
@@ -2549,7 +2656,7 @@ function createTrail(ctx) {
2549
2656
  seen.add(file.absolutePath);
2550
2657
  for (const abs of files) {
2551
2658
  if (seen.has(abs)) {
2552
- cut = true;
2659
+ cut.add(abs);
2553
2660
  continue;
2554
2661
  }
2555
2662
  const target = read(abs);
@@ -2558,7 +2665,7 @@ function createTrail(ctx) {
2558
2665
  continue;
2559
2666
  }
2560
2667
  const [r, c] = reach(target, test2, depth - 1, seen);
2561
- if (c) cut = true;
2668
+ for (const m of c) cut.add(m);
2562
2669
  if (r === "yes") {
2563
2670
  result = "yes";
2564
2671
  break;
@@ -2566,11 +2673,18 @@ function createTrail(ctx) {
2566
2673
  if (r === "unknown") unknown = true;
2567
2674
  }
2568
2675
  seen.delete(file.absolutePath);
2676
+ cut.delete(file.absolutePath);
2569
2677
  }
2570
2678
  if (result === "no" && unknown) result = "unknown";
2571
2679
  }
2572
- if (result === "yes" || !cut) cache.set(k, result);
2573
- return [result, cut && result !== "yes"];
2680
+ if (result === "yes" || cut.size === 0) {
2681
+ cache.free.set(k, result);
2682
+ return [result, /* @__PURE__ */ new Set()];
2683
+ }
2684
+ const entries = cache.cut.get(k) ?? [];
2685
+ entries.push({ cut: [...cut], result });
2686
+ cache.cut.set(k, entries);
2687
+ return [result, cut];
2574
2688
  };
2575
2689
  return { reach: (file, test2, depth = TRAIL_DEPTH) => reach(file, test2, depth, /* @__PURE__ */ new Set())[0] };
2576
2690
  }