Commit graph

2110 commits

Author SHA1 Message Date
sapphi-red
e353a018f0
feat(minifier): compress a != null ? a.b : undefined to a?.b (#8802) 2025-01-31 07:27:49 +00:00
sapphi-red
72d74a2fdc
feat(minifier): compress a != null ? a : b into a ?? b (#8801)
It seems this was implemented in past by #8352 and was reverted in #8411. I'm not sure what was buggy, but one difference with this PR is that the previous PR doesn't check whether the identifier is a global reference or not.
2025-01-31 06:47:47 +00:00
Boshen
a861d93722
refactor(minifier): port esbuild's mangleStmts (#8770) 2025-01-31 13:43:10 +08:00
sapphi-red
7ea99f40ac
feat(minifier): compress array of string literals to 'str1,str2'.split(',') (#8786)
Ported `["str1", "str2", ...]` => `"str1 str2".split(" ")` compression from closure compiler with some tweaks.
2025-01-31 04:13:41 +00:00
Yuji Sugiura
3e3fbefecc
refactor(prettier): Verify printing member expr (#8797)
Part of #5068 

- MemberExpression
  - Computed
  - Static, PrivateField
2025-01-31 11:48:12 +08:00
overlookmotel
d4eee5002b refactor(ast): comments for enums with no AstKind in generated code for Visit trait (#8796)
"No `AstKind` for this type" comments appear in `walk_*` functions for structs with no `AstKind`. Do the same for enums.
2025-01-31 01:24:51 +00:00
overlookmotel
87a7711d29 refactor(ast): shorten generated code for VisitMut (#8795)
`for x in vec.iter_mut()` is unnecessarily long - `for x in vec` works just as well.
2025-01-31 00:58:47 +00:00
overlookmotel
70ad8797b0 refactor(ast): remove unnecessary lint from generated code for AstKind (#8794) 2025-01-30 22:32:16 +00:00
overlookmotel
beeda9a3b8 refactor(ast): alter comments in generated Visit trait (#8793)
Make these comments more descriptive.
2025-01-30 22:16:42 +00:00
Yuji Sugiura
b3a57696a1
feat(tasks/prettier): Show total match ratio with updating snapshot format (#8788)
Previously, #8634 visualized each spec result, but this was not enough.

If the implementation has been updated but the spec still fails, there
is no way to know if there is an improvement or not.

This time, finally, all progress is visualized! 🤩 


![image](https://github.com/user-attachments/assets/df5b4658-450a-4b0c-8348-ffc9b977c50c)

e.g. here `arrow_function_expression` is mostly passing, except for
comment handling.
2025-01-30 22:27:34 +08:00
overlookmotel
fb5b1fa015 docs(ast): reformat AstBuilder doc comments (#8774)
Reformat doc comments for `AstBuilder` methods, for better readability and consistency.
2025-01-29 15:24:36 +00:00
sapphi-red
86b6219d26
feat(mangler): use characters in the order of their likely frequency (#8771)
Just noticed that we can use a static list here. I think this has no downsides. To have better compression, we can actually count the characters, but I guess there won't be much difference normally.
2025-01-29 02:20:44 +00:00
overlookmotel
8cf9d3443c refactor(ast): rename #[estree(type)] attr on struct fields to #[estree(ts_type)] (#8767)
To override the TS type of a struct field in ESTree AST, use `#[estree(ts_type = "Identifier")]` instead of `#[estree(type = "Identifier")]`.

This is clearer, and avoids using the keyword `type`, which makes the attributes simpler to parse.
2025-01-28 12:09:27 +00:00
overlookmotel
a316b10fcd refactor(ast): rename #[estree(type)] attr on types to #[estree(rename)] (#8766)
To rename a type in ESTree AST, use `#[estree(rename = "Identifier")]` instead of `#[estree(type = "Identifier")]`.

This aligns with `serde`'s attribute, and avoids using the keyword `type`, which makes the attributes simpler to parse.
2025-01-28 12:02:05 +00:00
翠 / green
66c33ed0af
fix(minifier): remove incorrect not + conditional expression compression (#8759)
```js
var foo = (a) => {
  if (!(a == null ? void 0 : a.b))
    return
  a.b(to);
};
foo(null)
```
was minified into
```js
var foo = (a) => {
	if (a == null && a.b) return;
	a.b(to);
};
foo(null);
```
which is incorrect (no error happens before and error happens after).
I found this while trying rolldown-vite with ecosystem-ci.

refs #8651
2025-01-28 13:30:00 +08:00
sapphi-red
ad14403f9e
feat(minifier): compress typeof a.b === 'undefined' to a.b === void 0 (#8751)
The special behavior of `typeof` is only for the identifier, so it should be safe to compress `typeof a.b === 'undefined'` (and other expressions) to `a.b === void 0`.
2025-01-27 14:31:13 +00:00
sapphi-red
f7f2d2f93a
feat(minifier): compress a == null && b to a ?? b when return value is ignored (#8749)
Compress `a == null && b` to `a ?? b` when return value is ignored

When `a` is `null` or `undefined`, `a == null && b` returns `b`.
When `a` is not `null` or `undefined`, `a == null && b` returns `false`.

When `a` is `null` or `undefined`, `a ?? b` returns `b`.
When `a` is not `null` or `undefined`, `a ?? b` returns `a`.

**References**
- [Spec of `??`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-binary-logical-operators-runtime-semantics-evaluation)
2025-01-27 14:31:12 +00:00
sapphi-red
3c1c92cac0
feat(minifier): support a[0] and this.a in has_no_side_effect_for_evaluation_same_target (#8748)
Add support for `a[0]` and `this.a` in `has_no_side_effect_for_evaluation_same_target` function.
2025-01-27 14:31:11 +00:00
Boshen
29417ddc03 feat(minifier): minimize !(a, b) -> a, !b (#8746) 2025-01-27 08:24:07 +00:00
renovate[bot]
e00e3abd84
chore(deps): update rust crates (major) (#8739)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [oxc_resolver](https://redirect.github.com/oxc-project/oxc-resolver) |
workspace.dependencies | major | `3` -> `4` |
| [ureq](https://redirect.github.com/algesten/ureq) |
workspace.dependencies | major | `2.12.1` -> `3.0.0` |

---

### Release Notes

<details>
<summary>oxc-project/oxc-resolver (oxc_resolver)</summary>

###
[`v4.0.0`](https://redirect.github.com/oxc-project/oxc-resolver/blob/HEAD/CHANGELOG.md#400---2025-01-20)

[Compare
Source](https://redirect.github.com/oxc-project/oxc-resolver/compare/oxc_resolver-v3.0.3...oxc_resolver-v4.0.0)

##### <!-- 0 -->Features

- \[**breaking**] generic fs cache `type Resolver =
ResolverGeneric<FsCache<FileSystemOs>>`
([#&#8203;358](https://redirect.github.com/oxc-project/oxc-resolver/issues/358))
- \[**breaking**] `PackageJson` and `TsConfig` traits
([#&#8203;360](https://redirect.github.com/oxc-project/oxc-resolver/issues/360))

##### <!-- 2 -->Performance

- use papaya instead of dashmap
([#&#8203;356](https://redirect.github.com/oxc-project/oxc-resolver/issues/356))

</details>

<details>
<summary>algesten/ureq (ureq)</summary>

###
[`v3.0.0`](https://redirect.github.com/algesten/ureq/blob/HEAD/CHANGELOG.md#300)

[Compare
Source](https://redirect.github.com/algesten/ureq/compare/2.12.1...3.0.0)

- Replace RequestBuilder Deref with explicit wrappers
([#&#8203;944](https://redirect.github.com/algesten/ureq/issues/944))
- Remove dependency on `url` crate
([#&#8203;943](https://redirect.github.com/algesten/ureq/issues/943))
- Feature `Config::save_redirect_history`
([#&#8203;939](https://redirect.github.com/algesten/ureq/issues/939))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on monday" in timezone Asia/Shanghai,
Automerge - "on monday" in timezone Asia/Shanghai.

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config
help](https://redirect.github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/oxc-project/oxc).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xMjUuMSIsInVwZGF0ZWRJblZlciI6IjM5LjEyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Boshen <boshenc@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-27 12:56:52 +08:00
renovate[bot]
40362dfc01
chore(deps): update npm packages (#8740) 2025-01-27 12:10:20 +08:00
sapphi-red
e0117db531
feat(minifier): replace const with let for non-exported read-only variables (#8733)
Replace `const` with `let` when that value does not have any assignments and not exposed.
2025-01-26 12:44:01 +00:00
sapphi-red
360d49e962
feat(minifier): replace Math.pow with ** (#8730)
Replaces `Math.pow(a, b)` with `(+a) ** (+b)`. `+` does `ToNumber` and `**` does `Number::exponentiate` when both operands are number.
`+` is not added when `a` or `b` is already a number.

**Reference**
- [Spec of `Math.pow`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.pow)
- [Spec of `**`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-exp-operator-runtime-semantics-evaluation)
- [Spec of unary `+`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-unary-plus-operator-runtime-semantics-evaluation)
2025-01-26 11:24:19 +00:00
Boshen
e9fb5febdf
feat(minifier): dce pure expressions such as new Map() (#8725) 2025-01-26 18:08:34 +08:00
branchseer
3e509e1c9b
fix(transformer/typescript): enum merging when same name declared in outer scope (#8691)
```typescript
var x = 10;
enum Merge { x = Math.random() }
enum Merge {
    y = x // <-- refers to Merge.x
}
```

This case wasn't covered in #8543 and by the [babel test
case](e568916ef3/packages/babel-plugin-transform-typescript/test/fixtures/enum/non-constant-member-reference/input.ts).
To handle it we still have to go through the scope ancestors.

---------

Co-authored-by: Dunqing <dengqing0821@gmail.com>
2025-01-26 09:18:00 +08:00
Dunqing
e7ab96cbb6 fix(transformer/jsx): incorrect isStaticChildren argument for Fragment with multiple children (#8713)
close: #8650
2025-01-25 16:11:28 +00:00
Boshen
0af0267077 refactor(minifier): side effect detection needs symbols resolution (#8715) 2025-01-25 15:01:00 +00:00
翠 / green
6589c3bbb3
feat(mangler): reuse variable names (#8562)
Changed the mangler to reuse variable names where possible.

This will reduce the code size as shorter variable names can be used in
more places. But requires global information and limits parallelism in a
single file and requires more memory.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-25 14:00:24 +08:00
Boshen
8587965e45
perf(minifier): normalize undefined to void 0 before everything else (#8699)
so subsequent code don't need to lookup `undefined`.
2025-01-25 11:50:08 +08:00
Boshen
b7f13e636e feat(ast): implement utf8 to utf16 span converter (#8687)
closes #8629
2025-01-24 16:57:44 +00:00
sapphi-red
343690e178
feat(minifier): replace Number.*_SAFE_INTEGER/Number.EPSILON (#8682)
The value of `Number.*_SAFE_INTEGER`, `Number.EPSILON` are constants as they cannot be changed. This PR replaces them with `2**53-1` / `-(2**53-1)` / `2**-52` for ES2016+. For ES2015, `Number.EPSILON` is not changed but `Number.*_SAFE_INTEGER`s are replaced with `9007199254740991` / `-9007199254740991`.

**Reference**
- Spec of [`Number.MAX_SAFE_INTEGER`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.max_safe_integer)
- Spec of [`Number.MIN_SAFE_INTEGER`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.min_safe_integer)
- Spec of [`Number.EPSILON`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.epsilon)

### Additional Information
- [`Number.MIN_VALUE`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.min_value) cannot be replaced as the value depends on the runtime
- [`Number.MAX_VALUE`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.max_value) can be replaced but I didn't come up with a shorter representation that does not lack precision
2025-01-24 03:47:52 +00:00
sapphi-red
0c5bb30859
feat(minifier): replace Number.POSITIVE_INFINITY/Number.NEGATIVE_INFINITY/Number.NaN (#8681)
The value of `Number.POSITIVE_INFINITY`, `Number.NEGATIVE_INFINITY`, `Number.NaN` are constants as they cannot be changed. This PR replaces them with `Infinity`/`-Infinity`/`NaN`.

**Reference**
- Spec of [`Number.POSITIVE_INFINITY`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
- Spec of [`Number.NEGATIVE_INFINITY`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
- Spec of [`Number.NaN`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.positive_infinity)
2025-01-23 15:26:22 +00:00
Boshen
99607d3f82 feat(codegen): print comments in TSTypeLiteral (#8679)
fixes #8665
2025-01-23 14:31:55 +00:00
Yuji Sugiura
a529412fe7
refactor(prettier): Verify printing module exports (#8670)
Part of #5068 

- ExportAllDeclaration
- ExportNamedDeclaration
- ExportDefaultDeclaration
- (ExportNamespaceSpecifier = ExportAllDeclaration+exported)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-23 14:54:06 +08:00
Boshen
ba201a6511 fix(minifier): remove "non esbuild optimizations" which is incorrect (#8668) 2025-01-23 03:45:42 +00:00
Yuji Sugiura
1e9f3857ab
refactor(prettier): Verify printing module imports (#8633)
Part of #5068 

- [x] ImportDeclaration
- [x] ImportSpecifier
- [x] ImportDefaultSpecifier
- [x] ImportNamespaceSpecifier
- [x] WithClause
- [x] ImportAttribute
2025-01-23 09:58:44 +08:00
Boshen
3802d28233 refactor(minifier): clean up try_minimize_conditional (#8660) 2025-01-22 15:17:47 +00:00
Boshen
9953ac7cad
perf(minifier): add LatePeepholeOptimizations (#8651)
This PR adds a `LatePeepholeOptimizations` pass for minifications that
don't interact with the fixed point loop.

While working on this I found a couple of cases where the previous fixed
point loop is not idempotent.
2025-01-22 16:19:06 +08:00
Boshen
5b3c412e26
perf(minifier): only run optimizations on local changes (#8644)
Previously all code are ran in a fixed point loop when ast changes.

This PR changes running code when a function changes its enclosing ast only.
2025-01-21 23:55:54 +08:00
camc314
b75d4919ee chore(linter): update doc comment to reflect existance of stylish formatter (#8645)
after:
```
Output
    -f, --format=ARG          Use a specific output format (default, json, unix, checkstyle, github,
                              stylish)
```
2025-01-21 15:38:15 +00:00
sapphi-red
835b25889b
feat(minifier): compress typeof foo === 'object' && foo !== null to typeof foo == 'object' && !!foo (#8638)
If `typeof foo == 'object'`, then `foo` is guaranteed to be an object or null. In that case, `foo !== null` can be replaced with `!!foo` because objects return `true` for `!!foo` and null returns `false` for it.

**References**
- [Spec of `typeof`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator)
- [Spec of `!`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-logical-not-operator)
- [Spec of `ToBoolean`](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toboolean)
2025-01-21 14:59:25 +00:00
sapphi-red
2bcbed2d50
feat(minifier): compress (a = b) === null || a === undefined to (a = b) == null (#8637) 2025-01-21 14:59:23 +00:00
Yuji Sugiura
891a9c2040
feat(tasks/prettier): Visualize test details (#8634)
For example, if there are 3 tests in 1 spec and all of them fail:

- 💥💥💥

After some implementation, if 1 test passes:

- 💥💥

However, in this case, the coverage as number does not change.

This PR visualizes these details for better mental well-being. 😃
(But in practice, specs are so detailed, there is not much opportunity
for this...)
2025-01-21 17:01:34 +08:00
Kevin Deng 三咲智子
178c2322f5
fix(parser): parse intrinsic TS keyword (#8627) 2025-01-21 09:54:10 +08:00
Kevin Deng 三咲智子
48717ab87c
fix(parser): parse true as TSLiteralType (#8626)
closes #8624
2025-01-21 09:50:49 +08:00
Boshen
6f95cd599a
refactor(minifier): remove all the unnecessary fake ast passes (#8618)
This also removes handling of making cjs-module-lexer to work.
2025-01-20 22:05:51 +08:00
翠 / green
8c8b5fa9be
fix(minifier): avoid minifing String(a) into "" + a for symbols (#8612)
We shouldn't change `String(a)` into `"" + a` if `a` can be a Symbol.
`String(Symbol())` does not throw an error, but `"" + Symbol()` does.

**References**
- [Spec of `ToString` (called for `"" +
variable`)](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tostring)
- [Spec of
`String(a)`](https://tc39.es/ecma262/multipage/text-processing.html#sec-string-constructor-string-value)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-20 13:34:46 +08:00
sapphi-red
997859c42e
refactor(ast): align #[estree(via)] behavior (#8599)
Follow-on after #8564. Aligned the behavior of `#[estree(via)]` for fields with `#[estree(via)]` for structs.
2025-01-19 20:44:33 +00:00
branchseer
ac4f98e376
refactor(span): derive Copy on Atom (#8596)
Follow up from
https://github.com/oxc-project/oxc/pull/8543#discussion_r1918592423

> I agree. https://github.com/oxc-project/backlog/issues/155

> Originally we were considering some form of interning and
reference-counting, so we didn't make it Copy to leave the door open for
that. But now all strings are stored in the arena anyway, so even if we
did decide to intern strings, reference-counting would be irrelevant -
our bump allocator doesn't allow freeing individual allocations anyway.

Most of the changes are done automatically by `just fix` (`cargo clippy
--fix` && `cargo fmt --all`). See the commit list for the manual edits.
2025-01-19 16:14:23 +08:00
Boshen
066ffe8e8d
fix(tasks/prettier_conformance): fix compile error 2025-01-19 12:17:25 +08:00