Skip to content

Commit

Permalink
Fix scope bug
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 27, 2023
1 parent 6966319 commit f6546ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// //! test_output("foo")
//! test_output("foo")

export default () => {
class X {
Expand Down
8 changes: 4 additions & 4 deletions valuescript_compiler/src/module_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions valuescript_compiler/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()),
Expand Down
9 changes: 8 additions & 1 deletion valuescript_compiler/src/scope_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f6546ab

Please sign in to comment.