Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feature(rome_js_semantic): Hoisting #2761

Merged
merged 7 commits into from
Jun 22, 2022
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Jun 21, 2022

This PR is the part that matches declarations and scopes of the main PR (#2489).
For more details: #2488

This PR implements hoisting.
We decided that the event extraction will not generate diagnostics. This will be a task to whoever read these events.

This PR also contains renaming some functions that did not enter in the last PR.

@xunilrj xunilrj temporarily deployed to aws June 21, 2022 20:09 Inactive
@github-actions
Copy link

github-actions bot commented Jun 21, 2022

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jun 21, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: c058442
Status: ✅  Deploy successful!
Preview URL: https://3f779f77.tools-8rn.pages.dev
Branch Preview URL: https://feature-semantic-events-hois.tools-8rn.pages.dev

View logs

@xunilrj xunilrj temporarily deployed to aws June 22, 2022 08:00 Inactive
@xunilrj xunilrj merged commit 8bd41b8 into main Jun 22, 2022
@xunilrj xunilrj deleted the feature/semantic-events-hoisting branch June 22, 2022 12:13
/// Generated for:
/// - All reference identifiers
Read {
range: TextRange,
declaration_at: Option<TextRange>,
},

/// Signifies that a new scope was started
HoistedRead {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document this?

/// Signifies that a new scope was started
HoistedRead {
range: TextRange,
declaration_at: TextRange,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declared_at?

/// Generated for:
/// - Blocks
/// - Function body
ScopeStarted { range: TextRange },

/// Signifies that a new scope was ended
/// Tracks when a scope ends
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when" => "where"?

Comment on lines +39 to +40
/// Tracks when a reference do no have any matching
/// binding
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracks where a reference is read, but it doesn't have any matching binding?

Would it be possible to add a concrete example? Is this a valid example?

var _window = window;
var _this = this;

declared: Vec<ScopeDeclaration>,
/// All bindings declared inside this scope
bindings: Vec<Binding>,
/// Reference that do not have a matching declaration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's some mistake in the wording of this doc

.position(|scope| !scope.allows_decl_hoisting);

match idx {
None => unreachable!(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a small explanation of why it's not reachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Some(())
}

fn solve_pending_references(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document this method please? Mainly to explain the cases where we use it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is seems that this function is used only inside a case where we already know that the current binding is a var. Do you plan to use it somewhere else in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It will be used for everything that can be hoisted, like functions.

Comment on lines +248 to +261
for scope in scopes {
if let Some(references) = scope.references.remove(&name) {
for reference in references {
self.stash.push_back(SemanticEvent::HoistedRead {
range: reference.range,
declaration_at: node.text_range(),
})
}
}

if !scope.allows_decl_hoisting {
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly are we doing here? Would it be possible add some snippet/example in the documentation? I don't really understand what "solving a pending reference" means in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this function and moved this code to the pop_scope. Hopefully comments there are enough.

shadowed: vec![],
allows_decl_hoisting,
});
}

fn pop_scope(&mut self, range: TextRange) {
Copy link
Contributor

@ematipico ematipico Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe some documentation/comments would help to understand the logic behind pop_scope. Probably because I find really hard to grasp the whole logic and why we are doing that. I think that maybe some snippet can help to explain why/what we are doing:

  • pushing events into the back of the stack
  • based on what we decide the type of event
  • the promotion of references

Comment on lines +39 to +44
ok_hoisting_redeclaration_before_use, r#"var a/*#A1*/ = 1;
function f() {
var a/*#A2*/ = 10;
console.log(a/*READ A2*/);
}
f();"#,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's worth adding a case where we have also console.log(a/*READ A1*/); outside of the function f

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants