js-code-detector 0.0.52 → 0.0.53

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 (78) hide show
  1. package/dist/cjs/index.d.ts +1 -1
  2. package/dist/cjs/services/DetectService.js +7 -1
  3. package/dist/cjs/services/projectServiceClass/ViteProjectService.d.ts +23 -0
  4. package/dist/cjs/services/projectServiceClass/ViteProjectService.js +142 -0
  5. package/dist/cjs/services/projectServiceClass/VueProjectService.js +1 -1
  6. package/dist/cjs/util/ast_util/AstKit.d.ts +5 -21
  7. package/dist/cjs/util/ast_util/AstKit.js +33 -524
  8. package/dist/cjs/util/ast_util/AstUtil.d.ts +2 -1
  9. package/dist/cjs/util/ast_util/AstUtil.js +14 -9
  10. package/dist/cjs/util/ast_util/getAstKitByFilePath.d.ts +1 -1
  11. package/dist/cjs/util/ast_util/getAstKitByFilePath.js +1 -1
  12. package/dist/cjs/util/ast_util/helper/collectDeclarationIdentifiers.d.ts +2 -0
  13. package/dist/cjs/util/ast_util/helper/collectDeclarationIdentifiers.js +31 -0
  14. package/dist/cjs/util/ast_util/helper/collectDependenceIds.d.ts +2 -0
  15. package/dist/cjs/util/ast_util/helper/collectDependenceIds.js +90 -0
  16. package/dist/cjs/util/ast_util/helper/collectDependenceIdsOfExportMembers.d.ts +2 -0
  17. package/dist/cjs/util/ast_util/helper/collectDependenceIdsOfExportMembers.js +44 -0
  18. package/dist/cjs/util/ast_util/helper/collectEffectId.d.ts +2 -0
  19. package/dist/cjs/util/ast_util/helper/collectEffectId.js +26 -0
  20. package/dist/cjs/util/ast_util/helper/collectExpressionIdentifiersShallow.d.ts +2 -0
  21. package/dist/cjs/util/ast_util/helper/collectExpressionIdentifiersShallow.js +39 -0
  22. package/dist/cjs/util/ast_util/helper/collectHoldingIds.d.ts +4 -0
  23. package/dist/cjs/util/ast_util/helper/collectHoldingIds.js +110 -0
  24. package/dist/cjs/util/ast_util/helper/collectImportDeclarationIdentifiers.d.ts +2 -0
  25. package/dist/cjs/util/ast_util/helper/collectImportDeclarationIdentifiers.js +38 -0
  26. package/dist/cjs/util/ast_util/helper/collectVariableDeclarationIdentifiers.d.ts +2 -0
  27. package/dist/cjs/util/ast_util/helper/collectVariableDeclarationIdentifiers.js +43 -0
  28. package/dist/cjs/util/ast_util/helper/createAstNodeExt.d.ts +40 -0
  29. package/dist/cjs/util/ast_util/helper/createAstNodeExt.js +54 -0
  30. package/dist/cjs/util/ast_util/helper/deepFirstTravel.d.ts +9 -0
  31. package/dist/cjs/util/ast_util/helper/deepFirstTravel.js +142 -0
  32. package/dist/cjs/util/ast_util/helper/deepSearchParamsIdentifier.d.ts +2 -0
  33. package/dist/cjs/util/ast_util/helper/deepSearchParamsIdentifier.js +55 -0
  34. package/dist/cjs/util/ast_util/helper/findOrCreateExportedMember.d.ts +9 -0
  35. package/dist/cjs/util/ast_util/helper/findOrCreateExportedMember.js +32 -0
  36. package/dist/cjs/util/ast_util/helper/findOrCreateImportedMember.d.ts +8 -0
  37. package/dist/cjs/util/ast_util/helper/findOrCreateImportedMember.js +32 -0
  38. package/dist/cjs/util/ast_util/helper/getExportedNameOfAncestor.d.ts +2 -0
  39. package/dist/cjs/util/ast_util/helper/getExportedNameOfAncestor.js +113 -0
  40. package/dist/cjs/util/ast_util/helper/getExportedNameOfDeclarationIdentifier.d.ts +2 -0
  41. package/dist/cjs/util/ast_util/helper/getExportedNameOfDeclarationIdentifier.js +40 -0
  42. package/dist/cjs/util/ast_util/helper/getImpactedNode.d.ts +2 -0
  43. package/dist/cjs/util/ast_util/helper/getImpactedNode.js +50 -0
  44. package/dist/cjs/util/ast_util/helper/getNearestImpactedNode.d.ts +2 -0
  45. package/dist/cjs/util/ast_util/helper/getNearestImpactedNode.js +43 -0
  46. package/dist/cjs/util/ast_util/helper/getNodePath.d.ts +2 -0
  47. package/dist/cjs/util/ast_util/helper/getNodePath.js +27 -0
  48. package/dist/cjs/util/ast_util/helper/getShortNodeMsg.d.ts +2 -0
  49. package/dist/cjs/util/ast_util/helper/getShortNodeMsg.js +38 -0
  50. package/dist/cjs/util/ast_util/helper/getTopScopeNodesByLineNumberRange.d.ts +2 -0
  51. package/dist/cjs/util/ast_util/helper/getTopScopeNodesByLineNumberRange.js +51 -0
  52. package/dist/cjs/util/ast_util/helper/isIdentifierUntracked.d.ts +2 -0
  53. package/dist/cjs/util/ast_util/helper/isIdentifierUntracked.js +39 -0
  54. package/dist/cjs/util/ast_util/helper/isValidArrayNodeCollect.d.ts +2 -0
  55. package/dist/cjs/util/ast_util/helper/isValidArrayNodeCollect.js +27 -0
  56. package/dist/cjs/util/ast_util/helper/isValidNodeCollect.d.ts +2 -0
  57. package/dist/cjs/util/ast_util/helper/isValidNodeCollect.js +27 -0
  58. package/dist/cjs/util/ast_util/helper/syncTravel.d.ts +11 -0
  59. package/dist/cjs/util/ast_util/helper/syncTravel.js +325 -0
  60. package/dist/cjs/util/ast_util/helper/updateImportedAndExportedMember.d.ts +2 -0
  61. package/dist/cjs/util/ast_util/helper/updateImportedAndExportedMember.js +135 -0
  62. package/dist/cjs/util/ast_util/helper/updateLoc.d.ts +6 -0
  63. package/dist/cjs/util/ast_util/helper/updateLoc.js +74 -0
  64. package/dist/cjs/util/project_umi_util/isVueEntryFile.d.ts +1 -0
  65. package/dist/cjs/util/project_umi_util/isVueEntryFile.js +57 -0
  66. package/dist/cjs/util/project_umi_util/tsConfigPathsToWebpackAlias.d.ts +10 -0
  67. package/dist/cjs/util/project_umi_util/tsConfigPathsToWebpackAlias.js +80 -0
  68. package/dist/cjs/util/report_util/filterEffectedCode.d.ts +1 -1
  69. package/dist/cjs/util/report_util/filterEffectedExportMember.d.ts +2 -0
  70. package/dist/cjs/util/report_util/filterEffectedExportMember.js +9 -1
  71. package/dist/cjs/util/report_util/generateGitDiffReport.d.ts +1 -1
  72. package/dist/cjs/util/report_util.d.ts +1 -1
  73. package/dist/cjs/util/shared/getMajorVersion.d.ts +1 -0
  74. package/dist/cjs/util/shared/getMajorVersion.js +37 -0
  75. package/dist/cjs/util/shared/getRepoSupportFlag.d.ts +1 -1
  76. package/dist/cjs/util/shared/getRepoSupportFlag.js +8 -3
  77. package/dist/cjs/util/shared/gitDiffTool.d.ts +1 -1
  78. package/package.json +1 -1
@@ -20,7 +20,7 @@ export declare function gitDiffDetect(): Promise<{
20
20
  effectsDownstream: string[];
21
21
  }[];
22
22
  }[];
23
- type: "add" | "delete" | "modify";
23
+ type: "modify" | "add" | "delete";
24
24
  filesDependsOnMe: string[];
25
25
  undefinedIdentifiers: string[];
26
26
  filePath: string;
@@ -44,6 +44,7 @@ var import_UmiProjectService = __toESM(require("./projectServiceClass/UmiProject
44
44
  var import_perf_hooks = require("perf_hooks");
45
45
  var import_cloneRepoWithBranchAndDefault = __toESM(require("../util/git_util/cloneRepoWithBranchAndDefault"));
46
46
  var import_VueProjectService = __toESM(require("./projectServiceClass/VueProjectService"));
47
+ var import_ViteProjectService = __toESM(require("./projectServiceClass/ViteProjectService"));
47
48
  var DetectService = class {
48
49
  constructor(option) {
49
50
  this.directoryInfo = {
@@ -158,7 +159,7 @@ var DetectService = class {
158
159
  await (0, import_execGitDiff.default)(targetDirPath, baseBranch, targetBranch);
159
160
  const repoType = await (0, import_getRepoSupportFlag.getRepoType)((0, import_path.join)(targetDirPath, "package.json"));
160
161
  this.gitInfo.repoType = repoType;
161
- import_utils.logger.info("克隆仓库成功");
162
+ import_utils.logger.info(`克隆仓库成功, repoType: ${repoType}`);
162
163
  }
163
164
  async move() {
164
165
  const repoType = this.gitInfo.repoType;
@@ -172,7 +173,9 @@ var DetectService = class {
172
173
  async projectHandle() {
173
174
  var _a;
174
175
  const { repoType } = this.gitInfo;
176
+ import_utils.logger.info("开始处理项目" + repoType);
175
177
  if (!(0, import_getRepoSupportFlag.isRepoTypeSupported)(repoType)) {
178
+ import_utils.logger.info("不支持的 repoType:" + repoType);
176
179
  return;
177
180
  }
178
181
  if (repoType === "umi") {
@@ -181,6 +184,9 @@ var DetectService = class {
181
184
  if (repoType === "vue2") {
182
185
  this.projectService = new import_VueProjectService.default(this);
183
186
  }
187
+ if (repoType === "vite") {
188
+ this.projectService = new import_ViteProjectService.default(this);
189
+ }
184
190
  await ((_a = this.projectService) == null ? void 0 : _a.run());
185
191
  }
186
192
  formatResult() {
@@ -0,0 +1,23 @@
1
+ import { GitDiffDetail } from "../../util/format_git_diff_content";
2
+ import { ProjectService } from "../ProjectService";
3
+ import { DetectService } from "../DetectService";
4
+ import { IMadgeInstance } from "../../util/report_util/getMadgeInstance";
5
+ export default class ViteProjectService implements ProjectService {
6
+ detectService: DetectService;
7
+ gitDiffDetail: GitDiffDetail[];
8
+ helpInfo: ProjectService['helpInfo'];
9
+ outputResult: ProjectService['outputResult'];
10
+ viteHelpInfo: {
11
+ madgeResult: IMadgeInstance | null;
12
+ tsConfigPath: string;
13
+ entryFiles: string[];
14
+ extensions: string[];
15
+ };
16
+ constructor(detectService: DetectService);
17
+ run(): Promise<void>;
18
+ parseGitDiffContent(): Promise<void>;
19
+ setAlias(): Promise<void>;
20
+ readSrcDirFilesAndSetEntries(): Promise<void>;
21
+ getMadgeResult(): Promise<void>;
22
+ collectEffectedFiles(): Promise<void>;
23
+ }
@@ -0,0 +1,142 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/services/projectServiceClass/ViteProjectService.ts
30
+ var ViteProjectService_exports = {};
31
+ __export(ViteProjectService_exports, {
32
+ default: () => ViteProjectService
33
+ });
34
+ module.exports = __toCommonJS(ViteProjectService_exports);
35
+ var import_format_git_diff_content = require("../../util/format_git_diff_content");
36
+ var import_fs = require("fs");
37
+ var import_path = require("path");
38
+ var import_constants = require("../../util/constants");
39
+ var import_utils = require("@umijs/utils");
40
+ var import_madge = __toESM(require("madge"));
41
+ var import_collectUpstreamFiles = __toESM(require("../../util/shared/collectUpstreamFiles"));
42
+ var import_createDependenceMap = require("../../util/report_util/createDependenceMap");
43
+ var import_filterEffectedExportMember = __toESM(require("../../util/report_util/filterEffectedExportMember"));
44
+ var import_isVueEntryFile = __toESM(require("../../util/project_umi_util/isVueEntryFile"));
45
+ var import_tsConfigPathsToWebpackAlias = require("../../util/project_umi_util/tsConfigPathsToWebpackAlias");
46
+ var ViteProjectService = class {
47
+ constructor(detectService) {
48
+ this.detectService = detectService;
49
+ this.gitDiffDetail = [];
50
+ this.helpInfo = {
51
+ projectFileList: [],
52
+ parsedAlias: {}
53
+ };
54
+ this.outputResult = {
55
+ effectedImportUsage: [],
56
+ error: null
57
+ };
58
+ this.viteHelpInfo = {
59
+ madgeResult: null,
60
+ tsConfigPath: "",
61
+ entryFiles: [],
62
+ extensions: []
63
+ };
64
+ this.detectService = detectService;
65
+ }
66
+ async run() {
67
+ import_utils.logger.info("格式化 git diff 内容...");
68
+ await this.parseGitDiffContent();
69
+ import_utils.logger.info("获取 Alias 配置...");
70
+ await this.setAlias();
71
+ import_utils.logger.info("读取 src 目录文件...");
72
+ await this.readSrcDirFilesAndSetEntries();
73
+ import_utils.logger.info("获取 madge 结果...");
74
+ await this.getMadgeResult();
75
+ import_utils.logger.info("收集受影响的文件...");
76
+ await this.collectEffectedFiles();
77
+ }
78
+ async parseGitDiffContent() {
79
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
80
+ const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import_constants.gitDiffFileName), "utf-8");
81
+ this.gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
82
+ }
83
+ async setAlias() {
84
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
85
+ this.helpInfo.parsedAlias = await (0, import_tsConfigPathsToWebpackAlias.tsConfigPathsToWebpackAlias)({
86
+ tsconfigPath: (0, import_path.join)(targetDirPath, "tsConfig.json"),
87
+ projectRoot: targetDirPath,
88
+ excludeTypeOnly: false
89
+ });
90
+ }
91
+ async readSrcDirFilesAndSetEntries() {
92
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
93
+ const projectFileList = (0, import_utils.readDirFiles)({ dir: (0, import_path.join)(targetDirPath, "src") }).filter((e) => e.filePath.endsWith(".ts") || e.filePath.endsWith(".js"));
94
+ this.viteHelpInfo.entryFiles = projectFileList.filter((file) => (0, import_isVueEntryFile.default)(file.filePath)).map((e) => e.filePath);
95
+ }
96
+ async getMadgeResult() {
97
+ const targetBranchDir = this.detectService.directoryInfo.targetBranchDir;
98
+ const entryFilePath = this.viteHelpInfo.entryFiles;
99
+ const tsConfig = (0, import_path.join)(targetBranchDir, "tsconfig.json");
100
+ const madgeConfig = {
101
+ baseDir: targetBranchDir,
102
+ fileExtensions: ["vue", "ts", "js"],
103
+ tsConfig,
104
+ excludeRegExp: [/node_modules/, /\.d\.ts$/]
105
+ };
106
+ const res = await (0, import_madge.default)(entryFilePath, madgeConfig);
107
+ this.viteHelpInfo.madgeResult = res;
108
+ this.helpInfo.projectFileList = Object.keys(res.tree);
109
+ }
110
+ async collectEffectedFiles() {
111
+ const { parsedAlias } = this.helpInfo;
112
+ const { madgeResult } = this.viteHelpInfo;
113
+ const projectFileList = this.helpInfo.projectFileList;
114
+ const targetBranchDir = this.detectService.directoryInfo.targetBranchDir;
115
+ const absPathPrefix = (0, import_path.join)(targetBranchDir, "/");
116
+ const validModifiedFiles = this.gitDiffDetail.reduce((acc, item) => {
117
+ const { filePath } = item;
118
+ this.helpInfo.projectFileList.includes(filePath) && acc.push(filePath);
119
+ return acc;
120
+ }, []);
121
+ const possibleEffectedFiles = (0, import_collectUpstreamFiles.default)(madgeResult, validModifiedFiles);
122
+ const possibleEffectedFilesFullPath = possibleEffectedFiles.map((file) => (0, import_path.join)(absPathPrefix, file));
123
+ const mapRef = (0, import_createDependenceMap.createExportedNameToReferenceLocalSet)(possibleEffectedFilesFullPath, parsedAlias, absPathPrefix, projectFileList);
124
+ const { import2export, indirectExportMembers } = mapRef;
125
+ const gitDiffDetail = this.gitDiffDetail;
126
+ const validGitDiffDetail = gitDiffDetail.filter((item) => possibleEffectedFiles.includes(item.filePath));
127
+ const effectedExportNames = validGitDiffDetail.map((item) => {
128
+ const { filePath, newBranchLineScope, startLineOfNew } = item;
129
+ const exportedNames = (0, import_filterEffectedExportMember.default)((0, import_path.join)(absPathPrefix, filePath), absPathPrefix, Number(startLineOfNew), Number(startLineOfNew) + Number(newBranchLineScope));
130
+ return exportedNames.map((name) => [filePath, name].join("#"));
131
+ }).flat();
132
+ const effectedImportUsage = [...Object.entries(import2export), ...Object.entries(indirectExportMembers)].filter(([_, value]) => {
133
+ return effectedExportNames.includes(value);
134
+ });
135
+ this.outputResult.effectedImportUsage = effectedImportUsage;
136
+ const token = this.detectService.gitInfo.token;
137
+ if (!token) {
138
+ const pwd = (0, import_path.join)(this.detectService.directoryInfo.tmpWorkDir, "..");
139
+ (0, import_fs.writeFileSync)((0, import_path.join)(pwd, "effectedImportUsage.json"), JSON.stringify({ tree: madgeResult == null ? void 0 : madgeResult.tree, projectFileList, possibleEffectedFiles, gitDiffDetailFiles: gitDiffDetail.map((e) => e.filePath), validGitDiffDetail, effectedImportUsage, ...mapRef }, null, 2));
140
+ }
141
+ }
142
+ };
@@ -176,7 +176,7 @@ var VueProjectService = class {
176
176
  baseDir: this.detectService.directoryInfo.targetBranchDir,
177
177
  fileExtensions: this.vueHelpInfo.extensions.map((ext) => ext.replace(/^\./, "")),
178
178
  webpackConfig: this.vueHelpInfo.webpackConfigPath,
179
- excludeRegExp: [/node_modules/]
179
+ excludeRegExp: [/node_modules/, /\.d\.ts$/]
180
180
  };
181
181
  const res = await (0, import_madge.default)(entryFilePath, madgeConfig);
182
182
  this.vueHelpInfo.madgeResult = res;
@@ -1,30 +1,14 @@
1
1
  import { AstNode } from "./AstUtil";
2
2
  export declare class AstUtil {
3
- static invalidNodeKey: string[];
4
3
  static EXPORT_DECLARATION_TYPES: readonly ["FunctionDeclaration", "TSEnumDeclaration", "TSInterfaceDeclaration", "TSTypeAliasDeclaration", "ClassDeclaration"];
5
4
  static windowProperties: string[];
6
5
  static intrinsicElements: string[];
7
- static standardAttributes: string[];
8
- static FUNCTION_TYPES: string[];
6
+ static isUntrackedId(id: AstNode): boolean;
9
7
  static getNodePath(node: AstNode): string;
10
8
  static getShortNodeMsg(node: AstNode, hideParentProperty?: boolean): string;
9
+ static findExportedMembersNameFromAncestors(node: AstNode | undefined | null): string[];
10
+ static getAncestorsFromBirth(occupationId: AstNode, sourceId: AstNode): AstNode[];
11
+ static getNearestImpactedNode(ancestors: AstNode[]): AstNode | undefined;
11
12
  static getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet: Map<number, Set<AstNode>>, lineNumberStart: number, lineNumberEnd: number, loose?: boolean): AstNode[];
12
- private static isValidNodeCollect;
13
- private static isValidArrayNodeCollect;
14
- private static collectEffectId;
15
- private static findOrCreateImportedMember;
16
- private static findOrCreateExportedMember;
17
- private static updateImportedAndExportedMember;
18
- private static deepSearchParamsIdentifier;
19
- private static collectDependenceIdsOfExportMembers;
20
- private static collectExpressionIdentifiersShallow;
21
- private static _collectExpressionIdentifiersShallow;
22
- private static collectDependenceIds;
23
- private static updateLoc;
24
- private static collectVariableDeclarationIdentifiers;
25
- private static collectDeclarationIdentifiers;
26
- private static collectImportDeclarationIdentifiers;
27
- private static collectHoldingIds;
28
- 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;
29
- private static _deepFirstTravel;
13
+ 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): void;
30
14
  }