Skip to content

Commit ea9dd1e

Browse files
committed
refactor(semantic): do not use Astnodes::get_node for check_unresolved_exports
part of #13646
1 parent af88e94 commit ea9dd1e

File tree

4 files changed

+28
-44
lines changed

4 files changed

+28
-44
lines changed

crates/oxc_semantic/src/builder.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ impl<'a> SemanticBuilder<'a> {
278278
}
279279

280280
debug_assert_eq!(self.unresolved_references.scope_depth(), 1);
281-
if self.check_syntax_error && !self.source_type.is_typescript() {
282-
checker::check_unresolved_exports(&self);
283-
}
284281
self.scoping.set_root_unresolved_references(
285282
self.unresolved_references.into_root().into_iter().map(|(k, v)| (k.as_str(), v)),
286283
);

crates/oxc_semantic/src/checker/javascript.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,31 @@ use oxc_syntax::{
1616
symbol::SymbolFlags,
1717
};
1818

19-
use crate::{builder::SemanticBuilder, diagnostics::redeclaration};
19+
use crate::{IsGlobalReference, builder::SemanticBuilder, diagnostics::redeclaration};
20+
21+
#[cold]
22+
fn undefined_export(x0: &str, span1: Span) -> OxcDiagnostic {
23+
OxcDiagnostic::error(format!("Export '{x0}' is not defined")).with_label(span1)
24+
}
25+
26+
/// It is a Syntax Error if any element of the ExportedBindings of ModuleItemList
27+
/// does not also occur in either the VarDeclaredNames of ModuleItemList, or the LexicallyDeclaredNames of ModuleItemList.
28+
pub fn check_unresolved_exports(program: &Program<'_>, ctx: &SemanticBuilder<'_>) {
29+
if ctx.source_type.is_typescript() || ctx.source_type.is_script() {
30+
return;
31+
}
32+
for stmt in &program.body {
33+
if let Statement::ExportNamedDeclaration(decl) = stmt {
34+
for specifier in &decl.specifiers {
35+
if let ModuleExportName::IdentifierReference(ident) = &specifier.local
36+
&& ident.is_global_reference(&ctx.scoping)
37+
{
38+
ctx.errors.borrow_mut().push(undefined_export(&ident.name, ident.span));
39+
}
40+
}
41+
}
42+
}
43+
}
2044

2145
pub fn check_duplicate_class_elements(ctx: &SemanticBuilder<'_>) {
2246
let classes = &ctx.class_table_builder.classes;

crates/oxc_semantic/src/checker/mod.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use oxc_ast::{
2-
AstKind,
3-
ast::{DoWhileStatement, ForStatement, WhileStatement},
4-
};
5-
use oxc_diagnostics::OxcDiagnostic;
6-
use oxc_span::Span;
1+
use oxc_ast::{AstKind, ast::*};
72

83
use crate::builder::SemanticBuilder;
94

@@ -16,8 +11,9 @@ pub use javascript::is_function_part_of_if_statement;
1611

1712
pub fn check<'a>(kind: AstKind<'a>, ctx: &SemanticBuilder<'a>) {
1813
match kind {
19-
AstKind::Program(_) => {
14+
AstKind::Program(program) => {
2015
js::check_duplicate_class_elements(ctx);
16+
js::check_unresolved_exports(program, ctx);
2117
}
2218
AstKind::BindingIdentifier(ident) => {
2319
js::check_identifier(&ident.name, ident.span, ctx);
@@ -124,25 +120,3 @@ pub fn check<'a>(kind: AstKind<'a>, ctx: &SemanticBuilder<'a>) {
124120
_ => {}
125121
}
126122
}
127-
128-
#[cold]
129-
fn undefined_export(x0: &str, span1: Span) -> OxcDiagnostic {
130-
OxcDiagnostic::error(format!("Export '{x0}' is not defined")).with_label(span1)
131-
}
132-
133-
/// It is a Syntax Error if any element of the ExportedBindings of ModuleItemList
134-
/// does not also occur in either the VarDeclaredNames of ModuleItemList, or the LexicallyDeclaredNames of ModuleItemList.
135-
pub fn check_unresolved_exports(ctx: &SemanticBuilder<'_>) {
136-
for reference_ids in ctx.unresolved_references.root().values() {
137-
for reference_id in reference_ids {
138-
let reference = ctx.scoping.get_reference(*reference_id);
139-
let node_id = reference.node_id();
140-
let node = ctx.nodes.get_node(node_id);
141-
if ctx.nodes.flags(node_id).has_export_specifier()
142-
&& let AstKind::IdentifierReference(ident) = node.kind()
143-
{
144-
ctx.errors.borrow_mut().push(undefined_export(&ident.name, ident.span));
145-
}
146-
}
147-
}
148-
}

crates/oxc_semantic/src/unresolved_stack.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ impl<'a> UnresolvedReferencesStack<'a> {
7272
self.current_scope_depth
7373
}
7474

75-
/// Get unresolved references hash map for current scope
76-
#[inline]
77-
pub(crate) fn root(&self) -> &TempUnresolvedReferences<'a> {
78-
// SAFETY: `stack.len() > current_scope_depth` initially.
79-
// Thereafter, `stack` never shrinks, only grows.
80-
// `current_scope_depth` is only increased in `increment_scope_depth`,
81-
// and it grows `stack` to ensure `stack.len()` always exceeds `current_scope_depth`.
82-
// So this read is always guaranteed to be in bounds.
83-
unsafe { self.stack.get_unchecked(0) }
84-
}
85-
8675
/// Get unresolved references hash map for current scope
8776
#[inline]
8877
pub(crate) fn current_mut(&mut self) -> &mut TempUnresolvedReferences<'a> {

0 commit comments

Comments
 (0)