From 172d6a035e406e18944d0e1b68ddf0902c659754 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 25 Dec 2022 13:32:54 +0900 Subject: [PATCH 1/2] Fix excepthandler_name_range --- src/ast/helpers.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 7db1875e96bac..2c997a8501638 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -402,16 +402,22 @@ 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 + if let ExcepthandlerKind::ExceptHandler { name: None, .. } = &handler.node { + None + } else { + 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 + } } /// Return `true` if a `Stmt` appears to be part of a multi-statement line, with From e58620111d20712d6db0be9ad4bf2bc6ced1f054 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 25 Dec 2022 14:04:10 +0900 Subject: [PATCH 2/2] Only tokenize 'as name' --- src/ast/helpers.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 2c997a8501638..57052ecbbab65 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -402,21 +402,29 @@ pub fn excepthandler_name_range( handler: &Excepthandler, locator: &SourceCodeLocator, ) -> Option { - if let ExcepthandlerKind::ExceptHandler { name: None, .. } = &handler.node { - None - } else { - 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), + 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, }); - range + 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, } }