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(es/minifier): Don't create invalid nodes #6191

Merged
merged 69 commits into from Oct 21, 2022

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Oct 18, 2022

Description:

  • A sequential expression should have at leat two elements.

Related issue:

@kdy1 kdy1 added this to the Planned milestone Oct 18, 2022
@kdy1 kdy1 self-assigned this Oct 18, 2022
@kdy1 kdy1 changed the title fix(es/minifier): Don't create invalid nodes refactor(es/minifier): Don't create invalid nodes Oct 18, 2022
Copy link
Member Author

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_transforms_optimization

crates/swc_html_parser/tests/html5lib_tests.rs Outdated Show resolved Hide resolved
Copy link
Member Author

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_transforms_optimization

@@ -1,5 +1,6 @@
//// [ES5For-of16.ts]
for(var _i = 0, _iter = []; _i < _iter.length; _i++){
_iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)_iter1[_i1];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++);
Copy link
Member Author

Choose a reason for hiding this comment

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

These tests changes are bugfix

Copy link
Member Author

Choose a reason for hiding this comment

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

Previously we generated foo = Expr::Seq(seq) so logic expecting identifier in foo didn't work correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now we generate foo = seq.0 so it's analyzed/processed correctly

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this output is actually incorrect for the input. The following line should be inside the inner for loop, not after.

@kdy1 kdy1 marked this pull request as ready for review October 18, 2022 05:41
kodiakhq[bot]
kodiakhq bot previously approved these changes Oct 18, 2022
@kdy1 kdy1 requested a review from jridgewell October 18, 2022 05:41
@kdy1 kdy1 marked this pull request as draft October 18, 2022 06:26
@kdy1 kdy1 marked this pull request as ready for review October 18, 2022 09:57
kodiakhq[bot]
kodiakhq bot previously approved these changes Oct 18, 2022
kodiakhq[bot]
kodiakhq bot previously approved these changes Oct 18, 2022
@@ -1,5 +1,6 @@
//// [ES5For-of16.ts]
for(var _i = 0, _iter = []; _i < _iter.length; _i++){
_iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)_iter1[_i1];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++);
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this output is actually incorrect for the input. The following line should be inside the inner for loop, not after.

@@ -986,7 +982,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

return Some(Expr::Seq(e));
let mut e = Expr::Seq(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we know exprs has more than 1 element?

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 normalize instead of verifying, at the line below.
I think we can improve this in the future

@@ -1008,7 +1006,9 @@ where
};
self.merge_sequences_in_seq_expr(&mut e);

Some(Expr::Seq(e))
let mut e = Expr::Seq(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

kodiakhq[bot]
kodiakhq bot previously approved these changes Oct 20, 2022
@kdy1 kdy1 requested a review from jridgewell October 20, 2022 08:26
match s {
Stmt::Decl(Decl::Var(v)) if v.decls.is_empty() => {
s.take();
if self.prepend_stmts.is_empty() && self.append_stmts.is_empty() {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the fix for wrong appending


if seq.exprs.len() == 1 {
*e = *seq.exprs.take().into_iter().next().unwrap();
self.normalize_expr(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: isn't this already normalized from the above for e in seq.exprs? Can extracting it out of the sequence expression change the normalization?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I think you are right

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually it had an effect beause for e does not handle len() == 1

@kdy1 kdy1 enabled auto-merge (squash) October 21, 2022 00:03
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script


@kdy1 kdy1 merged commit ea03ce1 into swc-project:main Oct 21, 2022
@kdy1 kdy1 deleted the more-rules branch October 21, 2022 00:33
@kdy1 kdy1 modified the milestones: Planned, v1.3.10 Oct 21, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants