Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformer/react): correct import binding names #3014

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Dunqing
Copy link
Member

@Dunqing Dunqing commented Apr 17, 2024

No description provided.

Copy link
Member Author

Dunqing commented Apr 17, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @Dunqing and the rest of your teammates on Graphite Graphite

@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Apr 17, 2024
@@ -79,6 +82,7 @@ impl<'a> Transformer<'a> {

impl<'a> VisitMut<'a> for Transformer<'a> {
fn visit_program(&mut self, program: &mut Program<'a>) {
self.ctx.borrow_mut().naming.visit_program(program);
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to collect all bindings first.

Copy link

codspeed-hq bot commented Apr 17, 2024

CodSpeed Performance Report

Merging #3014 will degrade performances by 4.13%

Comparing 04-17-feat_transformer_react_correct_import_binding_names (9660992) with main (5d89e75)

Summary

❌ 3 regressions
✅ 27 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main 04-17-feat_transformer_react_correct_import_binding_names Change
transformer[RadixUIAdoptionSection.jsx] 922.1 µs 961.9 µs -4.13%
transformer[antd.js] 1.5 s 1.6 s -3.62%
transformer[cal.com.tsx] 376.9 ms 389.4 ms -3.2%


pub type Ctx<'a> = Rc<TransformCtx<'a>>;
Copy link
Member Author

Choose a reason for hiding this comment

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

TransformerNaming needs to mutation.

In a previous implementation. All the TransformerCtx fields that cannot be copied are wrapped in Rc

#[derive(Clone)]
pub struct TransformerCtx<'a> {
pub ast: Rc<AstBuilder<'a>>,
pub options: Cow<'a, TransformOptions>,
semantic: Rc<RefCell<Semantic<'a>>>,
errors: Rc<RefCell<Vec<Error>>>,
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a big change.

It probably makes more sense to use interior mutability in TransformerNaming, instead of adding all these borrow calls everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

It probably makes more sense to use interior mutability in TransformerNaming, instead of adding all these borrow calls everywhere.

Yes, I agree. When we finally consider this PR, I'll refactor it.

@Boshen
Copy link
Member

Boshen commented Apr 17, 2024

Let me iterate on this tomorrow.

@Boshen
Copy link
Member

Boshen commented Apr 18, 2024

I'll find sometime to figure out recreating scopes and bindings during visits, probably this weekend.

@milesj
Copy link
Collaborator

milesj commented Apr 18, 2024

I'll find sometime to figure out recreating scopes and bindings during visits, probably this weekend.

The problem we had before is that the nodes don't have IDs, so there was no easy way to back reference. That's why @rzvxa was trying to add IDs to everything.

@rzvxa
Copy link
Collaborator

rzvxa commented Apr 19, 2024

I'll find sometime to figure out recreating scopes and bindings during visits, probably this weekend.

The problem we had before is that the nodes don't have IDs, so there was no easy way to back reference. That's why @rzvxa was trying to add IDs to everything.

We still can add this, I didn't receive any pushback while implementing it in #2876 and #2818, I just stopped to figure out the traverse first; Now that the traverse is almost sorted out we may want to revisit the ast_node_id.
Since now we have a parent field for all nodes because of the traverse, We can change the parent with an AstPath structure containing both the opaque parent's pointer and our own ID.

@rzvxa
Copy link
Collaborator

rzvxa commented Apr 19, 2024

I suspect that by adding node_ids to the nodes we can do a minimal refactor for linters which removes a bunch of useless checks and improves the performance by like 10% to 20%.

We can still provide a way to handle AstNodes so we don't have to refactor them all from day one, But we can offer additional traits to intercept the inner node without needing extra match on AstKind.


Edit:

The whole reason behind AstNode is having semantic information along with our nodes.

@rzvxa
Copy link
Collaborator

rzvxa commented Apr 19, 2024

I'm just thinking this one out loud;

Since now we have a parent pointer - even though it is opaque - we might be able to map between that and our own ast_node_id so we don't have to add an extra usize to our nodes.

For example if we always allocate all of our nodes in the same buffer and we have the root of our buffer we can find our relative position to that and use it to fetch our relative data in a different buffer similar to an indexing operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-transformer Area - Transformer / Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants