Skip to content

Commit 98c1fbb

Browse files
committed
fix(linter/require-await): improve async keyword detection in get_delete_span function (#12494)
1 parent e2d9b4d commit 98c1fbb

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/oxc_linter/src/rules/eslint/require_await.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ impl Rule for RequireAwait {
159159

160160
#[expect(clippy::cast_possible_truncation)]
161161
fn get_delete_span(ctx: &LintContext, start: u32) -> Span {
162-
let end = start + 5;
163-
let async_key_span = Span::new(start, end);
162+
let source_text = ctx.source_text();
163+
let source_from_start = &source_text[(start as usize)..];
164+
165+
// Find the position of "async" keyword from the start position
166+
let async_pos = source_from_start.find("async").unwrap_or(0);
167+
let async_start = start + async_pos as u32;
168+
let async_end = async_start + 5;
169+
let async_key_span = Span::new(async_start, async_end);
164170

165171
// debug assertions
166172
#[cfg(debug_assertions)]
@@ -173,7 +179,7 @@ fn get_delete_span(ctx: &LintContext, start: u32) -> Span {
173179
}
174180

175181
let mut offset: u32 = 0;
176-
for c in ctx.source_text()[(end as usize)..].chars() {
182+
for c in ctx.source_text()[(async_end as usize)..].chars() {
177183
if !c.is_whitespace() {
178184
break;
179185
}
@@ -304,6 +310,7 @@ fn test() {
304310
),
305311
("async function O(){r}", "function O(){r}"),
306312
("s={expoí:async function(){{}}}", "s={expoí:function(){{}}}"),
313+
("class foo { private async bar() { x() } }", "class foo { private bar() { x() } }"),
307314
];
308315

309316
Tester::new(RequireAwait::NAME, RequireAwait::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)