MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / scanSource

Function scanSource

__tests__/child-process-windows-hide.test.ts:262–307  ·  view source on GitHub ↗

Every spawn in one file, and each one that isn't provably hidden.

(rel: string, text: string)

Source from the content-addressed store, hash-verified

260
261/** Every spawn in one file, and each one that isn't provably hidden. */
262function scanSource(rel: string, text: string): { seen: number; offenders: string[] } {
263 const sf = ts.createSourceFile(rel, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
264 const bindings = childProcessBindings(sf);
265 const offenders: string[] = [];
266 let seen = 0;
267 const flag = (node: ts.Node, what: string): void => {
268 const { line } = sf.getLineAndCharacterOfPosition(node.getStart(sf));
269 offenders.push(`${rel}:${line + 1} ${what}`);
270 };
271 const visit = (node: ts.Node): void => {
272 if (ts.isExportDeclaration(node) && isChildProcessModule(node.moduleSpecifier)) {
273 flag(node, 're-exports child_process: spawns through the re-export are out of sight');
274 }
275 if (ts.isCallExpression(node)) {
276 const callee = node.expression;
277 const loads =
278 (ts.isIdentifier(callee) && callee.text === 'require') || callee.kind === ts.SyntaxKind.ImportKeyword;
279 if (loads && isChildProcessModule(node.arguments[0])) {
280 flag(node, `${callee.getText(sf)}(child_process): import it statically so its spawns can be checked`);
281 }
282 const spawner = spawnerOf(node, bindings);
283 if (spawner) {
284 seen++;
285 const key = `${rel}:${spawner}(${commandLabel(node, sf)})`;
286 if (!Object.hasOwn(ALLOWED, key) && !hidesWindow(optionsArg(node, spawner), sf)) flag(node, key);
287 }
288 }
289 const bound = ts.isIdentifier(node) && (bindings.named.has(node.text) || bindings.namespaces.has(node.text));
290 if (bound && isValueReference(node)) {
291 const imported = bindings.named.get(node.text);
292 if (imported !== undefined && Object.hasOwn(SPAWNERS, imported) && !isCallee(node)) {
293 flag(node, `${node.text} used as a value: call it directly so its options can be checked`);
294 } else if (bindings.namespaces.has(node.text)) {
295 const member = node.parent;
296 const direct =
297 ts.isPropertyAccessExpression(member) &&
298 member.expression === node &&
299 (!Object.hasOwn(SPAWNERS, member.name.text) || isCallee(member));
300 if (!direct) flag(node, `${node.getText(sf)} used as a value: call its spawners directly so their options can be checked`);
301 }
302 }
303 ts.forEachChild(node, visit);
304 };
305 visit(sf);
306 return { seen, offenders };
307}
308
309describe('child processes set windowsHide (#1092, #2094, #2312)', () => {
310 it('every child_process spawn under src/ sets windowsHide: true', () => {

Callers 2

scanFunction · 0.85

Calls 2

childProcessBindingsFunction · 0.85
visitFunction · 0.70

Tested by

no test coverage detected