Repository navigation
Expand file tree
/
Copy pathcheck-underscore-property-keys.mjs
More file actions
368 lines (322 loc) · 9.77 KB
/
Copy pathcheck-underscore-property-keys.mjs
File metadata and controls
368 lines (322 loc) · 9.77 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";
const projectRoot = process.cwd();
const sourceRoots = [path.join(projectRoot, "src")];
const SKIP_DIRS = new Set([
"node_modules",
"dist",
"bin",
"obj",
"build",
".build",
"_framework",
"vendor",
".cache",
"coverage",
".turbo",
"out",
"target",
]);
const TS_SCRIPT_EXTS = new Map([
[".ts", ts.ScriptKind.TS],
[".tsx", ts.ScriptKind.TSX],
[".js", ts.ScriptKind.JS],
[".jsx", ts.ScriptKind.JSX],
[".mjs", ts.ScriptKind.JS],
[".cjs", ts.ScriptKind.JS],
[".mts", ts.ScriptKind.TS],
[".cts", ts.ScriptKind.TS],
]);
const REGEX_CHECK_EXTS = new Set([
".java",
".kt",
".kts",
".dart",
".gleam",
".scala",
".rs",
".go",
".cpp",
".c",
".h",
".hpp",
".cc",
".cxx",
".zig",
".swift",
]);
function listSourceFiles(rootDir) {
const files = [];
function walk(currentPath) {
const entries = fs.readdirSync(currentPath, { withFileTypes: true });
for (const entry of entries) {
const absolutePath = path.join(currentPath, entry.name);
if (
entry.isDirectory() &&
(SKIP_DIRS.has(entry.name) || entry.name.startsWith("."))
) {
continue;
}
if (entry.isDirectory()) {
walk(absolutePath);
continue;
}
const extension = path.extname(entry.name).toLowerCase();
if (extension === ".d.ts") continue;
if (
entry.name.endsWith(".spec.ts") ||
entry.name.endsWith(".spec.js") ||
entry.name.endsWith(".test.ts") ||
entry.name.endsWith(".test.js")
) {
continue;
}
if (
!TS_SCRIPT_EXTS.has(extension) &&
!REGEX_CHECK_EXTS.has(extension)
)
continue;
files.push(absolutePath);
}
}
walk(rootDir);
return files;
}
function extractLiteralValue(node) {
if (ts.isStringLiteral(node)) return node.text;
if (ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
return null;
}
function isUnderscoreKey(value) {
return typeof value === "string" && /^_/.test(value);
}
function hasNodeWithTemplateSubstitution(node) {
return ts.isTemplateLiteral(node) && node.templateSpans.length > 0;
}
function getStringLiteralFromCallArg(node) {
if (!node) return null;
if (hasNodeWithTemplateSubstitution(node)) return null;
return extractLiteralValue(node);
}
function isReflectCallExpression(node) {
if (!ts.isCallExpression(node)) return false;
const callee = node.expression;
if (ts.isPropertyAccessExpression(callee)) {
const object = callee.expression;
const method = callee.name.text;
if (object && ts.isIdentifier(object) && object.text === "Reflect") {
return method === "get" || method === "set" || method === "has";
}
// Object.prototype.hasOwnProperty.call(...)
if (
method === "call" &&
ts.isPropertyAccessExpression(object) &&
ts.isPropertyAccessExpression(object.expression) &&
ts.isIdentifier(object.expression.expression) &&
object.expression.expression.text === "Object" &&
object.expression.name.text === "prototype" &&
object.name.text === "hasOwnProperty"
) {
return true;
}
}
return false;
}
function isHasOwnLikeCallExpression(node) {
if (!ts.isCallExpression(node)) return false;
const callee = node.expression;
if (!ts.isIdentifier(callee)) return false;
const name = callee.text;
return name === "hasOwn";
}
function isObjectHasOwnCallExpression(node) {
if (!ts.isCallExpression(node)) return false;
const callee = node.expression;
return (
ts.isPropertyAccessExpression(callee) &&
ts.isIdentifier(callee.expression) &&
callee.expression.text === "Object" &&
callee.name.text === "hasOwn"
);
}
function positionFromOffset(sourceText, offset) {
const chunk = sourceText.slice(0, offset);
const line = chunk.split("\n").length;
const lastLineBreak = chunk.lastIndexOf("\n");
const column = lastLineBreak === -1 ? chunk.length + 1 : offset - lastLineBreak;
return {
line,
column,
};
}
function recordRegexMatch(
findings,
fileName,
sourceText,
matchIndex,
matchLength,
key,
) {
if (!isUnderscoreKey(key)) return;
const { line, column } = positionFromOffset(sourceText, matchIndex);
const preview = sourceText.slice(matchIndex, matchIndex + matchLength);
if (!findings.has(key)) findings.set(key, []);
findings.get(key).push({
file: path.relative(projectRoot, fileName),
line,
column,
preview,
});
}
function collectKeysFromTextFileUsingRegex(
fileName,
sourceText,
findings,
) {
const bracketAccess = /\b(?:\w|\)|\]|\}|\$)\s*\[\s*(['"])(_[^'"]+)\1\s*\]/g;
const inOperator = /\b(['"])(_[^'"]+)\1\s+in\s+[^;\n)]+/g;
const reflectCalls = /\bReflect\.(?:get|set|has)\(\s*[^,\s)]+,\s*(['"])(_[^'"]+)\1/g;
const objectHasOwn = /\bObject\.hasOwn\(\s*[^,\s)]+,\s*(['"])(_[^'"]+)\1/g;
const objectHasOwnPropertyCall =
/\bObject\.prototype\.hasOwnProperty\.call\(\s*[^,\s)]+,\s*(['"])(_[^'"]+)\1/g;
const hasOwnCalls = /\bhasOwn\(\s*[^,\s)]+,\s*(['"])(_[^'"]+)\1/g;
const dartInteropProperties = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?(?:getProperty|setProperty|deleteProperty|getPropertyAs|setPropertyAs|deletePropertyAs)\(\s*r?(["'])(_[^"']+)\1(?:\.toJS)?/g;
const dartInteropCallMethodFirstArg = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?callMethod(?:VarArgs)?\(\s*r?(["'])(_[^"']+)\1(?:\.toJS)?/g;
const dartInteropCallMethodSecondArg = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?callMethod(?:VarArgs)?\(\s*[^,\n]+\s*,\s*r?(["'])(_[^"']+)\1(?:\.toJS)?/g;
const dartContainsKey = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?containsKey\(\s*r?(["'])(_[^"']+)\1/g;
const gleamPropertySetters = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?(?:set_property|set_string|set_bool)\(\s*[^,\n)]+,\s*(["'])(_[^"']+)\1/g;
const gleamCallMethods = /\b(?:[A-Za-z_$][A-Za-z0-9_$]*\.)?call_method[0-9]?\(\s*[^,\n)]+\s*,\s*(["'])(_[^"']+)\1/g;
const checks = [
bracketAccess,
inOperator,
reflectCalls,
objectHasOwn,
objectHasOwnPropertyCall,
hasOwnCalls,
dartInteropProperties,
dartInteropCallMethodFirstArg,
dartInteropCallMethodSecondArg,
dartContainsKey,
gleamPropertySetters,
gleamCallMethods,
];
for (const regex of checks) {
let match;
while ((match = regex.exec(sourceText)) !== null) {
recordRegexMatch(
findings,
fileName,
sourceText,
match.index,
match[0].length,
match[2] ?? match[1],
);
}
}
}
function recordKey(findings, fileName, node, key) {
if (!isUnderscoreKey(key)) return;
const sourceFile = node.getSourceFile();
const pos = sourceFile.getLineAndCharacterOfPosition(node.getStart());
const line = pos.line + 1;
const column = pos.character + 1;
if (!findings.has(key)) findings.set(key, []);
findings.get(key).push({
file: path.relative(projectRoot, fileName),
line,
column,
preview: node.getText(sourceFile),
});
}
function collectKeysFromSourceFile(fileName, findings) {
const sourceText = fs.readFileSync(fileName, "utf-8");
const extension = path.extname(fileName).toLowerCase();
const scriptKind = TS_SCRIPT_EXTS.get(extension);
if (!scriptKind) {
collectKeysFromTextFileUsingRegex(fileName, sourceText, findings);
return;
}
const sourceFile = ts.createSourceFile(
fileName,
sourceText,
ts.ScriptTarget.ESNext,
true,
scriptKind,
);
const visit = (node) => {
if (ts.isElementAccessExpression(node)) {
const keyLiteral = getStringLiteralFromCallArg(node.argumentExpression);
if (keyLiteral !== null) {
recordKey(findings, fileName, node.argumentExpression, keyLiteral);
}
} else if (
ts.isBinaryExpression(node) &&
node.operatorToken.kind === ts.SyntaxKind.InKeyword
) {
const key = getStringLiteralFromCallArg(node.left);
if (key !== null) {
recordKey(findings, fileName, node.left, key);
}
} else if (isReflectCallExpression(node)) {
const keyArg = getStringLiteralFromCallArg(node.arguments[1]);
if (keyArg !== null) {
recordKey(findings, fileName, node.arguments[1], keyArg);
}
} else if (isHasOwnLikeCallExpression(node)) {
const keyArg = getStringLiteralFromCallArg(node.arguments[1]);
if (keyArg !== null) {
recordKey(findings, fileName, node.arguments[1], keyArg);
}
} else if (isObjectHasOwnCallExpression(node)) {
const keyArg = getStringLiteralFromCallArg(node.arguments[1]);
if (keyArg !== null) {
recordKey(findings, fileName, node.arguments[1], keyArg);
}
}
ts.forEachChild(node, visit);
};
visit(sourceFile);
}
function main() {
const files = sourceRoots.flatMap((root) =>
fs.existsSync(root) ? listSourceFiles(root) : [],
);
const findings = new Map();
for (const fileName of files) {
collectKeysFromSourceFile(fileName, findings);
}
if (findings.size === 0) {
return;
}
const findingsList = Array.from(findings.entries()).map(
([key, locations]) => ({
key,
locations,
}),
);
const fileCount = findingsList.reduce(
(sum, item) => sum + item.locations.length,
0,
);
const plural = fileCount === 1 ? "occurrence" : "occurrences";
console.error(
`[underscore-prop-check] Found ${fileCount} ${plural} of underscore-prefixed runtime string keys`,
);
for (const { key, locations } of findingsList) {
for (const location of locations) {
console.error(
` - ${location.file}:${location.line}:${location.column} -> ${key}`,
);
}
console.error();
}
console.error(
"String-based property access on underscore-prefixed keys is disallowed. This is unsafe under property mangling.",
);
console.error(
"Remove the string access or refactor to direct property access.",
);
process.exitCode = 1;
}
main();