oxlint-plugin-effect 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rules/prefer-effect-fn.js +41 -13
- package/package.json +1 -1
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
import { make } from "../vendor/effect-oxlint/Diagnostic.js";
|
|
2
2
|
import { RuleContext } from "../vendor/effect-oxlint/RuleContext.js";
|
|
3
3
|
import { define, meta } from "../vendor/effect-oxlint/Rule.js";
|
|
4
|
+
import { findVariableUp } from "../vendor/effect-oxlint/Scope.js";
|
|
4
5
|
import "../vendor/effect-oxlint/index.js";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
5
7
|
import * as Effect from "effect/Effect";
|
|
6
8
|
//#region src/rules/prefer-effect-fn.ts
|
|
7
|
-
const isStaticMember = (node,
|
|
8
|
-
const isEffectGenCall = (node) => node.type === "CallExpression" && node.callee.type !== "Super" && isStaticMember(node.callee,
|
|
9
|
-
const isEffectWithSpanCall = (node) => node.type === "CallExpression" && node.callee.type !== "Super" && isStaticMember(node.callee,
|
|
10
|
-
const spansEffectGenDirectly = (node) => node.callee.type !== "Super" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && node.callee.property.name === "withSpan" && isEffectGenCall(node.callee.object);
|
|
11
|
-
const
|
|
9
|
+
const isStaticMember = (node, objects, property) => node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && objects.has(node.object.name) && node.property.type === "Identifier" && node.property.name === property;
|
|
10
|
+
const isEffectGenCall = (node, effectNamespaces) => node.type === "CallExpression" && node.callee.type !== "Super" && isStaticMember(node.callee, effectNamespaces, "gen");
|
|
11
|
+
const isEffectWithSpanCall = (node, effectNamespaces) => node.type === "CallExpression" && node.callee.type !== "Super" && isStaticMember(node.callee, effectNamespaces, "withSpan");
|
|
12
|
+
const spansEffectGenDirectly = (node, effectNamespaces) => node.callee.type !== "Super" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && node.callee.property.name === "withSpan" && isEffectGenCall(node.callee.object, effectNamespaces);
|
|
13
|
+
const pipesWithSpanFromEffectGen = (node, effectNamespaces) => node.callee.type !== "Super" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && node.callee.property.name === "pipe" && isEffectGenCall(node.callee.object, effectNamespaces) && node.arguments.some((argument) => isEffectWithSpanCall(argument, effectNamespaces));
|
|
14
|
+
const effectNamespaceFromImport = (node) => {
|
|
15
|
+
const source = node.source.value;
|
|
16
|
+
if (source !== "effect" && source !== "effect/Effect") return [];
|
|
17
|
+
const namespaces = [];
|
|
18
|
+
for (const specifier of node.specifiers) {
|
|
19
|
+
if (specifier.type === "ImportNamespaceSpecifier" && source === "effect/Effect") {
|
|
20
|
+
namespaces.push(specifier.local.name);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (specifier.type === "ImportSpecifier" && source === "effect" && specifier.imported.type === "Identifier" && specifier.imported.name === "Effect") namespaces.push(specifier.local.name);
|
|
24
|
+
}
|
|
25
|
+
return namespaces;
|
|
26
|
+
};
|
|
12
27
|
const preferEffectFn = define({
|
|
13
28
|
name: "prefer-effect-fn",
|
|
14
29
|
meta: meta({
|
|
@@ -17,14 +32,27 @@ const preferEffectFn = define({
|
|
|
17
32
|
}),
|
|
18
33
|
create: function* () {
|
|
19
34
|
const ctx = yield* RuleContext;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
const effectNamespaces = /* @__PURE__ */ new Set();
|
|
36
|
+
const visibleEffectNamespaces = (node) => new Set([...effectNamespaces].filter((namespace) => Option.match(findVariableUp(ctx.sourceCode.getScope(node), namespace), {
|
|
37
|
+
onNone: () => true,
|
|
38
|
+
onSome: (variable) => variable.defs.some((definition) => definition.type === "ImportBinding")
|
|
39
|
+
})));
|
|
40
|
+
return {
|
|
41
|
+
ImportDeclaration: (node) => {
|
|
42
|
+
if (node.type !== "ImportDeclaration") return Effect.void;
|
|
43
|
+
for (const namespace of effectNamespaceFromImport(node)) effectNamespaces.add(namespace);
|
|
44
|
+
return Effect.void;
|
|
45
|
+
},
|
|
46
|
+
CallExpression: (node) => {
|
|
47
|
+
if (node.type !== "CallExpression") return Effect.void;
|
|
48
|
+
const namespaces = visibleEffectNamespaces(node);
|
|
49
|
+
if (!spansEffectGenDirectly(node, namespaces) && !pipesWithSpanFromEffectGen(node, namespaces)) return Effect.void;
|
|
50
|
+
return ctx.report(make({
|
|
51
|
+
node,
|
|
52
|
+
message: "Avoid a span on Effect.gen. Define the named operation with Effect.fn."
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
28
56
|
}
|
|
29
57
|
});
|
|
30
58
|
//#endregion
|