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

fix(ast): add RestElements in serialized AST to elements array #2567

Conversation

overlookmotel
Copy link
Contributor

@overlookmotel overlookmotel commented Mar 2, 2024

A step towards #2463.

This PR adds rest onto end of elements / properties array in JSON AST for ObjectPattern, ArrayPattern, ObjectAssignmentTarget, ArrayAssignmentTarget and FormalParameters.

@github-actions github-actions bot added the A-ast Area - AST label Mar 2, 2024
Copy link
Contributor Author

overlookmotel commented Mar 2, 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

Copy link

codspeed-hq bot commented Mar 2, 2024

CodSpeed Performance Report

Merging #2567 will not alter performance

Comparing 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array (f2cd4ed) with main (d76ee6b)

Summary

✅ 29 untouched benchmarks

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Mar 2, 2024

@Boshen This is incomplete at present. But I came across one query in the process:

ObjectPattern and ArrayPattern contain field rest: Option<Box<'a, BindingRestElement<'a>>>. BindingRestElement has a span field which covers the whole rest element (including the ...).

pub struct BindingRestElement<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub argument: BindingPattern<'a>,
}

However, ObjectAssignmentTarget and ArrayAssignmentTarget just have rest: Option<AssignmentTarget<'a>>, so no span for the entire rest element:

pub struct ObjectAssignmentTarget<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
pub properties: Vec<'a, AssignmentTargetProperty<'a>>,
pub rest: Option<AssignmentTarget<'a>>,
}

If it's useful to have a span including ..., should we add a type AssignmentRestElement which wraps AssignmentTarget here? Or if this span is not useful, should we remove BindingRestElement?

Either way, it'd be more consistent.

Base automatically changed from 03-02-fix_ast_rename_serialized_fields_to_camel_case to main March 2, 2024 14:47
@Boshen
Copy link
Member

Boshen commented Mar 2, 2024

On one hand I want the version without the ... span, to align with jsparagus https://gist.github.com/Boshen/0b481a058cd715576aaf1624d2c6d469#file-types_generated-rs-L643-L648

On another hand I want to align with the spec, which has the named node BindingRestElement

🤔 Let me experiment with which one is better next week.

@overlookmotel
Copy link
Contributor Author

OK cool, let's leave this PR as draft for now then, as it'll need to change one way or another.

I'm guessing the span including ... might be good for source maps.

@Boshen
Copy link
Member

Boshen commented Mar 3, 2024

After some experimentation, I'm in favor of adding AssignmentRestElement.

The span for the ellipsis ... is useful in many ways (diagnostics and finding the rest element).

@overlookmotel
Copy link
Contributor Author

OK great. You want to do that, or shall I?

@ArnaudBarre
Copy link
Contributor

Oh yeah didn't yet discover that mismatch on my testing but this is needed to be able to produce an ESTree and have correct range for this code ({a, ... b} = {})

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Mar 4, 2024

Does ESTree have a span for the ... alone, or for the whole rest element ...x? Or both?

@ArnaudBarre
Copy link
Contributor

For ... b only

@Boshen
Copy link
Member

Boshen commented Mar 4, 2024

OK great. You want to do that, or shall I?

Shall we get this PR merged, and I'll follow up with an update for AssignmentRestElement?

@overlookmotel
Copy link
Contributor Author

Could we possibly do it the other way around? A lot of this PR will need to be reverted after that change.

@Boshen
Copy link
Member

Boshen commented Mar 4, 2024

Could we possibly do it the other way around? A lot of this PR will need to be reverted after that change.

Sure, I'll dive in.

@Boshen
Copy link
Member

Boshen commented Mar 4, 2024

Could we possibly do it the other way around? A lot of this PR will need to be reverted after that change.

Sure, I'll dive in.

Done in #2601

@overlookmotel
Copy link
Contributor Author

Amazing! Thank you.

@overlookmotel overlookmotel force-pushed the 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array branch 2 times, most recently from 50cbb11 to 07c5190 Compare March 4, 2024 14:20
@overlookmotel overlookmotel marked this pull request as ready for review March 4, 2024 14:21
@overlookmotel
Copy link
Contributor Author

overlookmotel commented Mar 4, 2024

@ArnaudBarre I think I've got this right. But could you please check that you can now remove the modifications for RestElements from your JS-side transform?

I'll need to fix the types created by tsify, but would be good to know the shape of the objects are right first.

@overlookmotel overlookmotel marked this pull request as draft March 4, 2024 14:27
@ArnaudBarre
Copy link
Contributor

Didn't have time to test today. My current fork is main only so checkout of your branch was not trivial (I didn't find a settings to revert main only fork). @Boshen is there a rule for being able to work directly on the main repo? If this is too early for me I will temporary update the remote of my repo tomorrow to test it

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Mar 5, 2024

Oh I'm sorry, I assumed that'd be easy, or I wouldn't have asked. Don't spend your time on it. I'll fix the types, we can get this merged, and then if it's incorrect in some way, can iterate again.

@ArnaudBarre
Copy link
Contributor

I started testing, it fixed a weird case I still need to understand if this is an bug from my wrapper or OXC. And I got another issue but maybe related to the binding pattern change, I will continue tonight

@overlookmotel overlookmotel force-pushed the 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array branch 3 times, most recently from 72c3c15 to eba945f Compare March 7, 2024 23:14
@overlookmotel overlookmotel force-pushed the 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array branch 2 times, most recently from 4cd286f to 6981385 Compare March 8, 2024 01:31
@overlookmotel overlookmotel marked this pull request as ready for review March 8, 2024 01:36
@overlookmotel
Copy link
Contributor Author

overlookmotel commented Mar 8, 2024

OK, I think this is ready now.

Obviously there are more changes still required, but this at least merges rest into elements / properties / items - which is how ESTree represents it.

@ArnaudBarre Once it's merged please let me know if you find any mistakes. FormalParameters in particular is a bit of a pain.

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.

Amazing! I'll never figure out how serde and all these different lifetimes work ...

@Boshen
Copy link
Member

Boshen commented Mar 8, 2024

Merge conflict 😅 Feel free to resolve and merge.

@overlookmotel overlookmotel force-pushed the 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array branch from 6981385 to f2cd4ed Compare March 8, 2024 02:46
@overlookmotel
Copy link
Contributor Author

Oh bollocks! It conflicted with my other PR. Should have stacked them.

Conflict fixed now, but please let tests pass before merging. Had to make a lot of changes to fix the conflicts.

@Boshen Boshen enabled auto-merge (squash) March 8, 2024 02:49
@Boshen Boshen merged commit 88f94bb into main Mar 8, 2024
21 of 35 checks passed
@Boshen Boshen deleted the 03-02-fix_ast_add_RestElement_s_in_serialized_AST_to_elements_array branch March 8, 2024 02:56
overlookmotel added a commit that referenced this pull request Mar 10, 2024
Fix a mistake I made in #2567. Length that `serialize_seq` is called
with should only be `+1` if there is a rest element being added on the
end.

Makes no difference for serializing to JSON, as JSON serializer doesn't
use the `len` value, but still better to get it right.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast Area - AST
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants