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

Make expr_simplifier handle expressions when indexing strings, arrays and objects #8750

Open
wants to merge 59 commits into
base: main
Choose a base branch
from

Conversation

levi-nz
Copy link
Contributor

@levi-nz levi-nz commented Mar 15, 2024

Description:
This PR is a WIP that fixes all known issues in #8747.

BREAKING CHANGE:

Related issue (if exists):
Closes #8747

@levi-nz levi-nz requested a review from a team as a code owner March 15, 2024 08:39
Copy link

changeset-bot bot commented Mar 15, 2024

⚠️ No Changeset found

Latest commit: 5458077

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 15, 2024

Not totally sure if the changed code for array literals is correct -- the code for this is a little more complicated than strings.

[][0] works correctly for now, but not [][[]]. Will look into this later.

@levi-nz levi-nz marked this pull request as draft March 16, 2024 03:28
@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 16, 2024

Another broken test case:

var _ = {0.5: 'test'}[0.5];

Currently simplifies to:

var _ = {
    0.5: 'test'
}[0.5];

Should simplify to:

var _ = 'test';

Not entirely sure if this is correct though because removing the ObjectLit could have side effects, ie if a value is a CallExpr

@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 16, 2024

Only one potential issue so far, that KnownOp no longer implements Eq because of Index now taking f64 instead of i64, but seems OK so far to me.

@kdy1 kdy1 self-assigned this Mar 18, 2024
@kdy1 kdy1 added this to the Planned milestone Mar 18, 2024
@levi-nz levi-nz marked this pull request as ready for review March 18, 2024 01:50
@levi-nz levi-nz changed the title (WIP) Make expr_simplifier return undefined when indexing out of bounds Make expr_simplifier handle expressions when indexing strings, arrays and objects Mar 18, 2024
@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 18, 2024

@kdy1 The only remaining issue in terms of code changes is this line. Currently is_literal(props) does not work with the unit test below because the 0.5 is not considered a literal. I'm not sure why this fract() check exists and I'm not sure if changing this would break something.

https://github.com/levi-nz/swc/blob/454378d7eaac82bae40c5a770888e84580a1381b/crates/swc_ecma_utils/src/lib.rs#L1883

fold("({0.5: 'a'})['0.5']", "'a';");

@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 18, 2024

I'm pretty sure Index(f64) can be changed back to Index(i64) as well, as IndexStr is always used for objects.

@levi-nz
Copy link
Contributor Author

levi-nz commented Mar 18, 2024

I have ran UPDATE=1 cargo test --test projects --test tsc, but not sure if all of these files should be changed, especially crates/swc/tests/tsc-references/templateStringInIndexExpressionES6.2.minified.js, which is now empty.

Copy link
Member

@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.

CI failed

@levi-nz
Copy link
Contributor Author

levi-nz commented Apr 8, 2024

To do:

  • Move stuff to ctx
  • Handle known object properties
  • Possibly add more unit tests

Will comment here again when everything is finalised

@levi-nz
Copy link
Contributor Author

levi-nz commented Apr 10, 2024

Is there a reason why stuff like [1][0] was replaced with (0, 1) before instead of just 1? The tests seem to pass fine but I want to be sure. I don't see why it should be replaced with a SeqExpr instead of just a Literal if there are no side effects.

@levi-nz
Copy link
Contributor Author

levi-nz commented Apr 10, 2024

cargo test -p swc_ecma_minifier terser_exec_tests__terser__compress__evaluate__unsafe_string_bad_index__input_js --features concurrent currently fails with a weird infinite loop bug, doesn't seem to be related to optimize_member_expr, need to investigate further

@kdy1
Copy link
Member

kdy1 commented Apr 22, 2024

(0, foo) is about making this undefined

@levi-nz
Copy link
Contributor Author

levi-nz commented Apr 25, 2024

(0, foo) is about making this undefined

Does this apply to objects too? Like should ({a: 5}).a be replaced with (0, 5) or just 5? Currently it's replaced with the latter but I want to be sure

@levi-nz
Copy link
Contributor Author

levi-nz commented Apr 25, 2024

Need to handle known object properties in simplify's optimize_member_expr. Will require a function signature change to accept obj and prop instead of expr. This PR should be ready for review tomorrow.

@kdy1
Copy link
Member

kdy1 commented May 5, 2024

(0, foo) is about making this undefined

Does this apply to objects too? Like should ({a: 5}).a be replaced with (0, 5) or just 5? Currently it's replaced with the latter but I want to be sure

I don't think so. this only matters for member exprs or direct eval calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

expr_simplifier does not simplify computed MemberExprs with non-literal properties
2 participants