@starci/eslint-canon-be 3.8.19 → 3.9.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -4,258 +4,341 @@ import { byCodeUnit } from '../../lib/list.mjs';
4
4
  import { MIGRATIONS_DIR } from '../rules/database-constants.mjs';
5
5
  import { databaseSqlStatements, databaseSqlTokens, isMigrationName, migrationStamp } from '../rules/database-migrations.mjs';
6
6
  import { readText } from '../rules/read.mjs';
7
-
8
7
  const word = (token, value) => token?.t === 'word' && token.v === value;
9
- const identifier = (token) => ['word', 'ident'].includes(token?.t) && token.v === token.v.toLowerCase() ? token.v : null;
8
+ const identifier = (token) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v === token.v.toLowerCase() ? token.v : null;
10
9
  const OPAQUE_STATEMENTS = new Set(['do', 'call', 'select', 'with', 'insert', 'update', 'delete', 'merge', 'truncate', 'execute', 'rollback', 'abort', 'prepare', 'discard', 'refresh', 'copy']);
11
- const settingName = (token, value) => ['word', 'ident'].includes(token?.t) && token.v.toLowerCase() === value;
12
-
10
+ const settingName = (token, value) => token !== undefined && ['word', 'ident'].includes(token.t) && token.v.toLowerCase() === value;
13
11
  /** A migration relation is public only when its qualification or the current search path proves it. */
14
12
  function relationAt(tokens, start, schema) {
15
- const first = identifier(tokens[start]);
16
- if (first === null) return null;
17
- if (tokens[start + 1]?.v !== '.') return { schema, table: first, next: start + 1 };
18
- const table = identifier(tokens[start + 2]);
19
- if (table === null || tokens[start + 3]?.v === '.') return null;
20
- return { schema: first, table, next: start + 3 };
13
+ const first = identifier(tokens[start]);
14
+ if (first === null)
15
+ return null;
16
+ if (tokens[start + 1]?.v !== '.')
17
+ return { schema, table: first, next: start + 1 };
18
+ const table = identifier(tokens[start + 2]);
19
+ if (table === null || tokens[start + 3]?.v === '.')
20
+ return null;
21
+ return { schema: first, table, next: start + 3 };
21
22
  }
22
-
23
23
  /** Split declarations at their own commas, keeping type arguments and constraint column lists together. */
24
24
  function clausesOf(tokens) {
25
- const clauses = [[]];
26
- let depth = 0;
27
- for (const token of tokens) {
28
- if (token.v === '(') depth += 1;
29
- if (token.v === ')') depth -= 1;
30
- if (depth < 0) return null;
31
- if (token.v === ',' && depth === 0) clauses.push([]);
32
- else clauses.at(-1).push(token);
33
- }
34
- return depth === 0 ? clauses : null;
25
+ let current = [];
26
+ const clauses = [current];
27
+ let depth = 0;
28
+ for (const token of tokens) {
29
+ if (token.v === '(')
30
+ depth += 1;
31
+ if (token.v === ')')
32
+ depth -= 1;
33
+ if (depth < 0)
34
+ return null;
35
+ if (token.v === ',' && depth === 0) {
36
+ current = [];
37
+ clauses.push(current);
38
+ }
39
+ else
40
+ current.push(token);
41
+ }
42
+ return depth === 0 ? clauses : null;
35
43
  }
36
-
37
44
  function parenthesized(tokens, start) {
38
- if (tokens[start]?.v !== '(') return null;
39
- let depth = 1;
40
- for (let index = start + 1; index < tokens.length; index += 1) {
41
- if (tokens[index].v === '(') depth += 1;
42
- if (tokens[index].v === ')') depth -= 1;
43
- if (depth === 0) return { body: tokens.slice(start + 1, index), next: index + 1 };
44
- }
45
- return null;
45
+ if (tokens[start]?.v !== '(')
46
+ return null;
47
+ let depth = 1;
48
+ for (let index = start + 1; index < tokens.length; index += 1) {
49
+ if (tokens[index]?.v === '(')
50
+ depth += 1;
51
+ if (tokens[index]?.v === ')')
52
+ depth -= 1;
53
+ if (depth === 0)
54
+ return { body: tokens.slice(start + 1, index), next: index + 1 };
55
+ }
56
+ return null;
46
57
  }
47
-
48
58
  const isDeferred = (tokens) => tokens.some((token, index) => word(token, 'deferrable') && !word(tokens[index - 1], 'not'));
49
-
50
59
  /** A declared key proves only its complete, plain column list; index expressions are never column names. */
51
60
  function tableKey(tokens) {
52
- if (isDeferred(tokens)) return null;
53
- let start = word(tokens[0], 'constraint') ? 2 : 0;
54
- if (word(tokens[start], 'primary') && word(tokens[start + 1], 'key')) start += 2;
55
- else if (word(tokens[start], 'unique')) start += 1;
56
- else return null;
57
- const enclosed = parenthesized(tokens, start);
58
- const clauses = enclosed && clausesOf(enclosed.body);
59
- if (!clauses?.length || clauses.some((clause) => clause.length !== 1 || identifier(clause[0]) === null)) return null;
60
- return clauses.map((clause) => identifier(clause[0]));
61
+ if (isDeferred(tokens))
62
+ return null;
63
+ let start = word(tokens[0], 'constraint') ? 2 : 0;
64
+ if (word(tokens[start], 'primary') && word(tokens[start + 1], 'key'))
65
+ start += 2;
66
+ else if (word(tokens[start], 'unique'))
67
+ start += 1;
68
+ else
69
+ return null;
70
+ const enclosed = parenthesized(tokens, start);
71
+ const clauses = enclosed && clausesOf(enclosed.body);
72
+ if (!clauses?.length || clauses.some((clause) => clause.length !== 1 || identifier(clause[0]) === null))
73
+ return null;
74
+ // Every clause was just proven to be one identifier, so no clause is dropped.
75
+ return clauses.flatMap((clause) => { const column = identifier(clause[0]); return column === null ? [] : [column]; });
61
76
  }
62
-
63
77
  /** Inline key clauses are read at column depth, outside defaults, generated expressions and type parameters. */
64
78
  function columnKey(tokens) {
65
- const column = identifier(tokens[0]);
66
- if (column === null || isDeferred(tokens)) return null;
67
- let depth = 0;
68
- for (let index = 1; index < tokens.length; index += 1) {
69
- if (tokens[index].v === '(') depth += 1;
70
- if (tokens[index].v === ')') depth -= 1;
71
- if (depth === 0 && (word(tokens[index], 'unique') || (word(tokens[index], 'primary') && word(tokens[index + 1], 'key')))) return [column];
72
- }
73
- return null;
79
+ const column = identifier(tokens[0]);
80
+ if (column === null || isDeferred(tokens))
81
+ return null;
82
+ let depth = 0;
83
+ for (let index = 1; index < tokens.length; index += 1) {
84
+ if (tokens[index]?.v === '(')
85
+ depth += 1;
86
+ if (tokens[index]?.v === ')')
87
+ depth -= 1;
88
+ if (depth === 0 && (word(tokens[index], 'unique') || (word(tokens[index], 'primary') && word(tokens[index + 1], 'key'))))
89
+ return [column];
90
+ }
91
+ return null;
74
92
  }
75
-
76
93
  function declarationKey(tokens) {
77
- const tableConstraint = ['constraint', 'primary', 'unique', 'check', 'foreign', 'exclude'].some((value) => word(tokens[0], value));
78
- return tableConstraint ? tableKey(tokens) : columnKey(tokens);
94
+ const tableConstraint = ['constraint', 'primary', 'unique', 'check', 'foreign', 'exclude'].some((value) => word(tokens[0], value));
95
+ return tableConstraint ? tableKey(tokens) : columnKey(tokens);
79
96
  }
80
-
81
97
  function addKey(keys, columns) {
82
- if (columns && !keys.some((key) => key.length === columns.length && key.every((column, index) => column === columns[index]))) keys.push(columns);
98
+ if (columns && !keys.some((key) => key.length === columns.length && key.every((column, index) => column === columns[index])))
99
+ keys.push(columns);
83
100
  }
84
-
85
101
  function clearKeys(state, uncertain = false) {
86
- for (const table of state.tables.keys()) state.tables.set(table, []);
87
- if (uncertain) state.safe.clear();
102
+ for (const table of state.tables.keys())
103
+ state.tables.set(table, []);
104
+ if (uncertain)
105
+ state.safe.clear();
88
106
  }
89
-
90
107
  function opaqueExecution(state) {
91
- clearKeys(state, true);
92
- state.schema = null;
108
+ clearKeys(state, true);
109
+ state.schema = null;
93
110
  }
94
-
95
111
  function forgetRelation(state, relation) {
96
- if (!relation || relation.schema === null) clearKeys(state, true);
97
- else if (relation.schema === 'public') {
98
- state.tables.set(relation.table, []);
99
- state.safe.delete(relation.table);
100
- }
112
+ if (!relation || relation.schema === null)
113
+ clearKeys(state, true);
114
+ else if (relation.schema === 'public') {
115
+ state.tables.set(relation.table, []);
116
+ state.safe.delete(relation.table);
117
+ }
101
118
  }
102
-
103
119
  /** Conditional table creation cannot establish what an unknown pre-existing table actually constrains. */
104
120
  function createTable(state, tokens, start) {
105
- const conditional = word(tokens[start], 'if') && word(tokens[start + 1], 'not') && word(tokens[start + 2], 'exists');
106
- if (conditional) start += 3;
107
- const relation = relationAt(tokens, start, state.schema);
108
- if (!relation || relation.schema !== 'public') return;
109
- if (conditional) {
110
- if (!state.tables.has(relation.table)) state.tables.set(relation.table, []);
111
- return;
112
- }
113
- const enclosed = parenthesized(tokens, relation.next);
114
- const clauses = enclosed && clausesOf(enclosed.body);
115
- const keys = [];
116
- state.safe.delete(relation.table);
117
- state.tables.set(relation.table, keys);
118
- if (!clauses) return;
119
- state.safe.add(relation.table);
120
- for (const clause of clauses) addKey(keys, declarationKey(clause));
121
+ const conditional = word(tokens[start], 'if') && word(tokens[start + 1], 'not') && word(tokens[start + 2], 'exists');
122
+ if (conditional)
123
+ start += 3;
124
+ const relation = relationAt(tokens, start, state.schema);
125
+ if (!relation || relation.schema !== 'public')
126
+ return;
127
+ if (conditional) {
128
+ if (!state.tables.has(relation.table))
129
+ state.tables.set(relation.table, []);
130
+ return;
131
+ }
132
+ const enclosed = parenthesized(tokens, relation.next);
133
+ const clauses = enclosed && clausesOf(enclosed.body);
134
+ const keys = [];
135
+ state.safe.delete(relation.table);
136
+ state.tables.set(relation.table, keys);
137
+ if (!clauses)
138
+ return;
139
+ state.safe.add(relation.table);
140
+ for (const clause of clauses)
141
+ addKey(keys, declarationKey(clause));
121
142
  }
122
-
123
143
  function safeAlter(tokens) {
124
- if (word(tokens[0], 'owner') || word(tokens[0], 'cluster')) return true;
125
- const security = tokens.some((token) => word(token, 'security'));
126
- if (security && ['enable', 'disable', 'force', 'no'].some((value) => word(tokens[0], value))) return true;
127
- return (word(tokens[0], 'set') || word(tokens[0], 'reset')) && tokens[1]?.v === '(';
144
+ if (word(tokens[0], 'owner') || word(tokens[0], 'cluster'))
145
+ return true;
146
+ const security = tokens.some((token) => word(token, 'security'));
147
+ if (security && ['enable', 'disable', 'force', 'no'].some((value) => word(tokens[0], value)))
148
+ return true;
149
+ return (word(tokens[0], 'set') || word(tokens[0], 'reset')) && tokens[1]?.v === '(';
128
150
  }
129
-
130
151
  /** ADD can establish a new key; drops, renames and unreadable alterations discard the affected proof. */
131
152
  function alterClause(state, relation, tokens) {
132
- if (tokens.some((token) => word(token, 'inherit') || word(token, 'partition'))) { clearKeys(state, true); return; }
133
- if (word(tokens[0], 'validate') || tokens.some((token) => word(token, 'default') || word(token, 'check') || word(token, 'generated') || word(token, 'using'))) {
134
- opaqueExecution(state);
135
- return;
136
- }
137
- if (safeAlter(tokens)) return;
138
- if (word(tokens[0], 'drop') && (word(tokens[1], 'column') || word(tokens[1], 'constraint'))) {
139
- if (relation.schema === 'public') state.tables.set(relation.table, []);
140
- return;
141
- }
142
- if (!word(tokens[0], 'add')) { forgetRelation(state, relation); return; }
143
- let start = word(tokens[1], 'column') ? 2 : 1;
144
- if (word(tokens[start], 'if')) return;
145
- if (relation.schema !== 'public' || !state.safe.has(relation.table)) return;
146
- const keys = state.tables.get(relation.table) ?? [];
147
- state.tables.set(relation.table, keys);
148
- addKey(keys, declarationKey(tokens.slice(start)));
153
+ if (tokens.some((token) => word(token, 'inherit') || word(token, 'partition'))) {
154
+ clearKeys(state, true);
155
+ return;
156
+ }
157
+ if (word(tokens[0], 'validate') || tokens.some((token) => word(token, 'default') || word(token, 'check') || word(token, 'generated') || word(token, 'using'))) {
158
+ opaqueExecution(state);
159
+ return;
160
+ }
161
+ if (safeAlter(tokens))
162
+ return;
163
+ if (word(tokens[0], 'drop') && (word(tokens[1], 'column') || word(tokens[1], 'constraint'))) {
164
+ if (relation.schema === 'public')
165
+ state.tables.set(relation.table, []);
166
+ return;
167
+ }
168
+ if (!word(tokens[0], 'add')) {
169
+ forgetRelation(state, relation);
170
+ return;
171
+ }
172
+ let start = word(tokens[1], 'column') ? 2 : 1;
173
+ if (word(tokens[start], 'if'))
174
+ return;
175
+ if (relation.schema !== 'public' || !state.safe.has(relation.table))
176
+ return;
177
+ const keys = state.tables.get(relation.table) ?? [];
178
+ state.tables.set(relation.table, keys);
179
+ addKey(keys, declarationKey(tokens.slice(start)));
149
180
  }
150
-
151
181
  function alterTable(state, tokens, start) {
152
- if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists')) start += 2;
153
- if (word(tokens[start], 'only')) start += 1;
154
- const relation = relationAt(tokens, start, state.schema);
155
- if (!relation || relation.schema === null) { clearKeys(state, true); return; }
156
- const clauses = clausesOf(tokens.slice(relation.next));
157
- if (!clauses) { forgetRelation(state, relation); return; }
158
- for (const clause of clauses) alterClause(state, relation, clause);
182
+ if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists'))
183
+ start += 2;
184
+ if (word(tokens[start], 'only'))
185
+ start += 1;
186
+ const relation = relationAt(tokens, start, state.schema);
187
+ if (!relation || relation.schema === null) {
188
+ clearKeys(state, true);
189
+ return;
190
+ }
191
+ const clauses = clausesOf(tokens.slice(relation.next));
192
+ if (!clauses) {
193
+ forgetRelation(state, relation);
194
+ return;
195
+ }
196
+ for (const clause of clauses)
197
+ alterClause(state, relation, clause);
159
198
  }
160
-
161
199
  function dropTable(state, tokens, start) {
162
- if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists')) start += 2;
163
- for (const clause of clausesOf(tokens.slice(start)) ?? []) forgetRelation(state, relationAt(clause, 0, state.schema));
200
+ if (word(tokens[start], 'if') && word(tokens[start + 1], 'exists'))
201
+ start += 2;
202
+ for (const clause of clausesOf(tokens.slice(start)) ?? [])
203
+ forgetRelation(state, relationAt(clause, 0, state.schema));
164
204
  }
165
-
166
205
  function searchPath(state, tokens) {
167
- const local = word(tokens[1], 'local');
168
- const start = local || word(tokens[1], 'session') ? 2 : 1;
169
- if (word(tokens[start], 'schema')) {
170
- state.schema = !local && tokens[start + 1]?.t === 'string' ? tokens[start + 1].v : null;
171
- return;
172
- }
173
- if (!settingName(tokens[start], 'search_path')) {
174
- if (tokens.some((token) => settingName(token, 'search_path')) || word(tokens[start], 'all')) state.schema = null;
175
- return;
176
- }
177
- if (word(tokens[0], 'reset')) { state.schema = 'public'; return; }
178
- if (!word(tokens[start + 1], 'to') && tokens[start + 1]?.v !== '=') { state.schema = null; return; }
179
- const value = tokens[start + 2];
180
- const declared = identifier(value) ?? (value?.t === 'string' ? value.v : null);
181
- state.schema = local ? null : declared;
206
+ const local = word(tokens[1], 'local');
207
+ const start = local || word(tokens[1], 'session') ? 2 : 1;
208
+ if (word(tokens[start], 'schema')) {
209
+ const named = tokens[start + 1];
210
+ state.schema = !local && named?.t === 'string' ? named.v : null;
211
+ return;
212
+ }
213
+ if (!settingName(tokens[start], 'search_path')) {
214
+ if (tokens.some((token) => settingName(token, 'search_path')) || word(tokens[start], 'all'))
215
+ state.schema = null;
216
+ return;
217
+ }
218
+ if (word(tokens[0], 'reset')) {
219
+ state.schema = 'public';
220
+ return;
221
+ }
222
+ if (!word(tokens[start + 1], 'to') && tokens[start + 1]?.v !== '=') {
223
+ state.schema = null;
224
+ return;
225
+ }
226
+ const value = tokens[start + 2];
227
+ const declared = identifier(value) ?? (value?.t === 'string' ? value.v : null);
228
+ state.schema = local ? null : declared;
182
229
  }
183
-
184
230
  function createObject(tokens) {
185
- let start = 1;
186
- if (word(tokens[start], 'or') && word(tokens[start + 1], 'replace')) start += 2;
187
- if (word(tokens[start], 'global') || word(tokens[start], 'local')) start += 1;
188
- const temporary = word(tokens[start], 'temp') || word(tokens[start], 'temporary');
189
- if (temporary || word(tokens[start], 'unlogged')) start += 1;
190
- if (word(tokens[start], 'unique')) start += 1;
191
- return { start, temporary };
231
+ let start = 1;
232
+ if (word(tokens[start], 'or') && word(tokens[start + 1], 'replace'))
233
+ start += 2;
234
+ if (word(tokens[start], 'global') || word(tokens[start], 'local'))
235
+ start += 1;
236
+ const temporary = word(tokens[start], 'temp') || word(tokens[start], 'temporary');
237
+ if (temporary || word(tokens[start], 'unlogged'))
238
+ start += 1;
239
+ if (word(tokens[start], 'unique'))
240
+ start += 1;
241
+ return { start, temporary };
192
242
  }
193
-
194
243
  /** CTAS, materialization and index building can call user code; event triggers also affect later DDL. */
195
244
  function executingCreate(state, tokens, start) {
196
- if (word(tokens[start], 'event') && word(tokens[start + 1], 'trigger')) {
197
- state.tainted = true;
198
- opaqueExecution(state);
199
- return true;
200
- }
201
- if (word(tokens[start], 'index') || (word(tokens[start], 'materialized') && word(tokens[start + 1], 'view'))) {
202
- opaqueExecution(state);
203
- return true;
204
- }
205
- if (!word(tokens[start], 'table')) return false;
206
- let depth = 0;
207
- for (const token of tokens.slice(start + 1)) {
208
- if (token.v === '(') depth += 1;
209
- if (token.v === ')') depth -= 1;
210
- if (depth === 0 && word(token, 'as')) { opaqueExecution(state); return true; }
211
- }
212
- return false;
245
+ if (word(tokens[start], 'event') && word(tokens[start + 1], 'trigger')) {
246
+ state.tainted = true;
247
+ opaqueExecution(state);
248
+ return true;
249
+ }
250
+ if (word(tokens[start], 'index') || (word(tokens[start], 'materialized') && word(tokens[start + 1], 'view'))) {
251
+ opaqueExecution(state);
252
+ return true;
253
+ }
254
+ if (!word(tokens[start], 'table'))
255
+ return false;
256
+ let depth = 0;
257
+ for (const token of tokens.slice(start + 1)) {
258
+ if (token.v === '(')
259
+ depth += 1;
260
+ if (token.v === ')')
261
+ depth -= 1;
262
+ if (depth === 0 && word(token, 'as')) {
263
+ opaqueExecution(state);
264
+ return true;
265
+ }
266
+ }
267
+ return false;
213
268
  }
214
-
215
269
  function applyCreate(state, tokens) {
216
- // A private or temporary child also contributes rows to a public parent's ordinary SELECT.
217
- if (tokens.some((token) => word(token, 'inherits'))) { clearKeys(state, true); return; }
218
- const { start, temporary } = createObject(tokens);
219
- if (executingCreate(state, tokens, start)) return;
220
- if (temporary) { opaqueExecution(state); return; }
221
- if (word(tokens[start], 'table')) createTable(state, tokens, start + 1);
270
+ // A private or temporary child also contributes rows to a public parent's ordinary SELECT.
271
+ if (tokens.some((token) => word(token, 'inherits'))) {
272
+ clearKeys(state, true);
273
+ return;
274
+ }
275
+ const { start, temporary } = createObject(tokens);
276
+ if (executingCreate(state, tokens, start))
277
+ return;
278
+ if (temporary) {
279
+ opaqueExecution(state);
280
+ return;
281
+ }
282
+ if (word(tokens[start], 'table'))
283
+ createTable(state, tokens, start + 1);
222
284
  }
223
-
224
285
  function applyTableCommand(state, tokens, command) {
225
- if (word(tokens[1], 'table')) command(state, tokens, 2);
226
- else clearKeys(state, true);
286
+ if (word(tokens[1], 'table'))
287
+ command(state, tokens, 2);
288
+ else
289
+ clearKeys(state, true);
227
290
  }
228
-
229
291
  /** Reduce only unconditional schema declarations; executable bodies may remove facts but never manufacture them. */
230
292
  function applyStatement(state, tokens) {
231
- if (state.tainted) return;
232
- if (word(tokens[0], 'set') || word(tokens[0], 'reset')) { searchPath(state, tokens); return; }
233
- if (tokens[0]?.t === 'word' && OPAQUE_STATEMENTS.has(tokens[0].v)) { opaqueExecution(state); return; }
234
- if (word(tokens[0], 'commit') && word(tokens[1], 'prepared')) { opaqueExecution(state); return; }
235
- if (word(tokens[0], 'create')) { applyCreate(state, tokens); return; }
236
- if (word(tokens[0], 'alter')) { applyTableCommand(state, tokens, alterTable); return; }
237
- if (word(tokens[0], 'drop')) applyTableCommand(state, tokens, dropTable);
293
+ if (state.tainted)
294
+ return;
295
+ if (word(tokens[0], 'set') || word(tokens[0], 'reset')) {
296
+ searchPath(state, tokens);
297
+ return;
298
+ }
299
+ if (tokens[0]?.t === 'word' && OPAQUE_STATEMENTS.has(tokens[0].v)) {
300
+ opaqueExecution(state);
301
+ return;
302
+ }
303
+ if (word(tokens[0], 'commit') && word(tokens[1], 'prepared')) {
304
+ opaqueExecution(state);
305
+ return;
306
+ }
307
+ if (word(tokens[0], 'create')) {
308
+ applyCreate(state, tokens);
309
+ return;
310
+ }
311
+ if (word(tokens[0], 'alter')) {
312
+ applyTableCommand(state, tokens, alterTable);
313
+ return;
314
+ }
315
+ if (word(tokens[0], 'drop'))
316
+ applyTableCommand(state, tokens, dropTable);
238
317
  }
239
-
240
318
  function activeMigrations(root) {
241
- const listed = lsFiles(['-z', '--cached', '--exclude-standard', '--', MIGRATIONS_DIR], { dir: root, maxBuffer: 256 * 1024 * 1024 });
242
- if (listed.error || listed.status !== 0) return null;
243
- return listed.stdout.split('\0').filter((file) => path.posix.dirname(file) === MIGRATIONS_DIR
244
- && isMigrationName(path.posix.basename(file)) && migrationStamp(path.posix.basename(file).slice(0, 14)) !== null).sort(byCodeUnit);
319
+ const listed = lsFiles(['-z', '--cached', '--exclude-standard', '--', MIGRATIONS_DIR], { dir: root, maxBuffer: 256 * 1024 * 1024 });
320
+ if (listed.error || listed.status !== 0)
321
+ return null;
322
+ return listed.stdout.split('\0').filter((file) => path.posix.dirname(file) === MIGRATIONS_DIR
323
+ && isMigrationName(path.posix.basename(file)) && migrationStamp(path.posix.basename(file).slice(0, 14)) !== null).sort(byCodeUnit);
245
324
  }
246
-
247
325
  /**
248
326
  * Public-table key sets proven by ordered, tracked active migrations. Direct event triggers taint later DDL proof.
249
327
  * Opaque bodies discard current facts; persistent schema machinery hidden inside them is outside static authority.
250
328
  */
251
329
  export function sqlUniqueSetsOf(appRoot) {
252
- const root = path.resolve(appRoot);
253
- const files = activeMigrations(root);
254
- const state = { tables: new Map(), safe: new Set(), schema: 'public', tainted: false };
255
- for (const file of files ?? []) {
256
- const text = readText(root, file);
257
- if (text === null) { clearKeys(state, true); state.schema = null; continue; }
258
- for (const statement of databaseSqlStatements(databaseSqlTokens(text))) applyStatement(state, statement);
259
- }
260
- return state.tainted ? new Map() : state.tables;
330
+ const root = path.resolve(appRoot);
331
+ const files = activeMigrations(root);
332
+ const state = { tables: new Map(), safe: new Set(), schema: 'public', tainted: false };
333
+ for (const file of files ?? []) {
334
+ const text = readText(root, file);
335
+ if (text === null) {
336
+ clearKeys(state, true);
337
+ state.schema = null;
338
+ continue;
339
+ }
340
+ for (const statement of databaseSqlStatements(databaseSqlTokens(text)))
341
+ applyStatement(state, statement);
342
+ }
343
+ return state.tainted ? new Map() : state.tables;
261
344
  }