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

Refactor transformer to use the instance of AstBuilder which is in TraverseCtx everywhere #6172

Closed
overlookmotel opened this issue Sep 30, 2024 · 4 comments · Fixed by #6209
Assignees
Labels
A-transformer Area - Transformer / Transpiler good first issue Experience Level - Good for newcomers

Comments

@overlookmotel
Copy link
Contributor

overlookmotel commented Sep 30, 2024

AstBuilder is the object which you use to generate AST nodes. It is heavily used in the transformer.

We currently have 2 copies of AstBuilder in transformer:

  • 1st copy is stored in TransformCtx
  • 2nd copy is stored in TraverseCtx.

This is problematic for 3 reasons:

  1. It blocks us from doing Move TransformCtx into TraverseCtx backlog#144, which will be a performance boost.
  2. For the introduction of AST node IDs (RFC: AST node IDs #5689), AstBuilder will become stateful, containing a counter for creating unique IDs. So only a single copy of it can exist.
  3. It's rather messy! Why do we have 2 copies anyway?

We need to go through the transformer and swap all uses of AstBuilder from TransformCtx over to use the copy in TraverseCtx.

For example:

let key = self.ctx.ast.property_key_identifier_name(SPAN, SOURCE);
let value = self.get_source_object(line, column, ctx);
self.ctx
.ast
.object_property_kind_object_property(SPAN, kind, key, value, None, false, false, false)

Should become:

let key = ctx.ast.property_key_identifier_name(SPAN, SOURCE); 
let value = self.get_source_object(line, column, ctx); 
ctx.ast.object_property_kind_object_property(SPAN, kind, key, value, None, false, false, false)

This will require passing TraverseCtx into some functions which it's currently not available in.

I have marked this as "good first issue" as it's not too technically tricky (but then again, there are always some unforeseen complications!), but is a good opportunity for anyone interested in the transformer to get to know the codebase.

Would anyone be able to help please?

@overlookmotel overlookmotel added good first issue Experience Level - Good for newcomers A-transformer Area - Transformer / Transpiler labels Sep 30, 2024
@shulaoda
Copy link
Member

🙌 I'm happy to

@shulaoda
Copy link
Member

shulaoda commented Sep 30, 2024

I found that there is also an AstBuilder instance in module_imports of TransformCtx. Does this need improvement?

@Dunqing
Copy link
Member

Dunqing commented Sep 30, 2024

I found that there is also an AstBuilder instance in module_imports of TransformCtx. Does this need improvement?

Leave module_imports for me, I am going to refactor it.

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Oct 1, 2024

Closed by #6173 and #6209. Thanks @shulaoda!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-transformer Area - Transformer / Transpiler good first issue Experience Level - Good for newcomers
Projects
None yet
3 participants