Commit graph

1839 commits

Author SHA1 Message Date
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
overlookmotel
88a0b9c4af test(transformer/class-properties): override test output for _super in class constructor (#7729)
Our output for this test differs from Babel, because we handle `super()` in class constructors differently, but our output is valid.
2024-12-08 17:57:57 +00:00
overlookmotel
3bae741bb4 test(transformer/class-properties): override test output for _super in class constructor (#7726)
Our output for this test differs from Babel, because we handle `super()` in class constructors differently, but our output is valid.
2024-12-08 15:52:27 +00:00
Boshen
85eec3c82e feat(napi/transform,napi/parser): return structured error object (#7724)
closes #7261
2024-12-08 14:11:56 +00:00
overlookmotel
37709cec18 test(transformer): transform conformance print code with double-space indent (#7707)
Transform conformance tester (`just test-transform`) print code with double-space indent, instead of tabs. This matches our formatting convention for JS files.
2024-12-08 02:58:18 +00:00
overlookmotel
34a8d91454 test(transformer): fix merging options in fixtures update script (#7712)
Fix how options are merged with options from parent folder(s). It's much simpler than I had thought. If folder's options contains `plugins`, that overwrites `plugins` from parent options. They don't get merged.
2024-12-08 01:41:39 +00:00
overlookmotel
dd55b84399 refactor(transformer/class-properties): shorten output when _super function (#7710)
Produce more compact output when insert a `_super` function into class constructor.
2024-12-08 01:41:36 +00:00
overlookmotel
97e4185c19 fix(transformer/class-properties): fix SymbolFlags for _super function (#7709)
Fix `SymbolFlags` for generated `_super` function outside class.
2024-12-08 01:41:35 +00:00
overlookmotel
de5b0b63a9 fix(transformer/class-properties): make _super function outside class strict mode (#7708)
When create a `_super` function outside class, ensure it's always strict mode. The code it contains was previously inside the class, so was strict mode.
2024-12-08 01:41:34 +00:00
overlookmotel
5fd136139f test(transformer/class-properties): override fixtures (#7689)
Override some transform conformance test fixtures for class properties transform, where:

* Our output differs from Babel in cosmetic manner only.
* Our transform intentionally works differently from Babel.
* Babel's fixtures enable arrow functions transform, which malfunctions in our implementation. But we're not trying to test arrow functions transform here, so disable it.
2024-12-08 01:41:33 +00:00
overlookmotel
efaaa97986 test(transformer): update fixtures script support overrides (#7688)
Support overriding test fixtures in `update_fixtures.js` script.

Any files in `tasks/transform_conformance/overrides` are copied into Babel test fixtures. If `options.json` is overridden, then script runs Babel with updated options, to generate a new `output` file for the fixture.
2024-12-08 01:41:32 +00:00
overlookmotel
da63e873d9 test(transformer/class-properties): exec test for this in computed key (#7687)
Add an exec test for #7686.
2024-12-08 01:41:31 +00:00
overlookmotel
65a1c311ab test(transformer): fix exec test reporter (#7722)
`JsonReporter` which the custom test reporter introduced in #7715 uses does not provide error message in `message` prop, where cause of failure is failing assertions. So tests failing due to failing assertions were omitted from snapshot. Include them.

Also add count of passing tests at top of the snapshot.
2024-12-08 01:41:30 +00:00
Boshen
5891166c80 feat(transform_conformance): custom reporter for exec test (#7715)
closes #7704
2024-12-07 11:25:00 +00:00
Boshen
72eab6cd96 feat(parser)!: stage 3 import source and import defer (#7706)
* https://github.com/tc39/proposal-defer-import-eval
* https://github.com/tc39/proposal-source-phase-imports
* https://tc39.es/proposal-source-phase-imports/#sec-ecmascript-language-expressions
* https://tc39.es/proposal-defer-import-eval/#prod-NameSpaceImport
2024-12-06 16:15:52 +00:00
Dunqing
86d4c90e4d feat(transformer/class-properties): support for transforming AssignmentTarget (#7697)
Instance prop:
 * `[object.#prop] = arr` -> `[_toSetter(_classPrivateFieldSet, [_prop, object])._] = arr`
 * `({x: object.#prop} = obj)` -> `({ x: _toSetter(_classPrivateFieldSet, [_prop, object])._ } = obj)`

 Static prop:
 * `[object.#prop] = arr` -> `[_assertClassBrand(Class, object, _prop)._] = arr`
 * `({x: object.#prop} = obj)` -> `({ x: _assertClassBrand(Class, object, _prop)._ } = obj)`
2024-12-06 12:18:54 +00:00
Boshen
66a680a119
deps(coverage): bump test262 (#7695) 2024-12-06 14:34:01 +08:00
Boshen
c3a538c47c
deps: node v22.12.0 (#7693) 2024-12-06 13:59:37 +08:00
overlookmotel
72b5d58560 fix(transformer/class-properties): create temp var for this in computed key (#7686)
`this` in computed key needs a temp var, otherwise once moved into constructor it refers to the wrong `this`.
2024-12-06 02:20:57 +00:00
Dunqing
0ca10e270a refactor(transformer): use ctx.var_declarations.create_var* methods (#7666)
It turns out these APIs are very useful, we remove many boilerplate code.
2024-12-05 14:31:20 +00:00
overlookmotel
3de4a43761 test(minifier): reformat minsize table (#7678)
Reformat the table which `minsize` task outputs. I always found the column headings confusing previously.
2024-12-05 12:16:29 +00:00
Boshen
d0b78f7361 feat(codegen): minify whitespace for some expressions (#7671)
part of #7638
2024-12-05 06:59:17 +00:00