Commit graph

458 commits

Author SHA1 Message Date
overlookmotel
97de0b7be1 fix(transformer/class-properties): transform this in static prop initializers (#7481)
Convert `this` in static property initializers to reference to class name.

`class C { static prop = this; }` -> `class C {}; C.prop = C;`
2024-11-25 20:10:05 +00:00
overlookmotel
9778298fdb feat(transformer): class properties transform (#7011)
Add class properties transform.

Implementation is incomplete. Notable missing parts:

* Scopes are not updated where property initializers move from class body into class constructor / `_super` function.
* Does not handle binding shadowing problems when property initializers move from class body into class constructor.
* `this` and references to class name in static property initializers need to be transformed to point to a temp var.
* Not all usages of private properties are supported (see below).
* Code which is moved to outside of class body is not transformed by other transforms for class declarations (works OK for class expressions). This includes static property initializers, static blocks, and computed property/method keys.
* Only basic checks for whether computed property/method keys may have side effects.
* Numerous other small issues noted in TODO comments through the code.

### Private properties

Currently does not handle the following usages of private properties:

```js
class Class {
  #prop;
  static #static;

  method() {
    object?.#prop;
    object?.#prop();
    [object.#prop] = [1];
    ({x: object.#prop} = {x: 1});
    object.#prop`xyz`;

    object?.#static;
    object?.#static();
    [object.#static] = [1];
    ({x: object.#static} = {x: 1});
    object.#static`xyz`;
  }
}
```
2024-11-25 10:24:20 +00:00
overlookmotel
3b2a3477d4 test(transformer): script to amend Babel fixtures (#7122)
Add a NodeJS script which amends Babel's fixtures in place to remove transform plugins which we don't support from `options.json` files. Where options are changed from the original, the script runs Babel transform with the new options to regenerate the fixture `output.js` files.

Currently limited to transforming the fixtures for `babel-plugin-transform-class-properties` transform, but we can also enable it for other plugins if we wish in future, to get additional test coverage.
2024-11-25 10:24:19 +00:00
Boshen
6f161de10f chore(coverage): bump test262, babel and TypeScript submodules (#7452) 2024-11-24 16:26:45 +00:00
Boshen
bb2c0c219b refactor(transformer)!: return String as error instead of OxcDiagnostic (#7424) 2024-11-22 16:22:49 +00:00
Dunqing
27b2268a6c refactor(semantic)!: remove SymbolFlags::Export (#7414)
close: #7338
close: #7344

The `SymbolFlags::Export` is Initially used to solve `ExportSpecifier` that is not `IdentifierReference` that causes we cannot determine whether a Binding is not used everywhere by `Semantic`.

Since #3820 this problem is solved, so we don't need `SymbolFlags::Export` no longer. Also, removing this can help us easier to pass the `Semantic` check in `Transformer`
2024-11-22 09:17:37 +00:00
Dunqing
7ff9f13973 fix(transformer): correct all ReferenceFlags (#7410) 2024-11-22 09:10:55 +00:00
Dunqing
6f0fe38bff fix(semantic)!: correct all ReferenceFlags::Write according to the spec (#7388)
close #7323

According to the specification re-design the JavaScript-part ReferenceFlags inferring approach.

* https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation
* https://tc39.es/ecma262/#sec-postfix-increment-operator-runtime-semantics-evaluation
* https://tc39.es/ecma262/#sec-runtime-semantics-restdestructuringassignmentevaluation
* ... See references of https://tc39.es/ecma262/#sec-putvalue

### Changes

1. The left-hand of `AssignmentExpression` is always `ReferenceFlags::Write`
```js
let a = 0;
console.log(a = 0);
            ^ Write only
```

2. The `argument` of `UpdateExpression` is always `ReferenceFlags::Read | Write`
```js
let a = 0;
a++;
^ Read and Write
```

This change might cause some trouble for `Minfier` to remove this code, because ‘a’ is not used elsewhere. I have taken a look at `esbuild` and `Terser`. Only the `Terser` can remove this code.
2024-11-22 06:08:30 +00:00
overlookmotel
f4fda2dba6 test(transformer): add --debug option to transform conformance (#7400)
Add `--debug` command line option for transformer conformance runner, same as for `cargo coverage`. It prints the paths of test fixtures before running them.
2024-11-22 02:23:10 +00:00
Boshen
224775c056 feat(transformer): transform object rest spread (#7003)
https://babel.dev/docs/babel-plugin-transform-object-rest-spread
2024-11-21 11:33:26 +00:00
Boshen
e6922df3bb feat(parser): fix incorrect AST for x?.f<T>() (#7387) 2024-11-21 06:10:48 +00:00
Boshen
885e37f8eb feat(transformer): Optional Chaining (#6990)
close: #6958
2024-11-21 03:12:18 +00:00
Boshen
666b6c104c fix(parser): add missing ChainExpression in optional TSInstantiationExpression (#7371) 2024-11-20 11:51:55 +00:00
renovate[bot]
b3965bbff0
chore(deps): update npm packages (#7364) 2024-11-20 17:26:48 +08:00
Boshen
514878d927 fix(transform_conformance): only run exec tests when specified (#7359) 2024-11-19 13:13:11 +00:00
Boshen
d60801218e feat(transform_conformance): snapshot our transformed outputs (#7358) 2024-11-19 12:49:44 +00:00
Boshen
84f3bf2f97 refactor(transform_conformance): clean up test execution code (#7357) 2024-11-19 12:22:42 +00:00
Boshen
d0e64fd80a refactor(transform_conformance): remove TestCase trait (#7356) 2024-11-19 10:52:42 +00:00
Boshen
65f1e82bd8 refactor(transform_conformance): clean up some code (#7354) 2024-11-19 09:15:14 +00:00
overlookmotel
5d853869eb refactor(transformer/arrow-functions): use IndexMap for super getter/setters (#7317)
Generate getter/setter declarations in same order as Babel by using `IndexMap` instead of `HashMap` to store `super` getter/setter method details.
2024-11-17 10:07:34 +00:00
Dunqing
7d75130865 fix(transformer/async-to-generator): arguments isn't correct after transformation (#7234)
Fix due to this plugin transforming the async method and async arrow function, it caused arguments no longer point the original function.

For example:

Before

```js
class Cls {
  async method() {
    () => {
    	console.log(arguments)
    }
  }
}
```

After:

```js
class Cls {
  method() {
    var _arguments = arguments;
    return babelHelpers.asyncToGenerator(function* () {
      () => {
        console.log(_arguments);
      };
    })();
  }
}
```

In this way, the `_arguments` is its original function's arguments

### For performance regression

It seems we need to check the IdentifierReference and BindingIdentifier if it's an `arguments`, that causes a significant regression, we may need a cheap way to do checking
2024-11-17 05:01:44 +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
Boshen
b4258ee58e feat(transformer): add defaulted Module::Preserve option (#7225) 2024-11-09 12:55:13 +00:00
Dunqing
5cfdc05d06 feat(transformer/typescript): support transform export = and import = require(...) when module is commonjs (#7206)
close: #7141
2024-11-09 09:32:28 +00:00
Dunqing
e536d47e56 feat(transformer/babel): add support for trying to get the Module from BabelPlugins (#7218)
When configuring a `transform-modules-commonjs` plugin in `options.json`, we should treat it as a `CommonJS` module
2024-11-09 08:38:15 +00:00
Boshen
324c3fe184 feat(transformer): add TransformOptions::module option (#7188) 2024-11-09 08:38:15 +00:00
Boshen
846711cf41 feat(transformer)!: change API to take a &TransformOptions instead of TransformOptions (#7213)
closes #7185

`TransformOption`s has an initialization cost, it should be initialized once and shared across files.
2024-11-09 06:01:13 +00:00
Dunqing
ede10dc030 fix(transformer/async-to-generator): incorrect transform when super expression is inside async method (#7171)
After transformation, super expressions have moved to unexpected places. This PR replaces super expression to call expression, and then inserts the super methods to the top of the method body.

For example:

Before:
```js
class G {
  async method() {
    super.foo()
  }
}
```

After:
```js
class G {
  method() {
    var _superprop_getFoo = () => super.foo,
      _this = this;
    return _asyncToGenerator(function* () {
      _superprop_getFoo().call(_this);
    })();
  }
}```
2024-11-08 08:18:45 +00:00
Dunqing
1910227590 feat(transformer/async-to-generator): support inferring the function name from the ObjectPropertyValue's key (#7201)
Support for inferring function name from ObjectPropertyValue's key

For example:
```js
({ foo: async function() {} })
```

After this, we will able to infer `foo` for the object method
2024-11-08 08:18:44 +00:00
Dunqing
293d072e77 fix(transformer/async-to-generator): only transform object method in exit_function (#7199)
part of #7175
2024-11-08 03:03:27 +00:00
Dunqing
b4cb587477 chore(transformer_conformance): only ignore root fixtures directory (#7198)
This blocks us from adding tests for plugins.

See the URL a2244ff089/tasks/transform_conformance/tests/babel-plugin-transform-async-to-generator/test/fixtures/arrow
2024-11-08 02:36:22 +00:00
Boshen
6e1abde5ef chore(transform_conformance): omit tests that we cannot pass right now (#7192) 2024-11-07 14:20:30 +00:00
Boshen
a579011e37 feat(transformer): add features ES2018NamedCapturingGroupsRegex and ES2018LookbehindRegex (#7182) 2024-11-07 07:39:33 +00:00
Boshen
ad3a2f518e feat(tasks/compat_data): generate our own compat table (#7176) 2024-11-07 07:39:33 +00:00
renovate
3da9443b0e chore(deps): update dependency @types/node to v22.9.0 (#7145)
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@types/node](https://redirect.github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node) ([source](https://redirect.github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node)) | [`22.8.4` -> `22.9.0`](https://renovatebot.com/diffs/npm/@types%2fnode/22.8.4/22.9.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@types%2fnode/22.9.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@types%2fnode/22.9.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@types%2fnode/22.8.4/22.9.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@types%2fnode/22.8.4/22.9.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Configuration

📅 **Schedule**: Branch creation - "before 10am on monday" in timezone Asia/Shanghai, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

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

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- 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:eyJjcmVhdGVkSW5WZXIiOiIzOC4xNDIuNyIsInVwZGF0ZWRJblZlciI6IjM4LjE0Mi43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
2024-11-06 06:17:36 +00:00
Dunqing
b2a888df0d fix(transformer/async-generator-functions): incorrect transformation for for await if it's not placed in a block (#7148)
Found in https://github.com/oxc-project/monitor-oxc/actions/runs/11681867380/job/32527898715
2024-11-06 05:51:01 +00:00
Dunqing
19892ede40 fix(transformer/async-generator-functions): transform incorrectly for for await if it's in LabeledStatement (#7147)
Found in https://github.com/oxc-project/monitor-oxc/actions/runs/11681867380/job/32527898715
2024-11-06 03:27:14 +00:00
Dunqing
cd1006fec1 refactor(transformer/async-generator-functions): do not transform yield expression where inside generator function (#7134) 2024-11-06 03:27:13 +00:00
Dunqing
2c5734d49b refactor(transformer/async-generator-functions): do not transform await expression where inside ArrowFunctionExpression (#7132)
The `await expr` should be transformed in AsyncToGenerator rather than AsyncGeneratorFunctions
2024-11-06 03:27:12 +00:00
Dunqing
b6a575009a feat(transformer/arrow-function-converter): move scope to changed scope for this_var if scope have changed (#7125)
The scope can be moved or deleted, we need to check whether the scope id is the same as the container's scope id, if not, we need to move the `this_var` to the target scope id.
2024-11-06 03:02:36 +00:00
Dunqing
df772411ea feat(transformer): enable ArrowFunctionConverter in async-to-generator and async-generator-functions plugins (#7113)
Part of https://github.com/oxc-project/oxc/pull/7074

In async-to-generator and async-generator-functions plugins, we may need to transform the async arrow function to a regular generator function, now we can reuse the ability of the ArrowFunction plugin by enabling `ArrowFunctionConverter`.

I will fix semantic errors in the follow-up PR
2024-11-06 03:02:36 +00:00
Boshen
9386435efb
feat(transform_conformance): use node instead of bun for exec tests (#7121) 2024-11-05 14:20:48 +08:00
Boshen
c2802e63fc feat(transform_conformance): add babel runtime to exec tests (#7114) 2024-11-04 14:38:15 +00:00
Dunqing
f80085c683 refactor(transformer/async-to-generator): move handling of MethodDefinition's value to exit_function (#7105)
Part of #7074

We need to handle this before the `arrow_function` plugin inserts `_this = this` because, in the `async-to-generator` plugin, we will move the body to an inner generator function. However, we don't want `_this = this` moved to the inner generator function as well. So as long as we move first, and then insert, we can fix this problem.

The new semantic error is related to another tricky problem, I will fix it in another PR
2024-11-04 14:32:42 +00:00
Dunqing
a2244ff089 fix(transformer/async-to-generator): output is incorrect when arrow function without params (#7052) 2024-11-01 15:35:57 +00:00
Boshen
f83a760d8a refactor(transformer): deserialize BabelOptions::presets (#7047) 2024-11-01 06:56:27 +00:00
Boshen
52c20d633c refactor(transformer): deserialize BabelOptions::plugins (#7045) 2024-11-01 05:44:57 +00:00
Dunqing
934cb5e746 feat(transformer): add async_generator_functions plugin (#6573)
Passed 15/19 tests. The remaining 4 failed tests related to `this` expression, the problem same as I mentioned in #6658. I will fix them in follow-up PRs.
2024-10-31 09:14:17 +00:00
overlookmotel
2a57a66753 test(transformer): support exec tests in Oxc folder (#7030)
Fix error when `tasks/transform_conformance/tests` contains exec tests.
2024-10-31 01:18:09 +00:00
Boshen
76947e2aec refactor(transform): refactor Babel Targets (#7026)
Found a trick from serde to get us from `BabelTargets` to `Targets`.
2024-10-30 14:08:19 +00:00