MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / findDeepestNest

Function findDeepestNest

apps/local/src/db/migration-nesting.test.ts:20–77  ·  view source on GitHub ↗
(sql: string)

Source from the content-addressed store, hash-verified

18// names. A "function call" is any IDENT immediately followed by `(`.
19// Returns { fn, depth } for the deepest single-name nest found, or null.
20const findDeepestNest = (sql: string) => {
21 const stack: string[] = [];
22 let worst: { fn: string; depth: number; offset: number } | null = null;
23 let i = 0;
24 while (i < sql.length) {
25 const ch = sql[i];
26 // Skip line comments
27 if (ch === "-" && sql[i + 1] === "-") {
28 const eol = sql.indexOf("\n", i);
29 i = eol < 0 ? sql.length : eol + 1;
30 continue;
31 }
32 // Skip string literals (single- and double-quoted, backtick identifiers)
33 if (ch === "'" || ch === '"' || ch === "`") {
34 const quote = ch;
35 i++;
36 while (i < sql.length) {
37 if (sql[i] === quote) {
38 if (sql[i + 1] === quote) {
39 i += 2;
40 continue;
41 }
42 i++;
43 break;
44 }
45 i++;
46 }
47 continue;
48 }
49 if (ch === "(") {
50 // Walk back to find an IDENT immediately preceding this paren
51 let j = i - 1;
52 while (j >= 0 && /\s/u.test(sql[j])) j--;
53 let end = j;
54 while (j >= 0 && /[A-Za-z0-9_]/u.test(sql[j])) j--;
55 const name = sql.slice(j + 1, end + 1).toLowerCase();
56 stack.push(name || "(");
57 // Count consecutive same-name frames in the stack
58 let same = 0;
59 for (let k = stack.length - 1; k >= 0; k--) {
60 if (stack[k] === stack[stack.length - 1]) same++;
61 else break;
62 }
63 if (name && (worst === null || same > worst.depth)) {
64 worst = { fn: name, depth: same, offset: i };
65 }
66 i++;
67 continue;
68 }
69 if (ch === ")") {
70 stack.pop();
71 i++;
72 continue;
73 }
74 i++;
75 }
76 return worst;
77};

Callers 1

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected