js-code-detector 0.0.12 → 0.0.14

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.
@@ -7,7 +7,7 @@ type EffectItem = {
7
7
  };
8
8
  export type BlockReport = {
9
9
  index: number;
10
- kind: BlockReportKind;
10
+ kindList: BlockReportKind[];
11
11
  diff_txt: string[];
12
12
  topAdded: AstNode[];
13
13
  topRemoved: AstNode[];
@@ -37,7 +37,7 @@ var import_getAstKitByFilePath = __toESM(require("../ast_util/getAstKitByFilePat
37
37
  var import_AstUtil = __toESM(require("../ast_util/AstUtil"));
38
38
  var createBlockReport = (kind, index) => ({
39
39
  index,
40
- kind,
40
+ kindList: [kind],
41
41
  diff_txt: [],
42
42
  topAdded: [],
43
43
  topRemoved: [],
@@ -49,16 +49,11 @@ var createBlockReport = (kind, index) => ({
49
49
  removedStillUsing: [],
50
50
  removedEffects: []
51
51
  });
52
- var findOrCreateBlockReport = (blockReports, kind, index) => {
53
- var _a;
54
- if (((_a = blockReports.at(-1)) == null ? void 0 : _a.index) === index) {
55
- const res = blockReports.at(-1);
56
- if (res.kind !== kind) {
57
- res.kind = `${res.kind} | ${kind}`;
58
- }
59
- return res;
60
- }
61
- return createBlockReport(kind, index);
52
+ var findOrCreateBlockReport = (blockReports, kind, index, diff_txt) => {
53
+ const res = blockReports.find((item) => item.index === index) || createBlockReport(kind, index);
54
+ res.diff_txt = diff_txt;
55
+ res.kindList = res.kindList.includes(kind) ? res.kindList : [...res.kindList, kind];
56
+ return res;
62
57
  };
63
58
  function codeBlockDetect(arg) {
64
59
  const { gitDiffItem, absPathPrefix, blockReports, index } = arg;
@@ -74,14 +69,8 @@ function codeBlockDetect(arg) {
74
69
  const lineNumberEndOld = lineNumberStartOld + items.filter((item) => item.startsWith("-")).length - 1;
75
70
  const addNodes = import_AstUtil.default.getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet, lineNumberStartNew, lineNumberEndNew);
76
71
  const removeNodes = import_AstUtil.default.getTopScopeNodesByLineNumberRange(mapFileLineToNodeSetOld, lineNumberStartOld, lineNumberEndOld);
77
- iterateNodes(addNodes, "add", { blockReports, programNode }, index);
78
- iterateNodes(removeNodes, "remove", { blockReports, programNode }, index);
79
- const lastReport = blockReports.at(-1);
80
- if (lastReport) {
81
- lastReport.diff_txt = items;
82
- lastReport.topAdded = addNodes;
83
- lastReport.topRemoved = removeNodes;
84
- }
72
+ iterateNodes(addNodes, "add", { blockReports, programNode, diff_txt: items }, index);
73
+ iterateNodes(removeNodes, "remove", { blockReports, programNode, diff_txt: items }, index);
85
74
  }
86
75
  blockReports.push(createBlockReport("Never", NaN));
87
76
  }
@@ -107,7 +96,7 @@ function extractEffectItem(list) {
107
96
  return list.map((e) => ({ ...e, effects: e.effects.map((item) => item.ele) }));
108
97
  }
109
98
  function pushBlockReport(blockReports, blockReport, programNode, index) {
110
- if (blockReport.kind.startsWith("Other:")) {
99
+ if (blockReport.kindList.includes("Other")) {
111
100
  ["added", "removed"].forEach((key) => {
112
101
  const tailElements = blockReport[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
113
102
  blockReport[key] = [...new Set(tailElements)];
@@ -157,17 +146,16 @@ function iterateNodes(topScopeNodes, operation, extra, index) {
157
146
  }
158
147
  }
159
148
  function detectOther(arg, index) {
160
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
161
- const blockReport = findOrCreateBlockReport(blockReports, "Other", index);
149
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
150
+ const blockReport = findOrCreateBlockReport(blockReports, "Other", index, diff_txt);
162
151
  const { added, removed } = blockReport;
163
152
  const nodePaths = getPathsOfNode(topScopeNode._util.nodeCollection);
164
153
  (operation === "add" ? added : removed).push(...nodePaths);
165
- blockReport.kind = "Other:" + topScopeNode.type;
166
154
  pushBlockReport(blockReports, blockReport, programNode, index);
167
155
  }
168
156
  function detectImport(arg, index) {
169
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
170
- const blockReport = findOrCreateBlockReport(blockReports, "Import", index);
157
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
158
+ const blockReport = findOrCreateBlockReport(blockReports, "Import", index, diff_txt);
171
159
  const { added, removed, addedEffects, removedEffects } = blockReport;
172
160
  const specifiers = topScopeNode.type === "ImportDeclaration" ? topScopeNode.specifiers : [topScopeNode];
173
161
  if (Array.isArray(specifiers)) {
@@ -180,8 +168,8 @@ function detectImport(arg, index) {
180
168
  pushBlockReport(blockReports, blockReport, programNode, index);
181
169
  }
182
170
  function detectFnClsDeclaration(arg, index) {
183
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
184
- const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index);
171
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
172
+ const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index, diff_txt);
185
173
  const { added, removed, addedEffects, removedEffects } = blockReport;
186
174
  const { id } = topScopeNode;
187
175
  (operation === "add" ? added : removed).push(id.name);
@@ -192,8 +180,8 @@ function insertPrefix(n, prefix, sep = ":") {
192
180
  return [prefix, n].join(sep);
193
181
  }
194
182
  function detectVariableDeclaration(arg, index) {
195
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
196
- const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index);
183
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
184
+ const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index, diff_txt);
197
185
  const { added, removed, addedEffects, removedEffects } = blockReport;
198
186
  if (["VariableDeclaration", "VariableDeclarator"].includes(topScopeNode.type)) {
199
187
  let declarations = [];
@@ -233,8 +221,8 @@ function detectVariableDeclaration(arg, index) {
233
221
  pushBlockReport(blockReports, blockReport, programNode, index);
234
222
  }
235
223
  function detectUpdateEffectExp(arg, index) {
236
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
237
- const blockReport = findOrCreateBlockReport(blockReports, "SelfUpdate", index);
224
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
225
+ const blockReport = findOrCreateBlockReport(blockReports, "SelfUpdate", index, diff_txt);
238
226
  const { added, removed, addedEffects, removedEffects } = blockReport;
239
227
  const { argument: args } = topScopeNode;
240
228
  const createdExpIdSet = /* @__PURE__ */ new Set();
@@ -246,8 +234,8 @@ function detectUpdateEffectExp(arg, index) {
246
234
  pushBlockReport(blockReports, blockReport, programNode, index);
247
235
  }
248
236
  function detectFnCallExp(arg, index) {
249
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
250
- const blockReport = findOrCreateBlockReport(blockReports, "Invoke", index);
237
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
238
+ const blockReport = findOrCreateBlockReport(blockReports, "Invoke", index, diff_txt);
251
239
  const { added, removed, addedEffects, removedEffects } = blockReport;
252
240
  const { callee, arguments: args } = topScopeNode;
253
241
  const argsIds = args.map((arg2) => arg2._util.nodeCollection.filter((n) => n.type === "Identifier")).flat();
@@ -264,8 +252,8 @@ function detectFnCallExp(arg, index) {
264
252
  pushBlockReport(blockReports, blockReport, programNode, index);
265
253
  }
266
254
  function detectAssignmentEffectExp(arg, index) {
267
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
268
- const blockReport = findOrCreateBlockReport(blockReports, "Assignment", index);
255
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
256
+ const blockReport = findOrCreateBlockReport(blockReports, "Assignment", index, diff_txt);
269
257
  const { added, removed, addedEffects, removedEffects } = blockReport;
270
258
  const { left, right } = topScopeNode;
271
259
  const idSetLeft = /* @__PURE__ */ new Set();
@@ -49,7 +49,7 @@ ${filesDependsOnMe.map((files) => `- ${files.join("、")}`).join("\n")}` : "",
49
49
  }
50
50
  function blockReportToMd(block, index) {
51
51
  const {
52
- kind,
52
+ kindList,
53
53
  diff_txt,
54
54
  added,
55
55
  addedEffects,
@@ -57,12 +57,11 @@ function blockReportToMd(block, index) {
57
57
  removedEffects
58
58
  } = block;
59
59
  return [
60
- `### 对比${index + 1}分析`,
60
+ `### 对比${index + 1}分析, 节点种类${kindList.join()}`,
61
61
  `- 原始diff内容
62
62
  \`\`\`txt
63
63
  ${diff_txt.join("\n")}
64
64
  \`\`\``,
65
- `#### 修改分类: ${kind}`,
66
65
  added.length > 0 ? `- 新增标识符
67
66
  > ${added.join(", ")}` : "",
68
67
  addedEffects.length > 0 ? `- 新增标识符影响
@@ -29,10 +29,10 @@ export declare function createDetectReport(arg: Arg): {
29
29
  addedNotFound: string[];
30
30
  removed: string[];
31
31
  removedStillUsing: string[];
32
- kind: "Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other" | "Never";
32
+ kindList: ("Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other" | "Never")[];
33
33
  }[];
34
34
  filePath: string;
35
- type: "add" | "modify" | "delete";
35
+ type: "modify" | "add" | "delete";
36
36
  filesDependsOnMe: string[][];
37
37
  dangerIdentifiers: string[];
38
38
  }[];
@@ -65,8 +65,8 @@ function createDetectReport(arg) {
65
65
  const allNodes = new Map([...astKit.mapUuidToNode.values()].map((ele) => [import_AstUtil.default.getNodePath(ele), ele]));
66
66
  return {
67
67
  ...report,
68
- blockReports: report.blockReports.filter((e) => e.kind !== "Never").map((blockReport) => {
69
- const { kind, addedEffects, removedEffects, topAdded, topRemoved, ...rest } = blockReport;
68
+ blockReports: report.blockReports.filter((e) => !e.kindList.includes("Never")).map((blockReport) => {
69
+ const { kindList, addedEffects, removedEffects, topAdded, topRemoved, ...rest } = blockReport;
70
70
  const removedEffectsInfos = removedEffects.map((item) => {
71
71
  const tmpList = item.effects.map((ele) => {
72
72
  const nodePath = import_AstUtil.default.getNodePath(ele);
@@ -87,7 +87,7 @@ function createDetectReport(arg) {
87
87
  };
88
88
  }).filter((item) => item.effects.length > 0);
89
89
  return {
90
- kind,
90
+ kindList,
91
91
  ...rest,
92
92
  addedEffects: addedEffectsInfos,
93
93
  removedEffects: removedEffectsInfos
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-code-detector",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",