@samitouri / QOS-React-1 / commits / 461083d7bd

[rust] Deprecate old manual ast visitor

Joe Savona committed Aug 4, 2023 at 14:44 UTC 461083d7bd5c4ba3d665788ac09a305f3bc098b1
6 files changed +11 -208
compiler/forget/crates/forget_build_hir/src/context.rs
+2 -2
@@ -1,6 +1,6 @@
1 use std::collections::HashSet;
2
3 -use forget_estree::{Binding, BindingId, Function, Identifier, Visitor};
3 +use forget_estree::{Binding, BindingId, Function, Identifier, Visitor_DEPRECATED};
4 use forget_hir::Environment;
5
6 pub(crate) fn get_context_identifiers<'ast>(
@@ -39,7 +39,7 @@ impl<'ast> ContextVisitor<'ast> {
39 }
40 }
41
42 -impl<'ast> Visitor<'ast> for ContextVisitor<'ast> {
42 +impl<'ast> Visitor_DEPRECATED<'ast> for ContextVisitor<'ast> {
43 fn visit_lvalue<F>(&mut self, f: F)
44 where
45 F: FnOnce(&mut Self) -> (),
compiler/forget/crates/forget_estree/src/generated.rs
+1 -1
@@ -8079,7 +8079,7 @@ impl std::str::FromStr for MethodKind {
8079 }
8080 }
8081 }
8082 -pub trait Visitor2 {
8082 +pub trait Visitor {
8083 fn visit_class(&mut self, ast: &Class) {
8084 if let Some(id) = &ast.id {
8085 self.visit_identifier(id);
compiler/forget/crates/forget_estree/src/visit.rs
+3 -1
@@ -9,7 +9,9 @@ use crate::{
9 };
10
11 /// Trait for visiting an estree
12 -pub trait Visitor<'ast> {
12 +#[allow(non_camel_case_types)]
13 +#[deprecated]
14 +pub trait Visitor_DEPRECATED<'ast> {
15 fn visit_lvalue<F>(&mut self, f: F)
16 where
17 F: FnOnce(&mut Self) -> (),
compiler/forget/crates/forget_estree_codegen/src/codegen.rs
+1 -1
@@ -105,7 +105,7 @@ impl Grammar {
105
106 #(#operator_defs)*
107
108 - pub trait Visitor2 {
108 + pub trait Visitor {
109 #(#object_visitors)*
110
111 #(#node_visitors)*
compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs
+2 -2
@@ -2,7 +2,7 @@ use forget_diagnostics::Diagnostic;
2 use forget_estree::{
3 AssignmentOperator, AssignmentPropertyOrRestElement, AssignmentTarget, Expression,
4 ExpressionOrSuper, ForInInit, ForInit, Function, FunctionBody, Identifier, JSXElementName,
5 - Pattern, Program, SourceRange, SourceType, Statement, VariableDeclarationKind, Visitor2,
5 + Pattern, Program, SourceRange, SourceType, Statement, VariableDeclarationKind, Visitor,
6 };
7
8 use crate::{AstNode, DeclarationKind, LabelKind, ReferenceKind, ScopeId, ScopeKind, ScopeManager};
@@ -218,7 +218,7 @@ impl Analyzer {
218 }
219 }
220
221 -impl Visitor2 for Analyzer {
221 +impl Visitor for Analyzer {
222 fn visit_function_declaration(&mut self, ast: &forget_estree::FunctionDeclaration) {
223 if let Some(id) = &ast.function.id {
224 let declaration = self.manager.add_declaration(
compiler/forget/crates/forget_semantic_analysis/src/scope_manager.rs
+2 -201
@@ -1,19 +1,13 @@
1 use forget_diagnostics::Diagnostic;
2 use forget_estree::{
3 - BreakStatement, ContinueStatement, ESTreeNode, Identifier, LabeledStatement, Program,
4 - Statement, VariableDeclarationKind, Visitor,
3 + BreakStatement, ContinueStatement, ESTreeNode, Identifier, LabeledStatement,
4 + VariableDeclarationKind,
5 };
6 use forget_utils::PointerAddress;
7 use indexmap::IndexMap;
8
9 use crate::scope_view::ScopeView;
10
11 -pub fn analyze(ast: &Program) -> ScopeManager {
12 - let mut analyzer = Analyzer::new();
13 - analyzer.visit_program(ast);
14 - analyzer.results
15 -}
16 -
11 pub struct ScopeManager {
12 root: ScopeId,
13
@@ -347,196 +341,3 @@ where
341 Self::new(value)
342 }
343 }
350 -
351 -struct Analyzer {
352 - results: ScopeManager,
353 - current: ScopeId,
354 - is_lvalue: bool,
355 -}
356 -
357 -impl Analyzer {
358 - fn new() -> Self {
359 - let results = ScopeManager::new();
360 - let current = results.root_id();
361 - Self {
362 - results,
363 - current,
364 - is_lvalue: false,
365 - }
366 - }
367 -
368 - pub(crate) fn enter<F>(&mut self, kind: ScopeKind, mut f: F) -> ScopeId
369 - where
370 - F: FnMut(&mut Self) -> (),
371 - {
372 - let scope = self.results.add_scope(self.current, kind);
373 - let previous = std::mem::replace(&mut self.current, scope);
374 - f(self);
375 - let scope = std::mem::replace(&mut self.current, previous);
376 - scope
377 - }
378 -
379 - pub(crate) fn enter_scope(&mut self, kind: ScopeKind) -> ScopeId {
380 - let scope = self.results.add_scope(self.current, kind);
381 - self.current = scope;
382 - scope
383 - }
384 -
385 - pub(crate) fn close_scope(&mut self) {
386 - let scope = self.results.scope(self.current);
387 - self.current = scope.parent.unwrap();
388 - }
389 -}
390 -
391 -impl<'ast> Visitor<'ast> for Analyzer {
392 - fn visit_function_declaration(
393 - &mut self,
394 - declaration: &'ast forget_estree::FunctionDeclaration,
395 - ) {
396 - let scope = self.enter(ScopeKind::Function, |visitor| {
397 - visitor.visit_function(&declaration.function);
398 - });
399 - self.results
400 - .node_scopes
401 - .insert(AstNode::from(declaration), scope);
402 - }
403 -
404 - fn visit_statement(&mut self, stmt: &'ast forget_estree::Statement) {
405 - match stmt {
406 - Statement::LabeledStatement(stmt) => {
407 - let inner = &stmt.body;
408 - let kind = match inner {
409 - Statement::ForStatement(_)
410 - | Statement::ForInStatement(_)
411 - | Statement::ForOfStatement(_)
412 - | Statement::WhileStatement(_)
413 - | Statement::DoWhileStatement(_) => LabelKind::Loop,
414 - _ => LabelKind::Other,
415 - };
416 - let id = self
417 - .results
418 - .add_label(self.current, kind, stmt.label.name.clone());
419 - self.results
420 - .node_labels
421 - .insert(AstNode::from(stmt.as_ref()), id);
422 - self.visit_statement(&stmt.body);
423 - }
424 - Statement::BreakStatement(stmt) => {
425 - if let Some(label) = &stmt.label {
426 - if let Some(label) = self.results.lookup_label(self.current, &label.name) {
427 - self.results
428 - .node_labels
429 - .insert(AstNode::from(stmt.as_ref()), label.id);
430 - } else {
431 - self.results.diagnostics.push(Diagnostic::invalid_syntax(
432 - "Undefined break label",
433 - label.range,
434 - ));
435 - }
436 - }
437 - }
438 - Statement::ContinueStatement(stmt) => {
439 - if let Some(label_node) = &stmt.label {
440 - if let Some(label) = self.results.lookup_label(self.current, &label_node.name) {
441 - if label.kind == LabelKind::Loop {
442 - self.results
443 - .node_labels
444 - .insert(AstNode::from(stmt.as_ref()), label.id);
445 - } else {
446 - self.results.diagnostics.push(Diagnostic::invalid_syntax(
447 - "Invalid continue statement, can only continue to a label associated with a loop statement (for, for..in, for..of, etc)",
448 - label_node.range,
449 - ));
450 - }
451 - } else {
452 - self.results.diagnostics.push(Diagnostic::invalid_syntax(
453 - "Undefined continue label",
454 - label_node.range,
455 - ));
456 - }
457 - }
458 - }
459 - Statement::BlockStatement(stmt) => {
460 - let scope = self.enter(ScopeKind::Block, |visitor| {
461 - for item in &stmt.body {
462 - visitor.visit_statement(item);
463 - }
464 - });
465 - self.results
466 - .node_scopes
467 - .insert(AstNode::from(stmt.as_ref()), scope);
468 - }
469 - _ => {
470 - self.default_visit_statement(stmt);
471 - }
472 - }
473 - }
474 -
475 - fn visit_identifier(&mut self, identifier: &'ast Identifier) {
476 - if self.is_lvalue {
477 - let declaration = self
478 - .results
479 - .lookup_declaration(self.current, &identifier.name);
480 - if let Some(declaration) = declaration {
481 - let id = self.results.add_reference(
482 - self.current,
483 - ReferenceKind::ReadWrite,
484 - declaration.id,
485 - );
486 - self.results
487 - .node_references
488 - .insert(AstNode::from(identifier), id);
489 - } else {
490 - let id = self.results.add_declaration(
491 - self.current,
492 - identifier.name.clone(),
493 - DeclarationKind::Let,
494 - ); // TODO: determine the correct kind!
495 - self.results
496 - .node_declarations
497 - .insert(AstNode::from(identifier), id);
498 - }
499 - } else {
500 - let declaration = self
501 - .results
502 - .lookup_declaration(self.current, &identifier.name);
503 - if let Some(declaration) = declaration {
504 - let declaration_id = declaration.id;
505 - let id =
506 - self.results
507 - .add_reference(self.current, ReferenceKind::Read, declaration.id);
508 - self.results
509 - .node_references
510 - .insert(AstNode::from(identifier), id);
511 - } else {
512 - // Oops, undefined variable
513 - self.results.diagnostics.push(Diagnostic::invalid_syntax(
514 - "Undefined variable",
515 - identifier.range,
516 - ));
517 - }
518 - }
519 - }
520 -
521 - fn visit_literal(&mut self, _literal: &'ast forget_estree::Literal) {}
522 -
523 - fn visit_lvalue<F>(&mut self, f: F)
524 - where
525 - F: FnOnce(&mut Self) -> (),
526 - {
527 - let prev = self.is_lvalue;
528 - self.is_lvalue = true;
529 - f(self);
530 - self.is_lvalue = prev;
531 - }
532 -
533 - fn visit_rvalue<F>(&mut self, f: F)
534 - where
535 - F: FnOnce(&mut Self) -> (),
536 - {
537 - let prev = self.is_lvalue;
538 - self.is_lvalue = false;
539 - f(self);
540 - self.is_lvalue = prev;
541 - }
542 -}