@opsydyn/oxlint-effect 2.4.0 → 2.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opsydyn/oxlint-effect",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Oxlint plugin rules for Effect TypeScript code shape constraints.",
5
5
  "author": "Alan Currie",
6
6
  "license": "MIT",
@@ -72,7 +72,7 @@
72
72
  "size-limit": [
73
73
  {
74
74
  "path": "dist/index.mjs",
75
- "limit": "31 KB"
75
+ "limit": "33 KB"
76
76
  }
77
77
  ],
78
78
  "publishConfig": {
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: oxlint-effect
3
- description: Use when configuring @opsydyn/oxlint-effect, investigating linteffect diagnostics, or repairing Effect domain-modeling and error-modeling code in a consuming project.
3
+ description: Use when configuring @opsydyn/oxlint-effect, investigating linteffect diagnostics, or reviewing Effect domain modeling, error modeling, and service provision in a consuming project.
4
4
  ---
5
5
 
6
6
  # Oxlint Effect
@@ -114,6 +114,14 @@ owner and migrate callers whenever the success or failure channel changes.
114
114
  The paired corpus covers all exported DDD rules with representative failures
115
115
  and passing controls; it does not prove every detection variant or agent repair.
116
116
 
117
+ ## Review Service Provision
118
+
119
+ Read [service provision](references/service-provision.md) before removing a
120
+ local `Effect.provideService`. Prove the intended execution environment and
121
+ requirements, preserve intentional pins and local overrides, and check the
122
+ resource's owning scope separately. The paired examples are review-only, not a
123
+ new lint policy.
124
+
117
125
  ## Verify And Report
118
126
 
119
127
  Run the consumer's lint and typecheck commands plus relevant behavioural tests.
@@ -0,0 +1,66 @@
1
+ import { Effect, Layer } from "effect";
2
+ import { Client, ClientClosed, ClientReader, ClientReaderLive, capturePinnedRead, readAmbient, readWithOverride } from "./service-provision.good";
3
+ import { captureWithoutPin, readImmediately } from "./service-provision.review";
4
+
5
+ function check(actual: unknown, expected: unknown, contract: string): void {
6
+ if (actual !== expected) throw new Error(`Service provision contract failed: ${contract}`);
7
+ }
8
+
9
+ const first = { read: Effect.succeed("first") };
10
+ const second = { read: Effect.succeed("second") };
11
+ const underFirst = Effect.provideService(Client, first);
12
+ const underSecond = Effect.provideService(Client, second);
13
+
14
+ // Same-frame redundancy: removing one redundant provision preserves either caller.
15
+ for (const [provide, expected] of [[underFirst, "first"], [underSecond, "second"]] as const) {
16
+ check(Effect.runSync(provide(readImmediately)), expected, "immediate re-provision");
17
+ check(Effect.runSync(provide(readAmbient)), expected, "ambient repair");
18
+ }
19
+
20
+ // Deferred work distinguishes capture from execution.
21
+ const pinned = Effect.runSync(underFirst(capturePinnedRead));
22
+ const unpinned = Effect.runSync(underFirst(captureWithoutPin));
23
+ check(Effect.runSync(underSecond(pinned)), "first", "deferred pin survives ambient change");
24
+ check(Effect.runSync(underSecond(unpinned)), "second", "dropping the pin changes behaviour");
25
+ check(Effect.runSync(underSecond(Effect.suspend(() => pinned))), "first", "pin survives nested callback");
26
+ check(Effect.runSync(underSecond(Effect.suspend(() => unpinned))), "second", "ambient callback resolves at execution");
27
+
28
+ // Layer.provide supplies Client at construction, but does not export it to callers.
29
+ const readerLayer = ClientReaderLive.pipe(Layer.provide(Layer.succeed(Client, first)));
30
+ const readFromLayer = Effect.flatMap(ClientReader, reader => reader.read()).pipe(Effect.provide(readerLayer));
31
+ check(Effect.runSync(readFromLayer), "first", "closed layer-built method");
32
+ check(Effect.runSync(underSecond(readFromLayer)), "first", "layer-built pin beats caller override");
33
+
34
+ const observations = Effect.gen(function* () {
35
+ const before = yield* readAmbient;
36
+ const overridden = yield* readWithOverride(second);
37
+ const after = yield* readAmbient;
38
+ return { before, overridden, after };
39
+ });
40
+ const observed = Effect.runSync(underFirst(observations));
41
+ check(observed.before, "first", "ambient before override");
42
+ check(observed.overridden, "second", "explicit dynamic override");
43
+ check(observed.after, "first", "ambient restored after override");
44
+
45
+ // A pin is not a lease. Keep this deliberately escaped operation as a negative control.
46
+ let open = false;
47
+ let releases = 0;
48
+ const closed = new ClientClosed({ reason: "scope closed" });
49
+ const acquireClient = Effect.acquireRelease(
50
+ Effect.sync(() => {
51
+ open = true;
52
+ return { read: Effect.suspend(() => open ? Effect.succeed("scoped") : Effect.fail(closed)) };
53
+ }),
54
+ () => Effect.sync(() => {
55
+ open = false;
56
+ releases++;
57
+ }),
58
+ );
59
+ const escaped = Effect.runSync(Effect.scoped(Effect.gen(function* () {
60
+ const client = yield* acquireClient;
61
+ const operation = readWithOverride(client);
62
+ check(yield* operation, "scoped", "pin works while resource is live");
63
+ return operation;
64
+ })));
65
+ check(releases, 1, "capture does not delay finalization");
66
+ check(Effect.runSync(Effect.flip(escaped)), closed, "closed resource cannot be reused through a pin");
@@ -0,0 +1,34 @@
1
+ // Effect 4: preserve a local pin only when the caller contract needs it.
2
+ import { Context, Data, Effect, Layer } from "effect";
3
+
4
+ export class ClientClosed extends Data.TaggedError("ClientClosed")<{ readonly reason: string }> {}
5
+
6
+ export interface ClientInstance {
7
+ readonly read: Effect.Effect<string, ClientClosed>;
8
+ }
9
+
10
+ export class Client extends Context.Service<Client, ClientInstance>()("skill/Client") {}
11
+
12
+ export class ClientReader extends Context.Service<
13
+ ClientReader,
14
+ { readonly read: () => Effect.Effect<string, ClientClosed> }
15
+ >()("skill/ClientReader") {}
16
+
17
+ // Ambient by design: the caller supplies Client when it executes the operation.
18
+ export const readAmbient = Effect.flatMap(Client, client => client.read);
19
+
20
+ // Intentional pin: a returned operation keeps the instance selected during capture.
21
+ export const capturePinnedRead = Effect.gen(function* () {
22
+ const client = yield* Client;
23
+ return readAmbient.pipe(Effect.provideService(Client, client));
24
+ });
25
+
26
+ // Construction owns Client; consumers of this reader do not have to provide it.
27
+ export const ClientReaderLive = Layer.effect(
28
+ ClientReader,
29
+ Effect.map(capturePinnedRead, read => ({ read: () => read })),
30
+ );
31
+
32
+ // A legitimate local override, such as a per-request implementation.
33
+ export const readWithOverride = (client: ClientInstance) =>
34
+ readAmbient.pipe(Effect.provideService(Client, client));
@@ -0,0 +1,17 @@
1
+ import { Effect } from "effect";
2
+ import { Client, readAmbient } from "./service-provision.good";
3
+
4
+ // REVIEW-ONLY: immediate execution under the same environment makes this
5
+ // provision redundant. Use readAmbient in this example.
6
+ export const readImmediately = Effect.gen(function* () {
7
+ const client = yield* Client;
8
+ return yield* readAmbient.pipe(Effect.provideService(Client, client));
9
+ });
10
+
11
+ // REVIEW-ONLY: this is the incorrect "repair" of capturePinnedRead.
12
+ // Returning the ambient operation drops the pin, even with the same tag
13
+ // and a successful initial lookup. Its later caller must supply Client again.
14
+ export const captureWithoutPin = Effect.gen(function* () {
15
+ yield* Client;
16
+ return readAmbient;
17
+ });
@@ -0,0 +1,19 @@
1
+ // Compile-only contracts. Do not execute this file.
2
+ import { Effect } from "effect";
3
+ import { Client, ClientClosed, capturePinnedRead, readAmbient } from "./service-provision.good";
4
+ import { captureWithoutPin, readImmediately } from "./service-provision.review";
5
+
6
+ type Read<R> = Effect.Effect<string, ClientClosed, R>;
7
+ const ambient: Read<Client> = readAmbient;
8
+ const immediate: Read<Client> = readImmediately;
9
+ const pinned: Effect.Effect<Read<never>, never, Client> = capturePinnedRead;
10
+ const unpinned: Effect.Effect<Read<Client>, never, Client> = captureWithoutPin;
11
+
12
+ // @ts-expect-error A deferred unpinned operation still requires Client at execution.
13
+ const lostPin: typeof pinned = unpinned;
14
+ // @ts-expect-error Pinning the returned operation does not close the initial lookup.
15
+ const missingCaptureDependency: Effect.Effect<Read<never>> = capturePinnedRead;
16
+ // @ts-expect-error A closed environment does not erase typed failures.
17
+ const missingFailure: Effect.Effect<Effect.Effect<string>, never, Client> = capturePinnedRead;
18
+
19
+ void [ambient, immediate, pinned, unpinned, lostPin, missingCaptureDependency, missingFailure];
@@ -0,0 +1,66 @@
1
+ import { Effect, Layer } from "effect";
2
+ import { Client, ClientClosed, ClientReader, ClientReaderLive, capturePinnedRead, readAmbient, readWithOverride } from "./service-provision.good";
3
+ import { captureWithoutPin, readImmediately } from "./service-provision.review";
4
+
5
+ function check(actual: unknown, expected: unknown, contract: string): void {
6
+ if (actual !== expected) throw new Error(`Service provision contract failed: ${contract}`);
7
+ }
8
+
9
+ const first = Client.make({ read: Effect.succeed("first") });
10
+ const second = Client.make({ read: Effect.succeed("second") });
11
+ const underFirst = Effect.provideService(Client, first);
12
+ const underSecond = Effect.provideService(Client, second);
13
+
14
+ // Same-frame redundancy: removing one redundant provision preserves either caller.
15
+ for (const [provide, expected] of [[underFirst, "first"], [underSecond, "second"]] as const) {
16
+ check(Effect.runSync(provide(readImmediately)), expected, "immediate re-provision");
17
+ check(Effect.runSync(provide(readAmbient)), expected, "ambient repair");
18
+ }
19
+
20
+ // Deferred work distinguishes capture from execution.
21
+ const pinned = Effect.runSync(underFirst(capturePinnedRead));
22
+ const unpinned = Effect.runSync(underFirst(captureWithoutPin));
23
+ check(Effect.runSync(underSecond(pinned)), "first", "deferred pin survives ambient change");
24
+ check(Effect.runSync(underSecond(unpinned)), "second", "dropping the pin changes behaviour");
25
+ check(Effect.runSync(underSecond(Effect.suspend(() => pinned))), "first", "pin survives nested callback");
26
+ check(Effect.runSync(underSecond(Effect.suspend(() => unpinned))), "second", "ambient callback resolves at execution");
27
+
28
+ // Layer.provide supplies Client at construction, but does not export it to callers.
29
+ const readerLayer = ClientReaderLive.pipe(Layer.provide(Layer.succeed(Client, first)));
30
+ const readFromLayer = Effect.flatMap(ClientReader, reader => reader.read()).pipe(Effect.provide(readerLayer));
31
+ check(Effect.runSync(readFromLayer), "first", "closed layer-built method");
32
+ check(Effect.runSync(underSecond(readFromLayer)), "first", "layer-built pin beats caller override");
33
+
34
+ const observations = Effect.gen(function* () {
35
+ const before = yield* readAmbient;
36
+ const overridden = yield* readWithOverride(second);
37
+ const after = yield* readAmbient;
38
+ return { before, overridden, after };
39
+ });
40
+ const observed = Effect.runSync(underFirst(observations));
41
+ check(observed.before, "first", "ambient before override");
42
+ check(observed.overridden, "second", "explicit dynamic override");
43
+ check(observed.after, "first", "ambient restored after override");
44
+
45
+ // A pin is not a lease. Keep this deliberately escaped operation as a negative control.
46
+ let open = false;
47
+ let releases = 0;
48
+ const closed = new ClientClosed({ reason: "scope closed" });
49
+ const acquireClient = Effect.acquireRelease(
50
+ Effect.sync(() => {
51
+ open = true;
52
+ return Client.make({ read: Effect.suspend(() => open ? Effect.succeed("scoped") : Effect.fail(closed)) });
53
+ }),
54
+ () => Effect.sync(() => {
55
+ open = false;
56
+ releases++;
57
+ }),
58
+ );
59
+ const escaped = Effect.runSync(Effect.scoped(Effect.gen(function* () {
60
+ const client = yield* acquireClient;
61
+ const operation = readWithOverride(client);
62
+ check(yield* operation, "scoped", "pin works while resource is live");
63
+ return operation;
64
+ })));
65
+ check(releases, 1, "capture does not delay finalization");
66
+ check(Effect.runSync(Effect.flip(escaped)), closed, "closed resource cannot be reused through a pin");
@@ -0,0 +1,36 @@
1
+ // Legacy Effect 3: preserve a local pin only when the caller contract needs it.
2
+ import { Data, Effect } from "effect";
3
+
4
+ export class ClientClosed extends Data.TaggedError("ClientClosed")<{ readonly reason: string }> {}
5
+
6
+ export interface ClientInstance {
7
+ readonly read: Effect.Effect<string, ClientClosed>;
8
+ }
9
+
10
+ const defaultClient: ClientInstance = {
11
+ read: Effect.fail(new ClientClosed({ reason: "Use an explicitly supplied fixture client" })),
12
+ };
13
+ export class Client extends Effect.Service<Client>()("skill/Client", {
14
+ accessors: true,
15
+ succeed: defaultClient,
16
+ }) {}
17
+
18
+ // Ambient by design: the caller supplies Client when it executes the operation.
19
+ export const readAmbient = Effect.flatMap(Client, client => client.read);
20
+
21
+ // Intentional pin: a returned operation keeps the instance selected during capture.
22
+ export const capturePinnedRead = Effect.gen(function* () {
23
+ const client = yield* Client;
24
+ return readAmbient.pipe(Effect.provideService(Client, client));
25
+ });
26
+
27
+ // Construction owns Client; consumers of this reader do not have to provide it.
28
+ export class ClientReader extends Effect.Service<ClientReader>()("skill/ClientReader", {
29
+ accessors: true,
30
+ effect: Effect.map(capturePinnedRead, read => ({ read: () => read })),
31
+ }) {}
32
+ export const ClientReaderLive = ClientReader.Default;
33
+
34
+ // A legitimate local override, such as a per-request implementation.
35
+ export const readWithOverride = (client: Client) =>
36
+ readAmbient.pipe(Effect.provideService(Client, client));
@@ -0,0 +1,17 @@
1
+ import { Effect } from "effect";
2
+ import { Client, readAmbient } from "./service-provision.good";
3
+
4
+ // REVIEW-ONLY: immediate execution under the same environment makes this
5
+ // provision redundant. Use readAmbient in this example.
6
+ export const readImmediately = Effect.gen(function* () {
7
+ const client = yield* Client;
8
+ return yield* readAmbient.pipe(Effect.provideService(Client, client));
9
+ });
10
+
11
+ // REVIEW-ONLY: this is the incorrect "repair" of capturePinnedRead.
12
+ // Returning the ambient operation drops the pin, even with the same tag
13
+ // and a successful initial lookup. Its later caller must supply Client again.
14
+ export const captureWithoutPin = Effect.gen(function* () {
15
+ yield* Client;
16
+ return readAmbient;
17
+ });
@@ -0,0 +1,19 @@
1
+ // Compile-only contracts. Do not execute this file.
2
+ import { Effect } from "effect";
3
+ import { Client, ClientClosed, capturePinnedRead, readAmbient } from "./service-provision.good";
4
+ import { captureWithoutPin, readImmediately } from "./service-provision.review";
5
+
6
+ type Read<R> = Effect.Effect<string, ClientClosed, R>;
7
+ const ambient: Read<Client> = readAmbient;
8
+ const immediate: Read<Client> = readImmediately;
9
+ const pinned: Effect.Effect<Read<never>, never, Client> = capturePinnedRead;
10
+ const unpinned: Effect.Effect<Read<Client>, never, Client> = captureWithoutPin;
11
+
12
+ // @ts-expect-error A deferred unpinned operation still requires Client at execution.
13
+ const lostPin: typeof pinned = unpinned;
14
+ // @ts-expect-error Pinning the returned operation does not close the initial lookup.
15
+ const missingCaptureDependency: Effect.Effect<Read<never>> = capturePinnedRead;
16
+ // @ts-expect-error A closed environment does not erase typed failures.
17
+ const missingFailure: Effect.Effect<Effect.Effect<string>, never, Client> = capturePinnedRead;
18
+
19
+ void [ambient, immediate, pinned, unpinned, lostPin, missingCaptureDependency, missingFailure];
@@ -0,0 +1,56 @@
1
+ # Service Provision And Environment Pinning
2
+
3
+ Use this when an operation looks up a service and passes that same instance to
4
+ `Effect.provideService`. This is a review question, not a new lint rule. Matching
5
+ tag and instance identities tell you what was passed, not whether the child
6
+ would otherwise run with that instance.
7
+
8
+ ## Identify The Owner
9
+
10
+ - **Ambient dependency:** Keep the service in the operation's requirements
11
+ (`R`) when the caller should choose it at execution. Compose its implementing
12
+ Layer at the application or test boundary. In the immediate, same-environment
13
+ example, using `readAmbient` instead of yielding and re-providing the instance
14
+ preserves both the result and the dependency requirement.
15
+ - **Intentional pin:** Preserve provision when a returned operation or layer-built
16
+ method should retain the instance selected at construction. The capture still
17
+ requires `Client`, but its returned operation has `R = never`. Removing
18
+ provision changes that inner requirement to `Client` and lets a later caller's
19
+ environment change the implementation. A layer's construction dependency is
20
+ not necessarily available to its consumers.
21
+ - **Local override:** Keep narrowly scoped replacements, such as per-request
22
+ clients and test doubles. The surrounding environment should still resolve its
23
+ original implementation when the override finishes.
24
+
25
+ Do not remove sole provision based on spelling, binding identity, or a passing
26
+ test that always uses the same environment. Leave escaped operations, opaque
27
+ helpers, callbacks, context updates and layer-local dependencies review-only
28
+ unless their execution context and the consumer contract are established. Even
29
+ in apparently redundant cases, check requirement types, failure behaviour,
30
+ resource ownership and callers before suggesting a change. Do not apply an
31
+ autofix or widen an API's requirements simply to remove a provision.
32
+
33
+ ## Pinning Does Not Extend Scope
34
+
35
+ Provision selects an implementation. It does not keep an acquired resource open
36
+ after its owning scope closes. Keep acquisition, use and release in the owning
37
+ scope, or have a longer-lived owner supply the service. A returned Effect can
38
+ have `R = never` and still contain a reference to a closed resource.
39
+
40
+ ## Examples And Regression Checks
41
+
42
+ | Major | Review-only counterexamples | Valid alternatives | Contracts |
43
+ | --- | --- | --- | --- |
44
+ | Effect 4 | [review](../assets/effect4/service-provision.review.ts) | [good](../assets/effect4/service-provision.good.ts) | [runtime](../assets/effect4/service-provision.contracts.ts), [types](../assets/effect4/service-provision.types.ts) |
45
+ | Legacy Effect 3 | [review](../assets/service-provision.review.ts) | [good](../assets/service-provision.good.ts) | [runtime](../assets/service-provision.contracts.ts), [types](../assets/service-provision.types.ts) |
46
+
47
+ The runtime checks exercise different construction and execution environments,
48
+ an intentionally unpinned control, override restoration, and a finalized
49
+ resource that remains closed even through a pin. The compile-only checks
50
+ distinguish the capture requirement, the returned operation's requirement, and
51
+ its typed failure channel. `REVIEW-ONLY` comments identify reasoning examples,
52
+ not expected diagnostics.
53
+
54
+ See the official [Effect 4 services](https://effect.website/docs/v4/requirements-management/services),
55
+ [Effect 3 services](https://effect.website/docs/v3/requirements-management/services),
56
+ and [Scope](https://effect.website/docs/v4/resource-management/scope) guides.