Skip to content

Commit 85bd19a

Browse files
committed
fix(linter/prefer-class-fields): insert value after type annotation in fixer (#15423)
1 parent 29f35b2 commit 85bd19a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/oxc_linter/src/rules/unicorn/prefer_class_fields.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ impl Rule for PreferClassFields {
162162
let mut codegen = fixer.codegen();
163163
codegen.print_str(" = ");
164164
codegen.print_expression(&assign.right);
165-
fix.push(fixer.insert_text_after(&prop.key, codegen.into_source_text()));
165+
let insert_span =
166+
prop.type_annotation.as_ref().map_or(prop.key.span(), |ty| ty.span);
167+
fix.push(fixer.insert_text_after_range(insert_span, codegen.into_source_text()));
166168
} else {
167169
let indent =
168170
ctx.source_range(constructor.span).lines().next().map_or("\t", |line| {
@@ -372,6 +374,11 @@ fn test() {
372374
"class Foo { bar = 1; constructor() { } }",
373375
None,
374376
),
377+
(
378+
"class Foo { closed: boolean; constructor() { this.closed = false; } }",
379+
"class Foo { closed: boolean = false; constructor() { } }",
380+
None,
381+
),
375382
];
376383

377384
Tester::new(PreferClassFields::NAME, PreferClassFields::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)