Commit graph

1860 commits

Author SHA1 Message Date
overlookmotel
6f8bb1cebf
ci(benchmarks): simplify benchmarks (#7939)
Pure refactor. Use `bench_function` instead of `bench_with_input` and
just borrow data from outside closure. This shortens the code and (I
think) makes it easier to read.
2024-12-16 22:01:01 +08:00
overlookmotel
3c73e86f8c ci(codegen): simplify codegen benchmark (#7938)
Follow-on after #7934. Pure refactor. Make codegen benchmark simpler.
2024-12-16 13:51:40 +00:00
camc314
9b3a2beaa3 refactor(benchmark): transform code before codegen (#7934)
@overlookmotel suggested this [here](https://github.com/oxc-project/oxc/pull/7926#discussion_r1886023496).
2024-12-16 13:08:11 +00:00
Dunqing
14c51ffa1d fix(semantic): remove inherting ScopeFlags::Modifier from parent scope (#7932)
close: #7900

After #4283 changed, we don't need to inherit `ScopeFlags` from the `constructor`, `set`, `get` anymore, I think this is a logic of forgetting to remove
2024-12-16 11:26:12 +00:00
Dunqing
596aead0e9 fix(semantic): reset references flags when resolved (#7923)
For this case, we set `current_reference_flags` to `ReferenceFlags::Type` for `TSInterfaceHeritage`, but never unset it, which causes resolving `fowardRef` identifier reuse `current_reference_flags` of `TSInterfaceHeritage`.

```ts
import { forwardRef } from "react";
export interface MenuTriggerProps extends Object {}
export const MenuTrigger = forwardRef();
```

In this PR, reset the `current_reference_flags` when resolved, so that we don't need to reset it in individual visit functions. This is a reasonable change because the `current_reference_flags` only applies to the next encountered identifier.
2024-12-16 02:26:52 +00:00
翠 / green
db9e93b554
feat(mangler): mangle top level variables (#7907)
Adds `top_level` option which is similar to [terser's `toplevel`
option](https://terser.org/docs/cli-usage/#cli-mangle-options).

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-12-15 21:31:41 +08: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
80d0b3e10f perf(transformer/class-properties): fast path for instance prop initializer scope re-parenting (#7901)
Add a fast path for inserting instance property initializers into constructor, when no existing constructor or constructor has no bindings. This should be reasonably common.

The `Scope flags mismatch` errors are due to #7900.
2024-12-15 01:53:13 +00:00
overlookmotel
feac02e65b feat(transformer/class-properties): only rename symbols if necessary (#7896)
#7872 implements renaming symbols in constructor which shadow references in instance property initializers. But we don't need to rename where the reference in initializer references a symbol which is bound within the initializer itself.

Input:

```js
class C {
  double = n => n * 2;
  constructor(n) {
    console.log(n);
  }
}
```

Output:

```js
class C {
  constructor(n) { // <-- not renamed
    this.double = n => n * 2; // <-- moved into constructor
    console.log(n); // <-- not renamed
  }
}
```

This produces better output, and avoids a traversal of constructor's AST renaming symbols.
2024-12-15 01:53:13 +00:00
overlookmotel
e76fbb0721 fix(transformer/class-properties): fix symbol clashes in instance prop initializers (#7872)
Instance property initializers are moved into constructor. If symbols they reference are shadowed within constructor, rename those symbols.

Input:

```js
class C {
  prop = foo();
  constructor(foo) {
    console.log(foo);
  }
}
```

Output:

```js
class C {
  constructor(_foo) { // <-- renamed
    this.prop = foo(); // <-- moved into constructor
    console.log(_foo); // <-- renamed
  }
}
```
2024-12-15 01:53:12 +00:00
Alexander S.
f0a8d8aab7
refactor(tasks/lint_rules): detect typescript alias from mod.rs file (#7891)
based on #7890

tried graphite, but I do not have write access to this repo.  
Do not know how to create Branches on a Fork and the PRs on the Forked
Project.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-12-15 00:14:15 +08:00
overlookmotel
0804916b28 test(transformer): make update fixtures script .mjs (#7892)
`.mjs` to match `reporter.mjs` in same directory.
2024-12-14 15:29:21 +00: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
overlookmotel
7637aac21f style(ast_tools): reformat macro (#7884)
Fix indentation in macro definition.
2024-12-14 13:51:50 +00:00
Boshen
81eedb10c0 feat(parser): 'readonly' type modifier is only permitted on array and tuple literal types. (#7880) 2024-12-14 12:00:52 +00:00
Boshen
7fb9d47460 style(rust): cargo +nightly fmt (#7877) 2024-12-14 06:03:31 +00:00
overlookmotel
588df9f366 test(transformer): transformer conformance runner --override output with spaces not tabs (#7865)
When running `just test-transform --override`, generate override files with indentation as double spaces, instead of tabs. This matches our convention for formatting JS files.
2024-12-14 04:57:00 +00:00
overlookmotel
c0576faf80 fix(transformer/class-properties): use UID for args in created class constructor (#7866)
When creating class constructor for a class which has super class, use UID `_args` for temp var (rather than `args`). This avoids shadowing a var called `args` used in an instance property initializer.

This diverges from Babel. Babel uses `args` unless it finds a var called `args` in an instance property initializer. But searching the AST of initializers can be fairly expensive, so it's better to skip it. The overrides for test fixtures included in this PR are just to account for that difference.
2024-12-14 04:18:48 +00:00
overlookmotel
d660d8d12d fix(transformer/optional-chaining): do not create unused reference when noDocumentAll assumption (#7847)
Similar to #7832. Only create an `IdentifierReference` if it's going to be used in AST.
2024-12-13 14:59:13 +00:00
Dunqing
6bc530d2e2 feat(transformer/class-properties): transform super call expression that is inside static prop initializer (#7831) 2024-12-13 13:47:38 +00:00
Dunqing
4920c6a455 fix(transformer/optional-chaining): avoid creating a useless reference when noDocumentAll is true (#7832) 2024-12-13 13:28:37 +00:00
Boshen
7610dc19ff fix(parser): parse import source from 'mod' (#7833) 2024-12-13 10:26:36 +00:00
Yuji Sugiura
40334ba66e
refactor(prettier): Align exported function names to Prettier (#7827)
Some refactoring works to update
https://github.com/oxc-project/oxc/issues/5068#issuecomment-2507272735
table.

- Implement `array::is_consisely_printed_array()` and use it
  - This improved compat-rate a bit ✌🏻 
- Align exported function names to align prettier's
- Split `format/mod.rs` into `js`, `jsx` and `typescript`
- Move `format/*.rs` to `format/print/*.rs`
2024-12-13 17:06:15 +08:00
overlookmotel
14896cb318 fix(transformer/class-properties): create temp vars in correct scope (#7824)
Create `var` temp vars in current *hoist* scope, not current scope.
2024-12-13 04:04:45 +00:00
Yunfei He
9479e2b0a2
fix(semantic): missing references when export {} references a type-only binding and a normal (#7812)
https://playground.oxc.rs/#eNpdTzEOgzAM/AryzFAqdUHq2rlD1YklUIMihTiyTQGh/L0BBEMn31l35/MCDZTQkBfNXmRE3xbHQKzZPSsqX3mcNrZkOgfcJU+mIPmfPEIOBOUCPPh1yOzVTFAqD5iDs14PLA0FPMnc1+QOpmy8tMT9vog5BMOCnAITXkMOfCq3BajhDtMFQLleihskRUMf7HDtkkhvvW3tYU7vKpN7OBpX8xe5JkmVWuMEY4w/P71iMw==

Current:

<img width="876" alt="image"
src="https://github.com/user-attachments/assets/58327920-44ef-469d-8c7c-a2d7b17da717"
/>


Expected:


https://babeljs.io/repl#?browsers=&build=&builtIns=false&corejs=false&spec=false&loose=false&code_lz=MYewdgzgLgBAKiAhtAagSwKYHcAOIBOsAvDAIwBQ5UAnjhvEtAAr4g4QwkXkYAeehGAG8YNOg2RQWbCABoJqTLgKwAvkA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.26.4&externalPlugins=&assumptions=%7B%7D

<img width="877" alt="image"
src="https://github.com/user-attachments/assets/9b847baa-5b5a-43be-b77d-d529fb3f8026"
/>

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Dunqing <dengqing0821@gmail.com>
2024-12-13 12:00:58 +08:00
overlookmotel
25bb6daa40 fix(transformer/class-properties): fix ScopeIds in instance prop initializers (#7823)
Code in instance property initializers moves from class body into constructor, or a `_super` function. Update parent `ScopeId`s for first level scopes in initializers.
2024-12-13 03:21:37 +00:00
Dunqing
e727ae9698 feat(transformer/class-properties): transform super member expressions that are inside static prop initializer (#7815)
This PR support for transforming `super.prop` to  `babelHelpers.superPropGet(_B, "prop", _B)`

Input:
```js
class A {
  static prop = 1;
}

class B extends A {
  static prop = 2;
  static propA = super.prop;
  static getPropA = () => super.prop;
}
```

Output:
```js
var _B;
class A {}
babelHelpers.defineProperty(A, "prop", 1);
class B extends A {}
_B = B;
babelHelpers.defineProperty(B, "prop", 2);
babelHelpers.defineProperty(B, "propA", babelHelpers.superPropGet(_B, "prop", _B));
babelHelpers.defineProperty(B, "getPropA", () => babelHelpers.superPropGet(_B, "prop", _B));
```
2024-12-13 02:33:37 +00:00
Dunqing
b290ebd2b0 refactor(transformer): handle <CWD> in test runner (#7799)
We should move the handling of  `<CWD>` to the test runner because this is just only used in testing, and it causes us always get a path by `self.ctx.source_path` like `<CWD>/xxx/xxx.js`, we should get a real path for this.
2024-12-12 17:22:01 +00:00
Boshen
fb325dce99 refactor(ast)!: span field must be the first element (#7821)
For consistency. And maybe a small performance improvement when enum
bytes are aligned.
2024-12-12 16:50:01 +00:00
Dunqing
7a832307c2 fix(semantic): missing reference when export default references a type alias binding (#7813)
Fixes: #7809

`ExportNamedDecalration` and `ExportDefaultDeclaration` can reference both type binding and value, so we need to make sure the `ReferenceFlags` is `Read | Type`
2024-12-12 14:12:30 +00:00
overlookmotel
8991f33fc7 feat(ast): add visit_span to Visit and VisitMut (#7816)
Add `Visit::visit_span` and `VisitMut::visit_span` methods, to facilitate #7811.

Both are no-ops by default, and marked `#[inline]`, so this produces no performance impact.
2024-12-12 13:33:12 +00:00
overlookmotel
d14d360061 test(transformer): remove dead code from fixtures update script (#7808)
Follow-up after #7771 and #7779. Remove dead code from transformer fixtures updater script.
2024-12-12 09:27:54 +00:00
Dunqing
74bf141b1a chore(tasks/transform-conformance): update only when the output differs from the original output (#7779)
From `5xx` files changed to `3xx`
2024-12-11 16:26:36 +00:00
Dunqing
4d33ffbbc8 chore(tasks/transform-conformance): support --override flag (#7774)
The `--override` flag used to write the output which is generated by the transformer to the `overrides` folder according to the test path. The acting is similar to the previous `takeover` mode
2024-12-11 16:18:16 +00:00
Dunqing
b089e8b519 chore(tasks/transform-conformance): support override to replace takeover mode (#7771)
This PR does the following things.

1. Move the override output of the `snapshots` folder to the `overrides` folder.
2. Support `override` mode to replace `takeover` mode
3. The `update_fixtures.js` no longer uses `overrides`'s `output.js` to replace Babel's `output.js`.

### How does `override` mode work?

When running each test, it checks whether an output file for that test exists in the ⁠`overrides` directory. If it does, the output file will be used to compare with the transformed code.
2024-12-11 16:18:14 +00:00
overlookmotel
450bb334da test(transformer): conformance runner use HelperLoaderMode::External in takeover mode (#7807)
In "takeover" mode, transformer conformance test runner was using `HelperLoaderMode::Runtime`. Switch this to `HelperLoaderMode::External` to match standard test runner mode.
2024-12-11 16:07:01 +00:00
Dunqing
2964a61546 fix(transformer/class-properties): unwrap failed when private field expression doesn't contain optional expression in ChainExpression (#7798)
The root cause is due to transform wrongly a PrivateFieldExpression that doesn't contain any optional expression, so call `to_member_expression_mut` causes unwrap to fail.  I have fixed the incorrect transform and changed `to_member_expression_mut` to `as_member_expression_mut`.
2024-12-11 11:32:21 +00:00
Dunqing
6fa6785d0d fix(transformer/class-properties): panic when the callee or member is ParenthesisExpression or TS-syntax expressions. (#7795)
We need to call `get_inner_expression_mut` to get actual expressions that we need to deal with
2024-12-11 11:15:20 +00:00
overlookmotel
bb22c67974 fix(transformer/class-properties): fix ScopeIds in static prop initializers (#7791)
Code in static property initializers moves from inside the class to outside. Update parent `ScopeId`s for first level scopes in initializers.
2024-12-11 03:18:29 +00:00
overlookmotel
caa57f1649 fix(transformer/class-properties): fix scope flags in static prop initializers (#7786)
Code in static property initializers moves from inside the class to outside. If environment outside the class is not strict mode, then scopes within the initializer become sloppy mode. Update `ScopeFlags` for scopes in static prop initializers accordingly.

We're following Babel for now, but this isn't actually correct. The initializers should be wrapped in a strict mode IIFE to maintain their strict mode behavior. But at least semantic data is now correct for the output.
2024-12-10 23:57:43 +00:00
overlookmotel
4a3bca8354 fix(semantic): fix identifying strict mode arrow functions (#7785)
Semantic analysis was not marking an arrow function containing a `"use strict"` directive as `ScopeFlags::StrictMode`. Fix that.
2024-12-10 23:57:42 +00:00
overlookmotel
4f1ab49440 test(transformer/class-properties): add output.js files to override fixtures (#7777)
In two of the overridden text fixtures for class properties transform, there was no `output.js` file because what was overridden was just `options.json` and `update_fixtures.js` script then generated new output files using Babel with the new options.

That was fine, but doesn't work with #7771. So add `output.js` files to the these overrides too.
2024-12-10 15:25:39 +00:00
Boshen
39b9c5d01b feat(linter)!: remove unmaintained security plugin (#7773) 2024-12-10 14:29:22 +00:00
Dunqing
19a8aa5238 test(transformer/class-properties): overrides optional-chain-related tests (#7749)
These tests only variable names difference from Babel's output, which is caused by transforming order.
2024-12-10 10:35:06 +00:00
Dunqing
9cacf64f1d refactor(transformer/class-properties): transform the remaining PrivateFieldExpression in ChainExpression first (#7763)
We need to transform the inner PrivateFieldExpression so that the variable name matches Babel's output as closely as possible.
2024-12-10 10:35:06 +00:00
overlookmotel
2e69720ba0 feat(transformer/class-properties): support private_fields_as_properties assumption (#7717)
Support `private_fields_as_properties` assumption in class properties transform. This assumption is also enabled by the transform's `loose` option.

Optional chain (e.g. `this?.#prop`) is not yet implemented, but all other usages of private fields are supported. We'll handle optional chain in a follow-on PR.
2024-12-10 02:28:31 +00:00
overlookmotel
e010b6a7a0 feat(transformer/logical-assignment-operators): no temp vars for literals (#7759)
`TransformCtx::duplicate_expression` (introduced in #7754) don't create temp vars for literals. This produces more compact output for the logical assignment operators transform.

This diverges from Babel (it's better!) so add an override for one of Babel's fixtures. Also add further tests for all literal types.
2024-12-10 02:28:28 +00:00
overlookmotel
8f8b4c31ad test(transformer): enable fixture overrides for logical-assignment-operators transform (#7758)
Enable overriding Babel's fixtures for logical assignment operators transform.
2024-12-10 02:28:27 +00:00
overlookmotel
e48769a45d fix(transformer/logic-assignment-operator): always create IdentifierReferences with ReferenceId (#7745)
Use `TransformCtx::duplicate_expression` (introduced in #7754) to decide when to create temp vars for member expression object and computed property.

This fixes a bug where `IdentifierReference`s created when transforming `key` in `object[key] &&= value` were created without a `ReferenceId` (due to `clone_in`).

We didn't catch this before because Babel's test fixtures only cover `object[key++] &&= value` not the simpler `object[key] &&= value`. Add tests for this.
2024-12-10 02:28:26 +00:00
overlookmotel
97acd88793 test(transformer/class-properties): add more tests (#7743)
Babel's tests only cover transforming `this.#prop &&= value` with logical assignment operators transform also enabled. Add tests for just class properties transform alone.
2024-12-09 14:17:12 +00:00