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