js-code-detector 0.0.13 → 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,9 +49,11 @@ var createBlockReport = (kind, index) => ({
49
49
  removedStillUsing: [],
50
50
  removedEffects: []
51
51
  });
52
- var findOrCreateBlockReport = (blockReports, kind, index) => {
53
- const res = blockReports.find((item) => item.index === index && item.kind === kind);
54
- return res || 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;
55
57
  };
56
58
  function codeBlockDetect(arg) {
57
59
  const { gitDiffItem, absPathPrefix, blockReports, index } = arg;
@@ -67,14 +69,8 @@ function codeBlockDetect(arg) {
67
69
  const lineNumberEndOld = lineNumberStartOld + items.filter((item) => item.startsWith("-")).length - 1;
68
70
  const addNodes = import_AstUtil.default.getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet, lineNumberStartNew, lineNumberEndNew);
69
71
  const removeNodes = import_AstUtil.default.getTopScopeNodesByLineNumberRange(mapFileLineToNodeSetOld, lineNumberStartOld, lineNumberEndOld);
70
- iterateNodes(addNodes, "add", { blockReports, programNode }, index);
71
- iterateNodes(removeNodes, "remove", { blockReports, programNode }, index);
72
- const lastReport = blockReports.at(-1);
73
- if (lastReport) {
74
- lastReport.diff_txt = items;
75
- lastReport.topAdded = addNodes;
76
- lastReport.topRemoved = removeNodes;
77
- }
72
+ iterateNodes(addNodes, "add", { blockReports, programNode, diff_txt: items }, index);
73
+ iterateNodes(removeNodes, "remove", { blockReports, programNode, diff_txt: items }, index);
78
74
  }
79
75
  blockReports.push(createBlockReport("Never", NaN));
80
76
  }
@@ -100,7 +96,7 @@ function extractEffectItem(list) {
100
96
  return list.map((e) => ({ ...e, effects: e.effects.map((item) => item.ele) }));
101
97
  }
102
98
  function pushBlockReport(blockReports, blockReport, programNode, index) {
103
- if (blockReport.kind.startsWith("Other:")) {
99
+ if (blockReport.kindList.includes("Other")) {
104
100
  ["added", "removed"].forEach((key) => {
105
101
  const tailElements = blockReport[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
106
102
  blockReport[key] = [...new Set(tailElements)];
@@ -150,17 +146,16 @@ function iterateNodes(topScopeNodes, operation, extra, index) {
150
146
  }
151
147
  }
152
148
  function detectOther(arg, index) {
153
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
154
- 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);
155
151
  const { added, removed } = blockReport;
156
152
  const nodePaths = getPathsOfNode(topScopeNode._util.nodeCollection);
157
153
  (operation === "add" ? added : removed).push(...nodePaths);
158
- blockReport.kind = "Other:" + topScopeNode.type;
159
154
  pushBlockReport(blockReports, blockReport, programNode, index);
160
155
  }
161
156
  function detectImport(arg, index) {
162
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
163
- 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);
164
159
  const { added, removed, addedEffects, removedEffects } = blockReport;
165
160
  const specifiers = topScopeNode.type === "ImportDeclaration" ? topScopeNode.specifiers : [topScopeNode];
166
161
  if (Array.isArray(specifiers)) {
@@ -173,8 +168,8 @@ function detectImport(arg, index) {
173
168
  pushBlockReport(blockReports, blockReport, programNode, index);
174
169
  }
175
170
  function detectFnClsDeclaration(arg, index) {
176
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
177
- 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);
178
173
  const { added, removed, addedEffects, removedEffects } = blockReport;
179
174
  const { id } = topScopeNode;
180
175
  (operation === "add" ? added : removed).push(id.name);
@@ -185,8 +180,8 @@ function insertPrefix(n, prefix, sep = ":") {
185
180
  return [prefix, n].join(sep);
186
181
  }
187
182
  function detectVariableDeclaration(arg, index) {
188
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
189
- 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);
190
185
  const { added, removed, addedEffects, removedEffects } = blockReport;
191
186
  if (["VariableDeclaration", "VariableDeclarator"].includes(topScopeNode.type)) {
192
187
  let declarations = [];
@@ -226,8 +221,8 @@ function detectVariableDeclaration(arg, index) {
226
221
  pushBlockReport(blockReports, blockReport, programNode, index);
227
222
  }
228
223
  function detectUpdateEffectExp(arg, index) {
229
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
230
- 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);
231
226
  const { added, removed, addedEffects, removedEffects } = blockReport;
232
227
  const { argument: args } = topScopeNode;
233
228
  const createdExpIdSet = /* @__PURE__ */ new Set();
@@ -239,8 +234,8 @@ function detectUpdateEffectExp(arg, index) {
239
234
  pushBlockReport(blockReports, blockReport, programNode, index);
240
235
  }
241
236
  function detectFnCallExp(arg, index) {
242
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
243
- 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);
244
239
  const { added, removed, addedEffects, removedEffects } = blockReport;
245
240
  const { callee, arguments: args } = topScopeNode;
246
241
  const argsIds = args.map((arg2) => arg2._util.nodeCollection.filter((n) => n.type === "Identifier")).flat();
@@ -257,8 +252,8 @@ function detectFnCallExp(arg, index) {
257
252
  pushBlockReport(blockReports, blockReport, programNode, index);
258
253
  }
259
254
  function detectAssignmentEffectExp(arg, index) {
260
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
261
- 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);
262
257
  const { added, removed, addedEffects, removedEffects } = blockReport;
263
258
  const { left, right } = topScopeNode;
264
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.13",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",