js-code-detector 0.0.14 → 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;
10
- kindList: BlockReportKind[];
11
- diff_txt: string[];
8
+ type BlockReportInfoItem = {
9
+ kind: BlockReportKind;
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
- kindList: [kind],
41
40
  diff_txt: [],
41
+ infos: []
42
+ });
43
+ var createBlockReportInfoItem = (kind) => ({
44
+ kind,
42
45
  topAdded: [],
43
46
  topRemoved: [],
44
47
  added: [],
@@ -52,8 +55,14 @@ var createBlockReport = (kind, index) => ({
52
55
  var findOrCreateBlockReport = (blockReports, kind, index, diff_txt) => {
53
56
  const res = blockReports.find((item) => item.index === index) || createBlockReport(kind, index);
54
57
  res.diff_txt = diff_txt;
55
- res.kindList = res.kindList.includes(kind) ? res.kindList : [...res.kindList, kind];
56
- return res;
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;
57
66
  };
58
67
  function codeBlockDetect(arg) {
59
68
  const { gitDiffItem, absPathPrefix, blockReports, index } = arg;
@@ -72,7 +81,6 @@ function codeBlockDetect(arg) {
72
81
  iterateNodes(addNodes, "add", { blockReports, programNode, diff_txt: items }, index);
73
82
  iterateNodes(removeNodes, "remove", { blockReports, programNode, diff_txt: items }, index);
74
83
  }
75
- blockReports.push(createBlockReport("Never", NaN));
76
84
  }
77
85
  function getPathsOfNode(topScopeNodes) {
78
86
  const paths = [];
@@ -95,32 +103,29 @@ function filterBySamePathAndLen(list, sameEffectsPaths) {
95
103
  function extractEffectItem(list) {
96
104
  return list.map((e) => ({ ...e, effects: e.effects.map((item) => item.ele) }));
97
105
  }
98
- function pushBlockReport(blockReports, blockReport, programNode, index) {
99
- if (blockReport.kindList.includes("Other")) {
106
+ function pushBlockReport(blockReportInfoItem, programNode, index) {
107
+ if (blockReportInfoItem.kind === "Other") {
100
108
  ["added", "removed"].forEach((key) => {
101
- const tailElements = blockReport[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
102
- blockReport[key] = [...new Set(tailElements)];
109
+ const tailElements = blockReportInfoItem[key].map((ele) => ele.split(":").at(-1)).filter(Boolean);
110
+ blockReportInfoItem[key] = [...new Set(tailElements)];
103
111
  });
104
112
  }
105
- const sameNames = blockReport.added.filter((path) => blockReport.removed.includes(path));
113
+ const sameNames = blockReportInfoItem.added.filter((path) => blockReportInfoItem.removed.includes(path));
106
114
  if (sameNames.length) {
107
115
  ["added", "removed"].forEach((key) => {
108
- blockReport[key] = blockReport[key].filter((path) => !sameNames.includes(path));
116
+ blockReportInfoItem[key] = blockReportInfoItem[key].filter((path) => !sameNames.includes(path));
109
117
  });
110
118
  }
111
- const addedEffectsList = mapNodePath(blockReport.addedEffects);
112
- const removedEffectsList = mapNodePath(blockReport.removedEffects);
119
+ const addedEffectsList = mapNodePath(blockReportInfoItem.addedEffects);
120
+ const removedEffectsList = mapNodePath(blockReportInfoItem.removedEffects);
113
121
  const addedEffectsPaths = addedEffectsList.map((item) => item.effects.map(({ path }) => path)).flat();
114
122
  const removedEffectsPaths = removedEffectsList.map((item) => item.effects.map(({ path }) => path)).flat();
115
123
  const sameEffectsPaths = addedEffectsPaths.filter((path) => removedEffectsPaths.includes(path));
116
124
  if (sameEffectsPaths.length) {
117
125
  const addedEffects = filterBySamePathAndLen(addedEffectsList, sameEffectsPaths);
118
126
  const removedEffects = filterBySamePathAndLen(removedEffectsList, sameEffectsPaths);
119
- blockReport.addedEffects = extractEffectItem(addedEffects);
120
- blockReport.removedEffects = extractEffectItem(removedEffects);
121
- }
122
- if (!blockReports.includes(blockReport)) {
123
- blockReports.push(blockReport);
127
+ blockReportInfoItem.addedEffects = extractEffectItem(addedEffects);
128
+ blockReportInfoItem.removedEffects = extractEffectItem(removedEffects);
124
129
  }
125
130
  }
126
131
  function iterateNodes(topScopeNodes, operation, extra, index) {
@@ -147,11 +152,11 @@ function iterateNodes(topScopeNodes, operation, extra, index) {
147
152
  }
148
153
  function detectOther(arg, index) {
149
154
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
150
- const blockReport = findOrCreateBlockReport(blockReports, "Other", index, diff_txt);
151
- const { added, removed } = blockReport;
155
+ const blockReportInfoItem = findOrCreateBlockReport(blockReports, "Other", index, diff_txt);
156
+ const { added, removed } = blockReportInfoItem;
152
157
  const nodePaths = getPathsOfNode(topScopeNode._util.nodeCollection);
153
158
  (operation === "add" ? added : removed).push(...nodePaths);
154
- pushBlockReport(blockReports, blockReport, programNode, index);
159
+ pushBlockReport(blockReportInfoItem, programNode, index);
155
160
  }
156
161
  function detectImport(arg, index) {
157
162
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
@@ -165,7 +170,7 @@ function detectImport(arg, index) {
165
170
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: local, effects: [...local._util.effectIds] });
166
171
  });
167
172
  }
168
- pushBlockReport(blockReports, blockReport, programNode, index);
173
+ pushBlockReport(blockReport, programNode, index);
169
174
  }
170
175
  function detectFnClsDeclaration(arg, index) {
171
176
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
@@ -174,7 +179,7 @@ function detectFnClsDeclaration(arg, index) {
174
179
  const { id } = topScopeNode;
175
180
  (operation === "add" ? added : removed).push(id.name);
176
181
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: id, effects: [...id._util.effectIds] });
177
- pushBlockReport(blockReports, blockReport, programNode, index);
182
+ pushBlockReport(blockReport, programNode, index);
178
183
  }
179
184
  function insertPrefix(n, prefix, sep = ":") {
180
185
  return [prefix, n].join(sep);
@@ -218,7 +223,7 @@ function detectVariableDeclaration(arg, index) {
218
223
  }
219
224
  });
220
225
  }
221
- pushBlockReport(blockReports, blockReport, programNode, index);
226
+ pushBlockReport(blockReport, programNode, index);
222
227
  }
223
228
  function detectUpdateEffectExp(arg, index) {
224
229
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
@@ -231,7 +236,7 @@ function detectUpdateEffectExp(arg, index) {
231
236
  (operation === "add" ? added : removed).push(createdExpId.name);
232
237
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: createdExpId, effects: [...createdExpId._util.effectIds] });
233
238
  }
234
- pushBlockReport(blockReports, blockReport, programNode, index);
239
+ pushBlockReport(blockReport, programNode, index);
235
240
  }
