js-code-detector 0.0.13 → 0.0.15

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.
@@ -1,14 +1,12 @@
1
1
  import { AstNode } from "../ast_util/AstUtil";
2
2
  import { GitDiffDetail } from "../format_git_diff_content";
3
- type BlockReportKind = "Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other" | "Never";
3
+ type BlockReportKind = "Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other";
4
4
  type EffectItem = {
5
5
  causeBy: AstNode;
6
6
  effects: AstNode[];
7
7
  };
8
- export type BlockReport = {
9
- index: number;
8
+ type BlockReportInfoItem = {
10
9
  kind: BlockReportKind;
11
- diff_txt: string[];
12
10
  topAdded: AstNode[];
13
11
  topRemoved: AstNode[];
14
12
  added: string[];
@@ -19,6 +17,11 @@ export type BlockReport = {
19
17
  removedStillUsing: string[];
20
18
  removedEffects: EffectItem[];
21
19
  };
20
+ export type BlockReport = {
21
+ index: number;
22
+ diff_txt: string[];
23
+ infos: BlockReportInfoItem[];
24
+ };
22
25
  type Arg = {
23
26
  gitDiffItem: GitDiffDetail;
24
27
  absPathPrefix: string;
@@ -37,8 +37,11 @@ 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,
41
40
  diff_txt: [],
41
+ infos: []
42
+ });
43
+ var createBlockReportInfoItem = (kind) => ({
44
+ kind,
42
45
  topAdded: [],
43
46
  topRemoved: [],
44
47
  added: [],
@@ -49,9 +52,17 @@ var createBlockReport = (kind, index) => ({
49
52
  removedStillUsing: [],
50
53
  removedEffects: []
51
54
  });
52
- var findOrCreateBlockReport = (blockReports, kind, index) => {
53
- const res = blockReports.find((item) => item.index === index && item.kind === kind);
54
- return res || createBlockReport(kind, index);
55
+ var findOrCreateBlockReport = (blockReports, kind, index, diff_txt) => {
56
+ const res = blockReports.find((item) => item.index === index) || createBlockReport(kind, index);
57
+ res.diff_txt = diff_txt;
58
+ if (!blockReports.includes(res)) {
59
+ blockReports.push(res);
60
+ }
61
+ const tmpInfo = res.infos.find((info) => info.kind === kind) || createBlockReportInfoItem(kind);
62
+ if (!res.infos.includes(tmpInfo)) {
63
+ res.infos.push(tmpInfo);
64
+ }
65
+ return tmpInfo;
55
66
  };
56
67
  function codeBlockDetect(arg) {
57
68
  const { gitDiffItem, absPathPrefix, blockReports, index } = arg;
@@ -67,16 +78,9 @@ function codeBlockDetect(arg) {
67
78
  const lineNumberEndOld = lineNumberStartOld + items.filter((item) => item.startsWith("-")).length - 1;
68
79
  const addNodes = import_AstUtil.default.getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet, lineNumberStartNew, lineNumberEndNew);
69
80
  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
- }
81
+ iterateNodes(addNodes, "add", { blockReports, programNode, diff_txt: items }, index);
82
+ iterateNodes(removeNodes, "remove", { blockReports, programNode, diff_txt: items }, index);
78
83
  }
79
- blockReports.push(createBlockReport("Never", NaN));
80
84
  }
81
85
  function getPathsOfNode(topScopeNodes) {
82
86
  const paths = [];
@@ -99,32 +103,29 @@ function filterBySamePathAndLen(list, sameEffectsPaths) {
99
103
  function extractEffectItem(list) {
100
104
  return list.map((e) => ({ ...e, effects: e.effects.map((item) => item.ele) }));
101
105
  }
102
- function pushBlockReport(blockReports, blockReport, programNode, index) {
103
- if (blockReport.kind.startsWith("Other:")) {
106
+ function pushBlockReport(blockReportInfoItem, programNode, index) {
107
+ if (blockReportInfoItem.kind === "Other") {
104
108
  ["added", "removed"].forEach((key) => {
105
- const tailElements = blockReport[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
106
- blockReport[key] = [...new Set(tailElements)];
109
+ const tailElements = blockReportInfoItem[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
110
+ blockReportInfoItem[key] = [...new Set(tailElements)];
107
111
  });
108
112
  }
109
- const sameNames = blockReport.added.filter((path) => blockReport.removed.includes(path));
113
+ const sameNames = blockReportInfoItem.added.filter((path) => blockReportInfoItem.removed.includes(path));
110
114
  if (sameNames.length) {
111
115
  ["added", "removed"].forEach((key) => {
112
- blockReport[key] = blockReport[key].filter((path) => !sameNames.includes(path));
116
+ blockReportInfoItem[key] = blockReportInfoItem[key].filter((path) => !sameNames.includes(path));
113
117
  });
114
118
  }
115
- const addedEffectsList = mapNodePath(blockReport.addedEffects);
116
- const removedEffectsList = mapNodePath(blockReport.removedEffects);
119
+ const addedEffectsList = mapNodePath(blockReportInfoItem.addedEffects);
120
+ const removedEffectsList = mapNodePath(blockReportInfoItem.removedEffects);
117
121
  const addedEffectsPaths = addedEffectsList.map((item) => item.effects.map(({ path }) => path)).flat();
118
122
  const removedEffectsPaths = removedEffectsList.map((item) => item.effects.map(({ path }) => path)).flat();
119
123
  const sameEffectsPaths = addedEffectsPaths.filter((path) => removedEffectsPaths.includes(path));
120
124
  if (sameEffectsPaths.length) {
121
125
  const addedEffects = filterBySamePathAndLen(addedEffectsList, sameEffectsPaths);
122
126
  const removedEffects = filterBySamePathAndLen(removedEffectsList, sameEffectsPaths);
123
- blockReport.addedEffects = extractEffectItem(addedEffects);
124
- blockReport.removedEffects = extractEffectItem(removedEffects);
125
- }
126
- if (!blockReports.includes(blockReport)) {
127
- blockReports.push(blockReport);
127
+ blockReportInfoItem.addedEffects = extractEffectItem(addedEffects);
128
+ blockReportInfoItem.removedEffects = extractEffectItem(removedEffects);
128
129
  }
129
130
  }
130
131
  function iterateNodes(topScopeNodes, operation, extra, index) {
@@ -150,17 +151,16 @@ function iterateNodes(topScopeNodes, operation, extra, index) {
150
151
  }
151
152
  }
152
153
  function detectOther(arg, index) {
153
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
154
- const blockReport = findOrCreateBlockReport(blockReports, "Other", index);
155
- const { added, removed } = blockReport;
154
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
155
+ const blockReportInfoItem = findOrCreateBlockReport(blockReports, "Other", index, diff_txt);
156
+ const { added, removed } = blockReportInfoItem;
156
157
  const nodePaths = getPathsOfNode(topScopeNode._util.nodeCollection);
157
158
  (operation === "add" ? added : removed).push(...nodePaths);
158
- blockReport.kind = "Other:" + topScopeNode.type;
159
- pushBlockReport(blockReports, blockReport, programNode, index);
159
+ pushBlockReport(blockReportInfoItem, programNode, index);
160
160
  }
161
161
  function detectImport(arg, index) {
162
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
163
- const blockReport = findOrCreateBlockReport(blockReports, "Import", index);
162
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
163
+ const blockReport = findOrCreateBlockReport(blockReports, "Import", index, diff_txt);
164
164
  const { added, removed, addedEffects, removedEffects } = blockReport;
165
165
  const specifiers = topScopeNode.type === "ImportDeclaration" ? topScopeNode.specifiers : [topScopeNode];
166
166
  if (Array.isArray(specifiers)) {
@@ -170,23 +170,23 @@ function detectImport(arg, index) {
170
170
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: local, effects: [...local._util.effectIds] });
171
171
  });
172
172
  }
173
- pushBlockReport(blockReports, blockReport, programNode, index);
173
+ pushBlockReport(blockReport, programNode, index);
174
174
  }
175
175
  function detectFnClsDeclaration(arg, index) {
176
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
177
- const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index);
176
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
177
+ const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index, diff_txt);
178
178
  const { added, removed, addedEffects, removedEffects } = blockReport;
179
179
  const { id } = topScopeNode;
180
180
  (operation === "add" ? added : removed).push(id.name);
181
181
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: id, effects: [...id._util.effectIds] });
182
- pushBlockReport(blockReports, blockReport, programNode, index);
182
+ pushBlockReport(blockReport, programNode, index);
183
183
  }
184
184
  function insertPrefix(n, prefix, sep = ":") {
185
185
  return [prefix, n].join(sep);
186
186
  }
187
187
  function detectVariableDeclaration(arg, index) {
188
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
189
- const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index);
188
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
189
+ const blockReport = findOrCreateBlockReport(blockReports, "Declaration", index, diff_txt);
190
190
  const { added, removed, addedEffects, removedEffects } = blockReport;
191
191
  if (["VariableDeclaration", "VariableDeclarator"].includes(topScopeNode.type)) {
192
192
  let declarations = [];
@@ -223,11 +223,11 @@ function detectVariableDeclaration(arg, index) {
223
223
  }
224
224
  });
