js-code-detector 0.0.19 → 0.0.21
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.
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +11 -3
- package/dist/cjs/util/ast_util/AstUtil.d.ts +4 -0
- package/dist/cjs/util/ast_util/AstUtil.js +87 -16
- package/dist/cjs/util/ast_util/FileUtil.js +8 -2
- package/dist/cjs/util/ast_util/getAstKitByFilePath.js +1 -1
- package/dist/cjs/util/report_util/createMdByJson.js +1 -1
- package/dist/cjs/util/report_util/diffBlockDetect.js +3 -2
- package/dist/cjs/util/report_util/generateGitDiffReport.d.ts +3 -0
- package/dist/cjs/util/report_util/generateGitDiffReport.js +145 -0
- package/dist/cjs/util/report_util/getMadgeInstance.d.ts +1 -0
- package/dist/cjs/util/report_util/getMadgeInstance.js +66 -0
- package/dist/cjs/util/report_util.js +3 -2
- package/package.json +2 -2
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export declare const gitDiffFileName = "git_diff.txt";
|
|
2
|
+
export declare const gitDiffJsonName = "git_diff.json";
|
|
1
3
|
export declare function umiPluginCallback(api: any): Promise<void>;
|
|
2
4
|
export declare function writeGitDiffTxt(gitUrl: string, branchName: string): Promise<string>;
|
|
3
5
|
export declare function getGitRepositoryAndBranch(): Promise<{
|
package/dist/cjs/index.js
CHANGED
|
@@ -33,6 +33,8 @@ __export(src_exports, {
|
|
|
33
33
|
getEslintCheckResult: () => getEslintCheckResult,
|
|
34
34
|
getGitRepositoryAndBranch: () => getGitRepositoryAndBranch,
|
|
35
35
|
gitDiffDetect: () => gitDiffDetect,
|
|
36
|
+
gitDiffFileName: () => gitDiffFileName,
|
|
37
|
+
gitDiffJsonName: () => gitDiffJsonName,
|
|
36
38
|
sameCodeDetect: () => sameCodeDetect,
|
|
37
39
|
umiPluginCallback: () => umiPluginCallback,
|
|
38
40
|
writeGitDiffTxt: () => writeGitDiffTxt
|
|
@@ -51,6 +53,7 @@ var import_readDirFiles = require("./util/shared/readDirFiles");
|
|
|
51
53
|
var import_Core = __toESM(require("./util/ast_util/Core"));
|
|
52
54
|
var import_constants = require("./util/constants");
|
|
53
55
|
var import_await_to_js = __toESM(require("await-to-js"));
|
|
56
|
+
var import_generateGitDiffReport = require("./util/report_util/generateGitDiffReport");
|
|
54
57
|
var jsonName = "git_diff_report.md";
|
|
55
58
|
var gitDiffFileName = "git_diff.txt";
|
|
56
59
|
var gitDiffJsonName = "git_diff.json";
|
|
@@ -151,9 +154,12 @@ async function gitDiffDetect() {
|
|
|
151
154
|
import_utils.logger.ready("准备生成插件文件");
|
|
152
155
|
(0, import_fs2.writeFileSync)((0, import_path.join)(process.cwd(), today, "target", "plugin.ts"), pluginFileContent, { encoding: "utf-8", flag: "w" });
|
|
153
156
|
import_utils.logger.info("插件文件生成完成");
|
|
154
|
-
import_utils.logger.wait("
|
|
155
|
-
await import_utils.execa.execa(`cd ${today}/target &&
|
|
156
|
-
import_utils.logger.info("
|
|
157
|
+
import_utils.logger.wait("准备生成 入口文件");
|
|
158
|
+
await import_utils.execa.execa(`cd ${today}/target && npx max setup`, { shell: "/bin/bash" });
|
|
159
|
+
import_utils.logger.info("入口文件 生成完成!");
|
|
160
|
+
import_utils.logger.ready("准备生成报告");
|
|
161
|
+
await (0, import_generateGitDiffReport.generateGitDiffReport)({ targetDirPath: (0, import_path.join)(process.cwd(), today, "target") });
|
|
162
|
+
import_utils.logger.info("报告完成");
|
|
157
163
|
import_utils.logger.ready("准备移动报告");
|
|
158
164
|
const content = (0, import_fs.readFileSync)((0, import_path.join)(process.cwd(), today, "target", jsonName), "utf-8");
|
|
159
165
|
const mdFileName = `${(0, import_dayjs.default)().format("YYYYMDD_HHmm")}_${jsonName}`;
|
|
@@ -192,6 +198,8 @@ async function getEslintCheckResult(today) {
|
|
|
192
198
|
getEslintCheckResult,
|
|
193
199
|
getGitRepositoryAndBranch,
|
|
194
200
|
gitDiffDetect,
|
|
201
|
+
gitDiffFileName,
|
|
202
|
+
gitDiffJsonName,
|
|
195
203
|
sameCodeDetect,
|
|
196
204
|
umiPluginCallback,
|
|
197
205
|
writeGitDiffTxt
|
|
@@ -25,6 +25,7 @@ export interface AstNode {
|
|
|
25
25
|
uuid: string;
|
|
26
26
|
variableScope: AstNode[];
|
|
27
27
|
dependenceIds: Set<AstNode>;
|
|
28
|
+
dependenceIdsNoScope: Set<AstNode>;
|
|
28
29
|
holdingIds: Set<AstNode>;
|
|
29
30
|
holdingIdNameMap: Map<string, Set<AstNode>>;
|
|
30
31
|
holdingIdType: 'Import' | 'Variable' | 'Function' | 'Class' | 'Param' | null;
|
|
@@ -40,6 +41,7 @@ export default class AstUtil {
|
|
|
40
41
|
static deepFirstTravel(node: AstNode, filePath: string, mapUuidToNode: Map<string, AstNode>, mapFileLineToNodeSet: Map<number, Set<AstNode>>, mapPathToNodeSet: Map<string, Set<AstNode>>): AstNode | undefined;
|
|
41
42
|
private static _deepFirstTravel;
|
|
42
43
|
private static collectInjectAndProvide;
|
|
44
|
+
private static handleDeclaration;
|
|
43
45
|
private static collectHoldingIds;
|
|
44
46
|
private static isVarInit;
|
|
45
47
|
static isReturnArgument(node: AstNode): boolean;
|
|
@@ -58,6 +60,8 @@ export default class AstUtil {
|
|
|
58
60
|
private static isBodyArray;
|
|
59
61
|
private static findExportIdentifiers;
|
|
60
62
|
private static expressionTypeIsIdentifier;
|
|
63
|
+
static collectExpressionIdentifiersShallow(exp: AstNode | null, callback: (identifier: AstNode) => void): void;
|
|
64
|
+
private static _collectExpressionIdentifiersShallow;
|
|
61
65
|
static collectExpressionIdentifiers(exp: AstNode | null, callback: (identifier: AstNode) => void): void;
|
|
62
66
|
private static _collectExpressionIdentifiers;
|
|
63
67
|
static deepFindIdOfExpression(exp: AstNode | null, callback: (identifier: AstNode) => void): void;
|
|
@@ -57,6 +57,7 @@ var _AstUtil = class {
|
|
|
57
57
|
uuid: "",
|
|
58
58
|
variableScope: [],
|
|
59
59
|
dependenceIds: /* @__PURE__ */ new Set(),
|
|
60
|
+
dependenceIdsNoScope: /* @__PURE__ */ new Set(),
|
|
60
61
|
holdingIdType: null,
|
|
61
62
|
holdingIds: /* @__PURE__ */ new Set(),
|
|
62
63
|
holdingIdNameMap: /* @__PURE__ */ new Map(),
|
|
@@ -137,6 +138,18 @@ var _AstUtil = class {
|
|
|
137
138
|
}
|
|
138
139
|
});
|
|
139
140
|
}
|
|
141
|
+
static handleDeclaration(node, callback) {
|
|
142
|
+
if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") {
|
|
143
|
+
const id = node.id;
|
|
144
|
+
if (id) {
|
|
145
|
+
callback(id);
|
|
146
|
+
}
|
|
147
|
+
} else if (node.type === "VariableDeclaration") {
|
|
148
|
+
this.findIdOfVariable(node, (id) => {
|
|
149
|
+
callback(id);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
140
153
|
static collectHoldingIds(node) {
|
|
141
154
|
const { holdingIds, holdingIdNameMap } = node._util;
|
|
142
155
|
if (this.isBodyArray(node)) {
|
|
@@ -149,18 +162,33 @@ var _AstUtil = class {
|
|
|
149
162
|
id._util.holdingIdType = "Import";
|
|
150
163
|
});
|
|
151
164
|
} else if (cur.type === "FunctionDeclaration" || cur.type === "ClassDeclaration") {
|
|
152
|
-
|
|
153
|
-
if (id) {
|
|
165
|
+
this.handleDeclaration(cur, (id) => {
|
|
154
166
|
holdingIds.add(id);
|
|
155
167
|
id._util.variableScope = [id];
|
|
156
168
|
id._util.holdingIdType = cur.type === "ClassDeclaration" ? "Class" : "Function";
|
|
157
|
-
}
|
|
169
|
+
});
|
|
158
170
|
} else if (cur.type === "VariableDeclaration") {
|
|
159
|
-
this.
|
|
171
|
+
this.handleDeclaration(cur, (id) => {
|
|
160
172
|
holdingIds.add(id);
|
|
161
173
|
id._util.variableScope = [id];
|
|
162
174
|
id._util.holdingIdType = "Variable";
|
|
163
175
|
});
|
|
176
|
+
} else if (cur.type === "ExportDefaultDeclaration") {
|
|
177
|
+
const declaration = cur.declaration;
|
|
178
|
+
this.handleDeclaration(declaration, (id) => {
|
|
179
|
+
holdingIds.add(id);
|
|
180
|
+
id._util.variableScope = [id];
|
|
181
|
+
id._util.holdingIdType = cur.type === "ClassDeclaration" ? "Class" : "Function";
|
|
182
|
+
});
|
|
183
|
+
} else if (cur.type === "ExportNamedDeclaration") {
|
|
184
|
+
const { declaration } = cur;
|
|
185
|
+
if (declaration) {
|
|
186
|
+
this.handleDeclaration(declaration, (id) => {
|
|
187
|
+
holdingIds.add(id);
|
|
188
|
+
id._util.variableScope = [id];
|
|
189
|
+
id._util.holdingIdType = "Variable";
|
|
190
|
+
});
|
|
191
|
+
}
|
|
164
192
|
}
|
|
165
193
|
});
|
|
166
194
|
}
|
|
@@ -183,9 +211,8 @@ var _AstUtil = class {
|
|
|
183
211
|
}
|
|
184
212
|
});
|
|
185
213
|
}
|
|
186
|
-
if (["FunctionDeclaration", "ArrowFunctionExpression", "FunctionExpression"].includes(node.type)) {
|
|
187
|
-
|
|
188
|
-
params.forEach((param) => this._deepFindIdentifier(param, (id) => {
|
|
214
|
+
if (["FunctionDeclaration", "ArrowFunctionExpression", "FunctionExpression", "ObjectMethod"].includes(node.type)) {
|
|
215
|
+
node.params.forEach((param) => this._deepFindIdentifier(param, (id) => {
|
|
189
216
|
holdingIds.add(id || node);
|
|
190
217
|
id._util.variableScope = [id];
|
|
191
218
|
id._util.holdingIdType = "Param";
|
|
@@ -207,18 +234,26 @@ var _AstUtil = class {
|
|
|
207
234
|
return node._util.parentProperty === "argument" && ((_a = node._util.parent) == null ? void 0 : _a.type) === "ReturnStatement";
|
|
208
235
|
}
|
|
209
236
|
static collectDependenceIds(node) {
|
|
210
|
-
const {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
237
|
+
const { type, _util } = node;
|
|
238
|
+
const { dependenceIds, holdingIdNameMap, children, dependenceIdsNoScope } = _util;
|
|
239
|
+
children.forEach((child) => {
|
|
240
|
+
if (child._util.dependenceIdsNoScope.size > 0) {
|
|
241
|
+
child._util.dependenceIdsNoScope.forEach((id) => dependenceIds.add(id));
|
|
242
|
+
}
|
|
243
|
+
this.findExportIdentifiers(child, (id) => dependenceIds.add(id));
|
|
244
|
+
this.collectExpressionIdentifiersShallow(child, (id) => dependenceIds.add(id));
|
|
214
245
|
});
|
|
215
246
|
for (const dependenceId of dependenceIds) {
|
|
216
247
|
if (dependenceId._util.variableScope.length === 0) {
|
|
217
248
|
const sameNameIds = [...holdingIdNameMap.get(dependenceId.name) || []];
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
firstPick._util.
|
|
249
|
+
if (sameNameIds.length > 0) {
|
|
250
|
+
dependenceId._util.variableScope.push(...sameNameIds);
|
|
251
|
+
const firstPick = sameNameIds[0];
|
|
252
|
+
if (firstPick && firstPick._util.uuid !== dependenceId._util.uuid) {
|
|
253
|
+
firstPick._util.effectIds.add(dependenceId);
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
dependenceIdsNoScope.add(dependenceId);
|
|
222
257
|
}
|
|
223
258
|
}
|
|
224
259
|
}
|
|
@@ -418,6 +453,35 @@ var _AstUtil = class {
|
|
|
418
453
|
static expressionTypeIsIdentifier(exp) {
|
|
419
454
|
return (exp == null ? void 0 : exp.type) === "Identifier";
|
|
420
455
|
}
|
|
456
|
+
static collectExpressionIdentifiersShallow(exp, callback) {
|
|
457
|
+
if (!exp || exp.type === "ThisExpression") {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
this._collectExpressionIdentifiersShallow(exp, callback);
|
|
461
|
+
}
|
|
462
|
+
static _collectExpressionIdentifiersShallow(exp, callback) {
|
|
463
|
+
var _a, _b, _c;
|
|
464
|
+
if (!exp || exp.type === "ThisExpression") {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (exp._util.holdingIdType !== null) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (exp.type === "Identifier") {
|
|
471
|
+
if (exp._util.parentProperty === "property" || exp._util.parentProperty === "key") {
|
|
472
|
+
if (exp._util.parent.computed) {
|
|
473
|
+
callback(exp);
|
|
474
|
+
}
|
|
475
|
+
} else if (!((_b = (_a = exp._util.parent) == null ? void 0 : _a.type) == null ? void 0 : _b.startsWith("TS"))) {
|
|
476
|
+
callback(exp);
|
|
477
|
+
}
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (exp.type === "JSXIdentifier" && ((_c = exp._util.parent) == null ? void 0 : _c.type) !== "JSXAttribute") {
|
|
481
|
+
callback(exp);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
421
485
|
static collectExpressionIdentifiers(exp, callback) {
|
|
422
486
|
if (!exp || exp.type === "ThisExpression") {
|
|
423
487
|
return;
|
|
@@ -564,6 +628,10 @@ var _AstUtil = class {
|
|
|
564
628
|
if (!id) {
|
|
565
629
|
return;
|
|
566
630
|
}
|
|
631
|
+
if (id.type === "AssignmentPattern") {
|
|
632
|
+
const left = id.left;
|
|
633
|
+
this._deepFindIdentifier(left, callback);
|
|
634
|
+
}
|
|
567
635
|
if (id.type === "Identifier") {
|
|
568
636
|
callback(id);
|
|
569
637
|
}
|
|
@@ -574,8 +642,11 @@ var _AstUtil = class {
|
|
|
574
642
|
const argument = property.argument;
|
|
575
643
|
this._deepFindIdentifier(argument, callback);
|
|
576
644
|
} else {
|
|
577
|
-
const value = property
|
|
645
|
+
const { value, key, computed } = property;
|
|
578
646
|
this._deepFindIdentifier(value, callback);
|
|
647
|
+
if (computed) {
|
|
648
|
+
this._deepFindIdentifier(key, callback);
|
|
649
|
+
}
|
|
579
650
|
}
|
|
580
651
|
}
|
|
581
652
|
}
|
|
@@ -36,6 +36,7 @@ __export(FileUtil_exports, {
|
|
|
36
36
|
module.exports = __toCommonJS(FileUtil_exports);
|
|
37
37
|
var babelParse = __toESM(require("@babel/parser"));
|
|
38
38
|
var vueParse = __toESM(require("vue-eslint-parser"));
|
|
39
|
+
var import_fs = __toESM(require("fs"));
|
|
39
40
|
var extensionsOfJs = [".js", ".jsx", ".ts", ".tsx"];
|
|
40
41
|
var extensions = [...extensionsOfJs, ".vue"];
|
|
41
42
|
var commonParsePlugins = [
|
|
@@ -131,8 +132,13 @@ var FileUtil = class {
|
|
|
131
132
|
return ["", null];
|
|
132
133
|
}
|
|
133
134
|
static getASTByFilePath(filePath) {
|
|
134
|
-
const
|
|
135
|
-
|
|
135
|
+
const existFlag = import_fs.default.existsSync(filePath);
|
|
136
|
+
if (existFlag) {
|
|
137
|
+
const fileContent = import_fs.default.readFileSync(filePath, "utf8");
|
|
138
|
+
return this.parseFile(filePath, fileContent)[1];
|
|
139
|
+
}
|
|
140
|
+
console.warn("文件不存在: " + filePath);
|
|
141
|
+
return null;
|
|
136
142
|
}
|
|
137
143
|
};
|
|
138
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -36,10 +36,10 @@ var import_AstUtil = __toESM(require("./AstUtil"));
|
|
|
36
36
|
var import_FileUtil = __toESM(require("./FileUtil"));
|
|
37
37
|
var mapFilePathToTools = /* @__PURE__ */ new Map();
|
|
38
38
|
var createMapFileLineToNodeSet = (file, absPathPrefix) => {
|
|
39
|
-
const ast = import_FileUtil.default.getASTByFilePath(file);
|
|
40
39
|
const mapUuidToNode = /* @__PURE__ */ new Map();
|
|
41
40
|
const mapPathToNodeSet = /* @__PURE__ */ new Map();
|
|
42
41
|
const mapFileLineToNodeSet = /* @__PURE__ */ new Map();
|
|
42
|
+
const ast = import_FileUtil.default.getASTByFilePath(file);
|
|
43
43
|
const filePathRelative = file.replace(absPathPrefix, "");
|
|
44
44
|
import_AstUtil.default.deepFirstTravel(ast, filePathRelative, mapUuidToNode, mapFileLineToNodeSet, mapPathToNodeSet);
|
|
45
45
|
return { mapFileLineToNodeSet, mapUuidToNode, mapPathToNodeSet };
|
|
@@ -40,7 +40,7 @@ function reportItemToMd(report) {
|
|
|
40
40
|
`## ${filePath}`,
|
|
41
41
|
`### 类型: ${mapReportType[type]}`,
|
|
42
42
|
filesDependsOnMe.length > 0 ? `### 所影响的文件(重要性由高到低)
|
|
43
|
-
${filesDependsOnMe.map((files, index) => files.map((file) => " ".repeat(index) + `- ${file}`)).flat().join("\n")}` : "",
|
|
43
|
+
${filesDependsOnMe.slice(0, 1).map((files, index) => files.map((file) => " ".repeat(index) + `- ${file}`)).flat().join("\n")}` : "",
|
|
44
44
|
undefinedIdentifiers.length > 0 ? `### 未定义的变量
|
|
45
45
|
> ${undefinedIdentifiers.map((e) => `**${e}**`).join(", ")}` : "",
|
|
46
46
|
dangerIdentifiers.length > 0 ? `### 重点检查使用的变量
|
|
@@ -60,8 +60,9 @@ function diffBlockDetect(gitDiffDetail, index, extra) {
|
|
|
60
60
|
const { reportItem, absPathPrefix } = extra;
|
|
61
61
|
const { blockReports, _fileRemovedNodesPaths, _fileAddedNodesPaths } = reportItem;
|
|
62
62
|
const blockReportItem = findOrCreateBlockReport(blockReports, index);
|
|
63
|
-
const
|
|
64
|
-
const
|
|
63
|
+
const filePathOfOld = (0, import_path.join)((0, import_path.dirname)(absPathPrefix), import_constants.SOURCE, filePath);
|
|
64
|
+
const filePathOfNew = (0, import_path.join)((0, import_path.dirname)(absPathPrefix), import_constants.TARGET, filePath);
|
|
65
|
+
const { mapFileLineToNodeSet, mapUuidToNode } = (0, import_getAstKitByFilePath.default)(filePathOfNew, absPathPrefix);
|
|
65
66
|
const { mapFileLineToNodeSet: mapFileLineToNodeSetOld } = (0, import_getAstKitByFilePath.default)(filePathOfOld, absPathPrefix.replace(`${import_constants.TARGET}/`, `${import_constants.SOURCE}/`));
|
|
66
67
|
const programNode = mapUuidToNode.get("Program");
|
|
67
68
|
if (programNode) {
|
|
@@ -0,0 +1,145 @@
|
|
|
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/report_util/generateGitDiffReport.ts
|
|
20
|
+
var generateGitDiffReport_exports = {};
|
|
21
|
+
__export(generateGitDiffReport_exports, {
|
|
22
|
+
generateGitDiffReport: () => generateGitDiffReport
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(generateGitDiffReport_exports);
|
|
25
|
+
var import_fs = require("fs");
|
|
26
|
+
var import_path = require("path");
|
|
27
|
+
var import__ = require("../../index");
|
|
28
|
+
var import_format_git_diff_content = require("../format_git_diff_content");
|
|
29
|
+
var import_utils = require("@umijs/utils");
|
|
30
|
+
var import_report_util = require("../report_util");
|
|
31
|
+
var import_createMdByJson = require("./createMdByJson");
|
|
32
|
+
var MADGE_NAME = "madge";
|
|
33
|
+
var userAliasGetter = (cwd, appData) => {
|
|
34
|
+
var _a;
|
|
35
|
+
return ((_a = appData.config) == null ? void 0 : _a.alias) || {
|
|
36
|
+
umi: "@@/exports",
|
|
37
|
+
"@": cwd + "/src",
|
|
38
|
+
"@@": cwd + "/src/.umi",
|
|
39
|
+
"@umijs/max": "@@/exports"
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
var reportFileName = "git_diff_report.md";
|
|
43
|
+
async function generateGitDiffReport(arg) {
|
|
44
|
+
const { targetDirPath } = arg;
|
|
45
|
+
const absSrcPath = (0, import_path.join)(targetDirPath, "src");
|
|
46
|
+
const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import__.gitDiffFileName), "utf-8");
|
|
47
|
+
const gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
|
|
48
|
+
(0, import_fs.writeFileSync)((0, import_path.join)(targetDirPath, import__.gitDiffJsonName), JSON.stringify(gitDiffDetail, null, 2), { encoding: "utf-8", flag: "w" });
|
|
49
|
+
const tsconfig = await import_utils.tsconfigPaths.loadConfig(targetDirPath);
|
|
50
|
+
const appDataContent = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, "src", ".umi", "appData.json"), "utf-8");
|
|
51
|
+
let appData = { config: null };
|
|
52
|
+
try {
|
|
53
|
+
appData = JSON.parse(appDataContent);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.warn("appData.json 解析失败,将使用默认别名");
|
|
56
|
+
}
|
|
57
|
+
const userAlias = userAliasGetter(targetDirPath, appData);
|
|
58
|
+
const exclude = [/node_modules/, /\.d\.ts$/, /\.umi/];
|
|
59
|
+
const isExclude = (path) => {
|
|
60
|
+
return exclude.some((reg) => reg.test(path));
|
|
61
|
+
};
|
|
62
|
+
const parsedAlias = import_utils.aliasUtils.parseCircleAlias({
|
|
63
|
+
alias: userAlias
|
|
64
|
+
});
|
|
65
|
+
const filteredAlias = Object.keys(parsedAlias).reduce(
|
|
66
|
+
(acc, key) => {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
const value = parsedAlias[key];
|
|
69
|
+
if (isExclude(value)) {
|
|
70
|
+
return acc;
|
|
71
|
+
}
|
|
72
|
+
if ((_a = tsconfig.paths) == null ? void 0 : _a[key]) {
|
|
73
|
+
return acc;
|
|
74
|
+
}
|
|
75
|
+
const tsconfigValue = [(0, import_path.join)((0, import_path.relative)(targetDirPath, value), "/*")];
|
|
76
|
+
const tsconfigKey = `${key}/*`;
|
|
77
|
+
if ((_b = tsconfig.paths) == null ? void 0 : _b[tsconfigKey]) {
|
|
78
|
+
return acc;
|
|
79
|
+
}
|
|
80
|
+
acc[tsconfigKey] = tsconfigValue;
|
|
81
|
+
return acc;
|
|
82
|
+
},
|
|
83
|
+
{}
|
|
84
|
+
);
|
|
85
|
+
const devTmpDir = (0, import_path.join)(absSrcPath, ".umi");
|
|
86
|
+
const exportsFile = (0, import_path.join)(devTmpDir, "exports.ts");
|
|
87
|
+
const madgePkg = (0, import_path.dirname)(
|
|
88
|
+
import_utils.resolve.sync(`${MADGE_NAME}/package.json`, {
|
|
89
|
+
basedir: process.cwd()
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
const madge = require(madgePkg);
|
|
93
|
+
const madgeConfig = {
|
|
94
|
+
tsConfig: {
|
|
95
|
+
compilerOptions: {
|
|
96
|
+
baseUrl: targetDirPath,
|
|
97
|
+
paths: {
|
|
98
|
+
...filteredAlias,
|
|
99
|
+
...tsconfig.paths,
|
|
100
|
+
umi: [exportsFile],
|
|
101
|
+
"@umijs/max": [exportsFile]
|
|
102
|
+
},
|
|
103
|
+
target: "esnext",
|
|
104
|
+
module: "esnext",
|
|
105
|
+
moduleResolution: "node",
|
|
106
|
+
importHelpers: true,
|
|
107
|
+
jsx: "react-jsx",
|
|
108
|
+
esModuleInterop: true,
|
|
109
|
+
strict: true,
|
|
110
|
+
resolveJsonModule: true,
|
|
111
|
+
allowSyntheticDefaultImports: true
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
115
|
+
excludeRegExp: exclude,
|
|
116
|
+
baseDir: targetDirPath
|
|
117
|
+
};
|
|
118
|
+
const res = await madge((0, import_path.join)(devTmpDir, "umi.ts"), madgeConfig);
|
|
119
|
+
const treeMap = res.tree;
|
|
120
|
+
const dependenceMap = Object.keys(treeMap).reduce(
|
|
121
|
+
(acc, key) => {
|
|
122
|
+
const path = (0, import_utils.winPath)((0, import_path.join)(targetDirPath, key));
|
|
123
|
+
acc[path] = true;
|
|
124
|
+
return acc;
|
|
125
|
+
},
|
|
126
|
+
{}
|
|
127
|
+
);
|
|
128
|
+
const usingFiles = (0, import_utils.readDirFiles)({
|
|
129
|
+
dir: absSrcPath,
|
|
130
|
+
exclude
|
|
131
|
+
}).filter(({ filePath }) => dependenceMap[filePath]);
|
|
132
|
+
const tree = res.tree;
|
|
133
|
+
const absPathPrefix = targetDirPath + "/";
|
|
134
|
+
const usingFileNoPrefix = usingFiles.map((item) => item.filePath.replace(absPathPrefix, ""));
|
|
135
|
+
const groupGitDiffLines = gitDiffDetail.filter((item) => usingFileNoPrefix.includes(item.filePath));
|
|
136
|
+
(0, import_fs.writeFileSync)((0, import_path.join)(targetDirPath, "reports_helper.json"), JSON.stringify({ groupGitDiffLines, absPathPrefix, tree, filteredAlias, parsedAlias, tsconfig, madgeConfig }, null, 2), { encoding: "utf-8", flag: "w" });
|
|
137
|
+
const reports = (0, import_report_util.createDetectReport)({ groupGitDiffLines, tree, absPathPrefix });
|
|
138
|
+
(0, import_fs.writeFileSync)((0, import_path.join)(targetDirPath, "reports.json"), JSON.stringify(reports, null, 2), { encoding: "utf-8", flag: "w" });
|
|
139
|
+
const mdContent = (0, import_createMdByJson.createMdByJson)(reports);
|
|
140
|
+
(0, import_fs.writeFileSync)((0, import_path.join)(targetDirPath, reportFileName), mdContent, { encoding: "utf-8", flag: "w" });
|
|
141
|
+
}
|
|
142
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
143
|
+
0 && (module.exports = {
|
|
144
|
+
generateGitDiffReport
|
|
145
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getMadgeInstance(devTmpDir: string, targetDirPath: string, exclude: RegExp[], filteredAlias: Record<string, string[]>, tsconfig?: any): Promise<void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
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/report_util/getMadgeInstance.ts
|
|
20
|
+
var getMadgeInstance_exports = {};
|
|
21
|
+
__export(getMadgeInstance_exports, {
|
|
22
|
+
getMadgeInstance: () => getMadgeInstance
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(getMadgeInstance_exports);
|
|
25
|
+
var import_path = require("path");
|
|
26
|
+
var import_utils = require("@umijs/utils");
|
|
27
|
+
var MADGE_NAME = "madge";
|
|
28
|
+
async function getMadgeInstance(devTmpDir, targetDirPath, exclude, filteredAlias, tsconfig = {}) {
|
|
29
|
+
const exportsFile = (0, import_path.join)(devTmpDir, "exports.ts");
|
|
30
|
+
const madgePkg = (0, import_path.dirname)(
|
|
31
|
+
import_utils.resolve.sync(`${MADGE_NAME}/package.json`, {
|
|
32
|
+
basedir: process.cwd()
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
const madge = require(madgePkg);
|
|
36
|
+
const madgeConfig = {
|
|
37
|
+
tsConfig: {
|
|
38
|
+
compilerOptions: {
|
|
39
|
+
baseUrl: tsconfig.baseUrl,
|
|
40
|
+
paths: {
|
|
41
|
+
...filteredAlias,
|
|
42
|
+
...tsconfig.paths,
|
|
43
|
+
umi: [exportsFile],
|
|
44
|
+
"@umijs/max": [exportsFile]
|
|
45
|
+
},
|
|
46
|
+
target: "esnext",
|
|
47
|
+
module: "esnext",
|
|
48
|
+
moduleResolution: "node",
|
|
49
|
+
importHelpers: true,
|
|
50
|
+
jsx: "react-jsx",
|
|
51
|
+
esModuleInterop: true,
|
|
52
|
+
strict: true,
|
|
53
|
+
resolveJsonModule: true,
|
|
54
|
+
allowSyntheticDefaultImports: true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
58
|
+
excludeRegExp: exclude,
|
|
59
|
+
baseDir: targetDirPath
|
|
60
|
+
};
|
|
61
|
+
const res = await madge((0, import_path.join)(devTmpDir, "umi.ts"), madgeConfig);
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
getMadgeInstance
|
|
66
|
+
});
|
|
@@ -36,6 +36,7 @@ var import_AstUtil = __toESM(require("./ast_util/AstUtil"));
|
|
|
36
36
|
var import_file_identifier_detect = require("./report_util/file_identifier_detect");
|
|
37
37
|
var import_getFileDepends = __toESM(require("./report_util/getFileDepends"));
|
|
38
38
|
var import_diffBlockDetect = require("./report_util/diffBlockDetect");
|
|
39
|
+
var import_path = require("path");
|
|
39
40
|
function createDetectReport(arg) {
|
|
40
41
|
const { groupGitDiffLines, tree, absPathPrefix } = arg;
|
|
41
42
|
const reports = [];
|
|
@@ -56,8 +57,8 @@ function createDetectReport(arg) {
|
|
|
56
57
|
};
|
|
57
58
|
reports.push(reportItem);
|
|
58
59
|
}
|
|
59
|
-
reportItem.undefinedIdentifiers = (0, import_file_identifier_detect.extractUndefinedIdentifiers)(filePath, absPathPrefix);
|
|
60
|
-
reportItem.dangerIdentifiers = (0, import_file_identifier_detect.fileIdentifierDetect)(filePath, absPathPrefix);
|
|
60
|
+
reportItem.undefinedIdentifiers = (0, import_file_identifier_detect.extractUndefinedIdentifiers)((0, import_path.join)(absPathPrefix, filePath), absPathPrefix);
|
|
61
|
+
reportItem.dangerIdentifiers = (0, import_file_identifier_detect.fileIdentifierDetect)((0, import_path.join)(absPathPrefix, filePath), absPathPrefix);
|
|
61
62
|
if (type === "modify") {
|
|
62
63
|
(0, import_diffBlockDetect.diffBlockDetect)(item, index, { reportItem, absPathPrefix });
|
|
63
64
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-code-detector",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/cjs/index.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"dev": "father dev",
|
|
9
9
|
"build": "father build",
|
|
10
10
|
"build:deps": "father prebundle",
|
|
11
|
-
"prepublishOnly": "father doctor && npm run build"
|
|
11
|
+
"prepublishOnly": "nrm use npm && father doctor && npm run build"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [],
|
|
14
14
|
"authors": [
|