|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use oxc::{ |
| 4 | + CompilerInterface, |
| 5 | + ast::ast::BindingIdentifier, |
| 6 | + ast_visit::{Visit, walk}, |
| 7 | + diagnostics::OxcDiagnostic, |
| 8 | + semantic::SemanticBuilderReturn, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{Case, Diagnostic, Driver, Source}; |
| 12 | + |
| 13 | +pub struct SemanticSymbolIdsRunner; |
| 14 | + |
| 15 | +impl Case for SemanticSymbolIdsRunner { |
| 16 | + fn name(&self) -> &'static str { |
| 17 | + "SemanticSymbolIds" |
| 18 | + } |
| 19 | + |
| 20 | + fn enable_runtime_test(&self) -> bool { |
| 21 | + false |
| 22 | + } |
| 23 | + |
| 24 | + fn test(&self, source: &Source) -> Result<(), Vec<Diagnostic>> { |
| 25 | + if !self.run_test(source) { |
| 26 | + return Ok(()); |
| 27 | + } |
| 28 | + |
| 29 | + let mut driver = SemanticIdsCheckDriver::new(source); |
| 30 | + driver.compile(&source.source_text, source.source_type, &source.path); |
| 31 | + |
| 32 | + if driver.checker.errors.is_empty() { Ok(()) } else { Err(driver.checker.errors) } |
| 33 | + } |
| 34 | + |
| 35 | + fn driver(&self) -> Driver { |
| 36 | + Driver::default() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +struct SemanticIdsCheckDriver<'a> { |
| 41 | + checker: SemanticIdsChecker<'a>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<'s> SemanticIdsCheckDriver<'s> { |
| 45 | + fn new(source: &'s Source) -> Self { |
| 46 | + Self { checker: SemanticIdsChecker { source, errors: vec![] } } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl CompilerInterface for SemanticIdsCheckDriver<'_> { |
| 51 | + fn handle_errors(&mut self, errors: Vec<OxcDiagnostic>) { |
| 52 | + // Ignore parse/semantic errors - we only care about missing symbol IDs |
| 53 | + // and reference IDs on successfully parsed identifiers |
| 54 | + let _ = errors; |
| 55 | + } |
| 56 | + |
| 57 | + fn after_semantic(&mut self, semantic_return: &mut SemanticBuilderReturn) -> ControlFlow<()> { |
| 58 | + self.checker.visit_program(semantic_return.semantic.nodes().program()); |
| 59 | + ControlFlow::Continue(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +struct SemanticIdsChecker<'a> { |
| 64 | + source: &'a Source, |
| 65 | + errors: Vec<Diagnostic>, |
| 66 | +} |
| 67 | + |
| 68 | +impl<'a> Visit<'a> for SemanticIdsChecker<'_> { |
| 69 | + fn visit_binding_identifier(&mut self, it: &BindingIdentifier<'a>) { |
| 70 | + if it.symbol_id.get().is_none() { |
| 71 | + self.errors.push(Diagnostic { |
| 72 | + case: "SemanticSymbolIds", |
| 73 | + path: self.source.path.clone(), |
| 74 | + message: format!( |
| 75 | + "BindingIdentifier '{}' at {:?} has no symbol_id", |
| 76 | + it.name, it.span |
| 77 | + ), |
| 78 | + }); |
| 79 | + } |
| 80 | + walk::walk_binding_identifier(self, it); |
| 81 | + } |
| 82 | + |
| 83 | + fn visit_identifier_reference(&mut self, it: &oxc::ast::ast::IdentifierReference<'a>) { |
| 84 | + if it.reference_id.get().is_none() { |
| 85 | + self.errors.push(Diagnostic { |
| 86 | + case: "SemanticSymbolIds", |
| 87 | + path: self.source.path.clone(), |
| 88 | + message: format!( |
| 89 | + "IdentifierReference '{}' at {:?} has no reference_id", |
| 90 | + it.name, it.span |
| 91 | + ), |
| 92 | + }); |
| 93 | + } |
| 94 | + walk::walk_identifier_reference(self, it); |
| 95 | + } |
| 96 | +} |
0 commit comments