Commit graph

7815 commits

Author SHA1 Message Date
overlookmotel
30c0689dfc refactor(linter/no-map-spread): remove Visit::enter_node usage (#8537)
Use `Visit::visit_*` method instead of `Visit::enter_node` / `leave_node`. A step towards solving #8461, but also may improve performance.
2025-01-16 10:55:16 +00:00
Alexander S.
b4c87e27a1
refactor(linter): move DiagnosticsReporters to oxlint (#8454)
it was never the plan that oxc_diagnostics should be the
`std::io::stdout` writer:

8fc238ac34/crates/oxc_diagnostics/src/service.rs (L84-L85)

This PR does refactor all reporters to `oxlint` crate and make the
current implementation of `oxc_diagnostics` public for others to
consume.

I added some tests to reflect to expected output (and found some bugs^^)

For the future I think the BufWriter for `std::io::stdout` should come
from outside.
Or maybe `std::fmt::stdout`? I do not know^^"

I could not test 100% of the code, I hope I can fix this with the next
PR which will include a own Tester for oxlint (like `oxc_linter`).
Please be extra careful when reviewing it.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-01-16 16:11:22 +08:00
Boshen
9ec4e24eb7
test(napi/minify): fix more broken tests 2025-01-16 16:09:23 +08:00
Boshen
0acbef356a
test(napi/minify): fix matching of arrays 2025-01-16 15:34:20 +08:00
Boshen
855c8395cf fix(codegen): shorthand assignment target identifier consider mangled names (#8536) 2025-01-16 07:26:07 +00:00
Boshen
946ad7690b fix(minifier): (-Infinity).toString() -> '-Infinity' (#8535) 2025-01-16 06:47:40 +00:00
Boshen
e0f5d6c7bb
test(minifier): update esbuild test 2025-01-16 14:46:12 +08:00
Boshen
927f43ff84 feat(minifier): improve .charCodeAt(arg) when arg is valid (#8534) 2025-01-16 06:36:34 +00:00
Boshen
b1d018622b fix(minifier): do not fold !!void b (#8533) 2025-01-16 05:52:03 +00:00
Dunqing
f5c5c3c9f1 chore(coverage/runtime): skip tests which are eval in class (#8532)
These tests need performing `eval`, we cannot support them. @overlookmotel and I have agreed to ignore them to reduce the noise.
2025-01-16 05:44:32 +00:00
Boshen
53adde5003
fix(minifier): x['-2147483648'] -> x[-2147483648] (#8528) 2025-01-16 13:43:23 +08:00
Boshen
405b73d8e7 fix(minifier): do not change delete undefined to delete void 0 (#8527)
`delete undefined` returns `false`
`delete void 0` returns `true`
2025-01-16 03:08:26 +00:00
Boshen
92e44cba6b fix(minifier): do not remove undefined in var x = undefined (#8526) 2025-01-16 02:57:28 +00:00
Boshen
06f14d526b feat(minifier): remove empty class static block class Foo { static {} } (#8525) 2025-01-16 02:46:25 +00:00
overlookmotel
2857ae15cd refactor(parser): refactor visitor in regexp example (#8524)
Refactor visitor in example to use `visit_*` methods instead of `enter_node`. This is our recommended style as it's more performant.
2025-01-16 02:40:29 +00:00
Boshen
1860411656 feat(minifier): remove last redundant return statement (#8523) 2025-01-16 02:07:28 +00:00
Valentinas Janeiko
4ce63298e8
fix(semantic)!: ensure program outlives semantic (#8455)
fixes: #8437 

In semantic builder make sure `Program` reference has a lifetime of the
Arena.

---------

Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
2025-01-16 10:04:25 +08:00
overlookmotel
250bbd193b perf(linter/react-exhaustive-deps): use stack of AstTypes instead of AstKinds (#8522)
This lint rule keeps a stack tracing the "visitation path" during `Visit`. Only the type of the nodes is used, not their values, so store only `AstType` (1 byte) rather than `AstKind` (16 bytes).

This should be more performant, but the main motivation is #8461. This is one of very few places in the codebase which uses `enter_node` and `leave_node`. Not storing `AstKind`s here clears the way to remove the unsound lifetime extension in `Visit::alloc`.
2025-01-16 00:32:38 +00:00
overlookmotel
a6d71f8f27 feat(ast): add AstKind::ty method (#8521)
Add `AstKind::ty` method to get `AstType` from an `AstKind`.

Works by setting the enum discriminants of `AstKind` and `AstType` to be the same, so one can be converted to the other at zero cost.
2025-01-16 00:32:37 +00:00
Dunqing
06ccb51fae fix(transformer/async-to-generator): move parameters to the inner generator function when they could throw errors (#8500)
The new implementation port from [esbuild](df815ac27b/internal/js_parser/js_parser_lower.go (L355-L467)), before from `Babel`.

Babel's transform implementation for the async method is incorrect because the async method should return a rejecting promise when it throws an error. Everything is good if the errors are thrown in the async method body, but the following case will throw an error in the parameters which causes the whole program crushed not a rejecting promise. So we should move the parameters to the inner generator function when the parameters could throw an error.

Input:
```js
class Cls {
  // ReferenceError: Cannot access 'b' before initialization
  async method(a = b, b = 0) {}
}
```

Before output
```js
class Cls {
  method(a = b, b = 0) {
    return babelHelpers.asyncToGenerator(function* () {})();
  }
}
```

After output:
```js
class Cls {
  method() {
     // ReferenceError: Cannot access 'b' before initialization
    return babelHelpers.asyncToGenerator(function* (a = b, b = 0) {}).apply(this, arguments);
  }
}
```

No override tests because Babel doesn't cover this case.
2025-01-16 00:18:05 +00:00
overlookmotel
3789d2faf9 style(linter/react-exhaustive-deps): fix indentation (#8520) 2025-01-15 23:46:38 +00:00
Dunqing
52bd0b1004 refactor(transformer): move common utils functions to the root module (#8513)
These utils functions are very useful for other plugins, I am going to gradually move utils functions where in separate plugins into the root utils module. It also will include scope adjusting functions.
2025-01-15 20:41:09 +00:00
overlookmotel
04bc25963d refactor(traverse): remove unnecessary #[allow] (#8518)
Pure refactor. Remove unnecessary `#[allow]` attrs. Replace remaining ones with `#[expect]`.
2025-01-15 17:38:03 +00:00
Boshen
209e313e40 fix(minifier): class C { ['-1']() {} } cannot be minifized (#8516) 2025-01-15 16:15:01 +00:00
overlookmotel
7e61b231b4 refactor(transformer/typescript): shorten code (#8504)
Pure refactor. Just shorten code by using `BoundIdentifier::create_write_target`, which returns an `AssignmentTarget`.
2025-01-15 15:17:59 +00:00
overlookmotel
356f0c1a6a fix(transformer/class-properties): handle nested super() calls (#8506)
Similar to #8494. Handle nested `super()` calls in class constructor e.g. `super(self = super())`. Very weird code, but legal!
2025-01-15 15:09:00 +00:00
overlookmotel
7066d1cc4f feat(ast, span, syntax, regular_expression)!: remove ContentHash (#8512)
#8483 removed codegen-ed impls of `ContentHash` for AST nodes, because `ContentHash` is not useful. Complete the removal by removing `ContentHash` trait definition, and all remaining references to it.
2025-01-15 15:01:14 +00:00
overlookmotel
cfd783aa19
chore(coverage): bump babel submodule (#8508)
Update babel submodule for conformance tests to latest HEAD.

The test fixtures include a new one for a bug fix which @branchseer
intends to also apply to Oxc - https://github.com/babel/babel/pull/17050
(see #8342).
2025-01-15 22:58:15 +08:00
overlookmotel
a368726208 refactor(traverse): harden soundness of Traverse and document safety invariants better (#8507)
Harden soundness of `Traverse` by:

1. Not exposing `walk_*` methods outside of `walk.rs`.
2. Adding more debug assertions.
3. Adding `#[must_use]` to `TraverseAncestry::push_stack`, so we'll get a lint error if codegen-ed code pushes to `TraverseAncestry`'s stack and doesn't pop it off again.

Document the safety invariants better.
2025-01-15 14:02:25 +00:00
renovate
8b6d331631 chore(deps): update rust crates (#8452)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [convert_case](https://redirect.github.com/rutrum/convert-case) | workspace.dependencies | minor | `0.6.0` -> `0.7.0` |
| [prettyplease](https://redirect.github.com/dtolnay/prettyplease) | workspace.dependencies | patch | `0.2.27` -> `0.2.28` |
| [proc-macro2](https://redirect.github.com/dtolnay/proc-macro2) | workspace.dependencies | patch | `1.0.92` -> `1.0.93` |

---

### Release Notes

<details>
<summary>dtolnay/prettyplease (prettyplease)</summary>

### [`v0.2.28`](https://redirect.github.com/dtolnay/prettyplease/releases/tag/0.2.28)

[Compare Source](https://redirect.github.com/dtolnay/prettyplease/compare/0.2.27...0.2.28)

-   Expression precedence fixes ([#&#8203;90](https://redirect.github.com/dtolnay/prettyplease/issues/90), [#&#8203;92](https://redirect.github.com/dtolnay/prettyplease/issues/92), [#&#8203;95](https://redirect.github.com/dtolnay/prettyplease/issues/95), [#&#8203;96](https://redirect.github.com/dtolnay/prettyplease/issues/96), [#&#8203;97](https://redirect.github.com/dtolnay/prettyplease/issues/97), [#&#8203;98](https://redirect.github.com/dtolnay/prettyplease/issues/98), [#&#8203;100](https://redirect.github.com/dtolnay/prettyplease/issues/100))

</details>

<details>
<summary>dtolnay/proc-macro2 (proc-macro2)</summary>

### [`v1.0.93`](https://redirect.github.com/dtolnay/proc-macro2/releases/tag/1.0.93)

[Compare Source](https://redirect.github.com/dtolnay/proc-macro2/compare/1.0.92...1.0.93)

-   Optimize TokenStream's Drop ([#&#8203;489](https://redirect.github.com/dtolnay/proc-macro2/issues/489), [#&#8203;490](https://redirect.github.com/dtolnay/proc-macro2/issues/490), thanks [@&#8203;WalkerKnapp](https://redirect.github.com/WalkerKnapp))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 11am on monday" in timezone Asia/Shanghai, Automerge - "before 11am 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:eyJjcmVhdGVkSW5WZXIiOiIzOS45Mi4wIiwidXBkYXRlZEluVmVyIjoiMzkuOTIuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
2025-01-15 13:29:46 +00:00
Boshen
c63c8b924d
test(napi/minify): add runtime test (#8501) 2025-01-15 21:13:00 +08:00
Boshen
aa9d7e47ea build(napi): napi build --no-dts-cache (#8499) 2025-01-15 07:02:29 +00:00
Boshen
629c41713b test(minifier): port esbuild minification tests (#8497) 2025-01-15 06:29:01 +00:00
Song Gao
a7918e6986
chore: add more extensions to devcontainer.json (#8498)
Make it easier to develop in Windows

cc @DonIsaac, who introduced this file :)
2025-01-15 14:24:18 +08:00
Boshen
6b52d7a7a4 perf(mangler): use a single allocation space for temporary vecs (#8495) 2025-01-15 05:09:36 +00:00
overlookmotel
99635330c7 fix(transformer/arrow-functions): visit arguments to super() call (#8494)
Follow-on after #8482.

Fix an edge case in arrow functions transform when inserting `_this = this` after `super()`. It is possible (though bizarre) for `super()` to contain another `super()` call. This will throw an error when evaluating the outer `super()`, but it can still be observable in some cases.

```js
let f;
class S {}
class C extends S {
  constructor(x) {
    super(super(), this.x = x, f = async () => this);
  }
}

try { new C(123) } catch {}

const c = await f();
assert(c.x === 123);
```

So, before bailing out from searching for more `super()`s in class constructor, visit the `super()` call's arguments.
2025-01-15 02:07:05 +00:00
overlookmotel
ca02c16f8f test(transformer): handle more exec tests (#8493)
Some of Babel's exec tests contain a return statement at top level, in order to return a Promise.

e.g. ad572fd1a1/packages/babel-plugin-transform-private-methods/test/fixtures/private-method-loose/async/exec.js

These test files could not be parsed due to illegal position of `return`, and the tests were silently excluded.

This PR:

1. Includes those tests by passing `allow_return_outside_function: true` option to parser for exec tests.
2. Includes a "transform error" message in snapshot for any exec tests which produce errors in parser/transformer.
2025-01-15 02:07:04 +00:00
Boshen
65854630be fix(minifier): always keep the last value of sequence expression (#8490)
Caught in react app:

```
React.useEffect(() => {
  isMountRef.current = false;
  return () => {
    isMountRef.current = true;
  };
}, []);
```

->

```
React.useEffect(() => isMountRef.current = !1, []);
```

Two problems: there were no unit tests guarding this, no good way of knowing when code gets deleted.
2025-01-14 15:35:00 +00:00
Dunqing
31746754a8
chore(coverage): update runtime snapshots (#8489)
Found a bunch of failed tests related to we don't use
`classPrivateSetter` and `classPrivateGetter` to transform private
getter and private setter. The simplest way is to add alternative helper
functions for these two. The reason we don't use I have explained in
#8132

```shell
tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-add.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «3») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-bitand.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «0») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-bitor.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «15») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-bitxor.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «257») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-div.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «0.5») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-exp.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «1000») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-lshift.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «96») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-mod.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «1») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-mult.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «6») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-rshift.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «3») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-srshift.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «3») to be true

tasks/coverage/test262/test/language/expressions/compound-assignment/left-hand-side-private-reference-accessor-property-sub.js
transform error: Test262Error: The expression should evaluate to the result Expected SameValue(«undefined», «1») to be true
```
2025-01-14 22:06:10 +08:00
overlookmotel
2bc51756ce refactor(transformer/arrow-functions): rename method (#8481)
Follow-on after #8435. Pure refactor. Rename method to `in_class_property_definition_value`. I think that name is more descriptive than `if_ancestor_of_class_property_definition_value`, since the method does not take an `Ancestor` as parameter.
2025-01-14 13:14:59 +00:00
overlookmotel
53ef263077 perf(transformer/arrow-functions): bail out of visiting early when inserting _this = this after super() (#8482)
In arrow functions transform (including async-to-generator), we need to insert a `_this = this` assignment in parent function, if the arrow function uses `this`. If the arrow function is in constructor of a class which `extend`s another class, this assignment needs to be inserted after `super()`, because `this` is invalid before a `super()` call has occurred.

Optimize the visitor which searches class constructor for `super()` for the common case where `super()` call occurs as a top-level statement early in the constructor. e.g.

```js
class C extends S {
    constructor() {
        super();
        // ... lots more code here ...
    }
}
```

In this case, we can insert `_this = this;` statement after `super();` and then exit the visitor. There's no need search the rest of the constructor for further `super()` calls, because `_this` is definitely now initialized prior to any of that code.
2025-01-14 13:09:57 +00:00
oxc-bot
3e05055783
release(crates): v0.46.0 (#8487)
## [0.46.0] - 2025-01-14

- 7eb6ccd ast: [**BREAKING**] Remove unused and not useful `ContentHash`
(#8483) (Boshen)

### Features

- 8accfef minifier: Minify `var x; void x` -> `void 0` (#8466) (Boshen)
- 870a583 minifier: Fold `false['toString']` (#8447) (Boshen)
- 4ad695d napi/minify: Implement napi (#8478) (Boshen)
- 9d550aa span: Add `Atom::r#static` (#8479) (_Kerman)

### Bug Fixes

- 4071878 isolated-declarations: Retain `declare` declarations when they
are exported (#8477) (Dunqing)
- 7ee7634 isolated-declarations: Import statement disappears when import
binding is referenced in nested `typeof` (#8476) (Dunqing)
- 7252cb0 isolated-declarations: Unexpected error when global `Symbol`
as property key (#8475) (Dunqing)
- 4c6675c minifier: Do not convert while to fors in DCE (#8484) (Boshen)
- 1d6e84d minifier: Fix incorrect `null.toString()` and `1n.toString()`
(#8464) (Boshen)
- 25d4bf9 minifier: Remove usage of empty spans (#8462) (Boshen)
- dd64340 minifier: Keep `return undefined` in async generator function
(#8439) (Boshen)
- c444de8 transformer/arrow-functions: Transform `this` and `super`
incorrectly in async arrow function (#8435) (Dunqing)
- 270245f transformer/typescript: Correct the semantic for
TSImportEqualsDeclaration transformation (#8463) (Dunqing)
- 2a400d6 transformer/typescript: Retain TSImportEqualsDeclaration when
it is exported (Dunqing)
- ab694b0 transformer/typescript: Retain `TSImportEqualsDeclaration` in
`namespace` when its binding has been referenced or
`onlyRemoveTypeImports` is true (#8458) (Dunqing)

### Performance

- 7a8200c mangler: Allocate base54 name without heap allocation (#8472)
(Boshen)
- 31dac22 mangler: Allocate data in arena (#8471) (Boshen)
- 8fc238a minifier: Merge `Normalize` and `RemoveSyntax` pass (#8467)
(Boshen)
- 372eb09 minifier: Preallocate mangler's semantic data (#8451) (Boshen)

### Refactor

- 6e64eef codegen: Remove `match_expression!` (#8450) (Boshen)
- de5b288 span: Rename `Atom::new_const` method (#8480) (overlookmotel)
- c83ce5c transformer/typescript: Improve transforming namespace (#8459)
(Dunqing)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2025-01-14 19:40:44 +08:00
Dunqing
270245fd6e fix(transformer/typescript): correct the semantic for TSImportEqualsDeclaration transformation (#8463) 2025-01-14 11:26:50 +00:00
Dunqing
2a400d66d4
fix(transformer/typescript): retain TSImportEqualsDeclaration when it is exported 2025-01-14 19:12:23 +08:00
Boshen
4c6675c46d fix(minifier): do not convert while to fors in DCE (#8484) 2025-01-14 10:01:35 +00:00
Boshen
7eb6ccde66 feat(ast)!: remove unused and not useful ContentHash (#8483)
`ContentEq` is preferred.
2025-01-14 09:53:27 +00:00
Dunqing
c83ce5c6cf refactor(transformer/typescript): improve transforming namespace (#8459)
I did a few things in this PR,

1. Remove `names` which store the declarations name used for checking re-declaration. (We can use SymbolTable to do it now.)
2. Correct semantic data for namespace transform
3. Simplify code
2025-01-14 09:42:31 +00:00
Alexander S.
bf00f82268
refactor(linter): move rule prefer-each from vitest to jest + remapping (#8448)
they are the same: 

https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/prefer-each.ts


https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/src/rules/prefer-each.ts
2025-01-14 17:41:41 +08:00
Yuichiro Yamashita
c6260c278b
fix(linter): support rest params for prefer_promise_reject_errors (#8468)
fixed:
https://github.com/oxc-project/oxc/pull/8254#issuecomment-2587461210
2025-01-14 17:40:52 +08:00
overlookmotel
de5b28809a
refactor(span): rename Atom::new_const method (#8480)
#8479 introduced `Atom::r#static` method. Rename it to
`Atom::new_const`, to match `CompactStr::new_const` which does the same
thing.
2025-01-14 17:16:32 +08:00