@kuafucloud/repo-kit 0.0.0-stage → 0.2.2-alpha.3

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 (212) hide show
  1. package/LICENSE.md +9 -0
  2. package/assets/git-hooks/check-push.cjs +21 -0
  3. package/assets/git-hooks/pre-push +7 -0
  4. package/assets/git-hooks/prepare-commit-msg +5 -0
  5. package/assets/verification/heavy-test-admission-core.d.mts +1 -0
  6. package/assets/verification/heavy-test-admission-core.mjs +10 -0
  7. package/assets/verification/host-process-lock.d.mts +24 -0
  8. package/assets/verification/host-process-lock.mjs +140 -0
  9. package/assets/verification/test-heavy-admission.d.mts +4 -0
  10. package/assets/verification/test-heavy-admission.mjs +34 -0
  11. package/bin/kuafu-guard.mjs +3 -0
  12. package/bin/kuafu-verify.mjs +3 -0
  13. package/dist/cli/guard.d.ts +1 -0
  14. package/dist/cli/guard.js +59 -0
  15. package/dist/cli/verify.d.ts +1 -0
  16. package/dist/cli/verify.js +58 -0
  17. package/dist/guard/core/config.d.ts +7 -0
  18. package/dist/guard/core/config.js +159 -0
  19. package/dist/guard/core/engine.d.ts +2 -0
  20. package/dist/guard/core/engine.js +151 -0
  21. package/dist/guard/core/formatters/sarif-rules.d.ts +2 -0
  22. package/dist/guard/core/formatters/sarif-rules.js +67 -0
  23. package/dist/guard/core/formatters/sarif.d.ts +2 -0
  24. package/dist/guard/core/formatters/sarif.js +3 -0
  25. package/dist/guard/core/formatters/text.d.ts +2 -0
  26. package/dist/guard/core/formatters/text.js +22 -0
  27. package/dist/guard/core/index.d.ts +5 -0
  28. package/dist/guard/core/index.js +5 -0
  29. package/dist/guard/core/types.d.ts +94 -0
  30. package/dist/guard/core/types.js +1 -0
  31. package/dist/guard/hooks/install-rules.d.ts +2 -0
  32. package/dist/guard/hooks/install-rules.js +5 -0
  33. package/dist/guard/hooks/install.d.ts +36 -0
  34. package/dist/guard/hooks/install.js +212 -0
  35. package/dist/guard/hooks/push.d.ts +1 -0
  36. package/dist/guard/hooks/push.js +5 -0
  37. package/dist/guard/index.d.ts +16 -0
  38. package/dist/guard/index.js +16 -0
  39. package/dist/guard/product-roots.d.ts +12 -0
  40. package/dist/guard/product-roots.js +37 -0
  41. package/dist/guard/repo-args.d.ts +8 -0
  42. package/dist/guard/repo-args.js +75 -0
  43. package/dist/guard/repo-output-rules.d.ts +12 -0
  44. package/dist/guard/repo-output-rules.js +44 -0
  45. package/dist/guard/repo.d.ts +21 -0
  46. package/dist/guard/repo.js +74 -0
  47. package/dist/guard/rules/external/api/collect-orphan-tests.d.ts +2 -0
  48. package/dist/guard/rules/external/api/collect-orphan-tests.js +34 -0
  49. package/dist/guard/rules/external/api/collect.d.ts +2 -0
  50. package/dist/guard/rules/external/api/collect.js +419 -0
  51. package/dist/guard/rules/external/api/validate-orphan-tests.d.ts +2 -0
  52. package/dist/guard/rules/external/api/validate-orphan-tests.js +10 -0
  53. package/dist/guard/rules/external/api/validate.d.ts +2 -0
  54. package/dist/guard/rules/external/api/validate.js +278 -0
  55. package/dist/guard/rules/external/app/native-source-rules.d.ts +6 -0
  56. package/dist/guard/rules/external/app/native-source-rules.js +164 -0
  57. package/dist/guard/rules/external/app/validate-native-source-boundary.d.ts +2 -0
  58. package/dist/guard/rules/external/app/validate-native-source-boundary.js +187 -0
  59. package/dist/guard/rules/external/boundary/check.d.ts +4 -0
  60. package/dist/guard/rules/external/boundary/check.js +198 -0
  61. package/dist/guard/rules/external/extension/collect.d.ts +1 -0
  62. package/dist/guard/rules/external/extension/collect.js +1 -0
  63. package/dist/guard/rules/external/extension/electron-boundary-rules.d.ts +6 -0
  64. package/dist/guard/rules/external/extension/electron-boundary-rules.js +10 -0
  65. package/dist/guard/rules/external/extension/index.d.ts +6 -0
  66. package/dist/guard/rules/external/extension/index.js +29 -0
  67. package/dist/guard/rules/external/extension/manual-cross-call-rules.d.ts +19 -0
  68. package/dist/guard/rules/external/extension/manual-cross-call-rules.js +19 -0
  69. package/dist/guard/rules/external/extension/resolve-import.d.ts +1 -0
  70. package/dist/guard/rules/external/extension/resolve-import.js +43 -0
  71. package/dist/guard/rules/external/extension/scan-relative-dependencies.d.ts +5 -0
  72. package/dist/guard/rules/external/extension/scan-relative-dependencies.js +53 -0
  73. package/dist/guard/rules/external/extension/scan-source.d.ts +11 -0
  74. package/dist/guard/rules/external/extension/scan-source.js +25 -0
  75. package/dist/guard/rules/external/extension/source-import-boundary-rules.d.ts +26 -0
  76. package/dist/guard/rules/external/extension/source-import-boundary-rules.js +40 -0
  77. package/dist/guard/rules/external/extension/test-layer-import-rules.d.ts +1 -0
  78. package/dist/guard/rules/external/extension/test-layer-import-rules.js +16 -0
  79. package/dist/guard/rules/external/extension/types.d.ts +51 -0
  80. package/dist/guard/rules/external/extension/types.js +1 -0
  81. package/dist/guard/rules/external/extension/unused-export-rules.d.ts +23 -0
  82. package/dist/guard/rules/external/extension/unused-export-rules.js +119 -0
  83. package/dist/guard/rules/external/extension/unused-module-rules.d.ts +6 -0
  84. package/dist/guard/rules/external/extension/unused-module-rules.js +90 -0
  85. package/dist/guard/rules/external/extension/validate-admin.d.ts +8 -0
  86. package/dist/guard/rules/external/extension/validate-admin.js +73 -0
  87. package/dist/guard/rules/external/extension/validate-database.d.ts +3 -0
  88. package/dist/guard/rules/external/extension/validate-database.js +4 -0
  89. package/dist/guard/rules/external/extension/validate-electron-boundary.d.ts +2 -0
  90. package/dist/guard/rules/external/extension/validate-electron-boundary.js +18 -0
  91. package/dist/guard/rules/external/extension/validate-empty-dirs.d.ts +9 -0
  92. package/dist/guard/rules/external/extension/validate-empty-dirs.js +44 -0
  93. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.d.ts +2 -0
  94. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.js +116 -0
  95. package/dist/guard/rules/external/extension/validate-features.d.ts +7 -0
  96. package/dist/guard/rules/external/extension/validate-features.js +27 -0
  97. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.d.ts +2 -0
  98. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.js +131 -0
  99. package/dist/guard/rules/external/extension/validate-peer-extension-import.d.ts +2 -0
  100. package/dist/guard/rules/external/extension/validate-peer-extension-import.js +41 -0
  101. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.d.ts +2 -0
  102. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.js +48 -0
  103. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.d.ts +1 -0
  104. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.js +1 -0
  105. package/dist/guard/rules/external/extension/validate-shared-import-boundary.d.ts +2 -0
  106. package/dist/guard/rules/external/extension/validate-shared-import-boundary.js +40 -0
  107. package/dist/guard/rules/external/extension/validate-shared.d.ts +1 -0
  108. package/dist/guard/rules/external/extension/validate-shared.js +1 -0
  109. package/dist/guard/rules/external/extension/validate-test-layer-boundary.d.ts +8 -0
  110. package/dist/guard/rules/external/extension/validate-test-layer-boundary.js +59 -0
  111. package/dist/guard/rules/external/extension/validate-test-mirror.d.ts +4 -0
  112. package/dist/guard/rules/external/extension/validate-test-mirror.js +32 -0
  113. package/dist/guard/rules/external/extension/validate-top-level.d.ts +7 -0
  114. package/dist/guard/rules/external/extension/validate-top-level.js +76 -0
  115. package/dist/guard/rules/external/extension/validate-unused-export.d.ts +2 -0
  116. package/dist/guard/rules/external/extension/validate-unused-export.js +187 -0
  117. package/dist/guard/rules/external/extension/validate-unused-module.d.ts +2 -0
  118. package/dist/guard/rules/external/extension/validate-unused-module.js +38 -0
  119. package/dist/guard/rules/external/index.d.ts +1 -0
  120. package/dist/guard/rules/external/index.js +1 -0
  121. package/dist/guard/rules/external/npm-dependency-policy.d.ts +22 -0
  122. package/dist/guard/rules/external/npm-dependency-policy.js +294 -0
  123. package/dist/guard/rules/external/rules.d.ts +2 -0
  124. package/dist/guard/rules/external/rules.js +231 -0
  125. package/dist/guard/rules/external/shared/config.d.ts +3 -0
  126. package/dist/guard/rules/external/shared/config.js +14 -0
  127. package/dist/guard/rules/external/shared/diagnostic.d.ts +4 -0
  128. package/dist/guard/rules/external/shared/diagnostic.js +12 -0
  129. package/dist/guard/rules/external/shared/records.d.ts +4 -0
  130. package/dist/guard/rules/external/shared/records.js +46 -0
  131. package/dist/guard/rules/external/shared/ts-ast.d.ts +8 -0
  132. package/dist/guard/rules/external/shared/ts-ast.js +103 -0
  133. package/dist/guard/rules/external/shared/types.d.ts +159 -0
  134. package/dist/guard/rules/external/shared/types.js +1 -0
  135. package/dist/guard/rules/external/shared/validate-layer-key.d.ts +2 -0
  136. package/dist/guard/rules/external/shared/validate-layer-key.js +35 -0
  137. package/dist/guard/rules/external/story-rules.d.ts +15 -0
  138. package/dist/guard/rules/external/story-rules.js +59 -0
  139. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.d.ts +7 -0
  140. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.js +6 -0
  141. package/dist/guard/rules/shared/extension/collect.d.ts +5 -0
  142. package/dist/guard/rules/shared/extension/collect.js +231 -0
  143. package/dist/guard/rules/shared/extension/database-reference-rules.d.ts +4 -0
  144. package/dist/guard/rules/shared/extension/database-reference-rules.js +5 -0
  145. package/dist/guard/rules/shared/extension/database-source-rules.d.ts +17 -0
  146. package/dist/guard/rules/shared/extension/database-source-rules.js +595 -0
  147. package/dist/guard/rules/shared/extension/database-structure-rules.d.ts +3 -0
  148. package/dist/guard/rules/shared/extension/database-structure-rules.js +63 -0
  149. package/dist/guard/rules/shared/extension/server-index-source-rules.d.ts +2 -0
  150. package/dist/guard/rules/shared/extension/server-index-source-rules.js +91 -0
  151. package/dist/guard/rules/shared/extension/shared-consumer-rules.d.ts +23 -0
  152. package/dist/guard/rules/shared/extension/shared-consumer-rules.js +158 -0
  153. package/dist/guard/rules/shared/extension/shared-structure-rules.d.ts +6 -0
  154. package/dist/guard/rules/shared/extension/shared-structure-rules.js +27 -0
  155. package/dist/guard/rules/shared/extension/validate-database.d.ts +4 -0
  156. package/dist/guard/rules/shared/extension/validate-database.js +15 -0
  157. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.d.ts +2 -0
  158. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.js +9 -0
  159. package/dist/guard/rules/shared/extension/validate-shared.d.ts +18 -0
  160. package/dist/guard/rules/shared/extension/validate-shared.js +149 -0
  161. package/dist/host-protocol.d.ts +2 -0
  162. package/dist/host-protocol.js +2 -0
  163. package/dist/index.d.ts +3 -0
  164. package/dist/index.js +3 -0
  165. package/dist/verify/artifact-recovery-rules.d.ts +39 -0
  166. package/dist/verify/artifact-recovery-rules.js +48 -0
  167. package/dist/verify/artifact-session.d.ts +91 -0
  168. package/dist/verify/artifact-session.js +443 -0
  169. package/dist/verify/checkout-environment-rules.d.ts +2 -0
  170. package/dist/verify/checkout-environment-rules.js +25 -0
  171. package/dist/verify/checkout-environment.d.ts +8 -0
  172. package/dist/verify/checkout-environment.js +24 -0
  173. package/dist/verify/commit-candidate-rules.d.ts +3 -0
  174. package/dist/verify/commit-candidate-rules.js +33 -0
  175. package/dist/verify/commit-gate.d.ts +64 -0
  176. package/dist/verify/commit-gate.js +207 -0
  177. package/dist/verify/delivery-verification.d.ts +3 -0
  178. package/dist/verify/delivery-verification.js +21 -0
  179. package/dist/verify/e2e-registry-rules.d.ts +21 -0
  180. package/dist/verify/e2e-registry-rules.js +33 -0
  181. package/dist/verify/e2e-registry.d.ts +2 -0
  182. package/dist/verify/e2e-registry.js +45 -0
  183. package/dist/verify/full-command.d.ts +35 -0
  184. package/dist/verify/full-command.js +138 -0
  185. package/dist/verify/impact-plan-rules.d.ts +61 -0
  186. package/dist/verify/impact-plan-rules.js +185 -0
  187. package/dist/verify/impact-plan.d.ts +10 -0
  188. package/dist/verify/impact-plan.js +151 -0
  189. package/dist/verify/index.d.ts +22 -0
  190. package/dist/verify/index.js +22 -0
  191. package/dist/verify/repository-recipe-rules.d.ts +31 -0
  192. package/dist/verify/repository-recipe-rules.js +60 -0
  193. package/dist/verify/repository-recipe.d.ts +3 -0
  194. package/dist/verify/repository-recipe.js +58 -0
  195. package/dist/verify/static-validation-rules.d.ts +7 -0
  196. package/dist/verify/static-validation-rules.js +36 -0
  197. package/dist/verify/story-selection-rules.d.ts +57 -0
  198. package/dist/verify/story-selection-rules.js +125 -0
  199. package/dist/verify/types.d.ts +63 -0
  200. package/dist/verify/types.js +18 -0
  201. package/dist/verify/verification-candidate.d.ts +1 -0
  202. package/dist/verify/verification-candidate.js +20 -0
  203. package/dist/verify/verification-identity.d.ts +6 -0
  204. package/dist/verify/verification-identity.js +26 -0
  205. package/dist/verify/verification-proof-rules.d.ts +35 -0
  206. package/dist/verify/verification-proof-rules.js +45 -0
  207. package/dist/verify/verify-full-command-rules.d.ts +18 -0
  208. package/dist/verify/verify-full-command-rules.js +77 -0
  209. package/dist/verify/verify-full-runner.d.ts +32 -0
  210. package/dist/verify/verify-full-runner.js +251 -0
  211. package/package.json +115 -4
  212. package/README.md +0 -3
@@ -0,0 +1,595 @@
1
+ import { crossLogicalDatabaseLabels } from "./database-reference-rules.js";
2
+ import { validateDatabaseStructure, DATABASE_MODEL_FILE_RE } from "./database-structure-rules.js";
3
+ import path from "node:path";
4
+ import ts from "typescript";
5
+ const DRIZZLE_QUERY_CALLEES = new Set([
6
+ "all",
7
+ "and",
8
+ "batch",
9
+ "delete",
10
+ "eq",
11
+ "execute",
12
+ "findFirst",
13
+ "findMany",
14
+ "from",
15
+ "fullJoin",
16
+ "get",
17
+ "groupBy",
18
+ "having",
19
+ "innerJoin",
20
+ "insert",
21
+ "leftJoin",
22
+ "limit",
23
+ "offset",
24
+ "or",
25
+ "orderBy",
26
+ "prepare",
27
+ "relations",
28
+ "returning",
29
+ "rightJoin",
30
+ "select",
31
+ "set",
32
+ "transaction",
33
+ "update",
34
+ "values",
35
+ "where",
36
+ ]);
37
+ /**
38
+ * 验证 extension 的 database 目录和源码里的逻辑 database 使用边界。
39
+ */
40
+ export function validateDatabaseSources(record, workspaceRoot, sources) {
41
+ const issues = [];
42
+ const databaseDir = path.join(record.extensionRoot, "src", "database");
43
+ if (record.databaseFiles.length === 0 && record.databaseDirs.length === 0) {
44
+ return issues;
45
+ }
46
+ issues.push(...validateDatabaseStructure(record, workspaceRoot, databaseDir));
47
+ issues.push(...validateLogicalDatabaseBoundaries(record, workspaceRoot, databaseDir));
48
+ return issues;
49
+ function validateLogicalDatabaseBoundaries(record, workspaceRoot, databaseDir) {
50
+ const candidateFiles = new Set(record.srcFiles.filter((filePath) => (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) &&
51
+ !filePath.endsWith(".d.ts")));
52
+ const moduleCache = new Map();
53
+ const modelFiles = [...candidateFiles].filter((filePath) => isDirectDatabaseModelFile(filePath, databaseDir));
54
+ for (const modelFile of modelFiles) {
55
+ collectModuleExports(modelFile, candidateFiles, moduleCache, new Set());
56
+ }
57
+ if (!hasAnyLogicalDatabaseModel(moduleCache)) {
58
+ return [];
59
+ }
60
+ const issues = [];
61
+ const emittedIssueKeys = new Set();
62
+ for (const filePath of candidateFiles) {
63
+ const sourceFile = readSourceFile(filePath);
64
+ if (!sourceFile) {
65
+ continue;
66
+ }
67
+ const localBindings = collectImportedBindings(sourceFile, filePath, candidateFiles, moduleCache);
68
+ collectLocalDatabaseBindings(sourceFile, filePath, localBindings, { named: new Map(), defaultTables: null });
69
+ collectSimpleLocalAliases(sourceFile, localBindings);
70
+ const visit = (node) => {
71
+ if (ts.isCallExpression(node) &&
72
+ isDatabaseCandidateCall(node) &&
73
+ !hasDatabaseCandidateCallAncestor(node)) {
74
+ const tableRefs = collectTableReferences(node, localBindings);
75
+ const databaseLabels = crossLogicalDatabaseLabels(tableRefs, record.extensionId);
76
+ if (databaseLabels !== null) {
77
+ const lineNumber = getLineNumber(sourceFile, node);
78
+ const issueKey = `${filePath}:${lineNumber}:${databaseLabels}`;
79
+ if (!emittedIssueKeys.has(issueKey)) {
80
+ emittedIssueKeys.add(issueKey);
81
+ issues.push({
82
+ rule: "extension-database-cross-logical-database",
83
+ filePath: toPosix(path.relative(workspaceRoot, filePath)),
84
+ lineNumber,
85
+ message: `单个 SQL 表达式、relations 或 transaction 不能同时引用多个逻辑 database 的表:${databaseLabels}。请拆成独立查询,并在业务层组合结果。`,
86
+ });
87
+ }
88
+ }
89
+ }
90
+ ts.forEachChild(node, visit);
91
+ };
92
+ visit(sourceFile);
93
+ }
94
+ return issues;
95
+ }
96
+ function collectModuleExports(filePath, candidateFiles, cache, loading) {
97
+ const cached = cache.get(filePath);
98
+ if (cached) {
99
+ return cached;
100
+ }
101
+ const moduleExports = {
102
+ named: new Map(),
103
+ defaultTables: null,
104
+ };
105
+ cache.set(filePath, moduleExports);
106
+ if (loading.has(filePath)) {
107
+ return moduleExports;
108
+ }
109
+ loading.add(filePath);
110
+ const sourceFile = readSourceFile(filePath);
111
+ if (!sourceFile) {
112
+ loading.delete(filePath);
113
+ return moduleExports;
114
+ }
115
+ const localBindings = collectImportedBindings(sourceFile, filePath, candidateFiles, cache, loading);
116
+ collectLocalDatabaseBindings(sourceFile, filePath, localBindings, moduleExports);
117
+ collectExportDeclarations(sourceFile, filePath, candidateFiles, cache, loading, localBindings, moduleExports);
118
+ collectDefaultTableObjectExport(sourceFile, localBindings, moduleExports);
119
+ loading.delete(filePath);
120
+ return moduleExports;
121
+ }
122
+ function collectImportedBindings(sourceFile, filePath, candidateFiles, cache, loading = new Set()) {
123
+ const localBindings = new Map();
124
+ for (const statement of sourceFile.statements) {
125
+ if (!ts.isImportDeclaration(statement)) {
126
+ continue;
127
+ }
128
+ if (!statement.importClause || statement.importClause.isTypeOnly) {
129
+ continue;
130
+ }
131
+ if (!ts.isStringLiteral(statement.moduleSpecifier)) {
132
+ continue;
133
+ }
134
+ const resolvedFile = resolveRelativeImportFile(filePath, statement.moduleSpecifier.text, candidateFiles);
135
+ if (!resolvedFile) {
136
+ continue;
137
+ }
138
+ const targetExports = collectModuleExports(resolvedFile, candidateFiles, cache, loading);
139
+ const importClause = statement.importClause;
140
+ if (importClause.name && targetExports.defaultTables) {
141
+ localBindings.set(importClause.name.text, {
142
+ kind: "tableObject",
143
+ tables: new Map(targetExports.defaultTables),
144
+ });
145
+ }
146
+ const namedBindings = importClause.namedBindings;
147
+ if (!namedBindings) {
148
+ continue;
149
+ }
150
+ if (ts.isNamespaceImport(namedBindings)) {
151
+ localBindings.set(namedBindings.name.text, {
152
+ kind: "moduleNamespace",
153
+ exports: targetExports.named,
154
+ });
155
+ continue;
156
+ }
157
+ for (const element of namedBindings.elements) {
158
+ if (element.isTypeOnly) {
159
+ continue;
160
+ }
161
+ const exportedName = element.propertyName?.text ?? element.name.text;
162
+ const binding = targetExports.named.get(exportedName);
163
+ if (binding) {
164
+ localBindings.set(element.name.text, binding);
165
+ }
166
+ }
167
+ }
168
+ return localBindings;
169
+ }
170
+ function collectLocalDatabaseBindings(sourceFile, filePath, localBindings, moduleExports) {
171
+ for (const statement of sourceFile.statements) {
172
+ if (!ts.isVariableStatement(statement)) {
173
+ continue;
174
+ }
175
+ if (!(statement.declarationList.flags & ts.NodeFlags.Const)) {
176
+ continue;
177
+ }
178
+ const isExported = hasExportModifier(statement);
179
+ for (const declaration of statement.declarationList.declarations) {
180
+ if (ts.isIdentifier(declaration.name)) {
181
+ collectIdentifierDeclarationBinding(declaration, filePath, localBindings, moduleExports, isExported);
182
+ continue;
183
+ }
184
+ if (ts.isObjectBindingPattern(declaration.name)) {
185
+ collectObjectBindingDeclaration(declaration, localBindings, moduleExports, isExported);
186
+ }
187
+ }
188
+ }
189
+ }
190
+ function collectIdentifierDeclarationBinding(declaration, filePath, localBindings, moduleExports, isExported) {
191
+ if (!ts.isIdentifier(declaration.name) || !declaration.initializer) {
192
+ return;
193
+ }
194
+ const localName = declaration.name.text;
195
+ const defineDatabaseCall = getDefineDatabaseCall(declaration.initializer);
196
+ if (defineDatabaseCall) {
197
+ const modelBinding = createModelBinding(defineDatabaseCall, filePath, localName);
198
+ if (!modelBinding) {
199
+ return;
200
+ }
201
+ localBindings.set(localName, modelBinding);
202
+ if (isExported) {
203
+ moduleExports.named.set(localName, modelBinding);
204
+ }
205
+ return;
206
+ }
207
+ const aliasBinding = resolveExpressionBinding(declaration.initializer, localBindings);
208
+ if (aliasBinding) {
209
+ localBindings.set(localName, aliasBinding);
210
+ if (isExported) {
211
+ moduleExports.named.set(localName, aliasBinding);
212
+ }
213
+ }
214
+ }
215
+ function collectObjectBindingDeclaration(declaration, localBindings, moduleExports, isExported) {
216
+ if (!ts.isObjectBindingPattern(declaration.name) || !declaration.initializer) {
217
+ return;
218
+ }
219
+ const sourceBinding = resolveExpressionBinding(declaration.initializer, localBindings);
220
+ if (!sourceBinding) {
221
+ return;
222
+ }
223
+ for (const element of declaration.name.elements) {
224
+ if (!ts.isIdentifier(element.name)) {
225
+ continue;
226
+ }
227
+ const exportedName = element.name.text;
228
+ const sourceName = getBindingElementPropertyName(element) ?? exportedName;
229
+ const binding = getPropertyBinding(sourceBinding, sourceName);
230
+ if (!binding) {
231
+ continue;
232
+ }
233
+ localBindings.set(exportedName, binding);
234
+ if (isExported) {
235
+ moduleExports.named.set(exportedName, binding);
236
+ }
237
+ }
238
+ }
239
+ function collectExportDeclarations(sourceFile, filePath, candidateFiles, cache, loading, localBindings, moduleExports) {
240
+ for (const statement of sourceFile.statements) {
241
+ if (!ts.isExportDeclaration(statement)) {
242
+ continue;
243
+ }
244
+ if (statement.isTypeOnly) {
245
+ continue;
246
+ }
247
+ const sourceExports = statement.moduleSpecifier &&
248
+ ts.isStringLiteral(statement.moduleSpecifier)
249
+ ? collectExportsFromSpecifier(filePath, statement.moduleSpecifier.text, candidateFiles, cache, loading)
250
+ : null;
251
+ if (!statement.exportClause) {
252
+ if (sourceExports) {
253
+ for (const [exportName, binding] of sourceExports.named) {
254
+ moduleExports.named.set(exportName, binding);
255
+ }
256
+ }
257
+ continue;
258
+ }
259
+ if (!ts.isNamedExports(statement.exportClause)) {
260
+ continue;
261
+ }
262
+ for (const element of statement.exportClause.elements) {
263
+ if (element.isTypeOnly) {
264
+ continue;
265
+ }
266
+ const sourceName = element.propertyName?.text ?? element.name.text;
267
+ const exportName = element.name.text;
268
+ const binding = sourceExports
269
+ ? sourceExports.named.get(sourceName)
270
+ : localBindings.get(sourceName);
271
+ if (binding) {
272
+ moduleExports.named.set(exportName, binding);
273
+ }
274
+ }
275
+ }
276
+ }
277
+ function collectExportsFromSpecifier(filePath, specifier, candidateFiles, cache, loading) {
278
+ const resolvedFile = resolveRelativeImportFile(filePath, specifier, candidateFiles);
279
+ if (!resolvedFile) {
280
+ return null;
281
+ }
282
+ return collectModuleExports(resolvedFile, candidateFiles, cache, loading);
283
+ }
284
+ function collectDefaultTableObjectExport(sourceFile, localBindings, moduleExports) {
285
+ for (const statement of sourceFile.statements) {
286
+ if (!ts.isExportAssignment(statement) || statement.isExportEquals) {
287
+ continue;
288
+ }
289
+ const expression = unwrapExpression(statement.expression);
290
+ if (!ts.isObjectLiteralExpression(expression)) {
291
+ continue;
292
+ }
293
+ const defaultTables = new Map();
294
+ for (const property of expression.properties) {
295
+ if (ts.isShorthandPropertyAssignment(property)) {
296
+ const binding = localBindings.get(property.name.text);
297
+ if (binding?.kind === "table") {
298
+ defaultTables.set(property.name.text, binding);
299
+ }
300
+ continue;
301
+ }
302
+ if (!ts.isPropertyAssignment(property)) {
303
+ continue;
304
+ }
305
+ const propertyName = getPropertyNameText(property.name);
306
+ if (!propertyName) {
307
+ continue;
308
+ }
309
+ const binding = resolveExpressionBinding(property.initializer, localBindings);
310
+ if (binding?.kind === "table") {
311
+ defaultTables.set(propertyName, binding);
312
+ }
313
+ }
314
+ if (defaultTables.size > 0) {
315
+ moduleExports.defaultTables = defaultTables;
316
+ }
317
+ }
318
+ }
319
+ function collectSimpleLocalAliases(sourceFile, localBindings) {
320
+ for (const statement of sourceFile.statements) {
321
+ if (!ts.isVariableStatement(statement)) {
322
+ continue;
323
+ }
324
+ if (!(statement.declarationList.flags & ts.NodeFlags.Const)) {
325
+ continue;
326
+ }
327
+ for (const declaration of statement.declarationList.declarations) {
328
+ if (ts.isIdentifier(declaration.name) && declaration.initializer) {
329
+ const binding = resolveExpressionBinding(declaration.initializer, localBindings);
330
+ if (binding) {
331
+ localBindings.set(declaration.name.text, binding);
332
+ }
333
+ continue;
334
+ }
335
+ if (ts.isObjectBindingPattern(declaration.name)) {
336
+ collectObjectBindingDeclaration(declaration, localBindings, { named: new Map(), defaultTables: null }, false);
337
+ }
338
+ }
339
+ }
340
+ }
341
+ function createModelBinding(call, filePath, modelName) {
342
+ const config = getFirstArgumentObject(call);
343
+ if (!config) {
344
+ return null;
345
+ }
346
+ const databaseName = getStringProperty(config, "databaseName");
347
+ const tablesProperty = getObjectProperty(config, "tables");
348
+ if (!databaseName || !tablesProperty) {
349
+ return null;
350
+ }
351
+ const tablesExpression = unwrapExpression(tablesProperty);
352
+ if (!ts.isObjectLiteralExpression(tablesExpression)) {
353
+ return null;
354
+ }
355
+ const tables = new Set();
356
+ for (const property of tablesExpression.properties) {
357
+ const tableName = getTableNameFromObjectProperty(property);
358
+ if (tableName) {
359
+ tables.add(tableName);
360
+ }
361
+ }
362
+ if (tables.size === 0) {
363
+ return null;
364
+ }
365
+ return {
366
+ kind: "model",
367
+ databaseName,
368
+ modelName,
369
+ sourceFile: filePath,
370
+ tables,
371
+ };
372
+ }
373
+ function getDefineDatabaseCall(expression) {
374
+ const unwrapped = unwrapExpression(expression);
375
+ if (!ts.isCallExpression(unwrapped)) {
376
+ return null;
377
+ }
378
+ if (!ts.isIdentifier(unwrapped.expression)) {
379
+ return null;
380
+ }
381
+ return unwrapped.expression.text === "defineDatabase" ? unwrapped : null;
382
+ }
383
+ function collectTableReferences(node, localBindings) {
384
+ const refs = new Map();
385
+ const visit = (current) => {
386
+ if (ts.isIdentifier(current) || ts.isPropertyAccessExpression(current)) {
387
+ const binding = resolveExpressionBinding(current, localBindings);
388
+ if (binding?.kind === "table") {
389
+ refs.set(`${binding.databaseName}:${binding.tableName}`, binding);
390
+ }
391
+ }
392
+ ts.forEachChild(current, visit);
393
+ };
394
+ visit(node);
395
+ return [...refs.values()];
396
+ }
397
+ function resolveExpressionBinding(expression, localBindings) {
398
+ const unwrapped = unwrapExpression(expression);
399
+ if (ts.isIdentifier(unwrapped)) {
400
+ return localBindings.get(unwrapped.text) ?? null;
401
+ }
402
+ if (!ts.isPropertyAccessExpression(unwrapped)) {
403
+ return null;
404
+ }
405
+ const ownerBinding = resolveExpressionBinding(unwrapped.expression, localBindings);
406
+ if (!ownerBinding) {
407
+ return null;
408
+ }
409
+ return getPropertyBinding(ownerBinding, unwrapped.name.text);
410
+ }
411
+ function getPropertyBinding(binding, propertyName) {
412
+ if (binding.kind === "moduleNamespace") {
413
+ return binding.exports.get(propertyName) ?? null;
414
+ }
415
+ if (binding.kind === "tableObject") {
416
+ return binding.tables.get(propertyName) ?? null;
417
+ }
418
+ if (binding.kind === "model" && binding.tables.has(propertyName)) {
419
+ return {
420
+ kind: "table",
421
+ databaseName: binding.databaseName,
422
+ tableName: propertyName,
423
+ sourceFile: binding.sourceFile,
424
+ };
425
+ }
426
+ return null;
427
+ }
428
+ function isDatabaseCandidateCall(call) {
429
+ const calleeName = getCalleeName(call.expression);
430
+ return calleeName ? DRIZZLE_QUERY_CALLEES.has(calleeName) : false;
431
+ }
432
+ function hasDatabaseCandidateCallAncestor(node) {
433
+ let current = node.parent;
434
+ while (current) {
435
+ if (ts.isCallExpression(current) && isDatabaseCandidateCall(current)) {
436
+ return true;
437
+ }
438
+ current = current.parent;
439
+ }
440
+ return false;
441
+ }
442
+ function getCalleeName(expression) {
443
+ const unwrapped = unwrapExpression(expression);
444
+ if (ts.isIdentifier(unwrapped)) {
445
+ return unwrapped.text;
446
+ }
447
+ if (ts.isPropertyAccessExpression(unwrapped)) {
448
+ return unwrapped.name.text;
449
+ }
450
+ return null;
451
+ }
452
+ function readSourceFile(filePath) {
453
+ try {
454
+ const content = sources.get(filePath);
455
+ if (content === undefined)
456
+ return null;
457
+ return ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
458
+ }
459
+ catch {
460
+ return null;
461
+ }
462
+ }
463
+ function resolveRelativeImportFile(fromFile, specifier, candidateFiles) {
464
+ if (!specifier.startsWith(".")) {
465
+ return null;
466
+ }
467
+ const basePath = path.normalize(path.resolve(path.dirname(fromFile), specifier));
468
+ for (const candidateBasePath of expandBasePaths(basePath)) {
469
+ const direct = findPathBySuffix(candidateBasePath, candidateFiles);
470
+ if (direct) {
471
+ return direct;
472
+ }
473
+ const indexResolved = findPathBySuffix(path.join(candidateBasePath, "index"), candidateFiles);
474
+ if (indexResolved) {
475
+ return indexResolved;
476
+ }
477
+ }
478
+ return null;
479
+ }
480
+ function findPathBySuffix(basePath, candidateFiles) {
481
+ if (candidateFiles.has(basePath)) {
482
+ return basePath;
483
+ }
484
+ for (const suffix of [".ts", ".tsx"]) {
485
+ const withSuffix = basePath.endsWith(suffix)
486
+ ? basePath
487
+ : `${basePath}${suffix}`;
488
+ if (candidateFiles.has(withSuffix)) {
489
+ return withSuffix;
490
+ }
491
+ }
492
+ return null;
493
+ }
494
+ function expandBasePaths(basePath) {
495
+ const results = new Set([basePath]);
496
+ for (const runtimeSuffix of [".js", ".mjs", ".cjs"]) {
497
+ if (basePath.endsWith(runtimeSuffix)) {
498
+ results.add(basePath.slice(0, -runtimeSuffix.length));
499
+ }
500
+ }
501
+ return [...results];
502
+ }
503
+ function unwrapExpression(expression) {
504
+ let current = expression;
505
+ while (ts.isAsExpression(current) ||
506
+ ts.isSatisfiesExpression(current) ||
507
+ ts.isParenthesizedExpression(current) ||
508
+ ts.isNonNullExpression(current) ||
509
+ ts.isTypeAssertionExpression(current)) {
510
+ current = current.expression;
511
+ }
512
+ return current;
513
+ }
514
+ function getFirstArgumentObject(call) {
515
+ const [firstArg] = call.arguments;
516
+ if (!firstArg) {
517
+ return null;
518
+ }
519
+ const unwrapped = unwrapExpression(firstArg);
520
+ return ts.isObjectLiteralExpression(unwrapped) ? unwrapped : null;
521
+ }
522
+ function getObjectProperty(objectLiteral, name) {
523
+ for (const property of objectLiteral.properties) {
524
+ if (!ts.isPropertyAssignment(property)) {
525
+ continue;
526
+ }
527
+ if (getPropertyNameText(property.name) === name) {
528
+ return property.initializer;
529
+ }
530
+ }
531
+ return null;
532
+ }
533
+ function getStringProperty(objectLiteral, name) {
534
+ const property = getObjectProperty(objectLiteral, name);
535
+ if (!property) {
536
+ return null;
537
+ }
538
+ const unwrapped = unwrapExpression(property);
539
+ if (ts.isStringLiteral(unwrapped) || ts.isNoSubstitutionTemplateLiteral(unwrapped)) {
540
+ return unwrapped.text;
541
+ }
542
+ return null;
543
+ }
544
+ function getTableNameFromObjectProperty(property) {
545
+ if (ts.isShorthandPropertyAssignment(property)) {
546
+ return property.name.text;
547
+ }
548
+ if (ts.isPropertyAssignment(property)) {
549
+ return getPropertyNameText(property.name);
550
+ }
551
+ return null;
552
+ }
553
+ function getBindingElementPropertyName(element) {
554
+ if (!element.propertyName) {
555
+ return null;
556
+ }
557
+ if (ts.isIdentifier(element.propertyName) || ts.isStringLiteral(element.propertyName)) {
558
+ return element.propertyName.text;
559
+ }
560
+ return null;
561
+ }
562
+ function getPropertyNameText(propertyName) {
563
+ if (ts.isIdentifier(propertyName) || ts.isStringLiteral(propertyName)) {
564
+ return propertyName.text;
565
+ }
566
+ if (ts.isNumericLiteral(propertyName)) {
567
+ return propertyName.text;
568
+ }
569
+ return null;
570
+ }
571
+ function hasExportModifier(node) {
572
+ return Boolean(ts.canHaveModifiers(node) &&
573
+ ts.getModifiers(node)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword));
574
+ }
575
+ function hasAnyLogicalDatabaseModel(cache) {
576
+ for (const moduleExports of cache.values()) {
577
+ for (const binding of moduleExports.named.values()) {
578
+ if (binding.kind === "model" || binding.kind === "table") {
579
+ return true;
580
+ }
581
+ }
582
+ }
583
+ return false;
584
+ }
585
+ function isDirectDatabaseModelFile(filePath, databaseDir) {
586
+ return (path.dirname(filePath) === databaseDir &&
587
+ DATABASE_MODEL_FILE_RE.test(path.basename(filePath)));
588
+ }
589
+ function getLineNumber(sourceFile, node) {
590
+ return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
591
+ }
592
+ function toPosix(value) {
593
+ return value.split(path.sep).join("/");
594
+ }
595
+ }
@@ -0,0 +1,3 @@
1
+ import type { ExtensionDatabaseRecord, DatabaseIssue } from "./database-source-rules.js";
2
+ export declare const DATABASE_MODEL_FILE_RE: RegExp;
3
+ export declare function validateDatabaseStructure(record: ExtensionDatabaseRecord, workspaceRoot: string, databaseDir: string): DatabaseIssue[];
@@ -0,0 +1,63 @@
1
+ import path from "node:path";
2
+ const ALLOWED_DB_ENTRIES = new Set([
3
+ "index.ts",
4
+ "install.ts",
5
+ "datasets",
6
+ "seeds",
7
+ ]);
8
+ const FORBIDDEN_DB_FILES = new Set(["bootstrap.ts"]);
9
+ export const DATABASE_MODEL_FILE_RE = /^[a-z][a-zA-Z0-9_-]*\.database\.ts$/;
10
+ export function validateDatabaseStructure(record, workspaceRoot, databaseDir) {
11
+ const issues = [];
12
+ if (!record.databaseFiles.length && !record.databaseDirs.length)
13
+ return issues;
14
+ for (const fileName of record.databaseFiles) {
15
+ if (FORBIDDEN_DB_FILES.has(fileName)) {
16
+ const filePath = path.relative(workspaceRoot, path.join(databaseDir, fileName));
17
+ issues.push({
18
+ rule: "extension-database-forbidden-file",
19
+ filePath: toPosix(filePath),
20
+ lineNumber: 1,
21
+ message: `database/ 下不允许存在文件 "${fileName}"。请将其移除或迁移。`,
22
+ });
23
+ }
24
+ }
25
+ for (const fileName of record.databaseFiles) {
26
+ if (!ALLOWED_DB_ENTRIES.has(fileName) &&
27
+ !FORBIDDEN_DB_FILES.has(fileName) &&
28
+ !DATABASE_MODEL_FILE_RE.test(fileName)) {
29
+ const filePath = path.relative(workspaceRoot, path.join(databaseDir, fileName));
30
+ issues.push({
31
+ rule: "extension-database-structure",
32
+ filePath: toPosix(filePath),
33
+ lineNumber: 1,
34
+ message: `database/ 下不允许存在文件 "${fileName}"。只允许 ${allowedEntryText()}。`,
35
+ });
36
+ }
37
+ }
38
+ for (const dirName of record.databaseDirs) {
39
+ if (!ALLOWED_DB_ENTRIES.has(dirName)) {
40
+ const dirPath = path.relative(workspaceRoot, path.join(databaseDir, dirName));
41
+ issues.push({
42
+ rule: "extension-database-structure",
43
+ filePath: toPosix(dirPath),
44
+ lineNumber: 1,
45
+ message: `database/ 下不允许存在目录 "${dirName}/"。只允许 ${allowedEntryText()}。`,
46
+ });
47
+ }
48
+ }
49
+ if (!record.databaseFiles.includes("index.ts")) {
50
+ const dbRelPath = path.relative(workspaceRoot, databaseDir);
51
+ issues.push({
52
+ rule: "extension-database-missing-index",
53
+ filePath: toPosix(dbRelPath),
54
+ lineNumber: 1,
55
+ message: `database/ 缺少 index.ts 文件。每个 database 目录必须包含 index.ts 作为入口。`,
56
+ });
57
+ }
58
+ return issues;
59
+ }
60
+ function allowedEntryText() {
61
+ return `${[...ALLOWED_DB_ENTRIES].join("、")}、*.database.ts`;
62
+ }
63
+ function toPosix(value) { return value.split(path.sep).join("/"); }
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../../external/shared/types.js";
2
+ export declare function validateServerIndexSource(filePath: string, content: string): Issue[];