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

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/README.md +6 -2
  3. package/assets/git-hooks/check-push.cjs +21 -0
  4. package/assets/git-hooks/pre-push +7 -0
  5. package/assets/git-hooks/prepare-commit-msg +5 -0
  6. package/assets/verification/heavy-test-admission-core.d.mts +1 -0
  7. package/assets/verification/heavy-test-admission-core.mjs +10 -0
  8. package/assets/verification/host-process-lock.d.mts +24 -0
  9. package/assets/verification/host-process-lock.mjs +140 -0
  10. package/assets/verification/test-heavy-admission.d.mts +4 -0
  11. package/assets/verification/test-heavy-admission.mjs +34 -0
  12. package/bin/kuafu-guard.mjs +3 -0
  13. package/bin/kuafu-verify.mjs +3 -0
  14. package/dist/cli/guard.d.ts +1 -0
  15. package/dist/cli/guard.js +59 -0
  16. package/dist/cli/verify.d.ts +1 -0
  17. package/dist/cli/verify.js +58 -0
  18. package/dist/guard/core/config.d.ts +7 -0
  19. package/dist/guard/core/config.js +159 -0
  20. package/dist/guard/core/engine.d.ts +2 -0
  21. package/dist/guard/core/engine.js +151 -0
  22. package/dist/guard/core/formatters/sarif-rules.d.ts +2 -0
  23. package/dist/guard/core/formatters/sarif-rules.js +67 -0
  24. package/dist/guard/core/formatters/sarif.d.ts +2 -0
  25. package/dist/guard/core/formatters/sarif.js +3 -0
  26. package/dist/guard/core/formatters/text.d.ts +2 -0
  27. package/dist/guard/core/formatters/text.js +22 -0
  28. package/dist/guard/core/index.d.ts +5 -0
  29. package/dist/guard/core/index.js +5 -0
  30. package/dist/guard/core/types.d.ts +94 -0
  31. package/dist/guard/core/types.js +1 -0
  32. package/dist/guard/hooks/install-rules.d.ts +2 -0
  33. package/dist/guard/hooks/install-rules.js +5 -0
  34. package/dist/guard/hooks/install.d.ts +36 -0
  35. package/dist/guard/hooks/install.js +212 -0
  36. package/dist/guard/hooks/push.d.ts +1 -0
  37. package/dist/guard/hooks/push.js +5 -0
  38. package/dist/guard/index.d.ts +16 -0
  39. package/dist/guard/index.js +16 -0
  40. package/dist/guard/product-roots.d.ts +12 -0
  41. package/dist/guard/product-roots.js +37 -0
  42. package/dist/guard/repo-args.d.ts +8 -0
  43. package/dist/guard/repo-args.js +75 -0
  44. package/dist/guard/repo-output-rules.d.ts +12 -0
  45. package/dist/guard/repo-output-rules.js +44 -0
  46. package/dist/guard/repo.d.ts +21 -0
  47. package/dist/guard/repo.js +74 -0
  48. package/dist/guard/rules/external/api/collect-orphan-tests.d.ts +2 -0
  49. package/dist/guard/rules/external/api/collect-orphan-tests.js +34 -0
  50. package/dist/guard/rules/external/api/collect.d.ts +2 -0
  51. package/dist/guard/rules/external/api/collect.js +419 -0
  52. package/dist/guard/rules/external/api/validate-orphan-tests.d.ts +2 -0
  53. package/dist/guard/rules/external/api/validate-orphan-tests.js +10 -0
  54. package/dist/guard/rules/external/api/validate.d.ts +2 -0
  55. package/dist/guard/rules/external/api/validate.js +278 -0
  56. package/dist/guard/rules/external/app/native-source-rules.d.ts +6 -0
  57. package/dist/guard/rules/external/app/native-source-rules.js +164 -0
  58. package/dist/guard/rules/external/app/validate-native-source-boundary.d.ts +2 -0
  59. package/dist/guard/rules/external/app/validate-native-source-boundary.js +187 -0
  60. package/dist/guard/rules/external/boundary/check.d.ts +4 -0
  61. package/dist/guard/rules/external/boundary/check.js +198 -0
  62. package/dist/guard/rules/external/extension/collect.d.ts +1 -0
  63. package/dist/guard/rules/external/extension/collect.js +1 -0
  64. package/dist/guard/rules/external/extension/electron-boundary-rules.d.ts +6 -0
  65. package/dist/guard/rules/external/extension/electron-boundary-rules.js +10 -0
  66. package/dist/guard/rules/external/extension/index.d.ts +6 -0
  67. package/dist/guard/rules/external/extension/index.js +29 -0
  68. package/dist/guard/rules/external/extension/manual-cross-call-rules.d.ts +19 -0
  69. package/dist/guard/rules/external/extension/manual-cross-call-rules.js +19 -0
  70. package/dist/guard/rules/external/extension/resolve-import.d.ts +1 -0
  71. package/dist/guard/rules/external/extension/resolve-import.js +43 -0
  72. package/dist/guard/rules/external/extension/scan-relative-dependencies.d.ts +5 -0
  73. package/dist/guard/rules/external/extension/scan-relative-dependencies.js +53 -0
  74. package/dist/guard/rules/external/extension/scan-source.d.ts +11 -0
  75. package/dist/guard/rules/external/extension/scan-source.js +25 -0
  76. package/dist/guard/rules/external/extension/source-import-boundary-rules.d.ts +26 -0
  77. package/dist/guard/rules/external/extension/source-import-boundary-rules.js +40 -0
  78. package/dist/guard/rules/external/extension/test-layer-import-rules.d.ts +1 -0
  79. package/dist/guard/rules/external/extension/test-layer-import-rules.js +16 -0
  80. package/dist/guard/rules/external/extension/types.d.ts +51 -0
  81. package/dist/guard/rules/external/extension/types.js +1 -0
  82. package/dist/guard/rules/external/extension/unused-export-rules.d.ts +23 -0
  83. package/dist/guard/rules/external/extension/unused-export-rules.js +119 -0
  84. package/dist/guard/rules/external/extension/unused-module-rules.d.ts +6 -0
  85. package/dist/guard/rules/external/extension/unused-module-rules.js +90 -0
  86. package/dist/guard/rules/external/extension/validate-admin.d.ts +8 -0
  87. package/dist/guard/rules/external/extension/validate-admin.js +73 -0
  88. package/dist/guard/rules/external/extension/validate-database.d.ts +3 -0
  89. package/dist/guard/rules/external/extension/validate-database.js +4 -0
  90. package/dist/guard/rules/external/extension/validate-electron-boundary.d.ts +2 -0
  91. package/dist/guard/rules/external/extension/validate-electron-boundary.js +18 -0
  92. package/dist/guard/rules/external/extension/validate-empty-dirs.d.ts +9 -0
  93. package/dist/guard/rules/external/extension/validate-empty-dirs.js +44 -0
  94. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.d.ts +2 -0
  95. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.js +116 -0
  96. package/dist/guard/rules/external/extension/validate-features.d.ts +7 -0
  97. package/dist/guard/rules/external/extension/validate-features.js +27 -0
  98. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.d.ts +2 -0
  99. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.js +131 -0
  100. package/dist/guard/rules/external/extension/validate-peer-extension-import.d.ts +2 -0
  101. package/dist/guard/rules/external/extension/validate-peer-extension-import.js +41 -0
  102. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.d.ts +2 -0
  103. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.js +48 -0
  104. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.d.ts +1 -0
  105. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.js +1 -0
  106. package/dist/guard/rules/external/extension/validate-shared-import-boundary.d.ts +2 -0
  107. package/dist/guard/rules/external/extension/validate-shared-import-boundary.js +40 -0
  108. package/dist/guard/rules/external/extension/validate-shared.d.ts +1 -0
  109. package/dist/guard/rules/external/extension/validate-shared.js +1 -0
  110. package/dist/guard/rules/external/extension/validate-test-layer-boundary.d.ts +8 -0
  111. package/dist/guard/rules/external/extension/validate-test-layer-boundary.js +59 -0
  112. package/dist/guard/rules/external/extension/validate-test-mirror.d.ts +4 -0
  113. package/dist/guard/rules/external/extension/validate-test-mirror.js +32 -0
  114. package/dist/guard/rules/external/extension/validate-top-level.d.ts +7 -0
  115. package/dist/guard/rules/external/extension/validate-top-level.js +76 -0
  116. package/dist/guard/rules/external/extension/validate-unused-export.d.ts +2 -0
  117. package/dist/guard/rules/external/extension/validate-unused-export.js +187 -0
  118. package/dist/guard/rules/external/extension/validate-unused-module.d.ts +2 -0
  119. package/dist/guard/rules/external/extension/validate-unused-module.js +38 -0
  120. package/dist/guard/rules/external/index.d.ts +1 -0
  121. package/dist/guard/rules/external/index.js +1 -0
  122. package/dist/guard/rules/external/npm-dependency-policy.d.ts +14 -0
  123. package/dist/guard/rules/external/npm-dependency-policy.js +216 -0
  124. package/dist/guard/rules/external/rules.d.ts +2 -0
  125. package/dist/guard/rules/external/rules.js +222 -0
  126. package/dist/guard/rules/external/shared/config.d.ts +3 -0
  127. package/dist/guard/rules/external/shared/config.js +14 -0
  128. package/dist/guard/rules/external/shared/diagnostic.d.ts +4 -0
  129. package/dist/guard/rules/external/shared/diagnostic.js +12 -0
  130. package/dist/guard/rules/external/shared/records.d.ts +4 -0
  131. package/dist/guard/rules/external/shared/records.js +46 -0
  132. package/dist/guard/rules/external/shared/ts-ast.d.ts +8 -0
  133. package/dist/guard/rules/external/shared/ts-ast.js +103 -0
  134. package/dist/guard/rules/external/shared/types.d.ts +159 -0
  135. package/dist/guard/rules/external/shared/types.js +1 -0
  136. package/dist/guard/rules/external/shared/validate-layer-key.d.ts +2 -0
  137. package/dist/guard/rules/external/shared/validate-layer-key.js +35 -0
  138. package/dist/guard/rules/external/story-rules.d.ts +15 -0
  139. package/dist/guard/rules/external/story-rules.js +59 -0
  140. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.d.ts +7 -0
  141. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.js +6 -0
  142. package/dist/guard/rules/shared/extension/collect.d.ts +5 -0
  143. package/dist/guard/rules/shared/extension/collect.js +231 -0
  144. package/dist/guard/rules/shared/extension/database-reference-rules.d.ts +4 -0
  145. package/dist/guard/rules/shared/extension/database-reference-rules.js +5 -0
  146. package/dist/guard/rules/shared/extension/database-source-rules.d.ts +17 -0
  147. package/dist/guard/rules/shared/extension/database-source-rules.js +595 -0
  148. package/dist/guard/rules/shared/extension/database-structure-rules.d.ts +3 -0
  149. package/dist/guard/rules/shared/extension/database-structure-rules.js +63 -0
  150. package/dist/guard/rules/shared/extension/server-index-source-rules.d.ts +2 -0
  151. package/dist/guard/rules/shared/extension/server-index-source-rules.js +91 -0
  152. package/dist/guard/rules/shared/extension/shared-consumer-rules.d.ts +23 -0
  153. package/dist/guard/rules/shared/extension/shared-consumer-rules.js +158 -0
  154. package/dist/guard/rules/shared/extension/shared-structure-rules.d.ts +6 -0
  155. package/dist/guard/rules/shared/extension/shared-structure-rules.js +27 -0
  156. package/dist/guard/rules/shared/extension/validate-database.d.ts +4 -0
  157. package/dist/guard/rules/shared/extension/validate-database.js +15 -0
  158. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.d.ts +2 -0
  159. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.js +9 -0
  160. package/dist/guard/rules/shared/extension/validate-shared.d.ts +18 -0
  161. package/dist/guard/rules/shared/extension/validate-shared.js +149 -0
  162. package/dist/host-protocol.d.ts +2 -0
  163. package/dist/host-protocol.js +2 -0
  164. package/dist/index.d.ts +3 -0
  165. package/dist/index.js +3 -0
  166. package/dist/verify/artifact-recovery-rules.d.ts +39 -0
  167. package/dist/verify/artifact-recovery-rules.js +48 -0
  168. package/dist/verify/artifact-session.d.ts +91 -0
  169. package/dist/verify/artifact-session.js +443 -0
  170. package/dist/verify/checkout-environment-rules.d.ts +2 -0
  171. package/dist/verify/checkout-environment-rules.js +25 -0
  172. package/dist/verify/checkout-environment.d.ts +8 -0
  173. package/dist/verify/checkout-environment.js +24 -0
  174. package/dist/verify/commit-candidate-rules.d.ts +3 -0
  175. package/dist/verify/commit-candidate-rules.js +33 -0
  176. package/dist/verify/commit-gate.d.ts +64 -0
  177. package/dist/verify/commit-gate.js +144 -0
  178. package/dist/verify/delivery-verification.d.ts +3 -0
  179. package/dist/verify/delivery-verification.js +21 -0
  180. package/dist/verify/e2e-registry-rules.d.ts +21 -0
  181. package/dist/verify/e2e-registry-rules.js +33 -0
  182. package/dist/verify/e2e-registry.d.ts +2 -0
  183. package/dist/verify/e2e-registry.js +45 -0
  184. package/dist/verify/full-command.d.ts +35 -0
  185. package/dist/verify/full-command.js +124 -0
  186. package/dist/verify/impact-plan-rules.d.ts +61 -0
  187. package/dist/verify/impact-plan-rules.js +185 -0
  188. package/dist/verify/impact-plan.d.ts +10 -0
  189. package/dist/verify/impact-plan.js +151 -0
  190. package/dist/verify/index.d.ts +22 -0
  191. package/dist/verify/index.js +22 -0
  192. package/dist/verify/repository-recipe-rules.d.ts +31 -0
  193. package/dist/verify/repository-recipe-rules.js +60 -0
  194. package/dist/verify/repository-recipe.d.ts +3 -0
  195. package/dist/verify/repository-recipe.js +58 -0
  196. package/dist/verify/static-validation-rules.d.ts +7 -0
  197. package/dist/verify/static-validation-rules.js +36 -0
  198. package/dist/verify/story-selection-rules.d.ts +57 -0
  199. package/dist/verify/story-selection-rules.js +125 -0
  200. package/dist/verify/types.d.ts +63 -0
  201. package/dist/verify/types.js +18 -0
  202. package/dist/verify/verification-candidate.d.ts +1 -0
  203. package/dist/verify/verification-candidate.js +20 -0
  204. package/dist/verify/verification-identity.d.ts +6 -0
  205. package/dist/verify/verification-identity.js +26 -0
  206. package/dist/verify/verification-proof-rules.d.ts +35 -0
  207. package/dist/verify/verification-proof-rules.js +45 -0
  208. package/dist/verify/verify-full-command-rules.d.ts +18 -0
  209. package/dist/verify/verify-full-command-rules.js +77 -0
  210. package/dist/verify/verify-full-runner.d.ts +32 -0
  211. package/dist/verify/verify-full-runner.js +251 -0
  212. package/package.json +115 -4
@@ -0,0 +1,278 @@
1
+ import { validateLayerKey } from "../shared/validate-layer-key.js";
2
+ export function validateApiRouteRecord(record) {
3
+ const issues = validateLayerKey(record, "api-route-path-valid");
4
+ // 同一路径的预检独立成文件;仅OPTIONS解释此后缀,方法与实际路径仍须严格一致。
5
+ const routeKey = record.routeMethod === "OPTIONS" ? record.layerKey.replace(/-options$/u, "") : record.layerKey;
6
+ const expectedPath = `/${routeKey}`;
7
+ const expectedModuleShape = "`const route = defineRoute({ ... }); export default defineApi(route, async (c, input) => { ... });`";
8
+ const sharedGuidance = "Keep one-off helper logic inside the current API file. If multiple APIs share it, move only that shared helper to `src/shared/` and import it back.";
9
+ if (!record.routeBindingFound &&
10
+ !record.defineRouteCallFound &&
11
+ !record.defineApiCallFound) {
12
+ issues.push({
13
+ rule: "api-route-format",
14
+ filePath: record.relativeFilePath,
15
+ lineNumber: 1,
16
+ message: [
17
+ "Files under `src/server/` must be concrete API modules.",
18
+ `Found a non-API file here. ${sharedGuidance}`,
19
+ `Only the root \`src/server/index.ts\` may be a non-API entry file.`,
20
+ `Expected shape: ${expectedModuleShape}`,
21
+ ].join(" "),
22
+ });
23
+ return issues;
24
+ }
25
+ if (!record.routeDefinitionValid) {
26
+ issues.push({
27
+ rule: "api-route-format",
28
+ filePath: record.relativeFilePath,
29
+ lineNumber: record.routeDefinitionLine,
30
+ message: [
31
+ "Declare the route in this file as a top-level `const route = defineRoute({ ... })`.",
32
+ "Use the exact variable name `route` and call `defineRoute` directly in this file.",
33
+ sharedGuidance,
34
+ ].join(" "),
35
+ });
36
+ }
37
+ if (!record.defineApiExportValid) {
38
+ issues.push({
39
+ rule: "api-route-format",
40
+ filePath: record.relativeFilePath,
41
+ lineNumber: record.defineApiExportLine,
42
+ message: [
43
+ "Export the API exactly as `export default defineApi(route, async (...) => { ... })`.",
44
+ "Do not wrap it in another symbol or leave helper-only files under `src/server/`.",
45
+ sharedGuidance,
46
+ ].join(" "),
47
+ });
48
+ }
49
+ else {
50
+ if (!record.defineApiRouteArgValid) {
51
+ issues.push({
52
+ rule: "api-route-format",
53
+ filePath: record.relativeFilePath,
54
+ lineNumber: record.defineApiRouteArgLine,
55
+ message: "Pass the top-level `route` identifier as the first argument: `export default defineApi(route, async (...) => { ... })`.",
56
+ });
57
+ }
58
+ if (!record.defineApiHandlerValid) {
59
+ issues.push({
60
+ rule: "api-route-format",
61
+ filePath: record.relativeFilePath,
62
+ lineNumber: record.defineApiHandlerLine,
63
+ message: [
64
+ "Pass the handler inline as an async arrow function.",
65
+ "Allowed forms are `async () => {}`, `async (c) => {}`, and `async (c, input) => {}`.",
66
+ "Do not extract the API handler into another `src/server/` helper file.",
67
+ ].join(" "),
68
+ });
69
+ }
70
+ }
71
+ if (record.routeDefinitionValid && record.defineApiExportValid) {
72
+ if (!record.routeBeforeDefineApi) {
73
+ issues.push({
74
+ rule: "api-route-order",
75
+ filePath: record.relativeFilePath,
76
+ lineNumber: record.routeBeforeDefineApiLine,
77
+ message: "API 文件声明顺序错误。请先声明 `const route = defineRoute(...)`,再声明 `export default defineApi(route, async (...) => { ... })`。",
78
+ });
79
+ }
80
+ for (const line of record.helperFunctionBeforeDefineApiLines) {
81
+ issues.push({
82
+ rule: "api-route-order",
83
+ filePath: record.relativeFilePath,
84
+ lineNumber: line,
85
+ message: "顶层辅助函数必须放在 `export default defineApi(...)` 之后。非函数常量不受此规则限制。",
86
+ });
87
+ }
88
+ }
89
+ if (!record.routeDefinitionValid) {
90
+ return issues;
91
+ }
92
+ if (record.routeAccessLevel === null) {
93
+ issues.push({
94
+ rule: "api-route-access-control",
95
+ filePath: record.relativeFilePath,
96
+ lineNumber: record.routeAccessLevelLine,
97
+ message: "Unable to resolve defineRoute({ accessLevel }) as a string literal.",
98
+ });
99
+ }
100
+ if (record.routeAccessLevel !== null
101
+ && record.routeAccessLevel !== "public"
102
+ && record.routeAuthScope === null) {
103
+ issues.push({
104
+ rule: "api-route-access-control",
105
+ filePath: record.relativeFilePath,
106
+ lineNumber: record.routeAccessLevelLine,
107
+ message: "Private/protected routes must declare `authScope: \"admin\" | \"account\"`.",
108
+ });
109
+ }
110
+ if (record.routeAccessLevel === "public" && record.routeAuthScope !== null) {
111
+ issues.push({
112
+ rule: "api-route-access-control",
113
+ filePath: record.relativeFilePath,
114
+ lineNumber: record.routeAuthScopeLine,
115
+ message: "`accessLevel: \"public\"` must not declare `authScope`.",
116
+ });
117
+ }
118
+ if (record.routeAccessLevel === "private"
119
+ && record.routeAuthScope !== null
120
+ && record.routeAuthScope !== "admin"
121
+ && record.routeAuthScope !== "account") {
122
+ issues.push({
123
+ rule: "api-route-access-control",
124
+ filePath: record.relativeFilePath,
125
+ lineNumber: record.routeAuthScopeLine,
126
+ message: "`accessLevel: \"private\"` only supports `authScope: \"admin\" | \"account\"`.",
127
+ });
128
+ }
129
+ if (record.routeAccessLevel === "protected") {
130
+ if (record.routeAuthScope === null) {
131
+ issues.push({
132
+ rule: "api-route-access-control",
133
+ filePath: record.relativeFilePath,
134
+ lineNumber: record.routeAccessLevelLine,
135
+ message: "`accessLevel: \"protected\"` must declare `authScope: \"admin\"`.",
136
+ });
137
+ }
138
+ else if (record.routeAuthScope !== "admin") {
139
+ issues.push({
140
+ rule: "api-route-access-control",
141
+ filePath: record.relativeFilePath,
142
+ lineNumber: record.routeAuthScopeLine,
143
+ message: "`accessLevel: \"protected\"` must use `authScope: \"admin\"`.",
144
+ });
145
+ }
146
+ if (!record.requirePermissionsFound) {
147
+ issues.push({
148
+ rule: "api-route-access-control",
149
+ filePath: record.relativeFilePath,
150
+ lineNumber: record.routeAccessLevelLine,
151
+ message: "`accessLevel: \"protected\"` must declare a non-empty `requirePermissions` array.",
152
+ });
153
+ }
154
+ else if (record.requirePermissionsCount === null) {
155
+ issues.push({
156
+ rule: "api-route-access-control",
157
+ filePath: record.relativeFilePath,
158
+ lineNumber: record.requirePermissionsLine,
159
+ message: "Declare `requirePermissions` as a static non-empty array literal on protected routes.",
160
+ });
161
+ }
162
+ else if (record.requirePermissionsCount <= 0) {
163
+ issues.push({
164
+ rule: "api-route-access-control",
165
+ filePath: record.relativeFilePath,
166
+ lineNumber: record.requirePermissionsLine,
167
+ message: "`requirePermissions` must contain at least one permission on protected routes.",
168
+ });
169
+ }
170
+ }
171
+ if (record.requirePermissionsFound &&
172
+ record.routeAccessLevel !== null &&
173
+ record.routeAccessLevel !== "protected") {
174
+ issues.push({
175
+ rule: "api-route-access-control",
176
+ filePath: record.relativeFilePath,
177
+ lineNumber: record.requirePermissionsLine,
178
+ message: "`requirePermissions` can only be used with `accessLevel: \"protected\"`.",
179
+ });
180
+ }
181
+ if (record.forbiddenOperationIdFound) {
182
+ issues.push({
183
+ rule: "api-route-forbidden-operation-id",
184
+ filePath: record.relativeFilePath,
185
+ lineNumber: record.forbiddenOperationIdLine,
186
+ message: [
187
+ "Do not hand-write `operationId` inside `defineRoute({ ... })`.",
188
+ "Remove this field from the source file.",
189
+ "If a stable operation identifier is needed later, generate it in build tooling from `extensionId + route path`, not in route source.",
190
+ ].join(" "),
191
+ });
192
+ }
193
+ for (const field of record.forbiddenClientCacheFields) {
194
+ issues.push({
195
+ rule: "api-route-client-cache-boundary",
196
+ filePath: record.relativeFilePath,
197
+ lineNumber: field.lineNumber,
198
+ message: [
199
+ `Do not declare \`${field.name}\` inside \`defineRoute({ ... })\`.`,
200
+ "Server routes describe transport, authentication, permissions, and schemas only.",
201
+ "Declare client caching at the consuming `defineAdminResource(...)` call instead.",
202
+ ].join(" "),
203
+ });
204
+ }
205
+ if (record.routePath === null) {
206
+ issues.push({
207
+ rule: "api-route-shape-match",
208
+ filePath: record.relativeFilePath,
209
+ lineNumber: record.routePathLine,
210
+ message: "Unable to resolve defineRoute({ path }) as a string literal.",
211
+ });
212
+ }
213
+ else if (record.routePath !== expectedPath) {
214
+ issues.push({
215
+ rule: "api-route-shape-match",
216
+ filePath: record.relativeFilePath,
217
+ lineNumber: record.routePathLine,
218
+ message: `Expected route path "${expectedPath}", received "${record.routePath}".`,
219
+ });
220
+ }
221
+ if (record.routeMethod === null) {
222
+ issues.push({
223
+ rule: "api-route-shape-match",
224
+ filePath: record.relativeFilePath,
225
+ lineNumber: record.routeMethodLine,
226
+ message: "Unable to resolve defineRoute({ method }) as a string literal.",
227
+ });
228
+ }
229
+ // 无独立Route镜像不代表缺少行为覆盖;实际handler/规则Unit和合并核心生命周期由语义审计选择。
230
+ if (!record.matchingTestFileFound)
231
+ return issues;
232
+ const productRoot = record.relativeFilePath.split("/src/server/")[0];
233
+ const mappedPaths = ["unit", "integration"].flatMap(layer => ["ts", "tsx"].map(extension => `${productRoot}/test/${layer}/server/${record.layerKey}.test.${extension}`));
234
+ if (!mappedPaths.includes(record.expectedTestFilePath)) {
235
+ issues.push({ rule: "api-route-test-file-match", filePath: record.expectedTestFilePath,
236
+ lineNumber: 1, message: "已有API镜像测试必须位于 test/unit/server 或 test/integration/server,并保持与 src/server 相同的相对源路径。" });
237
+ return issues;
238
+ }
239
+ if (!record.testPathFound || !record.testPathUsesBasePath || record.testPathSuffix === null) {
240
+ issues.push({
241
+ rule: "api-route-test-path-match",
242
+ filePath: record.expectedTestFilePath,
243
+ lineNumber: record.testPathLine,
244
+ message: `Declare a top-level PATH constant exactly as \`const PATH = \`\${BASE_PATH}${expectedPath}\`;\`. Reuse this constant for the route under test instead of hand-writing the path elsewhere.`,
245
+ });
246
+ return issues;
247
+ }
248
+ if (record.testPathSuffix !== expectedPath) {
249
+ issues.push({
250
+ rule: "api-route-test-path-match",
251
+ filePath: record.expectedTestFilePath,
252
+ lineNumber: record.testPathLine,
253
+ message: `Expected PATH to resolve to \`\${BASE_PATH}${expectedPath}\`, received \`\${BASE_PATH}${record.testPathSuffix}\`. Keep the test file path constant aligned with the API route exactly.`,
254
+ });
255
+ }
256
+ if (record.routeMethod === null || record.routePath === null) {
257
+ return issues;
258
+ }
259
+ const expectedDescribePrefix = `${record.routeMethod} `;
260
+ if (!record.testDescribeUsesPath || record.testDescribePrefix === null) {
261
+ issues.push({
262
+ rule: "api-route-test-describe-match",
263
+ filePath: record.expectedTestFilePath,
264
+ lineNumber: record.testDescribeLine,
265
+ message: `Write the top-level describe exactly as \`describe("${record.routeMethod} " + PATH, () => { ... })\`. Do not duplicate the route string literal here; compose it from the HTTP method and PATH.`,
266
+ });
267
+ return issues;
268
+ }
269
+ if (record.testDescribePrefix !== expectedDescribePrefix) {
270
+ issues.push({
271
+ rule: "api-route-test-describe-match",
272
+ filePath: record.expectedTestFilePath,
273
+ lineNumber: record.testDescribeLine,
274
+ message: `Expected top-level describe to start with \`${expectedDescribePrefix}\` and concatenate PATH: \`describe("${record.routeMethod} " + PATH, () => { ... })\`.`,
275
+ });
276
+ }
277
+ return issues;
278
+ }
@@ -0,0 +1,6 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function readForbiddenReason(fileName: string, relativeToApp: string, isDirectory: boolean): string;
3
+ export declare function findJsonKeyLine(source: string, key: string): number;
4
+ export declare function inspectNativeModuleManifest(relativeManifestPath: string, source: string): Issue[];
5
+ export declare function inspectNativeBaseDependencySource(filePath: string, source: string): Issue[];
6
+ export declare function inspectNativeBaseSource(filePath: string, source: string): Issue[];
@@ -0,0 +1,164 @@
1
+ import path from "node:path";
2
+ const FORBIDDEN_EXTENSIONS = new Set([
3
+ ".java",
4
+ ".kt",
5
+ ".kts",
6
+ ".m",
7
+ ".mm",
8
+ ".podspec",
9
+ ".swift",
10
+ ]);
11
+ const FORBIDDEN_FILE_NAMES = new Set([
12
+ "androidmanifest.xml",
13
+ "build.gradle",
14
+ "build.gradle.kts",
15
+ "podfile",
16
+ "settings.gradle",
17
+ "settings.gradle.kts",
18
+ ]);
19
+ export function readForbiddenReason(fileName, relativeToApp, isDirectory) {
20
+ const lowerName = fileName.toLowerCase();
21
+ if (isDirectory && lowerName.endsWith(".xcodeproj")) {
22
+ return `业务 App 禁止包含 Xcode 工程 ${relativeToApp}`;
23
+ }
24
+ if (isDirectory) {
25
+ return "";
26
+ }
27
+ if (FORBIDDEN_EXTENSIONS.has(path.extname(lowerName))) {
28
+ return `业务 App 禁止包含 OS Native 源码 ${relativeToApp}`;
29
+ }
30
+ if (FORBIDDEN_FILE_NAMES.has(lowerName)) {
31
+ return `业务 App 禁止包含 Native 构建文件 ${relativeToApp}`;
32
+ }
33
+ if (lowerName.startsWith("react-native.config.")) {
34
+ return `业务 App 禁止包含本地 Native module linking 配置 ${relativeToApp}`;
35
+ }
36
+ const segments = relativeToApp.toLowerCase().split("/");
37
+ if (lowerName === "native-module.json" && segments.includes("native")) {
38
+ return `业务 App 禁止包含本地 Native module metadata ${relativeToApp}`;
39
+ }
40
+ return "";
41
+ }
42
+ export function findJsonKeyLine(source, key) {
43
+ const index = source.indexOf(JSON.stringify(key));
44
+ if (index < 0) {
45
+ return 1;
46
+ }
47
+ return source.slice(0, index).split(/\r?\n/u).length;
48
+ }
49
+ export function inspectNativeModuleManifest(relativeManifestPath, source) {
50
+ let manifest;
51
+ try {
52
+ manifest = JSON.parse(source);
53
+ }
54
+ catch {
55
+ // product.json 的 JSON 合法性由 Product manifest/build 合同负责;本规则只负责 Native 边界。
56
+ return [];
57
+ }
58
+ if (!isRecord(manifest)) {
59
+ return [];
60
+ }
61
+ const moduleLists = [
62
+ { contractPath: "nativeModules", value: manifest.nativeModules },
63
+ ];
64
+ if (isRecord(manifest.build)) {
65
+ moduleLists.push({
66
+ contractPath: "build.nativeModules",
67
+ value: manifest.build.nativeModules,
68
+ });
69
+ }
70
+ const issues = [];
71
+ for (const moduleList of moduleLists) {
72
+ if (!Array.isArray(moduleList.value)) {
73
+ continue;
74
+ }
75
+ for (const [index, moduleEntry] of moduleList.value.entries()) {
76
+ if (!isRecord(moduleEntry) || !Object.hasOwn(moduleEntry, "path")) {
77
+ continue;
78
+ }
79
+ issues.push({
80
+ rule: "app-native-source-boundary",
81
+ filePath: relativeManifestPath,
82
+ lineNumber: findJsonKeyLine(source, "nativeModules"),
83
+ message: `${moduleList.contractPath}[${index}].path 不受支持。普通业务 App 必须通过已登记 Native module 的 id、version 与 compatibility 声明能力,不能引用本地 Native 源码路径。`,
84
+ });
85
+ }
86
+ }
87
+ return issues;
88
+ }
89
+ function isRecord(value) {
90
+ return typeof value === "object" && value !== null && !Array.isArray(value);
91
+ }
92
+ const NATIVE_BASE_PACKAGE = "@repo/native-base";
93
+ function extractModuleSpecifiers(line) {
94
+ const specifiers = [];
95
+ const staticImportPattern = /\b(?:import|export)\b(?:[^"']*?\bfrom\b)?\s*["']([^"']+)["']/gu;
96
+ for (const match of line.matchAll(staticImportPattern)) {
97
+ const specifier = match[1]?.trim();
98
+ if (specifier) {
99
+ specifiers.push(specifier);
100
+ }
101
+ }
102
+ const runtimeImportPattern = /\b(?:import\s*\(\s*|require\s*\(\s*)["']([^"']+)["']\s*\)/gu;
103
+ for (const match of line.matchAll(runtimeImportPattern)) {
104
+ const specifier = match[1]?.trim();
105
+ if (specifier) {
106
+ specifiers.push(specifier);
107
+ }
108
+ }
109
+ return specifiers;
110
+ }
111
+ function isNativeBaseSpecifier(specifier) {
112
+ return specifier === NATIVE_BASE_PACKAGE || specifier.startsWith(`${NATIVE_BASE_PACKAGE}/`);
113
+ }
114
+ export function inspectNativeBaseDependencySource(filePath, source) {
115
+ let manifest;
116
+ try {
117
+ manifest = JSON.parse(source);
118
+ }
119
+ catch {
120
+ return [];
121
+ }
122
+ if (!isRecord(manifest)) {
123
+ return [];
124
+ }
125
+ const issues = [];
126
+ for (const field of [
127
+ "dependencies",
128
+ "devDependencies",
129
+ "optionalDependencies",
130
+ "peerDependencies",
131
+ ]) {
132
+ const dependencies = manifest[field];
133
+ if (!isRecord(dependencies) || !Object.hasOwn(dependencies, NATIVE_BASE_PACKAGE)) {
134
+ continue;
135
+ }
136
+ issues.push({
137
+ rule: "app-native-source-boundary",
138
+ filePath: filePath,
139
+ lineNumber: findJsonKeyLine(source, NATIVE_BASE_PACKAGE),
140
+ message: `${field} 禁止声明 ${JSON.stringify(NATIVE_BASE_PACKAGE)}。` +
141
+ "普通业务 App 不安装 Native Base;Native 源码、Host 合成与设备构建归 kuafu-native-base。",
142
+ });
143
+ }
144
+ return issues;
145
+ }
146
+ export function inspectNativeBaseSource(filePath, source) {
147
+ const issues = [];
148
+ const lines = source.split(/\r?\n/u);
149
+ for (let index = 0; index < lines.length; index += 1) {
150
+ const forbiddenSpecifier = extractModuleSpecifiers(lines[index] ?? "")
151
+ .find(isNativeBaseSpecifier);
152
+ if (!forbiddenSpecifier) {
153
+ continue;
154
+ }
155
+ issues.push({
156
+ rule: "app-native-source-boundary",
157
+ filePath: filePath,
158
+ lineNumber: index + 1,
159
+ message: `普通业务 App 禁止 import ${JSON.stringify(forbiddenSpecifier)}。` +
160
+ "App 只能依赖 App Base 的公开 package、协议、API 与不可变制品,不能安装或解析 Native Base 源码。",
161
+ });
162
+ }
163
+ return issues;
164
+ }
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function collectAppNativeSourceBoundaryIssues(workspaceRoot: string): Promise<Issue[]>;
@@ -0,0 +1,187 @@
1
+ import { readForbiddenReason, inspectNativeModuleManifest, inspectNativeBaseDependencySource, inspectNativeBaseSource } from "./native-source-rules.js";
2
+ import { lstat, readFile, readdir } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { collectGuardProductRoots } from "../../../product-roots.js";
5
+ const SKIPPED_DIRECTORIES = new Set([
6
+ ".build",
7
+ ".cache",
8
+ ".git",
9
+ ".nx",
10
+ "coverage",
11
+ "dist",
12
+ "node_modules",
13
+ ]);
14
+ const SOURCE_EXTENSIONS = new Set([
15
+ ".cjs",
16
+ ".cts",
17
+ ".js",
18
+ ".jsx",
19
+ ".mjs",
20
+ ".mts",
21
+ ".ts",
22
+ ".tsx",
23
+ ]);
24
+ export async function collectAppNativeSourceBoundaryIssues(workspaceRoot) {
25
+ const issues = await collectProjectNativeBaseIssues(workspaceRoot);
26
+ for (const { root: appRoot } of await collectGuardProductRoots(workspaceRoot, "app")) {
27
+ await collectForbiddenPaths({ workspaceRoot, appRoot, currentRoot: appRoot, issues });
28
+ issues.push(...await collectManifestNativeModulePathIssues(workspaceRoot, appRoot));
29
+ issues.push(...await collectNativeBaseReferenceIssues(workspaceRoot, appRoot));
30
+ }
31
+ return issues;
32
+ }
33
+ async function collectNativeBaseReferenceIssues(workspaceRoot, appRoot) {
34
+ const issues = [];
35
+ const sourceFiles = await collectSourceFiles(appRoot);
36
+ for (const sourceFile of sourceFiles) {
37
+ const source = await readFile(sourceFile, "utf8");
38
+ issues.push(...inspectNativeBaseSource(toPosix(path.relative(workspaceRoot, sourceFile)), source));
39
+ }
40
+ return issues;
41
+ }
42
+ async function collectProjectNativeBaseIssues(workspaceRoot) {
43
+ if (!(await isProjectRepository(workspaceRoot))) {
44
+ return [];
45
+ }
46
+ const issues = [];
47
+ const nativeBaseRoot = path.join(workspaceRoot, "packages", "native-base");
48
+ if (await pathExists(nativeBaseRoot)) {
49
+ issues.push({
50
+ rule: "app-native-source-boundary",
51
+ filePath: "packages/native-base",
52
+ lineNumber: 1,
53
+ message: "普通业务仓禁止包含 packages/native-base。Native Base 源码、Host 合成与设备构建归独立 kuafu-native-base 仓库。",
54
+ });
55
+ }
56
+ const packagePaths = await collectFilesNamed(workspaceRoot, "package.json");
57
+ for (const packagePath of packagePaths) {
58
+ issues.push(...await collectNativeBaseDependencyIssues(workspaceRoot, packagePath));
59
+ }
60
+ return issues;
61
+ }
62
+ async function collectNativeBaseDependencyIssues(workspaceRoot, packagePath) {
63
+ let source;
64
+ try {
65
+ source = await readFile(packagePath, "utf8");
66
+ }
67
+ catch (error) {
68
+ if (isMissingFileError(error)) {
69
+ return [];
70
+ }
71
+ throw error;
72
+ }
73
+ return inspectNativeBaseDependencySource(toPosix(path.relative(workspaceRoot, packagePath)), source);
74
+ }
75
+ async function isProjectRepository(workspaceRoot) {
76
+ try {
77
+ const manifest = JSON.parse(await readFile(path.join(workspaceRoot, "package.json"), "utf8"));
78
+ return isRecord(manifest)
79
+ && isRecord(manifest.kuafucloud)
80
+ && manifest.kuafucloud.classification === "project";
81
+ }
82
+ catch (error) {
83
+ if (isMissingFileError(error) || error instanceof SyntaxError) {
84
+ return false;
85
+ }
86
+ throw error;
87
+ }
88
+ }
89
+ async function collectFilesNamed(root, fileName) {
90
+ const result = [];
91
+ const entries = await readdir(root, { withFileTypes: true });
92
+ entries.sort((left, right) => left.name.localeCompare(right.name));
93
+ for (const entry of entries) {
94
+ if (entry.isDirectory() && SKIPPED_DIRECTORIES.has(entry.name)) {
95
+ continue;
96
+ }
97
+ const absolutePath = path.join(root, entry.name);
98
+ if (entry.isDirectory()) {
99
+ result.push(...await collectFilesNamed(absolutePath, fileName));
100
+ continue;
101
+ }
102
+ if (entry.name === fileName) {
103
+ result.push(absolutePath);
104
+ }
105
+ }
106
+ return result;
107
+ }
108
+ async function pathExists(filePath) {
109
+ try {
110
+ await lstat(filePath);
111
+ return true;
112
+ }
113
+ catch (error) {
114
+ if (isMissingFileError(error)) {
115
+ return false;
116
+ }
117
+ throw error;
118
+ }
119
+ }
120
+ async function collectSourceFiles(root) {
121
+ const result = [];
122
+ const entries = await readdir(root, { withFileTypes: true });
123
+ entries.sort((left, right) => left.name.localeCompare(right.name));
124
+ for (const entry of entries) {
125
+ if (entry.isDirectory() && SKIPPED_DIRECTORIES.has(entry.name)) {
126
+ continue;
127
+ }
128
+ const absolutePath = path.join(root, entry.name);
129
+ if (entry.isDirectory()) {
130
+ result.push(...await collectSourceFiles(absolutePath));
131
+ continue;
132
+ }
133
+ if (SOURCE_EXTENSIONS.has(path.extname(entry.name).toLowerCase())) {
134
+ result.push(absolutePath);
135
+ }
136
+ }
137
+ return result;
138
+ }
139
+ async function collectForbiddenPaths(input) {
140
+ const entries = await readdir(input.currentRoot, { withFileTypes: true });
141
+ entries.sort((left, right) => left.name.localeCompare(right.name));
142
+ for (const entry of entries) {
143
+ if (entry.isDirectory() && SKIPPED_DIRECTORIES.has(entry.name)) {
144
+ continue;
145
+ }
146
+ const absolutePath = path.join(input.currentRoot, entry.name);
147
+ const relativeToApp = toPosix(path.relative(input.appRoot, absolutePath));
148
+ const forbiddenReason = readForbiddenReason(entry.name, relativeToApp, entry.isDirectory());
149
+ if (forbiddenReason) {
150
+ input.issues.push({
151
+ rule: "app-native-source-boundary",
152
+ filePath: toPosix(path.relative(input.workspaceRoot, absolutePath)),
153
+ lineNumber: 1,
154
+ message: `${forbiddenReason}。普通业务 App 只允许 JS/TS、静态资源、JS bundle 与 Product manifest;Native 实现归 kuafu-native-base。`,
155
+ });
156
+ if (entry.isDirectory()) {
157
+ continue;
158
+ }
159
+ }
160
+ if (entry.isDirectory()) {
161
+ await collectForbiddenPaths({ ...input, currentRoot: absolutePath });
162
+ }
163
+ }
164
+ }
165
+ async function collectManifestNativeModulePathIssues(workspaceRoot, appRoot) {
166
+ const manifestPath = path.join(appRoot, "product.json");
167
+ let source;
168
+ try {
169
+ source = await readFile(manifestPath, "utf8");
170
+ }
171
+ catch (error) {
172
+ if (isMissingFileError(error)) {
173
+ return [];
174
+ }
175
+ throw error;
176
+ }
177
+ return inspectNativeModuleManifest(toPosix(path.relative(workspaceRoot, manifestPath)), source);
178
+ }
179
+ function isMissingFileError(error) {
180
+ return error instanceof Error && "code" in error && error.code === "ENOENT";
181
+ }
182
+ function isRecord(value) {
183
+ return typeof value === "object" && value !== null && !Array.isArray(value);
184
+ }
185
+ function toPosix(value) {
186
+ return value.split(path.sep).join("/");
187
+ }
@@ -0,0 +1,4 @@
1
+ import type { GuardRuleDefinition } from "../../../core/types.js";
2
+ import type { Issue } from "../shared/types.js";
3
+ export declare function collectDependencyBoundaryIssues(workspaceRoot: string): Promise<Issue[]>;
4
+ export declare const dependencyBoundaryRule: GuardRuleDefinition;