-
Notifications
You must be signed in to change notification settings - Fork 31
/
check_list_case.ts
114 lines (92 loc) · 3.28 KB
/
check_list_case.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// const fs = require("fs");
// const path = require("path");
// // 指定要遍历的目录路径
// const directoryPath = "./src";
// // 递归遍历目录并获取所有.ts文件
// function getTsFiles(dir) {
// const files = fs.readdirSync(dir);
// const tsFiles: any = [];
// files.forEach((file) => {
// const filePath = path.join(dir, file);
// const stat = fs.statSync(filePath);
// if (stat.isDirectory()) {
// tsFiles.push(...getTsFiles(filePath)); // 递归处理子目录
// } else if (file.endsWith(".ts")) {
// tsFiles.push(filePath);
// }
// });
// return tsFiles;
// }
// const tsFiles = getTsFiles(directoryPath);
// console.log("所有.ts文件:", tsFiles);
const fs = require("fs");
const path = require("path");
// 指定要遍历的目录路径
const directoryPath = "./src";
// 递归遍历目录并获取所有.ts文件
function getTsFiles(dir) {
const files = fs.readdirSync(dir);
const tsFiles: any = [];
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
tsFiles.push(...getTsFiles(filePath)); // 递归处理子目录
} else if (file.endsWith(".ts")) {
tsFiles.push(filePath);
}
});
return tsFiles;
}
// 读取文件内容并查找listNotificationInterests函数
function findListNotificationInterestsContent(filePath) {
const fileContent = fs.readFileSync(filePath, "utf8");
const regex = /listNotificationInterests\(\): string\[\] {\s*return\s*\[([\s\S]*?)\s*\];/m;
const matches = fileContent.match(regex);
if (matches && matches[1]) {
// 获取返回内容
const content = matches[1]
.replace(/['"]+/g, "") // 去除引号
.split(",") // 分割成数组
.map((item) => item.trim()); // 移除空白字符
return content;
}
return null;
}
const tsFiles = getTsFiles(directoryPath);
// 读取文件内容并查找listNotificationInterests函数
function findListContent(filePath) {
const fileContent = fs.readFileSync(filePath, "utf8");
// 匹配 handleNotification 函数
const handleNotificationRegex = /async\s+handleNotification\s*\(.*\)\s*{([\s\S]*?)}/;
const handleNotificationMatches = handleNotificationRegex.exec(fileContent);
if (handleNotificationMatches && handleNotificationMatches[1]) {
const handleNotificationContent = handleNotificationMatches[1];
let caseRegex = /case\s+.*?:([\s\S]*?)(?=case\s+|$)/g;
let caseMatches = handleNotificationContent.match(caseRegex);
let new_result: any = [];
if (caseMatches) {
caseMatches.forEach(function (caseMatch) {
let trimmedCaseMatch = caseMatch.match(/case\s+(.*?):([\s\S]*)/);
if (trimmedCaseMatch) {
new_result.push(trimmedCaseMatch[1]);
}
// if (trimmedCaseMatch && !content.includes(trimmedCaseMatch[1])) {
// console.log(filePath, "没有:", trimmedCaseMatch[1], "\n");
// }
});
}
return new_result;
}
}
tsFiles.forEach((filePath) => {
const all_listen = findListNotificationInterestsContent(filePath);
let all_case = findListContent(filePath);
if (all_case) {
all_case.forEach((element) => {
if (!all_listen.includes(element)) {
console.log(filePath, "没有:", element, "\n");
}
});
}
});