oxc/crates
sapphi-red a4ae4505f1
feat(minifier): fold array concat chaining (#8440)
Compress `[].concat(a).concat(b)` into `[].concat(a, b)`.

**References**
- [Spec of `Array::concat`](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.concat)

---

### The new assumption

I added a new assumption description in `crates/oxc_minifier/README.md`: "Errors thrown when creating a String or an Array that exceeds the maximum length can disappear or moved".
This is an assumption held by other minifiers. Without this assumption, we have to treat `+` and array creation/update to have a sideeffect and that limits the minification.

For input:
```js
[...Array(Number(2n ** 32n - 1n)),""]
" ".repeat(Number(2n ** 29n - 24n - 1n)) + ' '
export {}
```
(Note that `2 ** 32 - 1` is the max array length and `2 ** 29 - 24` is the max string length on V8.)
This code errors with too long array and too long string on V8 based runtimes.

terser outputs:
```js
Array(Number(2n**32n-1n))," ".repeat(Number(536870887n));export{};
```
No error will happen with this code.

SWC outputs:
```js
[...Array(Number(2n**32n-1n))]," ".repeat(Number(2n**29n-24n-1n));export{};
```
No error will happen with this code.
[playground](https://play.swc.rs/?version=1.10.7&code=H4sIAAAAAAAAA4vW09NzLCpKrNTwK81NSi3SMMpT0NJSMAZSugqGeZqaOkpKsbxcSgpKekWpBamJJajqjCxB6oxMYKoVtBXUFdR5uVIrCvKLShSqa3m5AGRqy%2FBiAAAA&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOTYIyvXxoCNCxJuO7L0RJtjeG3En48AMkAOL7bbfrTmi6j903f%2FLPoAtCuf2zBcdE%2BsKWDkzUaIofqHtf6Qkn5HRAaKbrTDrSpQdqKtz%2F2u8XRRdyRlgViy365N34mNPkOBRAfLCxlUPWCInwf%2F3CSv6aAJX6aD%2FkHECnF0RpVD4R9FCkwCaHoAcEddZFiDKdVBePWUoxwUpg1VDyIPJkPfmcOOcztaCtMtmCgHwBQ%2F4MkoxzsSwhX0%2B4T8MWDrXvW59%2FqOGsQ9Uk5IRLawmfVoh6zB5JuZqkEs5wowYzXIr7U%2BmdKkC1pGfdKfu00ZO%2FAFyBoBGTjiDFbR6O52lL7V4qfXI8sjQKnOdbumWCnouqvHdCZafKQCEvdbOArQamyhrpOAveKB96Cwqc41kRQuOXJ3OUktI4QHYC4P5qJ03VDNTtFW7w6UG8wH%2F4liQP2OIRNR23KY7xkMOLBBHomO0LB24F5W1ceEtchm1ekwUeDbCiS8UGnpcAPwDKKrR9%2BTQb%2FDw4oupDPtzXxOJwve3hqFN%2Ff%2B%2FzKn5bHLqYbW1wWfJTf%2BfV%2FLu7O61beD1B5%2FFzFbac13%2FKOhgeLwYAAA%3D%3D)

esbuild outputs:
```js
[...Array(Number(2n**32n-1n))]," ".repeat(Number(2n**29n-24n-1n))+"";export{};
```
No error will happen with this code.
[esbuild try](https://esbuild.github.io/try/#dAAwLjI0LjIALS1taW5pZnkAWy4uLkFycmF5KE51bWJlcigybiAqKiAzMm4gLSAxbikpLCIiXQoiICIucmVwZWF0KE51bWJlcigybiAqKiAyOW4gLSAyNG4gLSAxbikpICsgJyAnCmV4cG9ydCB7fQ)

OXC outputs:
```js
[...Array(Number(2n**32n-1n))]," ".repeat(Number(2n**29n-24n-1n))+" ";export{};
```
The array error won't happen and the String error will happen with this code.
[playground](https://playground.oxc.rs/#eNpVT71OwzAQfhXrlv4QIhpgIBtLR8SOGZxwCUH22To7baMo746dNEVMd5/u+7sRaijhI8/zV2Y1bN96UyFvCxL7vXiM414caLfLJEj4lCRBSMgZHarwn1u8JG7xtCrEndiIjSS8OMtBjJMkyMBCOQL3lIYfKKgLlIF7zEB3FKBslPYR+No6XC9+MJXVKwqsyDeWzZU8ZeAUe+TZ0vZc47HTSMpEAwjoQ/7jY7JjjKQTvitG8n/ilDuL437zXtyC4hZjKUBfPByeITJq+4UtpvoRmI66plu4tTUpIRovNc/fXcx2qr69YRS1+opmJwps9VHbc9KfkCvr43npNU2/v/WIsw==)
2025-01-17 05:53:54 +00:00
..
oxc fix(semantic)!: ensure program outlives semantic (#8455) 2025-01-16 10:04:25 +08:00
oxc_allocator feat(allocator): add HashMap (#8553) 2025-01-16 23:29:57 +00:00
oxc_ast fix(codegen): shorthand assignment target identifier consider mangled names (#8536) 2025-01-16 07:26:07 +00:00
oxc_ast_macros feat(ast, span, syntax, regular_expression)!: remove ContentHash (#8512) 2025-01-15 15:01:14 +00:00
oxc_cfg release(crates): v0.46.0 (#8487) 2025-01-14 19:40:44 +08:00
oxc_codegen fix(codegen): shorthand assignment target identifier consider mangled names (#8536) 2025-01-16 07:26:07 +00:00
oxc_data_structures release(crates): v0.46.0 (#8487) 2025-01-14 19:40:44 +08:00
oxc_diagnostics refactor(linter): move DiagnosticsReporters to oxlint (#8454) 2025-01-16 16:11:22 +08:00
oxc_ecmascript feat(minifier): improve .charCodeAt(arg) when arg is valid (#8534) 2025-01-16 06:36:34 +00:00
oxc_estree release(crates): v0.46.0 (#8487) 2025-01-14 19:40:44 +08:00
oxc_isolated_declarations release(crates): v0.46.0 (#8487) 2025-01-14 19:40:44 +08:00
oxc_language_server chore(deps): update dependency rust to v1.84.0 (#8391) 2025-01-09 18:11:17 +00:00
oxc_linter refactor(linter/consistent-function-scoping): remove Visit::enter_node usage (#8538) 2025-01-16 11:00:11 +00:00
oxc_macros chore(deps): update rust crates (#8452) 2025-01-15 13:29:46 +00:00
oxc_mangler perf(mangler): optimize base54 function (#8557) 2025-01-17 05:47:28 +00:00
oxc_minifier feat(minifier): fold array concat chaining (#8440) 2025-01-17 05:53:54 +00:00
oxc_napi release(crates): v0.46.0 (#8487) 2025-01-14 19:40:44 +08:00
oxc_parser feat(napi/parser): expose dynamic import expressions (#8540) 2025-01-16 22:25:22 +08:00
oxc_prettier refactor(prettier): Verify printing class related nodes (#8559) 2025-01-17 13:46:07 +08:00
oxc_regular_expression feat(ast, span, syntax, regular_expression)!: remove ContentHash (#8512) 2025-01-15 15:01:14 +00:00
oxc_semantic perf(semantic): use oxc_allocator::HashMap in ScopeTree (#8554) 2025-01-16 23:29:58 +00:00
oxc_span feat(ast, span, syntax, regular_expression)!: remove ContentHash (#8512) 2025-01-15 15:01:14 +00:00
oxc_syntax feat(napi/parser): expose dynamic import expressions (#8540) 2025-01-16 22:25:22 +08:00
oxc_transformer fix(transformer/class-static-blocks): static block converted to IIFE use span of original block (#8549) 2025-01-16 14:55:53 +00:00
oxc_traverse refactor(traverse): remove unnecessary #[allow] (#8518) 2025-01-15 17:38:03 +00:00
oxc_wasm feat(napi/minify): implement napi (#8478) 2025-01-14 08:55:55 +00:00