| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4442,55 +4442,28 @@ impl Compiler { | |||
| 4442 | 4442 | /// order as symbol-table construction so the annotation scope's | |
| 4443 | 4443 | /// `sub_tables` cursor stays aligned. | |
| 4444 | 4444 | fn collect_annotations(body: &[ast::Stmt]) -> Vec<&ast::StmtAnnAssign> { | |
| 4445 | - fn walk<'a>(stmts: &'a [ast::Stmt], out: &mut Vec<&'a ast::StmtAnnAssign>) { | ||
| 4446 | - for stmt in stmts { | ||
| 4445 | + use ast::visitor::Visitor; | ||
| 4446 | + | ||
| 4447 | + #[derive(Default)] | ||
| 4448 | + struct AnnotationsVisitor<'a> { | ||
| 4449 | + annotations: Vec<&'a ast::StmtAnnAssign>, | ||
| 4450 | + } | ||
| 4451 | + | ||
| 4452 | + impl<'a> Visitor<'a> for AnnotationsVisitor<'a> { | ||
| 4453 | + fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { | ||
| 4447 | 4454 | match stmt { | |
| 4448 | - ast::Stmt::AnnAssign(stmt) => out.push(stmt), | ||
| 4449 | - ast::Stmt::If(ast::StmtIf { | ||
| 4450 | - body, | ||
| 4451 | - elif_else_clauses, | ||
| 4452 | - .. | ||
| 4453 | - }) => { | ||
| 4454 | - walk(body, out); | ||
| 4455 | - for clause in elif_else_clauses { | ||
| 4456 | - walk(&clause.body, out); | ||
| 4457 | - } | ||
| 4458 | - } | ||
| 4459 | - ast::Stmt::For(ast::StmtFor { body, orelse, .. }) | ||
| 4460 | - | ast::Stmt::While(ast::StmtWhile { body, orelse, .. }) => { | ||
| 4461 | - walk(body, out); | ||
| 4462 | - walk(orelse, out); | ||
| 4463 | - } | ||
| 4464 | - ast::Stmt::With(ast::StmtWith { body, .. }) => walk(body, out), | ||
| 4465 | - ast::Stmt::Try(ast::StmtTry { | ||
| 4466 | - body, | ||
| 4467 | - handlers, | ||
| 4468 | - orelse, | ||
| 4469 | - finalbody, | ||
| 4470 | - .. | ||
| 4471 | - }) => { | ||
| 4472 | - walk(body, out); | ||
| 4473 | - for handler in handlers { | ||
| 4474 | - let ast::ExceptHandler::ExceptHandler( | ||
| 4475 | - ast::ExceptHandlerExceptHandler { body, .. }, | ||
| 4476 | - ) = handler; | ||
| 4477 | - walk(body, out); | ||
| 4478 | - } | ||
| 4479 | - walk(orelse, out); | ||
| 4480 | - walk(finalbody, out); | ||
| 4481 | - } | ||
| 4482 | - ast::Stmt::Match(ast::StmtMatch { cases, .. }) => { | ||
| 4483 | - for case in cases { | ||
| 4484 | - walk(&case.body, out); | ||
| 4485 | - } | ||
| 4486 | - } | ||
| 4487 | - _ => {} | ||
| 4455 | + ast::Stmt::AnnAssign(ann_assign) => self.annotations.push(ann_assign), | ||
| 4456 | + ast::Stmt::ClassDef(_) | ast::Stmt::FunctionDef(_) => {} | ||
| 4457 | + _ => ast::visitor::walk_stmt(self, stmt), | ||
| 4488 | 4458 | } | |
| 4489 | 4459 | } | |
| 4490 | 4460 | } | |
| 4491 | - let mut annotations = Vec::new(); | ||
| 4492 | - walk(body, &mut annotations); | ||
| 4493 | - annotations | ||
| 4461 | + | ||
| 4462 | + let mut visitor = AnnotationsVisitor::default(); | ||
| 4463 | + for stmt in body { | ||
| 4464 | + visitor.visit_stmt(stmt); | ||
| 4465 | + } | ||
| 4466 | + visitor.annotations | ||
| 4494 | 4467 | } | |
| 4495 | 4468 | ||
| 4496 | 4469 | fn compile_annotation_for_symbol_cursor_only( | |
@@ -4509,12 +4482,11 @@ impl Compiler { | |||
| 4509 | 4482 | ) -> CompileResult<bool> { | |
| 4510 | 4483 | let loc = loc.unwrap_or(self.current_source_range); | |
| 4511 | 4484 | let annotations = Self::collect_annotations(body); | |
| 4512 | - let simple_annotation_count = annotations | ||
| 4485 | + let has_simple_annotation = annotations | ||
| 4513 | 4486 | .iter() | |
| 4514 | - .filter(|stmt| stmt.simple && matches!(stmt.target.as_ref(), ast::Expr::Name(_))) | ||
| 4515 | - .count(); | ||
| 4487 | + .any(|stmt| stmt.simple && matches!(stmt.target.as_ref(), ast::Expr::Name(_))); | ||
| 4516 | 4488 | ||
| 4517 | - if simple_annotation_count == 0 { | ||
| 4489 | + if !has_simple_annotation { | ||
| 4518 | 4490 | return Ok(false); | |
| 4519 | 4491 | } | |
| 4520 | 4492 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments