Skip to content

Commit

Permalink
test(semantic): add more symbol and reference checks to `PostTransfor…
Browse files Browse the repository at this point in the history
…mChecker`
  • Loading branch information
Boshen committed Aug 19, 2024
1 parent 4fdf26d commit 0df1a94
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions crates/oxc_semantic/src/post_transform_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,23 @@ current scope {cur_scope_id:?}: {current_bindings:?}
_previous_scopes: &ScopeTree,
current_collect: &SemanticCollector,
current_symbols: &SymbolTable,
_current_scopes: &ScopeTree,
current_scopes: &ScopeTree,
) {
// Check whether symbols are valid
for symbol_id in current_collect.symbol_ids.iter().copied() {
if current_symbols.get_flag(symbol_id).is_empty() {
let name = current_symbols.get_name(symbol_id);
self.errors.push(OxcDiagnostic::error(format!(
"Expect non-empty SymbolFlags for BindingIdentifier({name})"
)));
if !current_scopes.has_binding(current_symbols.get_scope_id(symbol_id), name) {
self.errors.push(OxcDiagnostic::error(
format!("Cannot find BindingIdentifier({name}) in the Scope corresponding to the Symbol"),
));
}
}
}

if self.previous_collect.symbol_ids.len() != current_collect.symbol_ids.len() {
self.errors.push(OxcDiagnostic::error("Symbols mismatch after transform"));
return;
Expand Down Expand Up @@ -143,6 +158,16 @@ current symbol {cur_symbol_id:?}: {cur_symbol_id:?}
current_symbols: &SymbolTable,
_current_scopes: &ScopeTree,
) {
// Check whether references are valid
for reference_id in current_collect.reference_ids.iter().copied() {
let reference = current_symbols.get_reference(reference_id);
if reference.flag().is_empty() {
self.errors.push(OxcDiagnostic::error(format!(
"Expect ReferenceFlags for IdentifierReference({reference_id:?}) to not be empty",
)));
}
}

if self.previous_collect.reference_ids.len() != current_collect.reference_ids.len() {
self.errors.push(OxcDiagnostic::error("ReferenceId mismatch after transform"));
return;
Expand Down Expand Up @@ -175,8 +200,8 @@ struct SemanticCollector {
scope_ids: Vec<ScopeId>,
symbol_ids: Vec<SymbolId>,
reference_ids: Vec<ReferenceId>,
missing_references: Vec<Span>,
missing_symbols: Vec<Span>,

errors: Vec<OxcDiagnostic>,
}

impl<'a> Visit<'a> for SemanticCollector {
Expand All @@ -190,15 +215,15 @@ impl<'a> Visit<'a> for SemanticCollector {
if let Some(reference_id) = ident.reference_id.get() {
self.reference_ids.push(reference_id);
} else {
self.missing_references.push(ident.span);
self.errors.push(OxcDiagnostic::error("Missing ReferenceId").with_label(ident.span));
}
}

fn visit_binding_identifier(&mut self, ident: &BindingIdentifier<'a>) {
if let Some(symbol_id) = ident.symbol_id.get() {
self.symbol_ids.push(symbol_id);
} else {
self.missing_symbols.push(ident.span);
self.errors.push(OxcDiagnostic::error("Missing SymbolId").with_label(ident.span));
}
}

Expand Down Expand Up @@ -248,17 +273,6 @@ impl SemanticCollector {
fn check_ast(&mut self, program: &Program<'_>) -> Option<Vec<OxcDiagnostic>> {
self.visit_program(program);

let diagnostics = self
.missing_references
.iter()
.map(|span| OxcDiagnostic::error("Missing ReferenceId").with_label(*span))
.chain(
self.missing_symbols
.iter()
.map(|span| OxcDiagnostic::error("Missing SymbolId").with_label(*span)),
)
.collect::<Vec<_>>();

(!diagnostics.is_empty()).then_some(diagnostics)
(!self.errors.is_empty()).then(|| mem::take(&mut self.errors))
}
}

0 comments on commit 0df1a94

Please sign in to comment.