From bccc43f264fc693548562de0366a612221ce1035 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Fri, 21 Jul 2023 17:21:22 +1000 Subject: [PATCH] Fix doubleCapture.ts --- .../captures}/doubleCapture.ts | 2 +- valuescript_compiler/src/scope_analysis.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) rename inputs/{failing => passing/captures}/doubleCapture.ts (83%) diff --git a/inputs/failing/doubleCapture.ts b/inputs/passing/captures/doubleCapture.ts similarity index 83% rename from inputs/failing/doubleCapture.ts rename to inputs/passing/captures/doubleCapture.ts index a486026..5b4a07f 100644 --- a/inputs/failing/doubleCapture.ts +++ b/inputs/passing/captures/doubleCapture.ts @@ -1,4 +1,4 @@ -// //! test_output(37) +//! test_output(37) export default function main() { const x = 37; diff --git a/valuescript_compiler/src/scope_analysis.rs b/valuescript_compiler/src/scope_analysis.rs index b0f83dc..3f5ec03 100644 --- a/valuescript_compiler/src/scope_analysis.rs +++ b/valuescript_compiler/src/scope_analysis.rs @@ -295,6 +295,18 @@ impl ScopeAnalysis { ); } + fn handle_capture_chain(&mut self, scope: &Scope, name: &Name, ref_: swc_common::Span) { + if scope.borrow().owner_id == name.owner_id { + return; + } + + self.insert_capture(&scope.borrow().owner_id, &name.id, ref_); + + if let Some(parent_scope) = &scope.borrow().parent { + self.handle_capture_chain(parent_scope, name, ref_); + } + } + fn insert_capture(&mut self, captor_id: &OwnerId, name_id: &NameId, ref_: swc_common::Span) { let name = match self.names.get_mut(name_id) { Some(name) => name, @@ -1629,7 +1641,7 @@ impl ScopeAnalysis { ); let name = match self.names.get(&name_id) { - Some(name) => name, + Some(name) => name.clone(), None => { self.diagnostics.push(Diagnostic { level: DiagnosticLevel::InternalError, @@ -1640,9 +1652,7 @@ impl ScopeAnalysis { } }; - if name.owner_id != scope.borrow().owner_id { - self.insert_capture(&scope.borrow().owner_id, &name_id, ident.span); - } + self.handle_capture_chain(scope, &name, ident.span); } fn prop_key(&mut self, scope: &Scope, prop_name: &swc_ecma_ast::PropName) {