Skip to content

fix(transformer): use UIDs in TS namespace transforms#3395

Merged
Dunqing merged 3 commits intomainfrom
05-23-fix_transformer_use_uids_in_ts_namespace_transforms
May 27, 2024
Merged

fix(transformer): use UIDs in TS namespace transforms#3395
Dunqing merged 3 commits intomainfrom
05-23-fix_transformer_use_uids_in_ts_namespace_transforms

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented May 23, 2024

Use the TraverseCtx::generate_uid method introduced in #3395 to fix some of the TS namespace test cases.

But... I honestly have no idea what I'm doing here!

I am running up against a combination of 3 different areas where I know very little:

  1. I am unfamiliar with Semantic.
  2. I am unfamiliar with Oxc's conventions for writing transforms.
  3. I don't "speak" Typescript!

This PR should not be merged as is. I'm pretty sure it's wrong. I've done it mostly just to test out generate_uid.

In particular:

  1. Is SymbolFlags::FunctionScopedVariable the right flags to use in all cases here?
  2. generate_uid creates a symbol, and registers a binding for it in the scope tree. So if there are any branches in this logic where name doesn't actually get used after generate_uid is called, then it's not right.
  3. Identifiers which are added to AST should also have corresponding ReferenceIds created for them (but I imagine this is also missing in many other places in the transformer).

On point 2: We could split up generate_uid into 2 functions. One function would find a valid UID name but not register a binding for it. If you want to actually use that name, you'd call a 2nd function to generate the binding. Would that be a better API?

Could someone help me out to progress this please?

(Sorry my lack of knowledge is a bit useless here. I will learn all these things in time, but just trying to be honest about where I'm at right now. I'm sure I could figure it out myself, but it would take me hours, whereas others will probably look at it and know what to do in about 5 mins.)

@graphite-app
Copy link
Contributor

graphite-app bot commented May 23, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label May 23, 2024
Copy link
Member Author

overlookmotel commented May 23, 2024

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

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

@codspeed-hq
Copy link

codspeed-hq bot commented May 23, 2024

CodSpeed Performance Report

Merging #3395 will not alter performance

Comparing 05-23-fix_transformer_use_uids_in_ts_namespace_transforms (2fcf871) with main (d80e9b4)

Summary

✅ 22 untouched benchmarks

@Boshen Boshen requested a review from Dunqing May 24, 2024 03:18
Copy link
Member

@Boshen Boshen left a comment

Choose a reason for hiding this comment

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

The code was setup this way so you can just replace it, and it's working 😁

Copy link
Member

@Dunqing Dunqing left a comment

Choose a reason for hiding this comment

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

In particular:

  1. Is SymbolFlags::FunctionScopedVariable the right flags to use in all cases here?

Correct!

  1. generate_uid creates a symbol, and registers a binding for it in the scope tree. So if there are any branches in this logic where name doesn't actually get used after generate_uid is called, then it's not right.

Yes.

  1. Identifiers which are added to AST should also have corresponding ReferenceIds created for them (but I imagine this is also missing in many other places in the transformer).

Yes, we need to bind all unresolved references for the new binding.

On point 2: We could split up generate_uid into 2 functions. One function would find a valid UID name but not register a binding for it. If you want to actually use that name, you'd call a 2nd function to generate the binding. Would that be a better API?

I think we can have all three API's at the same time, but not now, not until we really need it!

Haha, I think this PR we can merge. You did the right thing.

@Dunqing
Copy link
Member

Dunqing commented May 24, 2024

The remaining test cases are for me to take over, and I'm eager to start using these APIs!

Base automatically changed from 05-23-feat_transformer_add_traversectx_generate_uid_ to main May 24, 2024 09:19
@overlookmotel overlookmotel force-pushed the 05-23-fix_transformer_use_uids_in_ts_namespace_transforms branch from 35eca6d to 2fe6adf Compare May 26, 2024 20:58
@github-actions github-actions bot added the A-semantic Area - Semantic label May 26, 2024
@overlookmotel overlookmotel marked this pull request as ready for review May 26, 2024 23:37
@overlookmotel
Copy link
Member Author

overlookmotel commented May 26, 2024

I've been through this more thoroughly now. Fixed one problem with a scope binding being registered where no actual binding is created.

But found another problem:

// TODO: This binding is created in wrong scope.
// Needs to be created in scope of function which `transform_namespace` creates below.
let name = self.ctx.ast.new_atom(
&ctx.generate_uid_in_current_scope(real_name, SymbolFlags::FunctionScopedVariable),
);

I'm not quite sure best way to tackle that. If you guys are happy to merge this in its partially-broken state, with the "TODO" comment, then please do so.

The downside of having scopes in the transformer is that we now have to keep them in sync with changes to the AST. I imagine we'll have to do a 2nd pass through all the transforms implemented so far and find all the places where that's not happening.

@Dunqing
Copy link
Member

Dunqing commented May 27, 2024

I'm not quite sure best way to tackle that. If you guys are happy to merge this in its partially-broken state, with the "TODO" comment, then please do so.

Let's merge this, and I will take a try at this problem

@Dunqing Dunqing merged commit d4371e8 into main May 27, 2024
@Dunqing Dunqing deleted the 05-23-fix_transformer_use_uids_in_ts_namespace_transforms branch May 27, 2024 00:53
overlookmotel added a commit that referenced this pull request May 27, 2024
Use the `TraverseCtx::generate_uid` method introduced in #3395 to fix
some of the TS namespace test cases.

But... I honestly have no idea what I'm doing here!

I am running up against a combination of 3 different areas where I know
very little:

1. I am unfamiliar with `Semantic`.
2. I am unfamiliar with Oxc's conventions for writing transforms.
3. I don't "speak" Typescript!

This PR should not be merged as is. I'm pretty sure it's wrong. I've
done it mostly just to test out `generate_uid`.

In particular:

1. Is `SymbolFlags::FunctionScopedVariable` the right flags to use in
all cases here?
2. `generate_uid` creates a symbol, and registers a binding for it in
the scope tree. So if there are any branches in this logic where `name`
doesn't actually get used after `generate_uid` is called, then it's not
right.
3. Identifiers which are added to AST should also have corresponding
`ReferenceId`s created for them (but I imagine this is also missing in
many other places in the transformer).

On point 2: We could split up `generate_uid` into 2 functions. One
function would find a valid UID name *but not register a binding for
it*. If you want to actually use that name, you'd call a 2nd function to
generate the binding. Would that be a better API?

Could someone help me out to progress this please?

(Sorry my lack of knowledge is a bit useless here. I will learn all
these things in time, but just trying to be honest about where I'm at
right now. I'm sure I could figure it out myself, but it would take me
hours, whereas others will probably look at it and know what to do in
about 5 mins.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-semantic Area - Semantic A-transformer Area - Transformer / Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants