Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 166 lines 6.46 KB
Raw
1 import { defineRule } from '@oxlint/plugins';
2
3 import type { ESTree } from '@oxlint/plugins';
4
5 import {
6 collectEffectModuleNames,
7 isFunction,
8 isTestFile,
9 keyName,
10 moduleCallName,
11 moduleMemberName,
12 serviceClass,
13 unwrapNode,
14 type EffectModuleNames,
15 } from '../shared/effect-modules.ts';
16
17 const orDieMembers: ReadonlySet<string> = new Set(['orDie', 'orDieWith']);
18 /** Effect constructors whose function argument runs as part of the surrounding construction. */
19 const transparentThunks: ReadonlySet<string> = new Set(['suspend', 'sync']);
20 /** Effect constructors that define an operation; their arguments run when it is called, not during construction. */
21 const operationConstructors: ReadonlySet<string> = new Set(['fn', 'fnUntraced']);
22 const isLayerMemberName = (name: string): boolean =>
23 name === 'layer' || /^layer[A-Z0-9_]/u.test(name);
24 const isLayerConstructor = (name: string | null): boolean =>
25 name === 'effect' || name === 'scoped' || (name?.startsWith('unwrap') ?? false);
26
27 function isServiceClassNode(node: ESTree.Node | null, names: EffectModuleNames): boolean {
28 return (
29 (node?.type === 'ClassDeclaration' || node?.type === 'ClassExpression') &&
30 serviceClass(node, names) !== null
31 );
32 }
33
34 /** The parent outside parentheses and erased TypeScript expression wrappers. */
35 function expressionParent(node: ESTree.Node): ESTree.Node | null {
36 let current = node;
37 while (current.parent !== null && unwrapNode(current.parent) === node) {
38 current = current.parent;
39 }
40 return current.parent;
41 }
42
43 /** Whether `node`, entered from `child`, is where a service is constructed. */
44 function isConstructionRoot(
45 node: ESTree.Node,
46 child: ESTree.Node,
47 names: EffectModuleNames
48 ): boolean {
49 if (
50 (node.type === 'PropertyDefinition' || node.type === 'MethodDefinition') &&
51 node.static &&
52 !node.computed
53 ) {
54 const name = keyName(node.key);
55 return (
56 node.value === child &&
57 name !== null &&
58 (name === 'make' || isLayerMemberName(name)) &&
59 isServiceClassNode(node.parent.parent, names)
60 );
61 }
62 if (
63 node.type === 'Property' &&
64 !node.computed &&
65 node.value === child &&
66 keyName(node.key) === 'make'
67 ) {
68 const options = node.parent;
69 if (options.type !== 'ObjectExpression') return false;
70 const superCall = expressionParent(options);
71 if (superCall?.type !== 'CallExpression') return false;
72 const owner = expressionParent(superCall);
73 return (
74 (owner?.type === 'ClassDeclaration' || owner?.type === 'ClassExpression') &&
75 serviceClass(owner, names)?.options === options
76 );
77 }
78 if (node.type === 'CallExpression' && node.arguments.some(argument => argument === child)) {
79 return isLayerConstructor(moduleCallName(node, names, 'Layer'));
80 }
81 return false;
82 }
83
84 /** The Effect constructor a function is passed to, such as `gen` for `Effect.gen(function* () {})`. */
85 function effectConstructorOf(fn: ESTree.Node, names: EffectModuleNames): string | null {
86 const parent = fn.parent;
87 if (parent?.type !== 'CallExpression' || !parent.arguments.some(argument => argument === fn))
88 return null;
89 return moduleCallName(parent, names, 'Effect');
90 }
91
92 /** Whether `node` is `Effect.fn(...)(...)` or `Effect.fnUntraced(...)`: an operation definition. */
93 function isOperationDefinition(node: ESTree.Node, names: EffectModuleNames): boolean {
94 if (node.type !== 'CallExpression') return false;
95 const direct = moduleCallName(node, names, 'Effect');
96 if (direct !== null && operationConstructors.has(direct)) return true;
97 const curried = moduleCallName(node.callee, names, 'Effect');
98 return curried !== null && operationConstructors.has(curried);
99 }
100
101 /**
102 * Whether `node` runs while the service is constructed. Construction stops at operation boundaries: functions other
103 * than Effect.gen generators and suspend/sync thunks, Effect.fn definitions, object-literal properties without a
104 * delegated yield (operations in the returned service or callbacks in options), and effects a generator defines without yielding.
105 */
106 function isInServiceConstruction(node: ESTree.Node, names: EffectModuleNames): boolean {
107 let child: ESTree.Node = node;
108 let current: ESTree.Node | null = node.parent;
109 let yielded = false;
110 while (current !== null) {
111 if (isFunction(current)) {
112 if (current.parent !== null && isConstructionRoot(current.parent, current, names))
113 return true;
114 const constructor = effectConstructorOf(current, names);
115 if (constructor === 'gen') {
116 if (!yielded) return false;
117 yielded = false;
118 } else if (constructor === null || !transparentThunks.has(constructor)) {
119 return false;
120 }
121 } else if (isConstructionRoot(current, child, names)) {
122 return true;
123 } else if ((current.type === 'Property' && !yielded) || isOperationDefinition(current, names)) {
124 return false;
125 } else if (current.type === 'YieldExpression' && current.delegate) {
126 yielded = true;
127 }
128 child = current;
129 current = current.parent;
130 }
131 return false;
132 }
133
134 /** Keep service construction failures typed instead of turning them into defects. */
135 export const noOrDieInServiceConstructionRule = defineRule({
136 meta: {
137 type: 'problem',
138 docs: {
139 description:
140 'Disallow `Effect.orDie` while constructing a service (its make, its static layers, or Layer.effect/unwrap arguments) and `Layer.orDie` anywhere.',
141 },
142 messages: {
143 constructionOrDie:
144 "`Effect.{{member}}` turns a service construction failure into a defect. Classify the failure as a Data.TaggedError (or Schema.TaggedError when encoding is needed) and let it flow through the layer's error channel (`Layer.Layer<Service, ThatError>`), or handle it here with Effect.catchTag.",
145 layerOrDie:
146 "`Layer.orDie` hides this layer's construction failures as defects. Leave them in the layer's error channel as tagged errors and handle them where the runtime is built.",
147 },
148 },
149 create(context) {
150 if (isTestFile(context.filename)) return {};
151 const names = collectEffectModuleNames(context.sourceCode);
152
153 return {
154 MemberExpression(node) {
155 if (moduleMemberName(node, names, 'Layer') === 'orDie') {
156 context.report({ node, messageId: 'layerOrDie' });
157 return;
158 }
159 const member = moduleMemberName(node, names, 'Effect');
160 if (member === null || !orDieMembers.has(member) || !isInServiceConstruction(node, names))
161 return;
162 context.report({ node, messageId: 'constructionOrDie', data: { member } });
163 },
164 };
165 },
166 });