@samitouri / QOS-React-2 / commits / 4756c5ab26

[rust][sema] Shared logic for for-in/for-of

Shares the code for for..in and for..of, along with a few other internal refactorings.

Joe Savona committed Aug 4, 2023 at 12:02 UTC 4756c5ab26813215b3926a1e7ce542d414a38095
1 file changed +49 -24
compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs
+49 -24
@@ -183,6 +183,39 @@ impl Analyzer {
183 }
184 }
185 }
186 +
187 + fn visit_for_in_of(
188 + &mut self,
189 + ast: AstNode,
190 + left: &ForInInit,
191 + right: &Expression,
192 + body: &Statement,
193 + range: Option<SourceRange>,
194 + ) {
195 + // Record an anonymous label for the statement to resolve unlabeled break/continue
196 + let label = self
197 + .manager
198 + .add_anonymous_label(self.current, LabelKind::Loop);
199 + self.manager.node_labels.insert(ast, label);
200 +
201 + let mut for_scope: Option<ScopeId> = None;
202 + match left {
203 + ForInInit::VariableDeclaration(left) => {
204 + if left.kind != VariableDeclarationKind::Var {
205 + for_scope = Some(self.enter_scope(ScopeKind::For));
206 + }
207 + self.visit_variable_declaration(left);
208 + }
209 + ForInInit::Pattern(left) => {
210 + Analyzer::visit_declaration_pattern(self, left, None);
211 + }
212 + }
213 + self.visit_expression(right);
214 + self.visit_statement(body);
215 + if let Some(for_scope) = for_scope {
216 + self.close_scope(for_scope);
217 + }
218 + }
219 }
220
221 impl Visitor2 for Analyzer {
@@ -401,33 +434,25 @@ impl Visitor2 for Analyzer {
434 }
435
436 fn visit_for_in_statement(&mut self, ast: &forget_estree::ForInStatement) {
404 - // Record an anonymous label for the statement to resolve unlabeled break/continue
405 - let label = self
406 - .manager
407 - .add_anonymous_label(self.current, LabelKind::Loop);
408 - self.manager.node_labels.insert(AstNode::from(ast), label);
409 -
410 - let mut for_scope: Option<ScopeId> = None;
411 - match &ast.left {
412 - ForInInit::VariableDeclaration(left) => {
413 - if left.kind != VariableDeclarationKind::Var {
414 - for_scope = Some(self.enter_scope(ScopeKind::For));
415 - }
416 - self.visit_variable_declaration(left);
417 - }
418 - ForInInit::Pattern(left) => {
419 - Analyzer::visit_declaration_pattern(self, left, None);
420 - }
421 - }
422 - self.visit_expression(&ast.right);
423 - self.visit_statement(&ast.body);
424 - if let Some(for_scope) = for_scope {
425 - self.close_scope(for_scope);
426 - }
437 + Analyzer::visit_for_in_of(
438 + self,
439 + AstNode::from(ast),
440 + &ast.left,
441 + &ast.right,
442 + &ast.body,
443 + ast.range,
444 + );
445 }
446
447 fn visit_for_of_statement(&mut self, ast: &forget_estree::ForOfStatement) {
430 - todo!("ForOfStatement")
448 + Analyzer::visit_for_in_of(
449 + self,
450 + AstNode::from(ast),
451 + &ast.left,
452 + &ast.right,
453 + &ast.body,
454 + ast.range,
455 + );
456 }
457
458 fn visit_identifier(&mut self, ast: &forget_estree::Identifier) {