Skip to content

Commit a9e8e3a

Browse files
harupypull[bot]
authored andcommitted
Improve excepthandler_name_range (#1368)
1 parent 8c17dad commit a9e8e3a

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/ast/helpers.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,30 @@ pub fn excepthandler_name_range(
402402
handler: &Excepthandler,
403403
locator: &SourceCodeLocator,
404404
) -> Option<Range> {
405-
let contents = locator.slice_source_code_range(&Range::from_located(handler));
406-
let range = lexer::make_tokenizer(&contents)
407-
.flatten()
408-
.tuple_windows()
409-
.find(|(tok, next_tok)| matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. }))
410-
.map(|((..), (start, _, end))| Range {
411-
location: to_absolute(start, handler.location),
412-
end_location: to_absolute(end, handler.location),
413-
});
414-
range
405+
let ExcepthandlerKind::ExceptHandler {
406+
name, type_, body, ..
407+
} = &handler.node;
408+
match (name, type_) {
409+
(Some(_), Some(type_)) => {
410+
let type_end_location = type_.end_location.unwrap();
411+
let contents = locator.slice_source_code_range(&Range {
412+
location: type_end_location,
413+
end_location: body[0].location,
414+
});
415+
let range = lexer::make_tokenizer(&contents)
416+
.flatten()
417+
.tuple_windows()
418+
.find(|(tok, next_tok)| {
419+
matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. })
420+
})
421+
.map(|((..), (start, _, end))| Range {
422+
location: to_absolute(start, type_end_location),
423+
end_location: to_absolute(end, type_end_location),
424+
});
425+
range
426+
}
427+
_ => None,
428+
}
415429
}
416430

417431
/// Return `true` if a `Stmt` appears to be part of a multi-statement line, with

0 commit comments

Comments
 (0)