@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,131 @@
1
+ import { expressionContainsBuildApiPath } from "./manual-cross-call-rules.js";
2
+ import path from "node:path";
3
+ import ts from "typescript";
4
+ import { getLineNumber, parseSourceFile } from "../shared/ts-ast.js";
5
+ import { collectExtensionSourceRecords, toPosix } from "./scan-source.js";
6
+ export async function collectManualCrossExtensionCallIssues(workspaceRoot) {
7
+ const records = await collectExtensionSourceRecords(workspaceRoot, isGuardedSourceFile);
8
+ const issues = [];
9
+ for (const record of records) {
10
+ const serverRoot = path.join(record.srcRoot, "server");
11
+ const serverFiles = record.srcFiles.filter((filePath) => isPathInDirectory(filePath, serverRoot));
12
+ for (const serverFile of serverFiles) {
13
+ const sourceFile = await parseSourceFile(serverFile);
14
+ const buildApiPathIdentifiers = collectBuildApiPathIdentifiers(sourceFile);
15
+ if (buildApiPathIdentifiers.size === 0) {
16
+ continue;
17
+ }
18
+ const variableInitializerByName = collectVariableInitializers(sourceFile);
19
+ const diagnostics = collectFileDiagnostics({
20
+ sourceFile,
21
+ buildApiPathIdentifiers,
22
+ variableInitializerByName,
23
+ });
24
+ issues.push(...diagnostics.map((diagnostic) => ({
25
+ rule: "extension-no-manual-cross-extension-call",
26
+ filePath: toPosix(path.relative(workspaceRoot, serverFile)),
27
+ lineNumber: diagnostic.lineNumber,
28
+ message: "扩展 server 不允许手写 fetch(buildApiPath(...)) 互调样板。请改用 createInteropClient 或统一签名 helper。",
29
+ })));
30
+ }
31
+ }
32
+ return issues;
33
+ }
34
+ function collectFileDiagnostics(input) {
35
+ const diagnostics = [];
36
+ const seen = new Set();
37
+ const visit = (node) => {
38
+ if (!ts.isCallExpression(node) || !isFetchCall(node.expression)) {
39
+ ts.forEachChild(node, visit);
40
+ return;
41
+ }
42
+ const firstArg = node.arguments[0];
43
+ if (!firstArg) {
44
+ ts.forEachChild(node, visit);
45
+ return;
46
+ }
47
+ if (expressionContainsBuildApiPath(readCrossCallExpression(firstArg), {
48
+ buildApiPathIdentifiers: input.buildApiPathIdentifiers,
49
+ variableInitializerByName: new Map([...input.variableInitializerByName].map(([name, expression]) => [name, readCrossCallExpression(expression)])),
50
+ visiting: new Set(),
51
+ })) {
52
+ const lineNumber = getLineNumber(input.sourceFile, firstArg);
53
+ if (!seen.has(lineNumber)) {
54
+ seen.add(lineNumber);
55
+ diagnostics.push({ lineNumber });
56
+ }
57
+ }
58
+ ts.forEachChild(node, visit);
59
+ };
60
+ ts.forEachChild(input.sourceFile, visit);
61
+ return diagnostics;
62
+ }
63
+ function collectBuildApiPathIdentifiers(sourceFile) {
64
+ const identifiers = new Set();
65
+ for (const statement of sourceFile.statements) {
66
+ if (!ts.isImportDeclaration(statement)
67
+ || !ts.isStringLiteral(statement.moduleSpecifier)
68
+ || statement.moduleSpecifier.text !== "@kuafucloud/extension-kit/routing"
69
+ || !statement.importClause?.namedBindings
70
+ || !ts.isNamedImports(statement.importClause.namedBindings)) {
71
+ continue;
72
+ }
73
+ for (const specifier of statement.importClause.namedBindings.elements) {
74
+ if (specifier.propertyName?.text === "buildApiPath") {
75
+ identifiers.add(specifier.name.text);
76
+ }
77
+ if (!specifier.propertyName && specifier.name.text === "buildApiPath") {
78
+ identifiers.add(specifier.name.text);
79
+ }
80
+ }
81
+ }
82
+ return identifiers;
83
+ }
84
+ function collectVariableInitializers(sourceFile) {
85
+ const initializerByName = new Map();
86
+ const visit = (node) => {
87
+ if (ts.isVariableDeclaration(node)
88
+ && ts.isIdentifier(node.name)
89
+ && node.initializer) {
90
+ initializerByName.set(node.name.text, node.initializer);
91
+ }
92
+ ts.forEachChild(node, visit);
93
+ };
94
+ ts.forEachChild(sourceFile, visit);
95
+ return initializerByName;
96
+ }
97
+ function readCrossCallExpression(expression) {
98
+ if (ts.isIdentifier(expression))
99
+ return { kind: "identifier", name: expression.text };
100
+ if (ts.isCallExpression(expression))
101
+ return { kind: "call", ...(ts.isIdentifier(expression.expression) ? { callee: expression.expression.text } : {}), arguments: expression.arguments.map(readCrossCallExpression) };
102
+ if (ts.isBinaryExpression(expression) && expression.operatorToken.kind === ts.SyntaxKind.PlusToken)
103
+ return { kind: "children", children: [readCrossCallExpression(expression.left), readCrossCallExpression(expression.right)] };
104
+ if (ts.isTemplateExpression(expression))
105
+ return { kind: "children", children: expression.templateSpans.map(span => readCrossCallExpression(span.expression)) };
106
+ if (ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression) || ts.isTypeAssertionExpression(expression))
107
+ return readCrossCallExpression(expression.expression);
108
+ if (ts.isConditionalExpression(expression))
109
+ return { kind: "children", children: [readCrossCallExpression(expression.whenTrue), readCrossCallExpression(expression.whenFalse)] };
110
+ return { kind: "other" };
111
+ }
112
+ function isFetchCall(expression) {
113
+ return ((ts.isIdentifier(expression) && expression.text === "fetch")
114
+ || (ts.isPropertyAccessExpression(expression)
115
+ && expression.name.text === "fetch"));
116
+ }
117
+ function isPathInDirectory(filePath, directory) {
118
+ const normalizedDirectory = directory.endsWith(path.sep)
119
+ ? directory
120
+ : `${directory}${path.sep}`;
121
+ return filePath.startsWith(normalizedDirectory);
122
+ }
123
+ function isGuardedSourceFile(filePath) {
124
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
125
+ return false;
126
+ }
127
+ if (filePath.endsWith(".d.ts")) {
128
+ return false;
129
+ }
130
+ return true;
131
+ }
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function collectPeerExtensionImportIssues(workspaceRoot: string): Promise<Issue[]>;
@@ -0,0 +1,41 @@
1
+ import path from "node:path";
2
+ import ts from "typescript";
3
+ import { getLineNumber, parseSourceFile } from "../shared/ts-ast.js";
4
+ import { collectExtensionSourceRecords, toPosix } from "./scan-source.js";
5
+ import { SAME_LAYER_EXTENSIONS, inspectPeerExtensionImport } from "./source-import-boundary-rules.js";
6
+ export async function collectPeerExtensionImportIssues(workspaceRoot) {
7
+ const records = await collectExtensionSourceRecords(workspaceRoot, isGuardedSourceFile);
8
+ const issues = [];
9
+ for (const record of records) {
10
+ if (!SAME_LAYER_EXTENSIONS.has(record.extensionId)) {
11
+ continue;
12
+ }
13
+ for (const filePath of record.srcFiles) {
14
+ const sourceFile = await parseSourceFile(filePath);
15
+ for (const statement of sourceFile.statements) {
16
+ const moduleSpecifier = ts.isImportDeclaration(statement) &&
17
+ ts.isStringLiteral(statement.moduleSpecifier)
18
+ ? statement.moduleSpecifier
19
+ : ts.isExportDeclaration(statement) &&
20
+ statement.moduleSpecifier &&
21
+ ts.isStringLiteral(statement.moduleSpecifier)
22
+ ? statement.moduleSpecifier
23
+ : null;
24
+ if (!moduleSpecifier) {
25
+ continue;
26
+ }
27
+ issues.push(...inspectPeerExtensionImport({ extensionId: record.extensionId, specifier: moduleSpecifier.text, filePath: toPosix(path.relative(workspaceRoot, filePath)), lineNumber: getLineNumber(sourceFile, moduleSpecifier) }));
28
+ }
29
+ }
30
+ }
31
+ return issues;
32
+ }
33
+ function isGuardedSourceFile(filePath) {
34
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
35
+ return false;
36
+ }
37
+ if (filePath.endsWith(".d.ts")) {
38
+ return false;
39
+ }
40
+ return true;
41
+ }
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function collectServerCrossDomainImportIssues(workspaceRoot: string): Promise<Issue[]>;
@@ -0,0 +1,48 @@
1
+ import path from "node:path";
2
+ import ts from "typescript";
3
+ import { getLineNumber, parseSourceFile } from "../shared/ts-ast.js";
4
+ import { collectExtensionSourceRecords } from "./scan-source.js";
5
+ import { inspectServerDomainImport, isPathInDirectory, getServerDomain } from "./source-import-boundary-rules.js";
6
+ export async function collectServerCrossDomainImportIssues(workspaceRoot) {
7
+ const records = await collectExtensionSourceRecords(workspaceRoot, isGuardedSourceFile);
8
+ const issues = [];
9
+ for (const record of records) {
10
+ const serverRoot = path.join(record.srcRoot, "server");
11
+ const allFiles = new Set(record.srcFiles);
12
+ const serverFiles = record.srcFiles.filter((filePath) => isPathInDirectory(filePath, serverRoot));
13
+ for (const serverFile of serverFiles) {
14
+ if (path.basename(serverFile) === "index.ts") {
15
+ continue;
16
+ }
17
+ const sourceDomain = getServerDomain(serverRoot, serverFile);
18
+ if (!sourceDomain) {
19
+ continue;
20
+ }
21
+ const sourceFile = await parseSourceFile(serverFile);
22
+ for (const statement of sourceFile.statements) {
23
+ const moduleSpecifier = ts.isImportDeclaration(statement) &&
24
+ ts.isStringLiteral(statement.moduleSpecifier)
25
+ ? statement.moduleSpecifier
26
+ : ts.isExportDeclaration(statement) &&
27
+ statement.moduleSpecifier &&
28
+ ts.isStringLiteral(statement.moduleSpecifier)
29
+ ? statement.moduleSpecifier
30
+ : null;
31
+ if (!moduleSpecifier) {
32
+ continue;
33
+ }
34
+ issues.push(...inspectServerDomainImport({ workspaceRoot, serverRoot, source: serverFile, specifier: moduleSpecifier.text, allFiles, lineNumber: getLineNumber(sourceFile, moduleSpecifier) }));
35
+ }
36
+ }
37
+ }
38
+ return issues;
39
+ }
40
+ function isGuardedSourceFile(filePath) {
41
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
42
+ return false;
43
+ }
44
+ if (filePath.endsWith(".d.ts")) {
45
+ return false;
46
+ }
47
+ return true;
48
+ }
@@ -0,0 +1 @@
1
+ export { validateServerIndexApiEntrySatisfies } from "../../shared/extension/validate-server-index-api-entry-satisfies.js";
@@ -0,0 +1 @@
1
+ export { validateServerIndexApiEntrySatisfies } from "../../shared/extension/validate-server-index-api-entry-satisfies.js";
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function collectSharedImportBoundaryIssues(workspaceRoot: string): Promise<Issue[]>;
@@ -0,0 +1,40 @@
1
+ import path from "node:path";
2
+ import ts from "typescript";
3
+ import { getLineNumber, parseSourceFile } from "../shared/ts-ast.js";
4
+ import { collectExtensionSourceRecords } from "./scan-source.js";
5
+ import { inspectSharedImport, isPathInDirectory } from "./source-import-boundary-rules.js";
6
+ export async function collectSharedImportBoundaryIssues(workspaceRoot) {
7
+ const records = await collectExtensionSourceRecords(workspaceRoot, isGuardedSourceFile);
8
+ const issues = [];
9
+ for (const record of records) {
10
+ const allFiles = new Set(record.srcFiles);
11
+ const sharedFiles = record.srcFiles.filter((filePath) => isPathInDirectory(filePath, path.join(record.srcRoot, "shared")));
12
+ for (const sharedFile of sharedFiles) {
13
+ const sourceFile = await parseSourceFile(sharedFile);
14
+ for (const statement of sourceFile.statements) {
15
+ const moduleSpecifier = ts.isImportDeclaration(statement) &&
16
+ ts.isStringLiteral(statement.moduleSpecifier)
17
+ ? statement.moduleSpecifier
18
+ : ts.isExportDeclaration(statement) &&
19
+ statement.moduleSpecifier &&
20
+ ts.isStringLiteral(statement.moduleSpecifier)
21
+ ? statement.moduleSpecifier
22
+ : null;
23
+ if (!moduleSpecifier) {
24
+ continue;
25
+ }
26
+ issues.push(...inspectSharedImport({ workspaceRoot, srcRoot: record.srcRoot, source: sharedFile, specifier: moduleSpecifier.text, allFiles, lineNumber: getLineNumber(sourceFile, moduleSpecifier) }));
27
+ }
28
+ }
29
+ }
30
+ return issues;
31
+ }
32
+ function isGuardedSourceFile(filePath) {
33
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
34
+ return false;
35
+ }
36
+ if (filePath.endsWith(".d.ts")) {
37
+ return false;
38
+ }
39
+ return true;
40
+ }
@@ -0,0 +1 @@
1
+ export { validateShared } from "../../shared/extension/validate-shared.js";
@@ -0,0 +1 @@
1
+ export { validateShared } from "../../shared/extension/validate-shared.js";
@@ -0,0 +1,8 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ /**
3
+ * 规则 test-layer-boundary:
4
+ * 扫描 products/<extension>/test 下的 .test.ts 文件:
5
+ * 1. 全部禁止导入已删除的 @kuafucloud/product-test/smoke-*。
6
+ * 2. 只有 test/e2e/** 可以导入 Playwright 或 @kuafucloud/product-test/e2e。
7
+ */
8
+ export declare function collectTestLayerBoundaryIssues(workspaceRoot: string): Promise<Issue[]>;
@@ -0,0 +1,59 @@
1
+ import path from "node:path";
2
+ import ts from "typescript";
3
+ import { forbiddenTestImport } from "./test-layer-import-rules.js";
4
+ import { getLineNumber, parseSourceFile } from "../shared/ts-ast.js";
5
+ import { collectFiles, pathExists } from "../shared/records.js";
6
+ import { toPosix } from "./scan-source.js";
7
+ import { collectGuardProductRoots } from "../../../product-roots.js";
8
+ /**
9
+ * 规则 test-layer-boundary:
10
+ * 扫描 products/<extension>/test 下的 .test.ts 文件:
11
+ * 1. 全部禁止导入已删除的 @kuafucloud/product-test/smoke-*。
12
+ * 2. 只有 test/e2e/** 可以导入 Playwright 或 @kuafucloud/product-test/e2e。
13
+ */
14
+ export async function collectTestLayerBoundaryIssues(workspaceRoot) {
15
+ const issues = [];
16
+ for (const product of await collectGuardProductRoots(workspaceRoot, "extension")) {
17
+ const extensionRoot = product.root;
18
+ const testDir = path.join(extensionRoot, "test");
19
+ if (!(await pathExists(testDir))) {
20
+ continue;
21
+ }
22
+ const testFiles = await collectFiles(testDir, (f) => f.endsWith(".test.ts"));
23
+ for (const filePath of testFiles) {
24
+ const layer = path.relative(testDir, filePath).split(path.sep)[0];
25
+ const sourceFile = await parseSourceFile(filePath);
26
+ for (const statement of sourceFile.statements) {
27
+ const specifier = getModuleSpecifier(statement);
28
+ if (!specifier) {
29
+ continue;
30
+ }
31
+ const violatedPattern = forbiddenTestImport(layer, specifier.text);
32
+ if (!violatedPattern) {
33
+ continue;
34
+ }
35
+ issues.push({
36
+ rule: "test-layer-boundary",
37
+ filePath: toPosix(path.relative(workspaceRoot, filePath)),
38
+ lineNumber: getLineNumber(sourceFile, specifier),
39
+ message: violatedPattern === "removed"
40
+ ? `测试中禁止导入已删除的 "${specifier.text}"。smoke 测试体系已移除,请迁移到 Playwright E2E 或 Vitest。`
41
+ : `非 E2E 测试禁止导入 "${specifier.text}"。Playwright/E2E helper 只能放在 test/e2e/**。`,
42
+ });
43
+ }
44
+ }
45
+ }
46
+ return issues;
47
+ }
48
+ function getModuleSpecifier(statement) {
49
+ if (ts.isImportDeclaration(statement) &&
50
+ ts.isStringLiteral(statement.moduleSpecifier)) {
51
+ return statement.moduleSpecifier;
52
+ }
53
+ if (ts.isExportDeclaration(statement) &&
54
+ statement.moduleSpecifier &&
55
+ ts.isStringLiteral(statement.moduleSpecifier)) {
56
+ return statement.moduleSpecifier;
57
+ }
58
+ return null;
59
+ }
@@ -0,0 +1,4 @@
1
+ import type { ExtensionStructureRecord } from "./types.js";
2
+ import type { Issue } from "../shared/types.js";
3
+ /** 四层只由物理路径决定;不再强制每个Route创建独立HTTP测试。 */
4
+ export declare function validateTestMirror(record: ExtensionStructureRecord, workspaceRoot: string): Issue[];
@@ -0,0 +1,32 @@
1
+ import path from "node:path";
2
+ /** 四层只由物理路径决定;不再强制每个Route创建独立HTTP测试。 */
3
+ export function validateTestMirror(record, workspaceRoot) {
4
+ const issues = [];
5
+ const testRoot = path.join(record.extensionRoot, "test");
6
+ const sourceRoot = path.join(record.extensionRoot, "src", "server");
7
+ const sourceFiles = new Set(record.srcFiles);
8
+ for (const file of record.testFiles) {
9
+ if (!/\.(?:test|spec)\.[cm]?[jt]sx?$/u.test(file))
10
+ continue;
11
+ const relative = toPosix(path.relative(testRoot, file));
12
+ // 临时开发故事由显式dev-spec入口执行,不属于四层持久集合;不能按.development basename泛化。
13
+ if (/^e2e\/\.development\/[^/]+\/.+/u.test(relative))
14
+ continue;
15
+ if (!/^(?:unit|integration|e2e\/(?:regressions|product-flows))\/.+/u.test(relative)) {
16
+ issues.push({ rule: "extension-test-mirror", filePath: toPosix(path.relative(workspaceRoot, file)), lineNumber: 1,
17
+ message: "测试文件必须位于 test/unit、test/integration、test/e2e/regressions 或 test/e2e/product-flows;辅助模块不能包含测试声明文件。" });
18
+ continue;
19
+ }
20
+ // 只有主动选择server镜像命名的现存测试才声明逐文件源映射;聚合生命周期和纯规则测试不受镜像数量约束。
21
+ const mirror = /^(?:unit|integration)\/server\/(.+)\.test\.tsx?$/u.exec(relative);
22
+ if (!mirror)
23
+ continue;
24
+ const expected = path.join(sourceRoot, `${mirror[1]}.ts`);
25
+ if (!sourceFiles.has(expected)) {
26
+ issues.push({ rule: "extension-test-mirror", filePath: toPosix(path.relative(workspaceRoot, file)), lineNumber: 1,
27
+ message: `已有server镜像测试没有对应源文件 "${toPosix(path.relative(workspaceRoot, expected))}";纯规则或聚合测试应使用其实际职责命名,不能伪造Route映射。` });
28
+ }
29
+ }
30
+ return issues;
31
+ }
32
+ function toPosix(value) { return value.split(path.sep).join("/"); }
@@ -0,0 +1,7 @@
1
+ import type { ExtensionStructureRecord } from "./types.js";
2
+ import type { Issue } from "../shared/types.js";
3
+ /**
4
+ * 规则 extension-src-top-level:
5
+ * src/ 顶层只能包含 admin/、server/、database/、shared/、simulation/ 与白名单文件。
6
+ */
7
+ export declare function validateTopLevel(record: ExtensionStructureRecord, workspaceRoot: string): Issue[];
@@ -0,0 +1,76 @@
1
+ import path from "node:path";
2
+ /**
3
+ * src/ 顶层允许的目录
4
+ */
5
+ const ALLOWED_SRC_DIRS = new Set([
6
+ "admin",
7
+ "server",
8
+ "database",
9
+ "shared",
10
+ "simulation",
11
+ ]);
12
+ /**
13
+ * src/ 顶层允许的文件
14
+ */
15
+ const ALLOWED_SRC_FILES = new Set([
16
+ "index.ts",
17
+ "product.ts",
18
+ "events.ts",
19
+ "hooks.ts",
20
+ "permissions.ts",
21
+ "tasks.ts",
22
+ ]);
23
+ /**
24
+ * 规则 extension-src-top-level:
25
+ * src/ 顶层只能包含 admin/、server/、database/、shared/、simulation/ 与白名单文件。
26
+ */
27
+ export function validateTopLevel(record, workspaceRoot) {
28
+ const issues = [];
29
+ const srcDir = path.join(record.extensionRoot, "src");
30
+ // 从 srcFiles 中提取 src/ 直接子文件名
31
+ const directFiles = new Set();
32
+ for (const file of record.srcFiles) {
33
+ const relative = path.relative(srcDir, file);
34
+ const parts = relative.split(path.sep);
35
+ if (parts.length === 1) {
36
+ directFiles.add(parts[0]);
37
+ }
38
+ }
39
+ // 从 srcDirs 中提取 src/ 直接子目录名
40
+ const directDirs = new Set();
41
+ for (const dir of record.srcDirs) {
42
+ const relative = path.relative(srcDir, dir);
43
+ const parts = relative.split(path.sep);
44
+ if (parts.length === 1) {
45
+ directDirs.add(parts[0]);
46
+ }
47
+ }
48
+ // 检查不允许的文件
49
+ for (const fileName of directFiles) {
50
+ if (!ALLOWED_SRC_FILES.has(fileName)) {
51
+ const filePath = path.relative(workspaceRoot, path.join(srcDir, fileName));
52
+ issues.push({
53
+ rule: "extension-src-top-level",
54
+ filePath: toPosix(filePath),
55
+ lineNumber: 1,
56
+ message: `src/ 顶层不允许存在文件 "${fileName}"。只允许 ${[...ALLOWED_SRC_FILES].join("、")}。`,
57
+ });
58
+ }
59
+ }
60
+ // 检查不允许的目录
61
+ for (const dirName of directDirs) {
62
+ if (!ALLOWED_SRC_DIRS.has(dirName)) {
63
+ const dirPath = path.relative(workspaceRoot, path.join(srcDir, dirName));
64
+ issues.push({
65
+ rule: "extension-src-top-level",
66
+ filePath: toPosix(dirPath),
67
+ lineNumber: 1,
68
+ message: `src/ 顶层不允许存在目录 "${dirName}/"。只允许 ${[...ALLOWED_SRC_DIRS].map((d) => d + "/").join("、")}。`,
69
+ });
70
+ }
71
+ }
72
+ return issues;
73
+ }
74
+ function toPosix(value) {
75
+ return value.split(path.sep).join("/");
76
+ }
@@ -0,0 +1,2 @@
1
+ import type { Issue } from "../shared/types.js";
2
+ export declare function collectUnusedExportIssues(workspaceRoot: string): Promise<Issue[]>;