225
225
  }
226
- pushBlockReport(blockReports, blockReport, programNode, index);
226
+ pushBlockReport(blockReport, programNode, index);
227
227
  }
228
228
  function detectUpdateEffectExp(arg, index) {
229
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
230
- const blockReport = findOrCreateBlockReport(blockReports, "SelfUpdate", index);
229
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
230
+ const blockReport = findOrCreateBlockReport(blockReports, "SelfUpdate", index, diff_txt);
231
231
  const { added, removed, addedEffects, removedEffects } = blockReport;
232
232
  const { argument: args } = topScopeNode;
233
233
  const createdExpIdSet = /* @__PURE__ */ new Set();
@@ -236,11 +236,11 @@ function detectUpdateEffectExp(arg, index) {
236
236
  (operation === "add" ? added : removed).push(createdExpId.name);
237
237
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: createdExpId, effects: [...createdExpId._util.effectIds] });
238
238
  }
239
- pushBlockReport(blockReports, blockReport, programNode, index);
239
+ pushBlockReport(blockReport, programNode, index);
240
240
  }
241
241
  function detectFnCallExp(arg, index) {
242
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
243
- const blockReport = findOrCreateBlockReport(blockReports, "Invoke", index);
242
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
243
+ const blockReport = findOrCreateBlockReport(blockReports, "Invoke", index, diff_txt);
244
244
  const { added, removed, addedEffects, removedEffects } = blockReport;
245
245
  const { callee, arguments: args } = topScopeNode;
246
246
  const argsIds = args.map((arg2) => arg2._util.nodeCollection.filter((n) => n.type === "Identifier")).flat();
@@ -254,11 +254,11 @@ function detectFnCallExp(arg, index) {
254
254
  (operation === "add" ? added : removed).push(createdExpId.name);
255
255
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: createdExpId, effects: [...createdExpId._util.effectIds] });
256
256
  }
257
- pushBlockReport(blockReports, blockReport, programNode, index);
257
+ pushBlockReport(blockReport, programNode, index);
258
258
  }
259
259
  function detectAssignmentEffectExp(arg, index) {
260
- const { topScopeNode, operation, extra: { blockReports, programNode } } = arg;
261
- const blockReport = findOrCreateBlockReport(blockReports, "Assignment", index);
260
+ const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
261
+ const blockReport = findOrCreateBlockReport(blockReports, "Assignment", index, diff_txt);
262
262
  const { added, removed, addedEffects, removedEffects } = blockReport;
263
263
  const { left, right } = topScopeNode;
264
264
  const idSetLeft = /* @__PURE__ */ new Set();
@@ -272,5 +272,5 @@ function detectAssignmentEffectExp(arg, index) {
272
272
  for (const createdExp of idSetRight) {
273
273
  (operation === "add" ? added : removed).push(insertPrefix(createdExp.name, "right"));
274
274
  }
275
- pushBlockReport(blockReports, blockReport, programNode, index);
275
+ pushBlockReport(blockReport, programNode, index);
276
276
  }
@@ -49,12 +49,8 @@ ${filesDependsOnMe.map((files) => `- ${files.join("、")}`).join("\n")}` : "",
49
49
  }
50
50
  function blockReportToMd(block, index) {
51
51
  const {
52
- kind,
53
52
  diff_txt,
54
- added,
55
- addedEffects,
56
- removed,
57
- removedEffects
53
+ infos
58
54
  } = block;
59
55
  return [
60
56
  `### 对比${index + 1}分析`,
@@ -62,7 +58,19 @@ function blockReportToMd(block, index) {
62
58
  \`\`\`txt
63
59
  ${diff_txt.join("\n")}
64
60
  \`\`\``,
65
- `#### 修改分类: ${kind}`,
61
+ ...infos.map(blockReportInfoItemToMd)
62
+ ].filter(Boolean).join("\n\n");
63
+ }
64
+ function blockReportInfoItemToMd(info, index) {
65
+ const {
66
+ kind,
67
+ added,
68
+ addedEffects,
69
+ removed,
70
+ removedEffects
71
+ } = info;
72
+ return [
73
+ `#### 分类${index + 1}: ${kind}`,
66
74
  added.length > 0 ? `- 新增标识符
67
75
  > ${added.join(", ")}` : "",
68
76
  addedEffects.length > 0 ? `- 新增标识符影响
@@ -14,25 +14,26 @@ type Arg = {
14
14
  };
15
15
  export declare function createDetectReport(arg: Arg): {
16
16
  blockReports: {
17
- addedEffects: {
18
- causeBy: string;
19
- effects: string[];
20
- }[];
21
- removedEffects: {
22
- causeBy: string;
23
- effects: (string | undefined)[];
24
- }[];
25
- index: number;
26
17
  diff_txt: string[];
27
- added: string[];
28
- addedNotUsed: string[];
29
- addedNotFound: string[];
30
- removed: string[];
31
- removedStillUsing: string[];
32
- kind: "Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other" | "Never";
18
+ infos: {
19
+ addedEffects: {
20
+ causeBy: string;
21
+ effects: string[];
22
+ }[];
23
+ removedEffects: {
24
+ causeBy: string;
25
+ effects: (string | undefined)[];
26
+ }[];
27
+ kind: "Import" | "Declaration" | "Assignment" | "SelfUpdate" | "Invoke" | "Other";
28
+ added: string[];
29
+ addedNotUsed: string[];
30
+ addedNotFound: string[];
31
+ removed: string[];
32
+ removedStillUsing: string[];
33
+ }[];
33
34
  }[];
34
35
  filePath: string;
35
- type: "add" | "modify" | "delete";
36
+ type: "modify" | "add" | "delete";
36
37
  filesDependsOnMe: string[][];
37
38
  dangerIdentifiers: string[];
38
39
  }[];
@@ -65,32 +65,38 @@ 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;
70
- const removedEffectsInfos = removedEffects.map((item) => {
71
- const tmpList = item.effects.map((ele) => {
72
- const nodePath = import_AstUtil.default.getNodePath(ele);
73
- const item2 = allNodes.get(nodePath);
74
- return item2 && import_AstUtil.default.createScopeContent(item2);
75
- }).filter(Boolean);
76
- const effects = [...new Set(tmpList)];
68
+ blockReports: report.blockReports.map((blockReport) => {
69
+ const { infos, diff_txt } = blockReport;
70
+ const infosList = infos.map((info) => {
71
+ const { addedEffects, removedEffects, topAdded, topRemoved, ...rest } = info;
72
+ const removedEffectsInfos = removedEffects.map((item) => {
73
+ const tmpList = item.effects.map((ele) => {
74
+ const nodePath = import_AstUtil.default.getNodePath(ele);
75
+ const item2 = allNodes.get(nodePath);
76
+ return item2 && import_AstUtil.default.createScopeContent(item2);
77
+ }).filter(Boolean);
78
+ const effects = [...new Set(tmpList)];
79
+ return {
80
+ causeBy: item.causeBy.name || item.causeBy.type,
81
+ effects
82
+ };
83
+ }).filter((item) => item.effects.length > 0);
84
+ const addedEffectsInfos = addedEffects.map((item) => {
85
+ const effects = [...new Set(item.effects.map(import_AstUtil.default.createScopeContent))];
86
+ return {
87
+ causeBy: item.causeBy.name || item.causeBy.type,
88
+ effects
89
+ };
90
+ }).filter((item) => item.effects.length > 0);
77
91
  return {
78
- causeBy: item.causeBy.name || item.causeBy.type,
79
- effects
92
+ ...rest,
93
+ addedEffects: addedEffectsInfos,
94
+ removedEffects: removedEffectsInfos
80
95
  };
81
- }).filter((item) => item.effects.length > 0);
82
- const addedEffectsInfos = addedEffects.map((item) => {
83
- const effects = [...new Set(item.effects.map(import_AstUtil.default.createScopeContent))];
84
- return {
85
- causeBy: item.causeBy.name || item.causeBy.type,
86
- effects
87
- };
88
- }).filter((item) => item.effects.length > 0);
96
+ });
89
97
  return {
90
- kind,
91
- ...rest,
92
- addedEffects: addedEffectsInfos,
93
- removedEffects: removedEffectsInfos
98
+ diff_txt,
99
+ infos: infosList
94
100
  };
95
101
  })
96
102
  };
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.15",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",