-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(es/minifier): Handle more indexing expression #8750
Conversation
|
Not totally sure if the changed code for array literals is correct -- the code for this is a little more complicated than strings.
|
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 |
Only one potential issue so far, that |
expr_simplifier
return undefined
when indexing out of boundsexpr_simplifier
handle expressions when indexing strings, arrays and objects
@kdy1 The only remaining issue in terms of code changes is this line. Currently fold("({0.5: 'a'})['0.5']", "'a';"); |
I'm pretty sure |
I have ran |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI failed
This is getting a little complicated with property names, since we need to either correctly handle stuff like The solution I've implemented works and I don't think there's any issues, besides the fact that code like this will break: Object.prototype.foobar = 5;
var _ = ({}).foobar; // returns 5, but will return undefined from the minifier If there's no way to account for this correctly, we should ideally make this a configurable option where the user can make the minifier replace stuff like If we continue with this approach, should we include deprecated properties like In my opinion, replacing invalid properties needs to be a configurable option. In my use case, I want to replace invalid properties and ignore potential side effects, but this will introduce breaking changes for larger scripts and bundled scripts where other scripts are potentially changing the prototype of String, Object or Array. I assume there's no way to detect if the prototype of these are being changed, so it should just be an option that can be enabled/disabled. The whole reason this feature is implemented is so expressions like |
Babel's minify doesn't modify expressions like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move logics to https://github.com/swc-project/swc/blob/8eac0bec49f4f260a261d87f174ed2913c4bee63/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
swc_ecma_utils
is not a good place for this kind of optimizations
Do we not care about modified prototypes? For example, in the current release version:
We replace Array.prototype[-1] = "foo";
[][-1] // outputs "foo", but swc replaces with void 0 Shouldn't this logic be moved to compress as well? |
There's an option named |
Got it, I will update the minifier accordingly to try use it -- I think that option should be checked here in the aforementioned case |
Not sure why this exists, perhaps a bug with side effects?
Current test case |
I seem to have fixed this but not sure if it will cause side effects anywhere, will try take a look. It appears as |
Also, CI currently fails because of this. Is this normal? These unit tests don't seem to work on Windows, will try this on Mac later.
|
Not sure if I got the Where should I create unit tests for the new compress functionality? I assume in |
You can create a directory and place an Note that you need to touch (or save again) |
Will add Not sure if there's any other exclusions we should add for property/method polyfills that don't exist anymore |
Had to manually fix some files as rebasing messed a few up, hope that's OK |
expr_simplifier
handle expressions when indexing strings, arrays and objects
crates/swc_ecma_transforms_optimization/src/simplify/expr/tests.rs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
swc-bump:
- swc_ecma_utils
- swc_ecma_minifier
- swc_ecma_transforms_optimization
- swc_core
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
swc-bump:
- swc_ecma_utils
- swc_ecma_minifier
- swc_ecma_transforms_optimization
- swc_core
Description:
This PR is a WIP that fixes all known issues in #8747.
Related issue:
expr_simplifier
does not simplify computedMemberExpr
s with non-literal properties #8747