mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
fix(semantic): add export symbol flag to identifiers in export declarations (#2508)
Related PR: #2335
This commit is contained in:
parent
432791679f
commit
4c2e2bdf61
2 changed files with 40 additions and 1 deletions
|
|
@ -6,7 +6,10 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
|
||||||
use oxc_ast::{ast::*, AstKind, Trivias, TriviasMap, Visit};
|
use oxc_ast::{ast::*, AstKind, Trivias, TriviasMap, Visit};
|
||||||
use oxc_diagnostics::Error;
|
use oxc_diagnostics::Error;
|
||||||
use oxc_span::{Atom, SourceType, Span};
|
use oxc_span::{Atom, SourceType, Span};
|
||||||
use oxc_syntax::{module_record::ModuleRecord, operator::AssignmentOperator};
|
use oxc_syntax::{
|
||||||
|
module_record::{ExportLocalName, ModuleRecord},
|
||||||
|
operator::AssignmentOperator,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
binder::Binder,
|
binder::Binder,
|
||||||
|
|
@ -338,6 +341,22 @@ impl<'a> SemanticBuilder<'a> {
|
||||||
pub fn add_redeclared_variables(&mut self, variable: VariableInfo) {
|
pub fn add_redeclared_variables(&mut self, variable: VariableInfo) {
|
||||||
self.redeclare_variables.variables.push(variable);
|
self.redeclare_variables.variables.push(variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_export_flag_for_export_identifier(&mut self) {
|
||||||
|
self.module_record.local_export_entries.iter().for_each(|entry| match &entry.local_name {
|
||||||
|
ExportLocalName::Name(name_span) => {
|
||||||
|
if let Some(symbol_id) = self.scope.get_root_binding(name_span.name()) {
|
||||||
|
self.symbols.union_flag(symbol_id, SymbolFlags::Export);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExportLocalName::Default(span) => {
|
||||||
|
if let Some(symbol_id) = self.symbols.get_symbol_id_from_span(span) {
|
||||||
|
self.symbols.union_flag(symbol_id, SymbolFlags::Export);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ExportLocalName::Null => {}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
impl<'a> Visit<'a> for SemanticBuilder<'a> {
|
||||||
|
|
@ -1766,6 +1785,9 @@ impl<'a> SemanticBuilder<'a> {
|
||||||
#[allow(clippy::single_match)]
|
#[allow(clippy::single_match)]
|
||||||
fn leave_kind(&mut self, kind: AstKind<'a>) {
|
fn leave_kind(&mut self, kind: AstKind<'a>) {
|
||||||
match kind {
|
match kind {
|
||||||
|
AstKind::Program(_) => {
|
||||||
|
self.add_export_flag_for_export_identifier();
|
||||||
|
}
|
||||||
AstKind::Class(_) => {
|
AstKind::Class(_) => {
|
||||||
self.current_node_flags -= NodeFlags::Class;
|
self.current_node_flags -= NodeFlags::Class;
|
||||||
self.class_table_builder.pop_class();
|
self.class_table_builder.pop_class();
|
||||||
|
|
|
||||||
|
|
@ -86,3 +86,20 @@ fn test_types_simple() {
|
||||||
.has_number_of_references(1)
|
.has_number_of_references(1)
|
||||||
.test();
|
.test();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_export_flag() {
|
||||||
|
let tester = SemanticTester::js(
|
||||||
|
"
|
||||||
|
const a = 1;
|
||||||
|
export { a, b, c as d };
|
||||||
|
class b {}
|
||||||
|
export default c;
|
||||||
|
function c() {}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
tester.has_root_symbol("a").contains_flags(SymbolFlags::Export).test();
|
||||||
|
tester.has_root_symbol("b").contains_flags(SymbolFlags::Export).test();
|
||||||
|
tester.has_root_symbol("c").contains_flags(SymbolFlags::Export).test();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue