Skip to content

Commit ec499f2

Browse files
refactor(rules): move fix-only work from rule analysis into fixables (#1024)
Apply the "Fix work stays in the fixable" coding standard to every rule with a quick fix. Analyze functions now compute only what the diagnostic needs; scans, fresh names, rewrite-safety checks and node slices that only the fix reads run in the fixable, when code actions are requested. Three type-argument gates move into the fixable, because the typed call can still be rewritten by hand: provideLayerSucceedToProvideService (Layer.succeed/effect<...>), provideMergeAllToArray (Effect.provide<...> and Layer.mergeAll<...>) and the timeout step of timeoutCatchTagToTimeoutOrElse. Those shapes are now reported without a fix. Every other diagnostic is unchanged. The Rust port mirrors all changes. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a764f17 commit ec499f2

134 files changed

Lines changed: 2617 additions & 1946 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/fix-work-in-fixables.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@effect/tsgo": patch
3+
---
4+
5+
Move fix-only work out of rule analysis into the quick fixes, so it runs only when the editor asks for code actions instead of on every check. This covers 27 rules, among them `yieldInCallArgument`, `failCauseToConstructor`, `suspendFailToFailSync`, `catchTagToCatchReason`, `mapMapErrorToMapBoth` and `unnecessaryEffectGen`. Their diagnostics are unchanged.
6+
7+
Three rules now report explicitly typed calls that they used to skip, because those calls can still be rewritten by hand. They get no quick fix:
8+
9+
- `provideLayerSucceedToProvideService`: `Effect.provide(Layer.succeed<I, S>(Service, implementation))` (rewrite: `Effect.provideService<I, S>(Service, implementation)`), and the same for `Layer.effect<…>`
10+
- `provideMergeAllToArray`: `Effect.provide<…>(Layer.mergeAll(...))` and `Effect.provide(Layer.mergeAll<…>(...))`
11+
- `timeoutCatchTagToTimeoutOrElse`: explicit type arguments on the `Effect.timeout` step
12+
13+
```ts
14+
// now reported, without a quick fix
15+
program.pipe(Effect.provide(Layer.succeed<Service, { readonly value: number }>(Service, implementation)))
16+
```

‎crates/ets_fixables/src/acquire_release_disposable.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use ets_shim::ast::{self, NodeFlags};
55
use ets_shim::diagnostics::{self as tsdiag, Message};
66
use ets_shim::ls::CodeAction;
77

8+
use crate::all_of_map_to_for_each::has_call_type_arguments;
9+
810
/// Rust-only: the fixable's message (Go spells it out in `ErrorCodes`).
911
static MESSAGE: &Message = &tsdiag::EFFECT_ACQUIRE_DISPOSABLE_EXPRESSES_THIS_DISPOSABLE_RESOURCE_ACQUISITION_MORE_DIRECTLY_THAN_EFFECT_ACQUIRE_RELEASE_WITH_A_MANUAL_DISPOSAL_FINALIZER_EFFECT_ACQUIRE_RELEASE_DISPOSABLE;
1012

@@ -17,15 +19,15 @@ pub static ACQUIRE_RELEASE_DISPOSABLE_FIX: LazyLock<Fixable> = LazyLock::new(||
1719
run: run_acquire_release_disposable_fix,
1820
});
1921

20-
// go: internal/fixables/acquire_release_disposable.go:23 runAcquireReleaseDisposableFix e35812d6dc7ccfae
22+
// go: internal/fixables/acquire_release_disposable.go:23 runAcquireReleaseDisposableFix b8e7ba3c1c194f4d
2123
fn run_acquire_release_disposable_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction> {
2224
let source_file = ctx.source_file;
2325
for m in ets_rules::analyze_acquire_release_disposable(&mut ctx.type_parser, Some(source_file))
2426
{
2527
if !m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location) {
2628
continue;
2729
}
28-
if m.has_type_arguments {
30+
if has_call_type_arguments(Some(m.call_node.as_call_expression())) {
2931
continue;
3032
}
3133

‎crates/ets_fixables/src/all_of_map_to_for_each.rs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::LazyLock;
22

33
use ets_fixable::{Context, FixAction, Fixable};
4-
use ets_shim::ast::{self, NodeFlags};
4+
use ets_shim::ast::{self, CallExpression, NodeFlags};
55
use ets_shim::diagnostics::{self as tsdiag, Message};
66
use ets_shim::ls::CodeAction;
77

@@ -17,19 +17,23 @@ pub static ALL_OF_MAP_TO_FOR_EACH_FIX: LazyLock<Fixable> = LazyLock::new(|| Fixa
1717
run: run_all_of_map_to_for_each_fix,
1818
});
1919

20-
// go: internal/fixables/all_of_map_to_for_each.go:23 runAllOfMapToForEachFix ae0ac2637d074bfa
20+
// go: internal/fixables/all_of_map_to_for_each.go:23 runAllOfMapToForEachFix 092805e4f38f335d
2121
fn run_all_of_map_to_for_each_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction> {
2222
let source_file = ctx.source_file;
2323
for m in ets_rules::analyze_all_of_map_to_for_each(&mut ctx.type_parser, source_file) {
24-
if !m.can_fix {
24+
if m.call_node.is_none() {
2525
continue;
2626
}
2727
if !m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location) {
2828
continue;
2929
}
3030
// The generic parameter lists of Array#map, Effect.all, and Effect.forEach
3131
// differ, so an explicit type argument cannot be moved safely.
32-
if m.has_type_arguments {
32+
if has_call_type_arguments(m.call_node.map(|call_node| call_node.as_call_expression()))
33+
|| has_call_type_arguments(
34+
m.callback.parent().map(|parent| parent.as_call_expression()),
35+
)
36+
{
3337
continue;
3438
}
3539

@@ -63,3 +67,8 @@ fn run_all_of_map_to_for_each_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction>
6367
}
6468
Vec::new()
6569
}
70+
71+
// go: internal/fixables/all_of_map_to_for_each.go:61 hasCallTypeArguments 26b3e9eb8b75eeae
72+
pub(crate) fn has_call_type_arguments(call: Option<&CallExpression>) -> bool {
73+
call.is_some_and(|call| call.type_arguments().is_some_and(|t| !t.nodes().is_empty()))
74+
}

‎crates/ets_fixables/src/catch_all_tag_dispatch_to_catch_tag.rs‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ets_shim::scanner;
1313
/// Rust-only: the fixable's message (Go spells it out in `ErrorCodes`).
1414
static MESSAGE: &Message = &tsdiag::BRANCHING_ON_0_TAG_INSIDE_EFFECT_1_HAND_ROLLS_TAGGED_ERROR_DISPATCH_USE_EFFECT_CATCH_TAG_OR_EFFECT_CATCH_TAGS_WHICH_RE_FAIL_UNMATCHED_ERRORS_AUTOMATICALLY_EFFECT_CATCH_ALL_TAG_DISPATCH_TO_CATCH_TAG;
1515

16-
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:15 CatchAllTagDispatchToCatchTagFix f506792fa2f3559e
16+
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:16 CatchAllTagDispatchToCatchTagFix f506792fa2f3559e
1717
pub static CATCH_ALL_TAG_DISPATCH_TO_CATCH_TAG_FIX: LazyLock<Fixable> = LazyLock::new(|| Fixable {
1818
name: "catchAllTagDispatchToCatchTag",
1919
description: "Replace hand-rolled tagged error dispatch with Effect.catchTag or Effect.catchTags",
@@ -22,21 +22,47 @@ pub static CATCH_ALL_TAG_DISPATCH_TO_CATCH_TAG_FIX: LazyLock<Fixable> = LazyLock
2222
run: run_catch_all_tag_dispatch_to_catch_tag_fix,
2323
});
2424

25-
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:21 runCatchAllTagDispatchToCatchTagFix 7cba469b0aa3a0ad
25+
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:22 runCatchAllTagDispatchToCatchTagFix fcc20ab3d72176e2
26+
//
27+
// Rust: Go's edit closure asks the type parser, per branch, whether the branch result reads the parameter. The
28+
// parser is borrowed mutably by the context, which `new_fix_action` borrows, so those scans (which read only the
29+
// source and the checker, never the tracker) run just before `new_fix_action`, in branch order.
2630
fn run_catch_all_tag_dispatch_to_catch_tag_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction> {
2731
let source_file = ctx.source_file;
2832
for m in ets_rules::analyze_catch_all_tag_dispatch_to_catch_tag(
2933
&mut ctx.type_parser,
3034
Some(source_file),
3135
) {
32-
if !m.can_fix || (!m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location)) {
36+
if !m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location) {
37+
continue;
38+
}
39+
// The new handlers rebind the parameter per branch, which only holds for
40+
// an arrow function whose parameter is never reassigned.
41+
if m.handler.kind() != Kind::ArrowFunction
42+
|| ctx.checker().is_symbol_assigned(m.parameter_symbol)
43+
{
3344
continue;
3445
}
46+
let uses_parameter: Vec<bool> = m
47+
.branches
48+
.iter()
49+
.map(|branch| {
50+
ets_rules::conditional_refail_node_contains_parameter(
51+
&mut ctx.type_parser,
52+
Some(branch.result),
53+
Some(m.parameter_symbol),
54+
)
55+
})
56+
.collect();
3557
let action = ctx.new_fix_action(FixAction {
3658
description: "Replace with Effect.catchTag or Effect.catchTags".to_string(),
3759
run: Box::new(move |tracker| {
38-
let (callee, arguments) =
39-
build_catch_all_tag_dispatch_replacement(tracker, source_file, &m);
60+
let (callee, arguments) = build_catch_all_tag_dispatch_replacement(
61+
tracker,
62+
source_file,
63+
&m,
64+
&uses_parameter,
65+
);
4066
if callee.is_some() && arguments.is_some() {
4167
tracker.replace_piping_flow_transformation(
4268
Some(source_file),
@@ -57,13 +83,16 @@ fn run_catch_all_tag_dispatch_to_catch_tag_fix(ctx: &mut Context<'_, '_>) -> Vec
5783
Vec::new()
5884
}
5985

60-
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:39 buildCatchAllTagDispatchReplacement 15a636ebb6e35af5
86+
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:45 buildCatchAllTagDispatchReplacement f143328b41352638
6187
//
62-
// Rust: the tracker and the match's callee cannot be nil, so only Go's empty-branches check remains.
88+
// Rust: the tracker and the match's callee cannot be nil, so only Go's empty-branches check remains. Go's `ctx`
89+
// parameter is the source file plus `uses_parameter`, each branch's precomputed parameter scan (see
90+
// `run_catch_all_tag_dispatch_to_catch_tag_fix`).
6391
fn build_catch_all_tag_dispatch_replacement(
6492
tracker: &Tracker,
6593
sf: P<SourceFile>,
6694
m: &CatchAllTagDispatchMatch,
95+
uses_parameter: &[bool],
6796
) -> (Option<P<Node>>, Option<P<NodeList>>) {
6897
if m.branches.is_empty() {
6998
return (None, None);
@@ -81,12 +110,12 @@ fn build_catch_all_tag_dispatch_replacement(
81110
Some(callee),
82111
Some(tracker.new_node_list(vec![
83112
tracker.new_string_literal(alloc_str(&branch.tag), TokenFlags::None),
84-
new_catch_tag_handler(tracker, &m.parameter_name, branch),
113+
new_catch_tag_handler(tracker, m, branch, uses_parameter[0]),
85114
])),
86115
);
87116
}
88117
let mut properties = Vec::with_capacity(m.branches.len());
89-
for branch in &m.branches {
118+
for (index, branch) in m.branches.iter().enumerate() {
90119
let mut name = tracker.new_string_literal(alloc_str(&branch.tag), TokenFlags::None);
91120
if scanner::is_identifier_text(&branch.tag, LanguageVariant::Standard) {
92121
name = tracker.new_identifier(alloc_str(&branch.tag));
@@ -96,7 +125,7 @@ fn build_catch_all_tag_dispatch_replacement(
96125
name,
97126
None,
98127
None,
99-
new_catch_tag_handler(tracker, &m.parameter_name, branch),
128+
new_catch_tag_handler(tracker, m, branch, uses_parameter[index]),
100129
));
101130
}
102131
(
@@ -107,18 +136,24 @@ fn build_catch_all_tag_dispatch_replacement(
107136
)
108137
}
109138

110-
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:63 newCatchTagHandler 2c37a49d830c9b68
139+
// go: internal/fixables/catch_all_tag_dispatch_to_catch_tag.go:71 newCatchTagHandler a40080adf9dfc3ea
140+
// newCatchTagHandler builds a branch handler that keeps the error parameter
141+
// only when the branch result reads it.
142+
//
143+
// Rust: Go's `ctx` parameter is `uses_parameter`, the branch's precomputed
144+
// `ConditionalRefailNodeContainsParameter` result.
111145
fn new_catch_tag_handler(
112146
tracker: &Tracker,
113-
parameter_name: &str,
147+
m: &CatchAllTagDispatchMatch,
114148
branch: &CatchAllTagDispatchBranch,
149+
uses_parameter: bool,
115150
) -> P<Node> {
116151
let mut parameters = Vec::new();
117-
if branch.uses_parameter {
152+
if uses_parameter {
118153
parameters = vec![tracker.new_parameter_declaration(
119154
None,
120155
None,
121-
tracker.new_identifier(alloc_str(parameter_name)),
156+
tracker.new_identifier(alloc_str(&m.parameter_name)),
122157
None,
123158
None,
124159
None,

‎crates/ets_fixables/src/catch_die_to_or_die.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ pub static CATCH_DIE_TO_OR_DIE_FIX: LazyLock<Fixable> = LazyLock::new(|| Fixable
1717
run: run_catch_die_to_or_die_fix,
1818
});
1919

20-
// go: internal/fixables/catch_die_to_or_die.go:22 runCatchDieToOrDieFix f4771d57ebe9b9fb
20+
// go: internal/fixables/catch_die_to_or_die.go:22 runCatchDieToOrDieFix 52cd9f9db8c0a064
2121
//
2222
// Rust: the match's transformation is never nil (`CatchDieToOrDieMatch::transformation` is not optional), so only
23-
// Go's `HasTypeArguments` check remains.
23+
// Go's `HasExplicitTypeArguments` check remains.
2424
fn run_catch_die_to_or_die_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction> {
2525
let source_file = ctx.source_file;
2626
for m in ets_rules::analyze_catch_die_to_or_die(&mut ctx.type_parser, Some(source_file)) {
2727
if !m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location) {
2828
continue;
2929
}
30-
if m.has_type_arguments {
30+
if ets_rules::has_explicit_type_arguments(Some(&m.transformation)) {
3131
continue;
3232
}
3333

‎crates/ets_fixables/src/catch_if_tag_to_catch_tag.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ pub static CATCH_IF_TAG_TO_CATCH_TAG_FIX: LazyLock<Fixable> = LazyLock::new(|| F
1919
run: run_catch_if_tag_to_catch_tag_fix,
2020
});
2121

22-
// go: internal/fixables/catch_if_tag_to_catch_tag.go:19 runCatchIfTagToCatchTagFix 3ed0efb13d6148aa
22+
// go: internal/fixables/catch_if_tag_to_catch_tag.go:19 runCatchIfTagToCatchTagFix 0a512b2a274acd50
2323
fn run_catch_if_tag_to_catch_tag_fix(ctx: &mut Context<'_, '_>) -> Vec<CodeAction> {
2424
let source_file = ctx.source_file;
2525
for m in ets_rules::analyze_catch_if_tag_to_catch_tag(&mut ctx.type_parser, Some(source_file)) {
26-
if !m.can_fix {
27-
continue;
28-
}
2926
if !m.location.intersects(ctx.span) && !ctx.span.contained_by(m.location) {
3027
continue;
3128
}

0 commit comments

Comments
 (0)