236
241
  function detectFnCallExp(arg, index) {
237
242
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
@@ -249,7 +254,7 @@ function detectFnCallExp(arg, index) {
249
254
  (operation === "add" ? added : removed).push(createdExpId.name);
250
255
  (operation === "add" ? addedEffects : removedEffects).push({ causeBy: createdExpId, effects: [...createdExpId._util.effectIds] });
251
256
  }
252
- pushBlockReport(blockReports, blockReport, programNode, index);
257
+ pushBlockReport(blockReport, programNode, index);
253
258
  }
254
259
  function detectAssignmentEffectExp(arg, index) {
255
260
  const { topScopeNode, operation, extra: { blockReports, programNode, diff_txt } } = arg;
@@ -267,5 +272,5 @@ function detectAssignmentEffectExp(arg, index) {
267
272
  for (const createdExp of idSetRight) {
268
273
  (operation === "add" ? added : removed).push(insertPrefix(createdExp.name, "right"));
269
274
  }
270
- pushBlockReport(blockReports, blockReport, programNode, index);
275
+ pushBlockReport(blockReport, programNode, index);
271
276
  }
@@ -49,19 +49,28 @@ ${filesDependsOnMe.map((files) => `- ${files.join("、")}`).join("\n")}` : "",
49
49
  }
50
50
  function blockReportToMd(block, index) {
51
51
  const {
52
- kindList,
53
52
  diff_txt,
54
- added,
55
- addedEffects,
56
- removed,
57
- removedEffects
53
+ infos
58
54
  } = block;
59
55
  return [
60
- `### 对比${index + 1}分析, 节点种类${kindList.join()}`,
56
+ `### 对比${index + 1}分析`,
61
57
  `- 原始diff内容
62
58
  \`\`\`txt
63
59
  ${diff_txt.join("\n")}
64
60
  \`\`\``,
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}`,
65
74
  added.length > 0 ? `- 新增标识符
66
75
  > ${added.join(", ")}` : "",
67
76
  addedEffects.length > 0 ? `- 新增标识符影响
@@ -14,22 +14,23 @@ 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
- kindList: ("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
36
  type: "modify" | "add" | "delete";
@@ -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.kindList.includes("Never")).map((blockReport) => {
69
- const { kindList, 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
- kindList,
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.14",
3
+ "version": "0.0.15",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",