Fix scope bug

This commit is contained in:
Andrew Morris
2023-07-27 10:12:40 +10:00
parent 69663190ee
commit f6546abc97
4 changed files with 32 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
// //! test_output("foo")
//! test_output("foo")
export default () => {
class X {

View File

@@ -714,10 +714,10 @@ impl ModuleCompiler {
let mut static_: Object = Object::default();
let defn_name = match ident {
Some(ident) => match self
.scope_analysis
.lookup_value(&OwnerId::Module, &Ident::from_swc_ident(ident))
{
Some(ident) => match self.scope_analysis.lookup_value(
&OwnerId::Module, // TODO: Do we need the scope/owner_id to be passed in instead?
&Ident::from_swc_ident(ident),
) {
Some(Value::Pointer(p)) => p,
_ => {
self.internal_error(

View File

@@ -40,6 +40,7 @@ pub enum OwnerId {
}
pub trait ScopeTrait {
fn trace(&self);
fn get(&self, name: &swc_atoms::JsWord) -> Option<NameId>;
fn set(
&self,
@@ -52,6 +53,24 @@ pub trait ScopeTrait {
}
impl ScopeTrait for Scope {
fn trace(&self) {
println!(
"scope trace {:?} names: {:?}",
&self.borrow().owner_id,
&self
.borrow()
.name_map
.keys()
.map(|k| k.to_string())
.collect::<Vec<String>>()
);
match &self.borrow().parent {
Some(parent) => parent.trace(),
None => println!("scope trace end"),
};
}
fn get(&self, name: &swc_atoms::JsWord) -> Option<NameId> {
match self.borrow().name_map.get(name) {
Some(mapped_name) => Some(mapped_name.clone()),

View File

@@ -254,7 +254,14 @@ impl ScopeAnalysis {
Some(name_id) => {
let name = self.names.get(&name_id).expect("Name not found");
if name.type_ != NameType::This {
let name_span = match name.id {
NameId::Span(span) => Some(span),
NameId::This(span) => Some(span),
NameId::Builtin(_) => None,
NameId::Constant(_) => None,
};
if name.type_ != NameType::This && name_span == Some(origin_ident.span) {
match &name.value {
// Already inserted, skip
Value::Pointer(_) => return,