Repository navigation
Expand file tree
/
Copy pathprocess-globals-policy.ts
More file actions
133 lines (129 loc) · 5.66 KB
/
Copy pathprocess-globals-policy.ts
File metadata and controls
133 lines (129 loc) · 5.66 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
import { parseSync } from 'oxc-parser';
import { asNode, memberName, nodeList, propertyName, visitAst } from './layering-ast.ts';
import type { LayeringViolation } from './model.ts';
import { isProductionSourceFile } from './tracked-sources.ts';
export const PROCESS_GLOBALS_RULE = 'R86 process-globals';
/** Process capture and lifecycle belong to these executable or released factory boundaries. */
const PROCESS_ENTRIES = new Set([
'src/bin.ts',
'src/cli.ts',
'src/daemon.ts',
'src/agent-device-client.ts',
'packages/capture-kit/src/png-worker.ts',
'packages/react-native/src/companion/companion-tunnel.ts',
'packages/react-native/src/companion/client-companion-tunnel-worker.ts',
'packages/maestro/src/daemon-port/run-script-http-child.ts',
'packages/platform-apple/src/os/macos/helper.ts',
'packages/platform-apple/src/runner/runner-process-launch.ts',
]);
// src/cli/** and scripts/** are entry trees. Apple native helper executables live outside the
// TypeScript source trees. Reserve agent-device/host's entry here when #3757 supplies its path.
/** Only command execution and spawn adapters may consult the ambient command-host scope. */
const COMMAND_HOST_BOUNDARIES = new Set([
'packages/host-kit/src/internal/exec.ts',
'packages/platform-apple/src/core/tool-provider.ts',
'packages/platform-linux/src/tool-provider.ts',
'src/platform-runtime-android-adb-host.ts',
]);
const PROCESS_MEMBERS = new Set(['env', 'cwd', 'on', 'once', 'exit', 'emitWarning']);
/**
* Catches ambient configuration and process lifecycle in reusable modules, plus command-host
* scope reads outside spawn adapters. Uses the existing OXC layering parser; no debt baseline.
* Test support, including packages/test-kit, uses the shared production-source classification.
* Workers and children receive serializable configuration at their declared entry boundary.
* Kill criterion: retire only if reusable sources may own process configuration and lifecycle.
*/
export function processGlobalsViolations(
sources: ReadonlyMap<string, string>,
): LayeringViolation[] {
const violations: LayeringViolation[] = [];
for (const [file, source] of sources) {
if (!isProductionSourceFile(file)) continue;
const processEntry = PROCESS_ENTRIES.has(file) || file.startsWith('src/cli/');
const commandBoundary = COMMAND_HOST_BOUNDARIES.has(file);
if (processEntry && commandBoundary) continue;
const program = parseSync(file, source).program;
const processNames = new Set(['process']);
const commandHostNames = new Set(['commandHostEnvironment']);
const processExports = new Set<string>();
visitAst(program, (node) => {
if (node.type !== 'ImportDeclaration') return;
const specifier = asNode(node.source)?.value;
for (const value of nodeList(node.specifiers)) {
const binding = asNode(value);
const local = asNode(binding?.local)?.name;
if (typeof local !== 'string') continue;
if (specifier === 'node:process' || specifier === 'process') {
if (binding?.type !== 'ImportSpecifier') processNames.add(local);
else if (PROCESS_MEMBERS.has(propertyName(binding.imported) ?? '')) {
processExports.add(local);
}
}
if (propertyName(binding?.imported) === 'commandHostEnvironment') {
commandHostNames.add(local);
}
}
});
const isProcess = (value: unknown): boolean => {
const node = asNode(value);
if (node?.type === 'Identifier') return processNames.has(String(node.name));
return (
node?.type === 'MemberExpression' &&
asNode(node.object)?.type === 'Identifier' &&
['globalThis', 'global'].includes(String(asNode(node.object)?.name)) &&
memberName(node) === 'process'
);
};
visitAst(program, (node) => {
let message: string | undefined;
if (!processEntry) {
if (
node.type === 'MemberExpression' &&
isProcess(node.object) &&
PROCESS_MEMBERS.has(memberName(node) ?? '')
) {
message = `process.${memberName(node)} belongs at a declared process entry; pass the owning host explicitly`;
} else if (node.type === 'VariableDeclarator' && isProcess(node.init)) {
const pattern = asNode(node.id);
if (
pattern?.type === 'ObjectPattern' &&
nodeList(pattern.properties).some((value) =>
PROCESS_MEMBERS.has(propertyName(asNode(value)?.key) ?? ''),
)
) {
message =
'process configuration and lifecycle destructuring belongs at a declared process entry';
}
} else if (node.type === 'ImportDeclaration') {
if (
nodeList(node.specifiers).some((value) =>
processExports.has(String(asNode(asNode(value)?.local)?.name)),
)
) {
message =
'process configuration and lifecycle imports belong at a declared process entry';
}
}
}
if (!commandBoundary && node.type === 'CallExpression') {
const callee = asNode(node.callee);
if (
(callee?.type === 'Identifier' && commandHostNames.has(String(callee.name))) ||
(callee?.type === 'MemberExpression' && memberName(callee) === 'commandHostEnvironment')
) {
message =
'commandHostEnvironment() belongs at the command-execution/spawn boundary; pass the owning host explicitly';
}
}
if (message) {
violations.push({
rule: PROCESS_GLOBALS_RULE,
file,
line: source.slice(0, Number(node.start ?? 0)).split('\n').length,
message,
});
}
});
}
return violations;
}