|
| 1 | +import { parseSync } from 'oxc-parser'; |
| 2 | +import { asNode, memberName, nodeList, propertyName, visitAst } from './layering-ast.ts'; |
| 3 | +import type { LayeringViolation } from './model.ts'; |
| 4 | +import { isProductionSourceFile } from './tracked-sources.ts'; |
| 5 | + |
| 6 | +export const PROCESS_GLOBALS_RULE = 'R86 process-globals'; |
| 7 | + |
| 8 | +/** Process capture and lifecycle belong to these executable or released factory boundaries. */ |
| 9 | +const PROCESS_ENTRIES = new Set([ |
| 10 | + 'src/bin.ts', |
| 11 | + 'src/cli.ts', |
| 12 | + 'src/daemon.ts', |
| 13 | + 'src/agent-device-client.ts', |
| 14 | + 'packages/capture-kit/src/png-worker.ts', |
| 15 | + 'packages/react-native/src/companion/companion-tunnel.ts', |
| 16 | + 'packages/react-native/src/companion/client-companion-tunnel-worker.ts', |
| 17 | + 'packages/maestro/src/daemon-port/run-script-http-child.ts', |
| 18 | + 'packages/platform-apple/src/os/macos/helper.ts', |
| 19 | + 'packages/platform-apple/src/runner/runner-process-launch.ts', |
| 20 | +]); |
| 21 | +// src/cli/** and scripts/** are entry trees. Apple native helper executables live outside the |
| 22 | +// TypeScript source trees. Reserve agent-device/host's entry here when #3757 supplies its path. |
| 23 | + |
| 24 | +/** Only command execution and spawn adapters may consult the ambient command-host scope. */ |
| 25 | +const COMMAND_HOST_BOUNDARIES = new Set([ |
| 26 | + 'packages/host-kit/src/internal/exec.ts', |
| 27 | + 'packages/platform-apple/src/core/tool-provider.ts', |
| 28 | + 'packages/platform-linux/src/tool-provider.ts', |
| 29 | + 'src/platform-runtime-android-adb-host.ts', |
| 30 | +]); |
| 31 | +const PROCESS_MEMBERS = new Set(['env', 'cwd', 'on', 'once', 'exit', 'emitWarning']); |
| 32 | + |
| 33 | +/** |
| 34 | + * Catches ambient configuration and process lifecycle in reusable modules, plus command-host |
| 35 | + * scope reads outside spawn adapters. Uses the existing OXC layering parser; no debt baseline. |
| 36 | + * Test support, including packages/test-kit, uses the shared production-source classification. |
| 37 | + * Workers and children receive serializable configuration at their declared entry boundary. |
| 38 | + * Kill criterion: retire only if reusable sources may own process configuration and lifecycle. |
| 39 | + */ |
| 40 | +export function processGlobalsViolations( |
| 41 | + sources: ReadonlyMap<string, string>, |
| 42 | +): LayeringViolation[] { |
| 43 | + const violations: LayeringViolation[] = []; |
| 44 | + for (const [file, source] of sources) { |
| 45 | + if (!isProductionSourceFile(file)) continue; |
| 46 | + const processEntry = PROCESS_ENTRIES.has(file) || file.startsWith('src/cli/'); |
| 47 | + const commandBoundary = COMMAND_HOST_BOUNDARIES.has(file); |
| 48 | + if (processEntry && commandBoundary) continue; |
| 49 | + const program = parseSync(file, source).program; |
| 50 | + const processNames = new Set(['process']); |
| 51 | + const commandHostNames = new Set(['commandHostEnvironment']); |
| 52 | + const processExports = new Set<string>(); |
| 53 | + visitAst(program, (node) => { |
| 54 | + if (node.type !== 'ImportDeclaration') return; |
| 55 | + const specifier = asNode(node.source)?.value; |
| 56 | + for (const value of nodeList(node.specifiers)) { |
| 57 | + const binding = asNode(value); |
| 58 | + const local = asNode(binding?.local)?.name; |
| 59 | + if (typeof local !== 'string') continue; |
| 60 | + if (specifier === 'node:process' || specifier === 'process') { |
| 61 | + if (binding?.type !== 'ImportSpecifier') processNames.add(local); |
| 62 | + else if (PROCESS_MEMBERS.has(propertyName(binding.imported) ?? '')) { |
| 63 | + processExports.add(local); |
| 64 | + } |
| 65 | + } |
| 66 | + if (propertyName(binding?.imported) === 'commandHostEnvironment') { |
| 67 | + commandHostNames.add(local); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + const isProcess = (value: unknown): boolean => { |
| 72 | + const node = asNode(value); |
| 73 | + if (node?.type === 'Identifier') return processNames.has(String(node.name)); |
| 74 | + return ( |
| 75 | + node?.type === 'MemberExpression' && |
| 76 | + asNode(node.object)?.type === 'Identifier' && |
| 77 | + ['globalThis', 'global'].includes(String(asNode(node.object)?.name)) && |
| 78 | + memberName(node) === 'process' |
| 79 | + ); |
| 80 | + }; |
| 81 | + visitAst(program, (node) => { |
| 82 | + let message: string | undefined; |
| 83 | + if (!processEntry) { |
| 84 | + if ( |
| 85 | + node.type === 'MemberExpression' && |
| 86 | + isProcess(node.object) && |
| 87 | + PROCESS_MEMBERS.has(memberName(node) ?? '') |
| 88 | + ) { |
| 89 | + message = `process.${memberName(node)} belongs at a declared process entry; pass the owning host explicitly`; |
| 90 | + } else if (node.type === 'VariableDeclarator' && isProcess(node.init)) { |
| 91 | + const pattern = asNode(node.id); |
| 92 | + if ( |
| 93 | + pattern?.type === 'ObjectPattern' && |
| 94 | + nodeList(pattern.properties).some((value) => |
| 95 | + PROCESS_MEMBERS.has(propertyName(asNode(value)?.key) ?? ''), |
| 96 | + ) |
| 97 | + ) { |
| 98 | + message = |
| 99 | + 'process configuration and lifecycle destructuring belongs at a declared process entry'; |
| 100 | + } |
| 101 | + } else if (node.type === 'ImportDeclaration') { |
| 102 | + if ( |
| 103 | + nodeList(node.specifiers).some((value) => |
| 104 | + processExports.has(String(asNode(asNode(value)?.local)?.name)), |
| 105 | + ) |
| 106 | + ) { |
| 107 | + message = |
| 108 | + 'process configuration and lifecycle imports belong at a declared process entry'; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + if (!commandBoundary && node.type === 'CallExpression') { |
| 113 | + const callee = asNode(node.callee); |
| 114 | + if ( |
| 115 | + (callee?.type === 'Identifier' && commandHostNames.has(String(callee.name))) || |
| 116 | + (callee?.type === 'MemberExpression' && memberName(callee) === 'commandHostEnvironment') |
| 117 | + ) { |
| 118 | + message = |
| 119 | + 'commandHostEnvironment() belongs at the command-execution/spawn boundary; pass the owning host explicitly'; |
| 120 | + } |
| 121 | + } |
| 122 | + if (message) { |
| 123 | + violations.push({ |
| 124 | + rule: PROCESS_GLOBALS_RULE, |
| 125 | + file, |
| 126 | + line: source.slice(0, Number(node.start ?? 0)).split('\n').length, |
| 127 | + message, |
| 128 | + }); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + return violations; |
| 133 | +} |
0 commit comments