From dca3fcd8d1262e507220ebd522eeb463b5be2142 Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Sun, 25 Dec 2022 14:12:12 +0900 Subject: [PATCH] Improve `excepthandler_name_range` (#1368) --- src/ast/helpers.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 7db1875e96bac..57052ecbbab65 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -402,16 +402,30 @@ pub fn excepthandler_name_range( handler: &Excepthandler, locator: &SourceCodeLocator, ) -> Option { - let contents = locator.slice_source_code_range(&Range::from_located(handler)); - let range = lexer::make_tokenizer(&contents) - .flatten() - .tuple_windows() - .find(|(tok, next_tok)| matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. })) - .map(|((..), (start, _, end))| Range { - location: to_absolute(start, handler.location), - end_location: to_absolute(end, handler.location), - }); - range + let ExcepthandlerKind::ExceptHandler { + name, type_, body, .. + } = &handler.node; + match (name, type_) { + (Some(_), Some(type_)) => { + let type_end_location = type_.end_location.unwrap(); + let contents = locator.slice_source_code_range(&Range { + location: type_end_location, + end_location: body[0].location, + }); + let range = lexer::make_tokenizer(&contents) + .flatten() + .tuple_windows() + .find(|(tok, next_tok)| { + matches!(tok.1, Tok::As) && matches!(next_tok.1, Tok::Name { .. }) + }) + .map(|((..), (start, _, end))| Range { + location: to_absolute(start, type_end_location), + end_location: to_absolute(end, type_end_location), + }); + range + } + _ => None, + } } /// Return `true` if a `Stmt` appears to be part of a multi-statement line, with