Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

no-global-process-runtime.ts

144 lines5.3 KB

1import { defineRule } from "@oxlint/plugins";
2import * as Option from "effect/Option";
3​
4import { getPropertyName, isIdentifier, unwrapExpression } from "../utils.ts";
5​
6const RUNTIME_PROPERTIES = new Set(["platform", "arch"]);
7const HOST_PROCESS_REFERENCE_FILE = "packages/shared/src/hostProcess.ts";
8const NODE_OS_MODULES = new Set(["node:os", "os"]);
9​
10const normalizePath = (path: string) => path.replaceAll("\\", "/");
11​
12const toRepoPath = (filename: string, cwd: string) => {
13 const normalizedFilename = normalizePath(filename);
14 const normalizedCwd = normalizePath(cwd).replace(/\/+$/u, "");
15 const prefix = `${normalizedCwd}/`;
16 return normalizedFilename.startsWith(prefix)
17 ? normalizedFilename.slice(prefix.length)
18 : normalizedFilename;
19};
20​
21const isHostProcessReferenceFile = (filename: string, cwd: string) =>
22 toRepoPath(filename, cwd) === HOST_PROCESS_REFERENCE_FILE;
23​
24const isGlobalProcessObject = (node: unknown): boolean => {
25 const expression = unwrapExpression(node);
26 if (isIdentifier(expression, "process")) return true;
27 if (Option.isNone(expression) || expression.value.type !== "MemberExpression") return false;
28​
29 const object = unwrapExpression(expression.value.object);
30 const property = getPropertyName(expression.value.property);
31 return (
32 isIdentifier(object, "globalThis") && Option.isSome(property) && property.value === "process"
33 );
34};
35​
36const message = (property: string) =>
37 `Use HostProcess${property === "arch" ? "Architecture" : "Platform"} instead of process.${property}; inject the runtime reference in Effect code and provide it explicitly in tests.`;
38​
39const getLiteralStringValue = (node: unknown): Option.Option<string> => {
40 if (typeof node !== "object" || node === null) return Option.none();
41 if (!("type" in node) || node.type !== "Literal") return Option.none();
42 if (!("value" in node) || typeof node.value !== "string") return Option.none();
43 return Option.some(node.value);
44};
45​
46export default defineRule({
47 meta: {
48 type: "problem",
49 docs: {
50 description:
51 "Disallow direct host runtime platform/architecture reads outside the shared host process references.",
52 },
53 },
54 createOnce(context) {
55 const nodeOsNamespaces = new Set<string>();
56 const nodeOsRuntimeImports = new Map<string, string>();
57​
58 const resetBindings = () => {
59 nodeOsNamespaces.clear();
60 nodeOsRuntimeImports.clear();
61 };
62​
63 const trackImportDeclaration = (node: unknown) => {
64 if (typeof node !== "object" || node === null) return;
65 if (!("source" in node)) return;
66​
67 const source = getLiteralStringValue(node.source);
68 if (Option.isNone(source) || !NODE_OS_MODULES.has(source.value)) return;
69 if (!("specifiers" in node) || !Array.isArray(node.specifiers)) return;
70​
71 for (const specifier of node.specifiers) {
72 if (typeof specifier !== "object" || specifier === null) continue;
73 if (!("local" in specifier)) continue;
74​
75 const local = unwrapExpression(specifier.local);
76 if (Option.isNone(local) || local.value.type !== "Identifier") continue;
77 const localName = local.value.name;
78​
79 if (
80 specifier.type === "ImportNamespaceSpecifier" ||
81 specifier.type === "ImportDefaultSpecifier"
82 ) {
83 nodeOsNamespaces.add(localName);
84 continue;
85 }
86​
87 if (specifier.type !== "ImportSpecifier" || !("imported" in specifier)) continue;
88​
89 const imported = getPropertyName(specifier.imported);
90 if (Option.isSome(imported) && RUNTIME_PROPERTIES.has(imported.value)) {
91 nodeOsRuntimeImports.set(localName, imported.value);
92 }
93 }
94 };
95​
96 const getNodeOsRuntimeCall = (callee: unknown): Option.Option<string> => {
97 const expression = unwrapExpression(callee);
98 if (Option.isNone(expression)) return Option.none();
99​
100 if (expression.value.type === "Identifier") {
101 const property = nodeOsRuntimeImports.get(expression.value.name);
102 return property === undefined ? Option.none() : Option.some(property);
103 }
104​
105 if (expression.value.type !== "MemberExpression") return Option.none();
106​
107 const object = unwrapExpression(expression.value.object);
108 if (Option.isNone(object) || object.value.type !== "Identifier") return Option.none();
109 if (!nodeOsNamespaces.has(object.value.name)) return Option.none();
110​
111 return Option.filter(getPropertyName(expression.value.property), (property) =>
112 RUNTIME_PROPERTIES.has(property),
113 );
114 };
115​
116 return {
117 before: resetBindings,
118 ImportDeclaration: trackImportDeclaration,
119 MemberExpression(node) {
120 if (isHostProcessReferenceFile(context.filename, context.cwd)) return;
121​
122 const property = getPropertyName(node.property);
123 if (Option.isNone(property) || !RUNTIME_PROPERTIES.has(property.value)) return;
124 if (!isGlobalProcessObject(node.object)) return;
125​
126 context.report({
127 node,
128 message: message(property.value),
129 });
130 },
131 CallExpression(node) {
132 if (isHostProcessReferenceFile(context.filename, context.cwd)) return;
133​
134 const property = getNodeOsRuntimeCall(node.callee);
135 if (Option.isNone(property)) return;
136​
137 context.report({
138 node,
139 message: message(property.value),
140 });
141 },
142 };
143 },
144});