js-code-detector 0.0.47 → 0.0.50

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 (43) hide show
  1. package/dist/cjs/index.d.ts +1 -44
  2. package/dist/cjs/index.js +3 -25
  3. package/dist/cjs/services/DetectService.d.ts +2 -2
  4. package/dist/cjs/services/DetectService.js +11 -6
  5. package/dist/cjs/services/ProjectService.d.ts +1 -1
  6. package/dist/cjs/services/projectServiceClass/UmiProjectService.js +6 -7
  7. package/dist/cjs/util/ast_util/AstFeatUtil.js +2 -4
  8. package/dist/cjs/util/ast_util/AstKit.d.ts +24 -0
  9. package/dist/cjs/util/ast_util/AstKit.js +345 -0
  10. package/dist/cjs/util/ast_util/AstUtil.d.ts +7 -7
  11. package/dist/cjs/util/ast_util/AstUtil.js +9 -11
  12. package/dist/cjs/util/ast_util/SHARED_CONSTANTS.d.ts +3 -0
  13. package/dist/cjs/util/ast_util/SHARED_CONSTANTS.js +35 -0
  14. package/dist/cjs/util/constants.d.ts +1 -0
  15. package/dist/cjs/util/constants.js +5 -2
  16. package/dist/cjs/util/{shared/gitUtil.d.ts → git_util/cloneRepoWithBranchAndDefault.d.ts} +1 -5
  17. package/dist/cjs/util/{shared/gitUtil.js → git_util/cloneRepoWithBranchAndDefault.js} +5 -25
  18. package/dist/cjs/util/git_util/execGitDiff.d.ts +4 -0
  19. package/dist/cjs/util/git_util/execGitDiff.js +39 -0
  20. package/dist/cjs/util/git_util/getGitRepositoryAndBranch.d.ts +4 -0
  21. package/dist/cjs/util/{report_util/getFileDepends.js → git_util/getGitRepositoryAndBranch.js} +15 -18
  22. package/dist/cjs/util/{parseGitLabDiffUril.js → git_util/parseGitLabDiffUril.js} +2 -2
  23. package/dist/cjs/util/{shared → project_umi_util}/umi4ProjectUtil.js +1 -1
  24. package/dist/cjs/util/{shared → project_umi_util}/vueProjectUtil.js +1 -1
  25. package/dist/cjs/util/report_util/createDependenceMap.js +3 -3
  26. package/dist/cjs/util/report_util/filterEffectedCode.d.ts +1 -1
  27. package/dist/cjs/util/report_util/generateGitDiffReport.d.ts +1 -1
  28. package/dist/cjs/util/report_util/generateGitDiffReport.js +2 -2
  29. package/dist/cjs/util/{ast_util/filePathResolver.d.ts → report_util/resolveImportPath.d.ts} +1 -1
  30. package/dist/cjs/util/{ast_util/filePathResolver.js → report_util/resolveImportPath.js} +5 -9
  31. package/dist/cjs/util/{createRandomStr.js → shared/createRandomStr.js} +1 -1
  32. package/dist/cjs/util/shared/getRepoSupportFlag.d.ts +0 -2
  33. package/dist/cjs/util/shared/getRepoSupportFlag.js +0 -17
  34. package/dist/cjs/util/shared/gitDiffTool.d.ts +0 -40
  35. package/dist/cjs/util/shared/gitDiffTool.js +7 -116
  36. package/package.json +5 -1
  37. package/dist/cjs/util/madge_util.d.ts +0 -12
  38. package/dist/cjs/util/madge_util.js +0 -120
  39. package/dist/cjs/util/report_util/getFileDepends.d.ts +0 -1
  40. /package/dist/cjs/util/{parseGitLabDiffUril.d.ts → git_util/parseGitLabDiffUril.d.ts} +0 -0
  41. /package/dist/cjs/util/{shared → project_umi_util}/umi4ProjectUtil.d.ts +0 -0
  42. /package/dist/cjs/util/{shared → project_umi_util}/vueProjectUtil.d.ts +0 -0
  43. /package/dist/cjs/util/{createRandomStr.d.ts → shared/createRandomStr.d.ts} +0 -0
@@ -1,9 +1,3 @@
1
- import { cloneGitRepoAndGetDiff } from "./util/shared/gitDiffTool";
2
- export declare const gitDiffFileName = "git_diff.txt";
3
- export declare function getGitRepositoryAndBranch(): Promise<{
4
- gitUrl: string;
5
- branchName: string;
6
- }>;
7
1
  export declare function sameCodeDetect(dirOfCwd?: string): Promise<void>;
