@siim-m/config 0.2.0 → 0.3.0
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/README.md +58 -5
- package/dist/ai-guard.d.ts +5 -0
- package/dist/ai-guard.js +5 -0
- package/dist/anti-slop/effect/LICENSE-enfect +21 -0
- package/dist/anti-slop/effect/index.js +8 -0
- package/dist/anti-slop/effect/rules/no-ordie-in-service-construction.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-ordie-in-service-construction.js +117 -0
- package/dist/anti-slop/effect/rules/no-unbounded-retry.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-unbounded-retry.js +307 -0
- package/dist/anti-slop/effect/rules/require-cause-when-narrowing.d.ts +2 -0
- package/dist/anti-slop/effect/rules/require-cause-when-narrowing.js +250 -0
- package/dist/anti-slop/effect/rules/throw-in-effect-gen.d.ts +2 -0
- package/dist/anti-slop/effect/rules/throw-in-effect-gen.js +87 -0
- package/dist/anti-slop/effect/shared/effect-modules.d.ts +79 -0
- package/dist/anti-slop/effect/shared/effect-modules.js +336 -0
- package/dist/anti-slop/index.js +6 -0
- package/dist/anti-slop/rules/comments.d.ts +7 -0
- package/dist/anti-slop/rules/comments.js +376 -0
- package/dist/anti-slop/rules/no-disabled-type-assertion-rules.d.ts +2 -0
- package/dist/anti-slop/rules/no-disabled-type-assertion-rules.js +94 -0
- package/dist/anti-slop/rules/require-suppression-reason.d.ts +6 -0
- package/dist/anti-slop/rules/require-suppression-reason.js +61 -0
- package/dist/landmines.d.ts +5 -0
- package/dist/landmines.js +5 -0
- package/dist/oxlint.d.ts +41 -0
- package/dist/oxlint.js +48 -1
- package/package.json +16 -5
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Shared oxlint, oxfmt and tsconfig settings for TypeScript, with the
|
|
|
4
4
|
[anti-slop](https://github.com/dmmulroy/anti-slop) oxlint rules.
|
|
5
5
|
|
|
6
6
|
```sh
|
|
7
|
-
bun add -d @siim-m/config oxlint
|
|
7
|
+
bun add -d @siim-m/config oxlint tsrs oxfmt
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
```ts
|
|
@@ -24,9 +24,57 @@ export default { ...base, singleQuote: false };
|
|
|
24
24
|
{ "extends": "@siim-m/config/tsconfig.json" }
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
Every rule is on at `error`; a project turns
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
It needs Node 22.18 or later in 22, or 24.11 or later. Every rule is on at `error`; a project turns
|
|
28
|
+
off what it can't pass yet. Run oxlint with `--type-aware`, which uses
|
|
29
|
+
[tsrs](https://github.com/oxc-project/tsrs), the Rust port of TypeScript 7 (in beta), when it's
|
|
30
|
+
installed, or `oxlint-tsgolint` in its place. Either one only backs the lint rules, so keep the
|
|
31
|
+
project's own `tsc` or `@effect/tsgo` typecheck. For ts-reset's stricter built-in types, add
|
|
32
|
+
`import '@siim-m/config/reset.d.ts';` to a `.d.ts` file in the project.
|
|
33
|
+
|
|
34
|
+
A disable comment or `@ts-expect-error` must say why: after `--` (or, for a `@ts-` directive,
|
|
35
|
+
anywhere after it in the same comment), or on the line above
|
|
36
|
+
(`anti-slop/require-suppression-reason`). The type-assertion rules can't be disabled inline at all;
|
|
37
|
+
turn one off for a file in the lint config instead (`anti-slop/no-disabled-type-assertion-rules`). A
|
|
38
|
+
file-wide or same-line disable that names no rule, or that names one of these two checks, silences
|
|
39
|
+
them too, and oxlint gives a rule no way to report that, so catch one in review.
|
|
40
|
+
|
|
41
|
+
## Comments
|
|
42
|
+
|
|
43
|
+
`anti-slop/comments` holds a comment to a short why. By default a comment gets 30 words, a doc
|
|
44
|
+
comment on an export 45, and it may cite one issue. It also reports section dividers, test step
|
|
45
|
+
labels, commented-out code, TODOs without a link, JSDoc types in TypeScript, empty `@param` and
|
|
46
|
+
`@returns` (or `@return`) tags, and comments that only repeat the name below them. `forbidden` adds
|
|
47
|
+
patterns of your own:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
'anti-slop/comments': ['error', {
|
|
51
|
+
maxWords: 30,
|
|
52
|
+
forbidden: [{ pattern: '\\b\\d{4}-\\d{2}-\\d{2}\\b', message: 'Leave dates to git.' }],
|
|
53
|
+
}],
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`policy: 'tagged-only'` goes further: only directives, licence headers, doc comments on exports, the
|
|
57
|
+
reason above a suppression and comments that start with an `allow` prefix stay. The prefixes default
|
|
58
|
+
to `SAFETY`, `SYNC` and `WARNING`. An `allow` list replaces them, so keep `SAFETY` and `SYNC` in it:
|
|
59
|
+
the type-assertion and React effect rules ask for those.
|
|
60
|
+
|
|
61
|
+
## Tests and shortcuts
|
|
62
|
+
|
|
63
|
+
Ten rules from two npm packages report tests that pass whatever the code does or lean on time, and
|
|
64
|
+
other shortcuts agents take:
|
|
65
|
+
|
|
66
|
+
- From [eslint-plugin-ai-guard](https://github.com/ai-guard-dev/eslint-plugin-ai-guard):
|
|
67
|
+
`no-assertionless-test`, `no-tautological-assertion`, `no-placeholder-implementation`,
|
|
68
|
+
`no-test-env-branch`, `no-silent-fallback` and `no-undeclared-import`. Its test detection doesn't
|
|
69
|
+
know Effect's `it.effect`, so those tests go unchecked. It brings `eslint` and typescript-eslint,
|
|
70
|
+
which ask for TypeScript 6 as a peer; npm nests a copy under this package when the project has
|
|
71
|
+
TypeScript 7.
|
|
72
|
+
- From [landmines](https://github.com/interfere-inc/landmines): `no-fixed-sleep`,
|
|
73
|
+
`no-flake-masking`, `no-unsignaled-poll` and `strict-msw`. All four take an
|
|
74
|
+
`allow: { "<file>": <count> }` option for adopting them file by file.
|
|
75
|
+
|
|
76
|
+
Both plugins load whole, so their other rules are there to turn on, such as landmines'
|
|
77
|
+
`no-test-doubles`, which bans `vi.fn(impl)` and other replaced behaviour in tests.
|
|
30
78
|
|
|
31
79
|
## React
|
|
32
80
|
|
|
@@ -55,4 +103,9 @@ In `.tsx` files a number can't be a condition, because `{count && <p />}` render
|
|
|
55
103
|
|
|
56
104
|
MIT. The anti-slop rules are MIT, copyright Dillon Mulroy; `require-readable-spacing` uses MIT code
|
|
57
105
|
from ESLint Stylistic. The `anti-slop-react` rules are MIT, copyright T3 Tools Inc.: rewritten from
|
|
58
|
-
T3 Code's `oxlint-plugin-t3code` at `2de3370`, and not kept in step with it.
|
|
106
|
+
T3 Code's `oxlint-plugin-t3code` at `2de3370`, and not kept in step with it. `anti-slop/comments`
|
|
107
|
+
and `anti-slop/require-suppression-reason` adapt code from the same plugin. Four `anti-slop-effect`
|
|
108
|
+
rules and `no-disabled-type-assertion-rules` are MIT, from Kyle Mistele's fork of anti-slop;
|
|
109
|
+
`throw-in-effect-gen` there is ported from enfect, MIT, copyright Marc Suesser, and
|
|
110
|
+
`no-unbounded-retry` from cevr/effect-oxlint, MIT. eslint-plugin-ai-guard and @interfere/landmines
|
|
111
|
+
are MIT dependencies.
|
package/dist/ai-guard.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marc Suesser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
2
2
|
import { noManualEffectErrorTagRule } from "./rules/no-manual-effect-error-tag.js";
|
|
3
|
+
import { noOrDieInServiceConstructionRule } from "./rules/no-ordie-in-service-construction.js";
|
|
3
4
|
import { noServiceConstructorImportsRule } from "./rules/no-service-constructor-imports.js";
|
|
4
5
|
import { noServiceParametersRule } from "./rules/no-service-parameters.js";
|
|
6
|
+
import { noUnboundedRetryRule } from "./rules/no-unbounded-retry.js";
|
|
5
7
|
import { preferEffectMatchRule } from "./rules/prefer-effect-match.js";
|
|
8
|
+
import { requireCauseWhenNarrowingRule } from "./rules/require-cause-when-narrowing.js";
|
|
9
|
+
import { throwInEffectGenRule } from "./rules/throw-in-effect-gen.js";
|
|
6
10
|
/** Opt-in Oxlint rules for Effect service and Layer architecture. */
|
|
7
11
|
const antiSlopEffectPlugin = eslintCompatPlugin({
|
|
8
12
|
meta: { name: "anti-slop-effect" },
|
|
9
13
|
rules: {
|
|
10
14
|
"no-manual-effect-error-tag": noManualEffectErrorTagRule,
|
|
15
|
+
"no-ordie-in-service-construction": noOrDieInServiceConstructionRule,
|
|
11
16
|
"no-service-constructor-imports": noServiceConstructorImportsRule,
|
|
12
17
|
"no-service-parameters": noServiceParametersRule,
|
|
18
|
+
"no-unbounded-retry": noUnboundedRetryRule,
|
|
13
19
|
"prefer-effect-match": preferEffectMatchRule,
|
|
20
|
+
"require-cause-when-narrowing": requireCauseWhenNarrowingRule,
|
|
21
|
+
"throw-in-effect-gen": throwInEffectGenRule,
|
|
14
22
|
},
|
|
15
23
|
});
|
|
16
24
|
export default antiSlopEffectPlugin;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { collectEffectModuleNames, isFunction, isTestFile, keyName, moduleCallName, moduleMemberName, serviceClass, } from "../shared/effect-modules.js";
|
|
3
|
+
const orDieMembers = new Set(["orDie", "orDieWith"]);
|
|
4
|
+
/** Effect constructors whose function argument runs as part of the surrounding construction. */
|
|
5
|
+
const transparentThunks = new Set(["suspend", "sync"]);
|
|
6
|
+
/** Effect constructors that define an operation; their arguments run when it is called, not during construction. */
|
|
7
|
+
const operationConstructors = new Set(["fn", "fnUntraced"]);
|
|
8
|
+
const isLayerMemberName = (name) => name === "layer" || /^layer[A-Z0-9_]/u.test(name);
|
|
9
|
+
const isLayerConstructor = (name) => name === "effect" || name === "scoped" || (name?.startsWith("unwrap") ?? false);
|
|
10
|
+
function isServiceClassNode(node, names) {
|
|
11
|
+
return (node?.type === "ClassDeclaration" || node?.type === "ClassExpression") && serviceClass(node, names) !== null;
|
|
12
|
+
}
|
|
13
|
+
/** Whether `node`, entered from `child`, is where a service is constructed. */
|
|
14
|
+
function isConstructionRoot(node, child, names) {
|
|
15
|
+
if ((node.type === "PropertyDefinition" || node.type === "MethodDefinition") && node.static && !node.computed) {
|
|
16
|
+
const name = keyName(node.key);
|
|
17
|
+
return (node.value === child &&
|
|
18
|
+
name !== null &&
|
|
19
|
+
(name === "make" || isLayerMemberName(name)) &&
|
|
20
|
+
isServiceClassNode(node.parent.parent, names));
|
|
21
|
+
}
|
|
22
|
+
if (node.type === "Property" && !node.computed && node.value === child && keyName(node.key) === "make") {
|
|
23
|
+
const options = node.parent;
|
|
24
|
+
const superCall = options.parent;
|
|
25
|
+
return (options.type === "ObjectExpression" &&
|
|
26
|
+
superCall?.type === "CallExpression" &&
|
|
27
|
+
isServiceClassNode(superCall.parent, names));
|
|
28
|
+
}
|
|
29
|
+
if (node.type === "CallExpression" && node.arguments.some((argument) => argument === child)) {
|
|
30
|
+
return isLayerConstructor(moduleCallName(node, names, "Layer"));
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
/** The Effect constructor a function is passed to, such as `gen` for `Effect.gen(function* () {})`. */
|
|
35
|
+
function effectConstructorOf(fn, names) {
|
|
36
|
+
const parent = fn.parent;
|
|
37
|
+
if (parent?.type !== "CallExpression" || !parent.arguments.some((argument) => argument === fn))
|
|
38
|
+
return null;
|
|
39
|
+
return moduleCallName(parent, names, "Effect");
|
|
40
|
+
}
|
|
41
|
+
/** Whether `node` is `Effect.fn(...)(...)` or `Effect.fnUntraced(...)`: an operation definition. */
|
|
42
|
+
function isOperationDefinition(node, names) {
|
|
43
|
+
if (node.type !== "CallExpression")
|
|
44
|
+
return false;
|
|
45
|
+
const direct = moduleCallName(node, names, "Effect");
|
|
46
|
+
if (direct !== null && operationConstructors.has(direct))
|
|
47
|
+
return true;
|
|
48
|
+
const curried = moduleCallName(node.callee, names, "Effect");
|
|
49
|
+
return curried !== null && operationConstructors.has(curried);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether `node` runs while the service is constructed. Construction stops at operation boundaries: functions other
|
|
53
|
+
* than Effect.gen generators and suspend/sync thunks, Effect.fn definitions, object-literal properties (operations in
|
|
54
|
+
* the returned service or callbacks in options), and effects a generator defines without yielding.
|
|
55
|
+
*/
|
|
56
|
+
function isInServiceConstruction(node, names) {
|
|
57
|
+
let child = node;
|
|
58
|
+
let current = node.parent;
|
|
59
|
+
let yielded = false;
|
|
60
|
+
while (current !== null) {
|
|
61
|
+
if (isFunction(current)) {
|
|
62
|
+
if (current.parent !== null && isConstructionRoot(current.parent, current, names))
|
|
63
|
+
return true;
|
|
64
|
+
const constructor = effectConstructorOf(current, names);
|
|
65
|
+
if (constructor === "gen") {
|
|
66
|
+
if (!yielded)
|
|
67
|
+
return false;
|
|
68
|
+
yielded = false;
|
|
69
|
+
}
|
|
70
|
+
else if (constructor === null || !transparentThunks.has(constructor)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (isConstructionRoot(current, child, names)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
else if (current.type === "Property" || isOperationDefinition(current, names)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
else if (current.type === "YieldExpression" && current.delegate) {
|
|
81
|
+
yielded = true;
|
|
82
|
+
}
|
|
83
|
+
child = current;
|
|
84
|
+
current = current.parent;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
/** Keep service construction failures typed instead of turning them into defects. */
|
|
89
|
+
export const noOrDieInServiceConstructionRule = defineRule({
|
|
90
|
+
meta: {
|
|
91
|
+
type: "problem",
|
|
92
|
+
docs: {
|
|
93
|
+
description: "Disallow `Effect.orDie` while constructing a service (its make, its static layers, or Layer.effect/unwrap arguments) and `Layer.orDie` anywhere.",
|
|
94
|
+
},
|
|
95
|
+
messages: {
|
|
96
|
+
constructionOrDie: "`Effect.{{member}}` turns a service construction failure into a defect. Classify the failure as a Schema.TaggedError and let it flow through the layer's error channel (`Layer.Layer<Service, ThatError>`), or handle it here with Effect.catchTag.",
|
|
97
|
+
layerOrDie: "`Layer.orDie` hides this layer's construction failures as defects. Leave them in the layer's error channel as Schema.TaggedErrors and handle them where the runtime is built.",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
create(context) {
|
|
101
|
+
if (isTestFile(context.filename))
|
|
102
|
+
return {};
|
|
103
|
+
const names = collectEffectModuleNames(context.sourceCode.ast);
|
|
104
|
+
return {
|
|
105
|
+
MemberExpression(node) {
|
|
106
|
+
if (moduleMemberName(node, names, "Layer") === "orDie") {
|
|
107
|
+
context.report({ node, messageId: "layerOrDie" });
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const member = moduleMemberName(node, names, "Effect");
|
|
111
|
+
if (member === null || !orDieMembers.has(member) || !isInServiceConstruction(node, names))
|
|
112
|
+
return;
|
|
113
|
+
context.report({ node, messageId: "constructionOrDie", data: { member } });
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
});
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// Ported from cevr/effect-oxlint's `no-unbounded-retry` (https://github.com/cevr/effect-oxlint, MIT,
|
|
2
|
+
// oxlint-plugin-effect 0.32.4). This version also accepts the bounds Effect 4.1 offers: `Schedule.upTo`,
|
|
3
|
+
// `Schedule.max` with a bounded schedule, bounded schedule constructors, and a later `Effect.timeout`.
|
|
4
|
+
// A combinator it doesn't know, such as `Schedule.while`, may stop the schedule, so it reads as unknown. Options
|
|
5
|
+
// with no `schedule` and no `times` retry on Effect's default, `Schedule.forever`.
|
|
6
|
+
import { defineRule } from "@oxlint/plugins";
|
|
7
|
+
import { collectEffectModuleNames, keyName, moduleCallName, moduleMemberName, resolveIdentifier, unwrapExpression, unwrapNode, } from "../shared/effect-modules.js";
|
|
8
|
+
/** Constructors whose schedules recur forever. */
|
|
9
|
+
const unboundedConstructors = new Set(["cron", "exponential", "fibonacci", "fixed", "spaced", "windowed"]);
|
|
10
|
+
/** Constructors whose schedules stop after a count or a duration. */
|
|
11
|
+
const boundedConstructors = new Set(["duration", "during", "recurs"]);
|
|
12
|
+
/** Combinators that keep recurring only while every schedule recurs, so one bounded schedule bounds them all. */
|
|
13
|
+
const intersections = new Set(["max"]);
|
|
14
|
+
/** Combinators that keep recurring while any schedule recurs, so every schedule must be bounded. */
|
|
15
|
+
const unions = new Set(["concat", "concatResult", "min"]);
|
|
16
|
+
/** Combinators that change a schedule's delays or outputs but not when it stops. Others may stop it, so they read as unknown. */
|
|
17
|
+
const keepers = new Set([
|
|
18
|
+
"addDelay",
|
|
19
|
+
"as",
|
|
20
|
+
"asVoid",
|
|
21
|
+
"delayed",
|
|
22
|
+
"jittered",
|
|
23
|
+
"map",
|
|
24
|
+
"modifyDelay",
|
|
25
|
+
"passthrough",
|
|
26
|
+
"setInputType",
|
|
27
|
+
"tap",
|
|
28
|
+
"tapInput",
|
|
29
|
+
"tapOutput",
|
|
30
|
+
]);
|
|
31
|
+
/** Combinators that bound the schedule they are applied to. `take` is the Effect 3 name. */
|
|
32
|
+
const limits = new Set(["take", "upTo"]);
|
|
33
|
+
/** Effect operators that bound the elapsed time of everything before them in a pipeline. */
|
|
34
|
+
const timeouts = new Set(["timeout", "timeoutOption", "timeoutOrElse"]);
|
|
35
|
+
const MAX_RESOLUTION_DEPTH = 16;
|
|
36
|
+
/** The initializer of a single `const` binding, or null. */
|
|
37
|
+
function constInitializer(sourceCode, identifier) {
|
|
38
|
+
const variable = resolveIdentifier(sourceCode, identifier);
|
|
39
|
+
if (variable === null || variable.defs.length !== 1)
|
|
40
|
+
return null;
|
|
41
|
+
const [definition] = variable.defs;
|
|
42
|
+
if (definition?.type !== "Variable" || definition.node.type !== "VariableDeclarator")
|
|
43
|
+
return null;
|
|
44
|
+
// A destructured binding is one part of its initializer, not all of it.
|
|
45
|
+
if (definition.node.id.type !== "Identifier")
|
|
46
|
+
return null;
|
|
47
|
+
if (definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const")
|
|
48
|
+
return null;
|
|
49
|
+
return definition.node.init ?? null;
|
|
50
|
+
}
|
|
51
|
+
/** Follow `const` bindings and strip wrappers until the expression is not an identifier. */
|
|
52
|
+
function resolveConst(sourceCode, expression) {
|
|
53
|
+
let current = unwrapExpression(expression);
|
|
54
|
+
for (let depth = 0; depth < MAX_RESOLUTION_DEPTH && current.type === "Identifier"; depth++) {
|
|
55
|
+
const initializer = constInitializer(sourceCode, current);
|
|
56
|
+
if (initializer === null)
|
|
57
|
+
return current;
|
|
58
|
+
current = unwrapExpression(initializer);
|
|
59
|
+
}
|
|
60
|
+
return current;
|
|
61
|
+
}
|
|
62
|
+
/** The expression arguments of a call, or null when one is a spread. */
|
|
63
|
+
function expressionArguments(call) {
|
|
64
|
+
const expressions = [];
|
|
65
|
+
for (const argument of call.arguments) {
|
|
66
|
+
if (argument.type === "SpreadElement")
|
|
67
|
+
return null;
|
|
68
|
+
expressions.push(argument);
|
|
69
|
+
}
|
|
70
|
+
return expressions;
|
|
71
|
+
}
|
|
72
|
+
/** The value of a non-computed property of an object literal, or null when it is missing or `undefined`. */
|
|
73
|
+
function propertyValue(object, name) {
|
|
74
|
+
for (const property of object.properties) {
|
|
75
|
+
if (property.type !== "Property" || property.computed || keyName(property.key) !== name)
|
|
76
|
+
continue;
|
|
77
|
+
const value = unwrapExpression(property.value);
|
|
78
|
+
if (value.type === "Identifier" && value.name === "undefined")
|
|
79
|
+
return null;
|
|
80
|
+
if (value.type === "UnaryExpression" && value.operator === "void")
|
|
81
|
+
return null;
|
|
82
|
+
return property.value;
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const both = (bounds) => {
|
|
87
|
+
if (bounds.includes("bounded"))
|
|
88
|
+
return "bounded";
|
|
89
|
+
return bounds.includes("unknown") ? "unknown" : "unbounded";
|
|
90
|
+
};
|
|
91
|
+
const every = (bounds) => {
|
|
92
|
+
if (bounds.includes("unbounded"))
|
|
93
|
+
return "unbounded";
|
|
94
|
+
return bounds.includes("unknown") ? "unknown" : "bounded";
|
|
95
|
+
};
|
|
96
|
+
/** Whether a `Schedule.upTo` options argument names a limit. Options the rule cannot read count as one. */
|
|
97
|
+
function limitsSchedule(reader, options) {
|
|
98
|
+
if (options === undefined)
|
|
99
|
+
return false;
|
|
100
|
+
const value = resolveConst(reader.sourceCode, options);
|
|
101
|
+
if (value.type !== "ObjectExpression")
|
|
102
|
+
return true;
|
|
103
|
+
return propertyValue(value, "times") !== null || propertyValue(value, "duration") !== null;
|
|
104
|
+
}
|
|
105
|
+
/** Apply `Schedule.<member>(...args)` to a schedule with bound `current`. Other combinators keep the bound. */
|
|
106
|
+
function applyCombinator(reader, current, member, args) {
|
|
107
|
+
if (limits.has(member))
|
|
108
|
+
return limitsSchedule(reader, args[0]) ? "bounded" : current;
|
|
109
|
+
const others = args.map((argument) => scheduleBound(reader, argument));
|
|
110
|
+
if (intersections.has(member))
|
|
111
|
+
return both([current, ...others]);
|
|
112
|
+
if (unions.has(member))
|
|
113
|
+
return every([current, ...others]);
|
|
114
|
+
return keepers.has(member) ? current : "unknown";
|
|
115
|
+
}
|
|
116
|
+
/** Apply one `.pipe` step to the bound of the schedule before it. A step that is not a Schedule combinator is unknown. */
|
|
117
|
+
function applyStep(reader, current, step) {
|
|
118
|
+
const resolved = resolveConst(reader.sourceCode, step);
|
|
119
|
+
// A point-free combinator such as `Schedule.jittered` keeps the bound.
|
|
120
|
+
if (moduleMemberName(resolved, reader.names, "Schedule") !== null)
|
|
121
|
+
return current;
|
|
122
|
+
const member = moduleCallName(resolved, reader.names, "Schedule");
|
|
123
|
+
if (member === null || resolved.type !== "CallExpression")
|
|
124
|
+
return "unknown";
|
|
125
|
+
const args = expressionArguments(resolved);
|
|
126
|
+
return args === null ? "unknown" : applyCombinator(reader, current, member, args);
|
|
127
|
+
}
|
|
128
|
+
/** Whether a schedule expression stops on its own. */
|
|
129
|
+
function scheduleBound(reader, expression) {
|
|
130
|
+
const node = resolveConst(reader.sourceCode, expression);
|
|
131
|
+
if (reader.reading.has(node))
|
|
132
|
+
return "unknown";
|
|
133
|
+
reader.reading.add(node);
|
|
134
|
+
try {
|
|
135
|
+
return readScheduleBound(reader, node);
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
reader.reading.delete(node);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** The bounds of the schedules in an array literal, or null when an element is a spread or a hole. */
|
|
142
|
+
function elementBounds(reader, expression) {
|
|
143
|
+
const array = resolveConst(reader.sourceCode, expression);
|
|
144
|
+
if (array.type !== "ArrayExpression" || array.elements.length === 0)
|
|
145
|
+
return null;
|
|
146
|
+
const bounds = [];
|
|
147
|
+
for (const element of array.elements) {
|
|
148
|
+
if (element === null || element.type === "SpreadElement")
|
|
149
|
+
return null;
|
|
150
|
+
bounds.push(scheduleBound(reader, element));
|
|
151
|
+
}
|
|
152
|
+
return bounds;
|
|
153
|
+
}
|
|
154
|
+
function readScheduleBound(reader, node) {
|
|
155
|
+
const constant = moduleMemberName(node, reader.names, "Schedule");
|
|
156
|
+
if (constant === "forever")
|
|
157
|
+
return "unbounded";
|
|
158
|
+
if (constant === "once")
|
|
159
|
+
return "bounded";
|
|
160
|
+
if (node.type !== "CallExpression")
|
|
161
|
+
return "unknown";
|
|
162
|
+
const callee = unwrapExpression(node.callee);
|
|
163
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "pipe") {
|
|
164
|
+
const args = expressionArguments(node);
|
|
165
|
+
if (args === null || callee.object.type === "Super")
|
|
166
|
+
return "unknown";
|
|
167
|
+
return args.reduce((bound, step) => applyStep(reader, bound, step), scheduleBound(reader, callee.object));
|
|
168
|
+
}
|
|
169
|
+
const member = moduleCallName(node, reader.names, "Schedule");
|
|
170
|
+
if (member === null)
|
|
171
|
+
return "unknown";
|
|
172
|
+
if (unboundedConstructors.has(member))
|
|
173
|
+
return "unbounded";
|
|
174
|
+
if (boundedConstructors.has(member))
|
|
175
|
+
return "bounded";
|
|
176
|
+
const args = expressionArguments(node);
|
|
177
|
+
const [self, ...rest] = args ?? [];
|
|
178
|
+
// Effect 4 combines schedules from one array: `Schedule.max([a, b])`.
|
|
179
|
+
if (self !== undefined && rest.length === 0 && (intersections.has(member) || unions.has(member))) {
|
|
180
|
+
const bounds = elementBounds(reader, self);
|
|
181
|
+
if (bounds === null)
|
|
182
|
+
return "unknown";
|
|
183
|
+
return intersections.has(member) ? both(bounds) : every(bounds);
|
|
184
|
+
}
|
|
185
|
+
// Data-first combinators take the schedule first: `Schedule.upTo(schedule, { times: 3 })`.
|
|
186
|
+
if (self === undefined || rest.length === 0)
|
|
187
|
+
return "unknown";
|
|
188
|
+
return applyCombinator(reader, scheduleBound(reader, self), member, rest);
|
|
189
|
+
}
|
|
190
|
+
function retryCall(call, names) {
|
|
191
|
+
const effectMember = moduleCallName(call, names, "Effect");
|
|
192
|
+
let arity = null;
|
|
193
|
+
if (effectMember === "retry")
|
|
194
|
+
arity = 1;
|
|
195
|
+
else if (effectMember === "retryOrElse")
|
|
196
|
+
arity = 2;
|
|
197
|
+
else if (moduleCallName(call, names, "Stream") === "retry" || moduleCallName(call, names, "HttpClient") === "retryTransient")
|
|
198
|
+
arity = 1;
|
|
199
|
+
if (arity === null)
|
|
200
|
+
return null;
|
|
201
|
+
const args = expressionArguments(call);
|
|
202
|
+
if (args === null || args.length < arity)
|
|
203
|
+
return null;
|
|
204
|
+
// The data-first form takes the effect, stream, or client first.
|
|
205
|
+
const policy = args.length === arity ? args[0] : args[1];
|
|
206
|
+
if (policy === undefined)
|
|
207
|
+
return null;
|
|
208
|
+
return { policy, timeoutApplies: effectMember !== null };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The schedule a policy retries on, or null when the policy bounds attempts with `times` or cannot be read.
|
|
212
|
+
* Options with neither retry on Effect's default, `Schedule.forever`.
|
|
213
|
+
*/
|
|
214
|
+
function policySchedule(sourceCode, policy) {
|
|
215
|
+
const value = resolveConst(sourceCode, policy);
|
|
216
|
+
if (value.type !== "ObjectExpression")
|
|
217
|
+
return value;
|
|
218
|
+
if (value.properties.some((property) => property.type === "SpreadElement"))
|
|
219
|
+
return null;
|
|
220
|
+
if (propertyValue(value, "times") !== null)
|
|
221
|
+
return null;
|
|
222
|
+
return propertyValue(value, "schedule") ?? "forever";
|
|
223
|
+
}
|
|
224
|
+
const isPipeCallee = (callee) => (callee.type === "Identifier" && callee.name === "pipe") ||
|
|
225
|
+
(callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "pipe");
|
|
226
|
+
/** Whether a call's arguments after the first run as a pipeline: `Effect.fn(body, ...steps)` or `Effect.fn("name")(body, ...steps)`. */
|
|
227
|
+
function isEffectFnPipeline(call, names) {
|
|
228
|
+
const callee = unwrapExpression(call.callee);
|
|
229
|
+
const fn = callee.type === "CallExpression" ? unwrapExpression(callee.callee) : callee;
|
|
230
|
+
const member = moduleMemberName(fn, names, "Effect");
|
|
231
|
+
return member === "fn" || member === "fnUntraced";
|
|
232
|
+
}
|
|
233
|
+
/** Whether `node` is a data-first timeout of `inner`: `Effect.timeout(inner, "5 seconds")`. */
|
|
234
|
+
function isTimeoutOf(node, inner, names) {
|
|
235
|
+
const member = moduleCallName(node, names, "Effect");
|
|
236
|
+
return member !== null && timeouts.has(member) && node.arguments.length >= 2 && node.arguments[0] === inner;
|
|
237
|
+
}
|
|
238
|
+
/** Whether a later step of the pipeline that `call` runs in is an Effect timeout. */
|
|
239
|
+
function hasLaterTimeout(call, names) {
|
|
240
|
+
const isTimeoutStep = (step) => {
|
|
241
|
+
const member = moduleCallName(unwrapNode(step), names, "Effect");
|
|
242
|
+
return member !== null && timeouts.has(member);
|
|
243
|
+
};
|
|
244
|
+
let current = call;
|
|
245
|
+
for (;;) {
|
|
246
|
+
let parent = current.parent;
|
|
247
|
+
while (parent !== null && unwrapNode(parent) !== parent) {
|
|
248
|
+
current = parent;
|
|
249
|
+
parent = parent.parent;
|
|
250
|
+
}
|
|
251
|
+
if (parent === null)
|
|
252
|
+
return false;
|
|
253
|
+
if (parent.type === "MemberExpression" && parent.object === current && isPipeCallee(parent)) {
|
|
254
|
+
const pipeCall = parent.parent;
|
|
255
|
+
if (pipeCall?.type !== "CallExpression" || pipeCall.callee !== parent)
|
|
256
|
+
return false;
|
|
257
|
+
if (pipeCall.arguments.some(isTimeoutStep))
|
|
258
|
+
return true;
|
|
259
|
+
current = pipeCall;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (parent.type !== "CallExpression")
|
|
263
|
+
return false;
|
|
264
|
+
if (isTimeoutOf(parent, current, names))
|
|
265
|
+
return true;
|
|
266
|
+
const index = parent.arguments.findIndex((argument) => argument === current);
|
|
267
|
+
if (index < 0)
|
|
268
|
+
return false;
|
|
269
|
+
const callee = unwrapExpression(parent.callee);
|
|
270
|
+
if (!isPipeCallee(callee) && !isEffectFnPipeline(parent, names))
|
|
271
|
+
return false;
|
|
272
|
+
if (parent.arguments.slice(index + 1).some(isTimeoutStep))
|
|
273
|
+
return true;
|
|
274
|
+
current = parent;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** Require retry policies to stop after a number of attempts or an elapsed time. */
|
|
278
|
+
export const noUnboundedRetryRule = defineRule({
|
|
279
|
+
meta: {
|
|
280
|
+
type: "problem",
|
|
281
|
+
docs: {
|
|
282
|
+
description: "Require Effect retry policies to have an attempt or duration bound.",
|
|
283
|
+
},
|
|
284
|
+
messages: {
|
|
285
|
+
unboundedRetry: "This retry has no limit on attempts or time. Bound it with `times`, `Schedule.upTo({ times })` or `Schedule.upTo({ duration })`, `Schedule.max([schedule, Schedule.recurs(n)])`, or a later `Effect.timeout`.",
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
create(context) {
|
|
289
|
+
const names = collectEffectModuleNames(context.sourceCode.ast);
|
|
290
|
+
const reader = { sourceCode: context.sourceCode, names, reading: new Set() };
|
|
291
|
+
return {
|
|
292
|
+
CallExpression(node) {
|
|
293
|
+
const retry = retryCall(node, names);
|
|
294
|
+
if (retry === null)
|
|
295
|
+
return;
|
|
296
|
+
const schedule = policySchedule(context.sourceCode, retry.policy);
|
|
297
|
+
if (schedule === null)
|
|
298
|
+
return;
|
|
299
|
+
if (schedule !== "forever" && scheduleBound(reader, schedule) !== "unbounded")
|
|
300
|
+
return;
|
|
301
|
+
if (retry.timeoutApplies && hasLaterTimeout(node, names))
|
|
302
|
+
return;
|
|
303
|
+
context.report({ node, messageId: "unboundedRetry" });
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
},
|
|
307
|
+
});
|