Commit graph

156 commits

Author SHA1 Message Date
Boshen
f8200a8882 feat(minifier): minimize if(foo) bar -> foo && bar (#8121) 2024-12-26 09:01:38 +00:00
Boshen
72d996709e feat(minifier): add Normalize ast pass (#8120) 2024-12-26 07:02:45 +00:00
Boshen
fef0b25fd3 feat(minifier): collapse var into for loop initializer (#8119)
`var a = 0; for(;a<0;a++) {}` => `for(var a = 0;a<0;a++) {}`
2024-12-26 05:22:02 +00:00
Boshen
c22062bab3 refactor(minifier): cleanup peephole_minimize_conditions (#8114) 2024-12-25 15:47:42 +00:00
Boshen
2331ea85d9 feat(minifier): typeof foo === 'number' => typeof foo == 'number' (#8112) 2024-12-25 14:52:58 +00:00
Boshen
e594c3988d refactor(minifier): clean up peephole_substitute_alternate_syntax.rs (#8111) 2024-12-25 13:54:51 +00:00
Boshen
b605baa4ac fix(minifier): constant fold strings with tab char (#8096) 2024-12-24 12:15:06 +00:00
Boshen
5397fe978a feat(minifier): constant fold undefined?.bar -> undefined (#8075) 2024-12-23 15:20:37 +00:00
Boshen
8b54d898b5 refactor(minifier): remove parens must happen on enter (#8060) 2024-12-23 06:56:36 +00:00
翠 / green
1932f1e0a0
feat(minifier): fold foo === undefined || foo === null (#8063)
This PR implements folding `foo === undefined || foo === null` into `foo
== null`.

I checked the minified output diff this time, so hoping that there isn't
a bug.
2024-12-23 00:10:44 +08:00
Boshen
7cb84f34d1 refactor(minifier): only minify on ast node exit (#8059) 2024-12-21 11:08:38 +00:00
Boshen
77d845a6b8 refactor(minifier): fuse DCE AST passes (#8058) 2024-12-21 10:30:38 +00:00
Boshen
6123f5e6ff refactor(minifier): fold statements on exit (#8057) 2024-12-21 10:06:04 +00:00
翠 / green
de8a86e356
fix(minifier): incorrect minification in try_fold_left_child_op (#7949)
fixes #7944
2024-12-17 10:40:22 +08:00
Boshen
4799471c1e fix(minfier): bigint bitwise operation only works with bigint (#7937) 2024-12-16 13:25:39 +00:00
翠 / green
075bd165a8
feat(minifier): fold bitwise operation (#7908)
This PR implements constant evaluation for bitwise operations (`&`, `|`,
`^`).

I wanted to play around with the minifier a bit 🙂
2024-12-15 21:27:05 +08:00
overlookmotel
3858221f45 refactor(global): sort imports (#7883)
Pure refactor. Re-order imports for clarity:

1. `std`
2. External crates
3. `oxc_*` crates
4. Current crate `use crate::...`
5. Super `use super::...`
6. Local modules

This order is from "furthest away" to "closest". This makes it clearer to see what is coming from where.

`cargo +nightly fmt` (#7877) did a lot of the work, but unfortunately `rustfmt` does not have an option to (a) put workspace crates in a separate block from external crates and (b) move `mod` statements to after `use` statements.
2024-12-14 15:07:21 +00:00
Boshen
7fb9d47460 style(rust): cargo +nightly fmt (#7877) 2024-12-14 06:03:31 +00:00
Song Gao
ebc80f6749
refactor(ast)!: change 'raw' from &str to Option<Atom> (#7547)
Fix #7254 

Changed all "raw" properties of literal types (if they have this property) to `Option<Atom>`.

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2024-12-05 00:34:45 +00:00
Boshen
62a8c5eb59 chore(minifier): temporary remove try_fold_if_block_one and try_fold_if_one_child (#7536)
They are not idempotent.
2024-11-29 05:05:07 +00:00
Boshen
d942a8d41a
chore: Rust v1.83.0 changes (#7535)
This PR does not upgrade rustc. Only changes are applied.

We cannot upgrade to the lastet Rust version yet due to wasm-bindgen
breaking some generated types.

THere's also some elided lifetimes in `**/generated/**`, which requires
modification to ast tools.
2024-11-29 11:59:45 +08:00
Boshen
896ff860f9 fix(minifier): do not fold if statement block with lexical declaration (#7519) 2024-11-28 10:37:41 +00:00
Boshen
625a5bad02 refactor(minifier): improve ast passes (#7518) 2024-11-28 09:33:31 +00:00
overlookmotel
f2f31a8d7a
fix(traverse)!: remove unsound APIs (#7514)
It's essential to `oxc_traverse`'s safety scheme that the user cannot create a `TraverseAncestry`, because they could then substitute it for the one stored in `TraverseCtx`, and cause a buffer underrun when an ancestor gets popped off stack which should never be empty - but it is because user has sneakily swapped it for another one.

Not being able to create a `TraverseAncestry` also requires that user cannot obtain an owned `TraverseCtx` either, because you can obtain an owned `TraverseAncestry` from an owned `TraverseCtx`.

Therefore, it's unsound for `TraverseCtx::new` to be public.

However, it is useful in minifier to be able to re-use the same `TraverseCtx` over and over, which requires having an owned `TraverseCtx`.

To support this use case, introduce `ReusableTraverseCtx`. It is an opaque wrapper around `TraverseCtx`, which prevents accessing the `TraverseCtx` inside it. It's safe for user to own a `ReusableTraverseCtx`, because there's nothing they can do with it except for using it to traverse via `traverse_mut_with_ctx`, which ensures the safety invariants are upheld.

At some point, we'll hopefully be able to reduce the number of passes in the minifier, and so remove the need for `ReusableTraverseCtx`.But in the meantime, this keeps `Traverse`'s API safe from unsound abuse.

Note: Strictly speaking, there is still room to abuse the API and produce UB by initiating a 2nd traversal of a different AST in an `Traverse` visitor, and then `mem::swap` the 2 x `&mut TraverseCtx`s. But this is a completely bizarre thing to do, and would basically require you to write malicious code specifically designed to cause UB, so it's not a real risk in practice. We'd need branded lifetimes to close that hole too.

So this PR doesn't 100% ensure safety in a formal sense, but it at least makes it very hard to trigger UB *by accident*, which was the risk before.
2024-11-28 10:30:53 +08:00
Boshen
b0e1c03284
feat(ast)!: add StringLiteral::raw field (#7393)
part of #7254
2024-11-26 23:33:56 +08:00
7086cmd
97af341cac feat(minifier): minify alternated one child if block (#7231) 2024-11-26 15:00:16 +00:00
Ethan Goh
ac0d25c426
feat(minifier): minify one child if statement expression (#7230) 2024-11-26 22:48:27 +08:00
Boshen
c133693e5c perf(minifier): fuse ast passes (#7493) 2024-11-26 14:30:04 +00:00
overlookmotel
39afb48025 feat(allocator): introduce Vec::from_array_in (#7331)
Because we lack specialization in stable Rust, `Vec::from_iter_in` is unable to take advantage of the fact that `[T; N]` has a statically knowable size.

Introduce `Vec::from_array_in` for this case, which should be able to create the `Vec` with a single static-sized memcpy, or may allow the compiler to see that it can construct the array directly in the arena, rather than construct on stack and then copy to the arena.

Also add a corresponding `AstBuilder::vec_from_array` method, and use it in various places in codebase.
2024-11-18 02:35:46 +00:00
Song Gao
cf3415b0e4
chore(doc): replace main/master to tag/commit to make the url always accessible (#7298) 2024-11-16 21:00:30 +08:00
7086cmd
cf99be0a0d fix(minifier): do not compare bigint with object (#7294)
@Boshen, could you please update the snap of runtime and commit to this PR? I want to see the effects after the unit tests are added.
2024-11-16 06:01:06 +00:00
7086cmd
0d6a66aa9e test(minifier): fix minimize condition tests (#7222) 2024-11-11 08:36:19 +00:00
7086cmd
a2977655c8 refactor(minifier): use map and and_then instead of let else (#7178)
For the test case, Closure Compiler doesn't handle this at all in the REPL! If it's necessary, I will turn it back.

This PR uses builtin `and_then` and `map` method, which is better instead of a lot of `if let Some`.
2024-11-09 11:48:48 +00:00
overlookmotel
97caae16e2 refactor(minifier): do not use AstBuilder::*_from_* methods (#7072)
Preparation for #7073. Avoid using `AstBuilder::*_from_*` methods to construct enums, use explicit construction instead.

Before:

```rs
let ident = self.ast.binding_pattern_kind_from_binding_identifier(ident);
```

After:

```rs
let ident = BindingPatternKind::BindingIdentifier(ident);
```

Often this produces shorter code, as well as (in my opinion) being easier to read.
2024-11-02 01:22:56 +00:00
Boshen
2c7ac29ece refactor(minifier): remove Tri, use Option<bool> instead (#6912) 2024-10-26 04:28:34 +00:00
Boshen
741571f645 refactor(minifier): remove extra compress options (#6893)
Closure Compiler and ESBuild does not have these kind of granularity.
2024-10-26 01:30:13 +00:00
Boshen
8bcaf59cf9 feat(minifier): late peeophole optimization (#6882) 2024-10-25 16:12:29 +00:00
camc314
860cbca446 feat(minifier): implement folding simple arrow fns (#6875)
basically
```ts
const foo = () => {
    return baz
}
```
becomes
```ts
const foo = () => baz
```
2024-10-25 10:26:33 +00:00
Boshen
442975408b feat(ecmascript): constant eval null to number (#6879) 2024-10-25 04:23:24 +00:00
camc314
c26020e553 feat(minifier): implement folding String.prototype.replaceAll (#6871) 2024-10-25 01:29:57 +00:00
camc314
50744f341b feat(minifier): implement folding String.prototype.replace (#6870) 2024-10-25 01:29:57 +00:00
camc314
fccf82e4df feat(minifier): implement folding substring string fns (#6869) 2024-10-25 01:29:56 +00:00
camc314
322f9d44b7 chore(minifier): remove planned minification for String.substr as its deprecated (#6868)
According to mdn, `String.prototype.substr()` has been deprecated, hence i do not think it makes sense to attempt to optimize this code.

This pull request removes the test cases along with the placeholder code for minification of this method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
2024-10-24 14:53:22 +00:00
camc314
e6a5a1b11c feat(minifier): implement folding charCodeAt string fns (#6475) 2024-10-24 14:37:50 +00:00
Boshen
a47c70e425 fix(minifier): fix remaining runtime bugs (#6855) 2024-10-24 12:22:18 +00:00
Boshen
686727fc96 fix(minifier): reference read has side effect (#6851) 2024-10-24 08:32:38 +00:00
Boshen
c658d9336d fix(minifier): keep template literals with expressions (#6849) 2024-10-24 08:12:44 +00:00
Boshen
fd57e00108 feat(ecmascript): add abstract_relational_comparison to dce (#6846)
I removed bigint comparisons because they were incorrect
2024-10-24 06:46:07 +00:00
Boshen
ca799936b0 fix(minifier): do not dce object literals yet (#6839)
closes #6769
2024-10-24 01:05:07 +00:00
Boshen
ec5a19b880 fix(minifier): do not remove binary expressions (#6829) 2024-10-23 16:07:29 +00:00