8
2
  export declare function gitDiffDetect(): Promise<{
9
3
  dependenceJson: {
@@ -32,45 +26,8 @@ export declare function gitDiffDetect(): Promise<{
32
26
  filePath: string;
33
27
  }[];
34
28
  } | undefined>;
35
- export declare function getUpstreamDependenceJson(inputUrl: string, token: string, jsonKeys?: (keyof Awaited<ReturnType<typeof cloneGitRepoAndGetDiff>>)[]): Promise<{
36
- reports: {
37
- dangerIdentifiers: string[];
38
- topEffectedExportsAdded: string[];
39
- topEffectedExportsRemoved: string[];
40
- blockReports: {
41
- diff_txt: string[];
42
- infos: {
43
- causeBy: string;
44
- effectsUpstream: string[];
45
- occupations: string[];
46
- effectsDownstream: string[];
47
- }[];
48
- }[];
49
- type: "modify" | "add" | "delete";
50
- filesDependsOnMe: string[];
51
- undefinedIdentifiers: string[];
52
- filePath: string;
53
- }[];
54
- dependenceJson: {
55
- import2export: Record<string, string>;
56
- export2export: Record<string, string>;
57
- mapFilePathToExportAllSources: Record<string, string[]>;
58
- indirectExportMembers: Record<string, string>;
59
- };
60
- effectedCode: Record<string, any>;
61
- upstreamDependenceJson: {};
62
- repoType: string;
63
- message: string;
64
- } | {
65
- effectedCode: Record<string, any>;
66
- upstreamDependenceJson: {};
67
- repoType: string;
68
- message: any;
69
- reports?: undefined;
70
- dependenceJson?: undefined;
71
- }>;
72
29
  export { isRepoTypeSupported } from "./util/shared/getRepoSupportFlag";
73
30
  export declare function runDiffDetect(compareUrl?: string, token?: string): Promise<{
74
31
  repoType: string;
75
- effectedImportUsage: string[];
32
+ effectedImportUsage: [string, string][];
76
33
  }>;
package/dist/cjs/index.js CHANGED
@@ -29,10 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/index.ts
30
30
  var src_exports = {};
31
31
  __export(src_exports, {
32
- getGitRepositoryAndBranch: () => getGitRepositoryAndBranch,
33
- getUpstreamDependenceJson: () => getUpstreamDependenceJson,
34
32
  gitDiffDetect: () => gitDiffDetect,
35
- gitDiffFileName: () => gitDiffFileName,
36
33
  isRepoTypeSupported: () => import_getRepoSupportFlag.isRepoTypeSupported,
37
34
  runDiffDetect: () => runDiffDetect,
38
35
  sameCodeDetect: () => sameCodeDetect
@@ -40,25 +37,13 @@ __export(src_exports, {
40
37
  module.exports = __toCommonJS(src_exports);
41
38
  var import_path = require("path");
42
39
  var import_fs = require("fs");
43
- var import_utils = require("@umijs/utils");
44
40
  var import_dayjs = __toESM(require("dayjs"));
45
41
  var import_readDirFiles = require("./util/shared/readDirFiles");
46
42
  var import_Core = __toESM(require("./util/ast_util/Core"));
47
- var import_parseGitLabDiffUril = require("./util/parseGitLabDiffUril");
48
43
  var import_gitDiffTool = require("./util/shared/gitDiffTool");
49
44
  var import_DetectService = require("./services/DetectService");
45
+ var import_getGitRepositoryAndBranch = __toESM(require("./util/git_util/getGitRepositoryAndBranch"));
50
46
  var import_getRepoSupportFlag = require("./util/shared/getRepoSupportFlag");
51
- var gitDiffFileName = "git_diff.txt";
52
- async function getGitRepositoryAndBranch() {
53
- const res = await import_utils.execa.execa("git remote get-url origin", { shell: true });
54
- import_utils.chalk.green(["仓库地址:", res.stdout]);
55
- const branch = await import_utils.execa.execa("git rev-parse --abbrev-ref HEAD", { shell: true });
56
- import_utils.chalk.green(["分支名称:", branch.stdout]);
57
- return {
58
- gitUrl: res.stdout,
59
- branchName: branch.stdout
60
- };
61
- }
62
47
  async function sameCodeDetect(dirOfCwd) {
63
48
  const filesAndContent = await (0, import_readDirFiles.readSrcFiles)(dirOfCwd);
64
49
  const { nodeContentGroupList, depthList } = import_Core.default.investigate(filesAndContent);
@@ -69,14 +54,10 @@ async function sameCodeDetect(dirOfCwd) {
69
54
  (0, import_fs.writeFileSync)((0, import_path.join)(process.cwd(), `${(0, import_dayjs.default)().format("YYYYMDD_HHmm")}_same_code.md`), md, { encoding: "utf-8", flag: "w" });
70
55
  }
71
56
  async function gitDiffDetect() {
72
- const { gitUrl, branchName } = await getGitRepositoryAndBranch();
57
+ const { gitUrl, branchName } = await (0, import_getGitRepositoryAndBranch.default)();
73
58
  const today = (0, import_dayjs.default)().format("YYYYMDD_HHmmss");
74
59
  return (0, import_gitDiffTool.gitDiffTool)({ gitRepoUrl: gitUrl, targetBranch: branchName, baseBranch: "master", tempDirPath: today, generateFile: void 0 });
75
60
  }
76
- async function getUpstreamDependenceJson(inputUrl, token, jsonKeys) {
77
- const gitInfo = (0, import_parseGitLabDiffUril.parseGitLabCompareUrl)(inputUrl);
78
- return (0, import_gitDiffTool.cloneGitRepoAndGetDiff)(gitInfo.gitRepoUrl, gitInfo.targetBranch, { token, jsonKeys });
79
- }
80
61
  async function runDiffDetect(compareUrl, token) {
81
62
  const option = {
82
63
  compareUrl,
@@ -85,7 +66,7 @@ async function runDiffDetect(compareUrl, token) {
85
66
  branchName: ""
86
67
  };
87
68
  if (!compareUrl || !token) {
88
- const { gitUrl, branchName } = await getGitRepositoryAndBranch();
69
+ const { gitUrl, branchName } = await (0, import_getGitRepositoryAndBranch.default)();
89
70
  option.gitRepoUrl = gitUrl;
90
71
  option.branchName = branchName;
91
72
  }
@@ -95,10 +76,7 @@ async function runDiffDetect(compareUrl, token) {
95
76
  }
96
77
  // Annotate the CommonJS export names for ESM import in node:
97
78
  0 && (module.exports = {
98
- getGitRepositoryAndBranch,
99
- getUpstreamDependenceJson,
100
79
  gitDiffDetect,
101
- gitDiffFileName,
102
80
  isRepoTypeSupported,
103
81
  runDiffDetect,
104
82
  sameCodeDetect
@@ -1,4 +1,4 @@
1
- import { parseGitLabCompareUrl } from "../util/parseGitLabDiffUril";
1
+ import { parseGitLabCompareUrl } from "../util/git_util/parseGitLabDiffUril";
2
2
  import { ProjectService } from "./ProjectService";
3
3
  export declare class DetectService {
4
4
  directoryInfo: {
@@ -30,6 +30,6 @@ export declare class DetectService {
30
30
  projectHandle(): Promise<void>;
31
31
  formatResult(): {
32
32
  repoType: string;
33
- effectedImportUsage: string[];
33
+ effectedImportUsage: [string, string][];
34
34
  };
35
35
  }
@@ -35,13 +35,14 @@ module.exports = __toCommonJS(DetectService_exports);
35
35
  var import_path = require("path");
36
36
  var import_await_to_js = __toESM(require("await-to-js"));
37
37
  var import_utils = require("@umijs/utils");
38
- var import_parseGitLabDiffUril = require("../util/parseGitLabDiffUril");
39
- var import_createRandomStr = __toESM(require("../util/createRandomStr"));
38
+ var import_parseGitLabDiffUril = require("../util/git_util/parseGitLabDiffUril");
39
+ var import_createRandomStr = __toESM(require("../util/shared/createRandomStr"));
40
40
  var import_constants = require("../util/constants");
41
- var import_gitUtil = require("../util/shared/gitUtil");
41
+ var import_execGitDiff = __toESM(require("../util/git_util/execGitDiff"));
42
42
  var import_getRepoSupportFlag = require("../util/shared/getRepoSupportFlag");
43
43
  var import_UmiProjectService = __toESM(require("./projectServiceClass/UmiProjectService"));
44
44
  var import_perf_hooks = require("perf_hooks");
45
+ var import_cloneRepoWithBranchAndDefault = __toESM(require("../util/git_util/cloneRepoWithBranchAndDefault"));
45
46
  var DetectService = class {
46
47
  constructor(option) {
47
48
  this.directoryInfo = {
@@ -61,7 +62,7 @@ var DetectService = class {
61
62
  this.init(option);
62
63
  }
63
64
  async init(option) {
64
- const { compareUrl, token, gitRepoUrl, branchName } = option;
65
+ const { compareUrl, token = "", gitRepoUrl, branchName } = option;
65
66
  const tmpWorkDir = (0, import_createRandomStr.default)();
66
67
  this.directoryInfo = {
67
68
  tmpWorkDir,
@@ -72,6 +73,7 @@ var DetectService = class {
72
73
  this.gitInfo = {
73
74
  ...this.gitInfo,
74
75
  ...(0, import_parseGitLabDiffUril.parseGitLabCompareUrl)(compareUrl),
76
+ token,
75
77
  repoType: ""
76
78
  };
77
79
  }
@@ -130,7 +132,10 @@ var DetectService = class {
130
132
  import_utils.logger.info("开始克隆仓库");
131
133
  const { tmpWorkDir } = this.directoryInfo;
132
134
  const { gitRepoUrl, token, baseBranch, targetBranch } = this.gitInfo;
135
+ import_utils.logger.info(`开始克隆仓库: ${gitRepoUrl}`);
136
+ import_utils.logger.info(`token: ${token}`);
133
137
  const repoUrl = token ? (0, import_parseGitLabDiffUril.getGitRepoUrlByToken)(gitRepoUrl, token || "") : gitRepoUrl;
138
+ import_utils.logger.info(`克隆仓库URL: ${repoUrl}`);
134
139
  const branchesAndTargetDirPaths = [
135
140
  {
136
141
  branch: baseBranch,
@@ -142,10 +147,10 @@ var DetectService = class {
142
147
  }
143
148
  ];
144
149
  await Promise.all(branchesAndTargetDirPaths.map(({ branch, targetDirPath: targetDirPath2 }) => {
145
- return (0, import_gitUtil.cloneRepoWithBranchAndDefault)(repoUrl, branch, targetDirPath2);
150
+ return (0, import_cloneRepoWithBranchAndDefault.default)(repoUrl, branch, targetDirPath2);
146
151
  }));
147
152
  const targetDirPath = (0, import_path.join)(tmpWorkDir, import_constants.TARGET);
148
- await (0, import_gitUtil.execGitDiff)(targetDirPath, baseBranch, targetBranch);
153
+ await (0, import_execGitDiff.default)(targetDirPath, baseBranch, targetBranch);
149
154
  const repoType = await (0, import_getRepoSupportFlag.getRepoType)((0, import_path.join)(targetDirPath, "package.json"));
150
155
  this.gitInfo.repoType = repoType;
151
156
  import_utils.logger.info("克隆仓库成功");
@@ -10,7 +10,7 @@ export interface ProjectService {
10
10
  parsedAlias: Record<string, any>;
11
11
  };
12
12
  outputResult: {
13
- effectedImportUsage: string[];
13
+ effectedImportUsage: [string, string][];
14
14
  };
15
15
  run(): void;
16
16
  }
@@ -33,13 +33,13 @@ __export(UmiProjectService_exports, {
33
33
  });
34
34
  module.exports = __toCommonJS(UmiProjectService_exports);
35
35
  var import_format_git_diff_content = require("../../util/format_git_diff_content");
36
- var import_umi4ProjectUtil = require("../../util/shared/umi4ProjectUtil");
36
+ var import_umi4ProjectUtil = require("../../util/project_umi_util/umi4ProjectUtil");
37
37
  var import_fs = require("fs");
38
38
  var import_path = require("path");
39
- var import__ = require("../../index");
40
39
  var import_collectUpstreamFiles = __toESM(require("../../util/shared/collectUpstreamFiles"));
41
40
  var import_createDependenceMap = require("../../util/report_util/createDependenceMap");
42
41
  var import_filterEffectedExportMember = __toESM(require("../../util/report_util/filterEffectedExportMember"));
42
+ var import_constants = require("../../util/constants");
43
43
  var UmiProjectService = class {
44
44
  constructor(detectService) {
45
45
  this.detectService = detectService;
@@ -65,7 +65,7 @@ var UmiProjectService = class {
65
65
  }
66
66
  async parseGitDiffContent() {
67
67
  const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
68
- const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import__.gitDiffFileName), "utf-8");
68
+ const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import_constants.gitDiffFileName), "utf-8");
69
69
  this.gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
70
70
  }
71
71
  async collectEffectedFiles() {
@@ -88,10 +88,9 @@ var UmiProjectService = class {
88
88
  const exportedNames = (0, import_filterEffectedExportMember.default)((0, import_path.join)(absPathPrefix, filePath), absPathPrefix, Number(startLineOfNew), Number(startLineOfNew) + Number(newBranchLineScope));
89
89
  return exportedNames.map((name) => [filePath, name].join("#"));
90
90
  }).flat();
91
- const effectedImportUsage = [...Object.entries(import2export), ...Object.entries(indirectExportMembers)].reduce((acc, [key, value]) => {
92
- effectedExportNames.includes(value) && acc.push(key);
93
- return acc;
94
- }, []);
91
+ const effectedImportUsage = [...Object.entries(import2export), ...Object.entries(indirectExportMembers)].filter(([_, value]) => {
92
+ return effectedExportNames.includes(value);
93
+ });
95
94
  this.outputResult.effectedImportUsage = effectedImportUsage;
96
95
  }
97
96
  };
@@ -37,6 +37,7 @@ var import_sha1 = __toESM(require("crypto-js/sha1"));
37
37
  var import_enc_base64 = __toESM(require("crypto-js/enc-base64"));
38
38
  var import_featureLevel = require("../shared/featureLevel");
39
39
  var import_astNodeFeatureExtractorMap = require("../shared/astNodeFeatureExtractorMap");
40
+ var import_SHARED_CONSTANTS = require("./SHARED_CONSTANTS");
40
41
  var AstFeatUtil = class {
41
42
  // 单节点判断
42
43
  static isValidNodeCollect(astNode) {
@@ -220,10 +221,7 @@ var AstFeatUtil = class {
220
221
  }
221
222
  };
222
223
  AstFeatUtil.skipHashCreateTypes = ["Program", "File", "ExportDeclaration"];
223
- AstFeatUtil.invalidNodeKey = [
224
- "comments",
225
- "tokens"
226
- ];
224
+ AstFeatUtil.invalidNodeKey = import_SHARED_CONSTANTS.INVALID_NODE_KEY;
227
225
  // 忽略的 节点类型
228
226
  AstFeatUtil.AstNodeTypeConf = /* @__PURE__ */ new Map([
229
227
  ["ImportDeclaration", {
@@ -0,0 +1,24 @@
1
+ import { AstNode } from "./AstUtil";
2
+ export declare class AstUtil {
3
+ static invalidNodeKey: string[];
4
+ static EXPORT_DECLARATION_TYPES: readonly ["FunctionDeclaration", "TSEnumDeclaration", "TSInterfaceDeclaration", "TSTypeAliasDeclaration", "ClassDeclaration"];
5
+ static windowProperties: string[];
6
+ static intrinsicElements: string[];
7
+ static standardAttributes: string[];
8
+ static FUNCTION_TYPES: string[];
9
+ static getNodePath(node: AstNode): string;
10
+ static getShortNodeMsg(node: AstNode, hideParentProperty?: boolean): string;
11
+ private static isValidNodeCollect;
12
+ private static isValidArrayNodeCollect;
13
+ private static collectEffectId;
14
+ private static updateImportedAndExportedMember;
15
+ private static deepSearchParamsIdentifier;
16
+ private static collectDependenceIds;
17
+ private static updateLoc;
18
+ private static collectVariableDeclarationIdentifiers;
19
+ private static collectDeclarationIdentifiers;
20
+ private static collectImportDeclarationIdentifiers;
21
+ private static collectHoldingIds;
22
+ static deepFirstTravel(node: AstNode, filePath: string, mapUuidToNode: Map<string, AstNode>, mapFileLineToNodeSet: Map<number, Set<AstNode>>, mapPathToNodeSet: Map<string, Set<AstNode>>, stopTravelCallback?: (node: AstNode) => boolean | void): AstNode | undefined;
23
+ private static _deepFirstTravel;
24
+ }
@@ -0,0 +1,345 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util/ast_util/AstKit.ts
20
+ var AstKit_exports = {};
21
+ __export(AstKit_exports, {
22
+ AstUtil: () => AstUtil
23
+ });
24
+ module.exports = __toCommonJS(AstKit_exports);
25
+ var import_intrinsicElements = require("./intrinsicElements");
26
+ var import_SHARED_CONSTANTS = require("./SHARED_CONSTANTS");
27
+ var import_windowProperties = require("./windowProperties");
28
+ var _AstUtil = class {
29
+ static getNodePath(node) {
30
+ return [...node._util.ancestors, node].map((n) => n.type).join(":") + ":" + node.name;
31
+ }
32
+ static getShortNodeMsg(node, hideParentProperty = false) {
33
+ const { _util: { startLine, startColumn, endLine, endColumn, parentProperty, indexOfProperty } } = node;
34
+ let type = node.type;
35
+ let name = node.name;
36
+ if (name && typeof name === "object") {
37
+ type = name.type;
38
+ name = name.name;
39
+ }
40
+ const msg = [
41
+ hideParentProperty ? [] : [parentProperty, indexOfProperty !== null ? String(indexOfProperty) : null],
42
+ [type, name]
43
+ ].map((e) => e.filter(Boolean).join(":")).filter(Boolean).join(" ");
44
+ return `${msg}「${startLine}:${startColumn}, ${endLine}:${endColumn}」`;
45
+ }
46
+ static isValidNodeCollect(astNode) {
47
+ return typeof (astNode == null ? void 0 : astNode.type) === "string";
48
+ }
49
+ static isValidArrayNodeCollect(astNode) {
50
+ return Array.isArray(astNode) && astNode.some((v) => typeof (v == null ? void 0 : v.type) === "string");
51
+ }
52
+ static collectEffectId(id) {
53
+ }
54
+ static updateImportedAndExportedMember(programBodyChild, programNode) {
55
+ if (programNode.type !== "Program")
56
+ return;
57
+ }
58
+ static deepSearchParamsIdentifier(id, callback) {
59
+ if (!id) {
60
+ return;
61
+ }
62
+ if (id.type === "Identifier") {
63
+ callback(id);
64
+ }
65
+ if (id.type === "AssignmentPattern") {
66
+ const left = id.left;
67
+ this.deepSearchParamsIdentifier(left, callback);
68
+ }
69
+ if (id.type === "RestElement") {
70
+ this.deepSearchParamsIdentifier(id.argument, callback);
71
+ }
72
+ if (id.type === "ObjectProperty") {
73
+ const value = id.value;
74
+ this.deepSearchParamsIdentifier(value, callback);
75
+ }
76
+ if (id.type === "ObjectPattern") {
77
+ const properties = id.properties;
78
+ for (const property of properties) {
79
+ this.deepSearchParamsIdentifier(property, callback);
80
+ }
81
+ }
82
+ if (id.type === "ArrayPattern") {
83
+ const elements = id.elements;
84
+ for (const element of elements) {
85
+ this.deepSearchParamsIdentifier(element, callback);
86
+ }
87
+ }
88
+ }
89
+ static collectDependenceIds(node) {
90
+ }
91
+ static updateLoc(astNode, extra) {
92
+ var _a, _b, _c, _d, _e, _f, _g;
93
+ const { _util, type, name } = astNode;
94
+ const { mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet } = extra;
95
+ const { nodeCollection, filePath, parent } = _util;
96
+ _util.startLine = Math.min(...nodeCollection.map((n) => {
97
+ var _a2, _b2;
98
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.start) == null ? void 0 : _b2.line;
99
+ }), (_a = astNode.loc) == null ? void 0 : _a.start.line);
100
+ _util.endLine = Math.max(...nodeCollection.map((n) => {
101
+ var _a2, _b2;
102
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.end) == null ? void 0 : _b2.line;
103
+ }), (_b = astNode.loc) == null ? void 0 : _b.end.line);
104
+ _util.startColumn = ((_c = astNode.loc) == null ? void 0 : _c.start.column) ?? Math.min(...nodeCollection.map((n) => {
105
+ var _a2, _b2;
106
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.start) == null ? void 0 : _b2.column;
107
+ }), (_d = astNode.loc) == null ? void 0 : _d.start.column);
108
+ _util.endColumn = ((_e = astNode.loc) == null ? void 0 : _e.end.column) ?? Math.max(...nodeCollection.map((n) => {
109
+ var _a2, _b2;
110
+ return (_b2 = (_a2 = n.loc) == null ? void 0 : _a2.end) == null ? void 0 : _b2.column;
111
+ }), (_f = astNode.loc) == null ? void 0 : _f.end.column);
112
+ _util.uuid = `${filePath}:${type}:${name}「${_util.startLine}:${_util.startColumn},${_util.endLine}:${_util.endColumn}」`;
113
+ mapUuidToNode.set(_util.uuid, astNode);
114
+ for (let i = _util.startLine; i <= _util.endLine; i++) {
115
+ mapFileLineToNodeSet.set(i, mapFileLineToNodeSet.get(i) || /* @__PURE__ */ new Set());
116
+ (_g = mapFileLineToNodeSet.get(i)) == null ? void 0 : _g.add(astNode);
117
+ }
118
+ if (astNode.type === "Program") {
119
+ mapUuidToNode.set(astNode.type, astNode);
120
+ }
121
+ if (parent === null) {
122
+ nodeCollection.forEach((n) => {
123
+ var _a2;
124
+ const path = _AstUtil.getNodePath(n);
125
+ mapPathToNodeSet.set(path, mapPathToNodeSet.get(path) || /* @__PURE__ */ new Set());
126
+ (_a2 = mapPathToNodeSet.get(path)) == null ? void 0 : _a2.add(n);
127
+ });
128
+ }
129
+ }
130
+ static collectVariableDeclarationIdentifiers(node, callback) {
131
+ if (node.type !== "VariableDeclaration")
132
+ return;
133
+ const declarations = node.declarations;
134
+ for (const declaration of declarations) {
135
+ this.deepSearchParamsIdentifier(declaration.id, callback);
136
+ }
137
+ }
138
+ static collectDeclarationIdentifiers(node, callback) {
139
+ if (!this.EXPORT_DECLARATION_TYPES.includes(node.type))
140
+ return;
141
+ const id = node.id;
142
+ id && callback(id);
143
+ }
144
+ static collectImportDeclarationIdentifiers(node, callback) {
145
+ if (node.type !== "Program")
146
+ return;
147
+ const { body } = node;
148
+ Array.isArray(body) && body.forEach((node2) => {
149
+ if (node2.type === "ImportDeclaration") {
150
+ const specifiers = node2.specifiers;
151
+ for (const specifier of specifiers) {
152
+ const local = specifier.local;
153
+ callback(local);
154
+ }
155
+ }
156
+ });
157
+ }
158
+ static collectHoldingIds(node) {
159
+ const { holdingIds, holdingIdNameMap } = node._util;
160
+ if (this.FUNCTION_TYPES.includes(node.type)) {
161
+ const { params, id } = node;
162
+ if (Array.isArray(params) && params.length > 0) {
163
+ params.forEach((param) => this.deepSearchParamsIdentifier(param, (id2) => {
164
+ holdingIds.add(id2 || node);
165
+ id2._util.variableScope = [id2];
166
+ id2._util.holdingIdType = "Param";
167
+ }));
168
+ }
169
+ if (node.body && node.body.type === "BlockStatement") {
170
+ node.body._util.holdingIds.forEach((id2) => {
171
+ holdingIds.add(id2);
172
+ });
173
+ }
174
+ }
175
+ this.collectImportDeclarationIdentifiers(node, (id) => {
176
+ holdingIds.add(id);
177
+ id._util.variableScope = [id];
178
+ id._util.holdingIdType = "Import";
179
+ });
180
+ if (["BlockStatement", "Program"].includes(node.type)) {
181
+ const body = node.body;
182
+ Array.isArray(body) && body.forEach((bodyChild) => {
183
+ this.collectVariableDeclarationIdentifiers(bodyChild, (id) => {
184
+ holdingIds.add(id);
185
+ id._util.variableScope = [id];
186
+ id._util.holdingIdType = "Variable";
187
+ });
188
+ if (this.EXPORT_DECLARATION_TYPES.includes(bodyChild.type)) {
189
+ this.collectDeclarationIdentifiers(bodyChild, (id) => {
190
+ var _a, _b;
191
+ holdingIds.add(id);
192
+ (_b = (_a = id._util.parent) == null ? void 0 : _a._util.holdingIds) == null ? void 0 : _b.add(id);
193
+ id._util.variableScope = [id];
194
+ id._util.holdingIdType = node.type.replace("Declaration", "");
195
+ });
196
+ }
197
+ if (["ExportNamedDeclaration", "ExportDefaultDeclaration"].includes(bodyChild.type)) {
198
+ const declaration = bodyChild.declaration;
199
+ if (declaration) {
200
+ if (declaration.type === "VariableDeclaration") {
201
+ this.collectVariableDeclarationIdentifiers(declaration, (id) => {
202
+ holdingIds.add(id);
203
+ id._util.variableScope = [id];
204
+ id._util.holdingIdType = "Variable";
205
+ });
206
+ } else {
207
+ this.collectDeclarationIdentifiers(declaration, (id) => {
208
+ var _a, _b;
209
+ holdingIds.add(id);
210
+ (_b = (_a = id._util.parent) == null ? void 0 : _a._util.holdingIds) == null ? void 0 : _b.add(id);
211
+ id._util.variableScope = [id];
212
+ id._util.holdingIdType = declaration.type.replace("Declaration", "");
213
+ });
214
+ }
215
+ }
216
+ }
217
+ });
218
+ }
219
+ ;
220
+ holdingIds.forEach((holdingId) => {
221
+ const holdingIdName = holdingId.name;
222
+ if (typeof holdingIdName !== "string")
223
+ return;
224
+ const nodeSetOfIdName = holdingIdNameMap.get(holdingIdName) || /* @__PURE__ */ new Set();
225
+ nodeSetOfIdName.add(holdingId);
226
+ holdingIdNameMap.set(holdingIdName, nodeSetOfIdName);
227
+ });
228
+ }
229
+ static deepFirstTravel(node, filePath, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet, stopTravelCallback) {
230
+ const visitedNodeSet = /* @__PURE__ */ new Set();
231
+ if (!node) {
232
+ return;
233
+ }
234
+ return this._deepFirstTravel(node, visitedNodeSet, { filePath, depth: 0, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet, stopTravelCallback });
235
+ }
236
+ static _deepFirstTravel(node, visitedNodeSet, extra) {
237
+ visitedNodeSet.add(node);
238
+ const { filePath, depth, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet, stopTravelCallback } = extra;
239
+ const _util = {
240
+ startLine: NaN,
241
+ endLine: NaN,
242
+ startColumn: NaN,
243
+ endColumn: NaN,
244
+ filePath,
245
+ parent: null,
246
+ parentProperty: "",
247
+ indexOfProperty: null,
248
+ ancestors: [],
249
+ nodeCollection: [],
250
+ children: [],
251
+ uuid: "",
252
+ variableScope: [],
253
+ dependenceIds: /* @__PURE__ */ new Set(),
254
+ dependenceIdsNoScope: /* @__PURE__ */ new Set(),
255
+ holdingIdType: null,
256
+ holdingIds: /* @__PURE__ */ new Set(),
257
+ holdingIdNameMap: /* @__PURE__ */ new Map(),
258
+ inject: /* @__PURE__ */ new Set(),
259
+ provide: /* @__PURE__ */ new Set(),
260
+ effectIds: /* @__PURE__ */ new Set(),
261
+ occupation: /* @__PURE__ */ new Set(),
262
+ importedMember: [],
263
+ exportedMember: []
264
+ };
265
+ node._util = _util;
266
+ const { nodeCollection, children } = _util;
267
+ const stopTravel = stopTravelCallback == null ? void 0 : stopTravelCallback(node);
268
+ if (stopTravel) {
269
+ return node;
270
+ }
271
+ Object.keys(node).forEach((nodeKey) => {
272
+ if (this.invalidNodeKey.includes(nodeKey)) {
273
+ return;
274
+ }
275
+ const nodeValue = node[nodeKey];
276
+ if (visitedNodeSet.has(nodeValue) || !nodeValue) {
277
+ return;
278
+ }
279
+ if (this.isValidNodeCollect(nodeValue)) {
280
+ const childNode = this._deepFirstTravel(nodeValue, visitedNodeSet, { filePath, depth: depth + 1, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet, stopTravelCallback });
281
+ nodeCollection.push(childNode, ...childNode._util.nodeCollection);
282
+ children.push(childNode);
283
+ childNode._util.parentProperty = nodeKey;
284
+ } else if (this.isValidArrayNodeCollect(nodeValue)) {
285
+ const validNodeArray = nodeValue.filter((nodeItem) => this.isValidNodeCollect(nodeItem)).map((v) => {
286
+ return this._deepFirstTravel(v, visitedNodeSet, { filePath, depth: depth + 1, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet, stopTravelCallback });
287
+ });
288
+ nodeCollection.push(...validNodeArray.map((n) => [n, ...n._util.nodeCollection]).flat());
289
+ children.push(...validNodeArray);
290
+ validNodeArray.forEach((v, index) => {
291
+ v._util.parentProperty = nodeKey;
292
+ v._util.indexOfProperty = index;
293
+ });
294
+ }
295
+ });
296
+ try {
297
+ children.forEach((child) => child._util.parent = node);
298
+ nodeCollection.forEach((nodeItem) => nodeItem._util.ancestors.unshift(node));
299
+ } catch (e) {
300
+ console.error("parent ancestors update", e.message);
301
+ }
302
+ try {
303
+ const skip = nodeCollection.some((nodeItem) => stopTravelCallback == null ? void 0 : stopTravelCallback(nodeItem));
304
+ if (!skip) {
305
+ this.collectHoldingIds(node);
306
+ }
307
+ } catch (e) {
308
+ console.error("收集持有的 identifier 出错", e.message);
309
+ }
310
+ try {
311
+ this.collectDependenceIds(node);
312
+ } catch (e) {
313
+ console.error("收集使用的标识符号", e.message);
314
+ }
315
+ if (node.type === "Program") {
316
+ try {
317
+ nodeCollection.forEach((child) => this.collectEffectId(child));
318
+ } catch (e) {
319
+ console.error("collectEffectId", e.message);
320
+ }
321
+ try {
322
+ node.body.forEach((child) => this.updateImportedAndExportedMember(child, node));
323
+ } catch (e) {
324
+ console.error("收集导入、导出成员信息报错", e.message);
325
+ }
326
+ }
327
+ try {
328
+ this.updateLoc(node, { mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet });
329
+ } catch (e) {
330
+ console.error("updateLoc", e.message);
331
+ }
332
+ return node;
333
+ }
334
+ };
335
+ var AstUtil = _AstUtil;
336
+ AstUtil.invalidNodeKey = import_SHARED_CONSTANTS.INVALID_NODE_KEY;
337
+ AstUtil.EXPORT_DECLARATION_TYPES = import_SHARED_CONSTANTS.EXPORT_DECLARATION_TYPES;
338
+ AstUtil.windowProperties = import_windowProperties.windowProperties;
339
+ AstUtil.intrinsicElements = import_intrinsicElements.intrinsicElements;
340
+ AstUtil.standardAttributes = import_intrinsicElements.standardAttributes;
341
+ AstUtil.FUNCTION_TYPES = import_SHARED_CONSTANTS.FUNCTION_TYPES;
342
+ // Annotate the CommonJS export names for ESM import in node:
343
+ 0 && (module.exports = {
344
+ AstUtil
345
+ });
@@ -28,7 +28,7 @@ export interface AstNode {
28
28
  dependenceIdsNoScope: Set<AstNode>;
29
29
  holdingIds: Set<AstNode>;
30
30
  holdingIdNameMap: Map<string, Set<AstNode>>;
31
- holdingIdType: 'Import' | 'Variable' | 'Function' | 'Class' | 'Param' | null;
31
+ holdingIdType: 'Import' | 'Variable' | 'Function' | 'Class' | 'Param' | 'Enum' | 'Interface' | 'TypeAlias' | null;
32
32
  inject: Set<AstNode>;
33
33
  provide: Set<AstNode>;
34
34
  effectIds: Set<AstNode>;
@@ -52,7 +52,12 @@ export interface AstNode {
52
52
  }
53
53
  export default class AstUtil {
54
54
  static invalidNodeKey: string[];
55
- static EXPORT_DECLARATION_TYPES: string[];
55
+ static EXPORT_DECLARATION_TYPES: readonly ["FunctionDeclaration", "TSEnumDeclaration", "TSInterfaceDeclaration", "TSTypeAliasDeclaration", "ClassDeclaration"];
56
+ static windowProperties: string[];
57
+ static intrinsicElements: string[];
58
+ static standardAttributes: string[];
59
+ private static isValidNodeCollect;
60
+ private static isValidArrayNodeCollect;
56
61
  static getNodePath(node: AstNode): string;
57
62
  static getShortNodeMsg(node: AstNode, hideParentProperty?: boolean): string;
58
63
  static getAncestorsFromBirth(occupationId: AstNode, sourceId: AstNode): AstNode[];
@@ -88,11 +93,6 @@ export default class AstUtil {
88
93
  private static findIdOfVariable;
89
94
  private static _deepFindIdentifier;
90
95
  private static updateLoc;
91
- private static isValidArrayNodeCollect;
92
- private static isValidNodeCollect;
93
- static windowProperties: string[];
94
- static intrinsicElements: string[];
95
- static standardAttributes: string[];
96
96
  static isUntrackedId(id: AstNode): boolean;
97
97
  static isPropertyOfGlobal(node: AstNode): boolean;
98
98
  static isIntrinsicElement(node: AstNode): boolean | "" | undefined;