Skip to content

Commit

Permalink
fix(transformer/async-to-generator): incorrect transform when super e…
Browse files Browse the repository at this point in the history
…xpression 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);
    })();
  }
}```
  • Loading branch information
Dunqing committed Nov 8, 2024
1 parent 1910227 commit ede10dc
Show file tree
Hide file tree
Showing 12 changed files with 485 additions and 87 deletions.
449 changes: 394 additions & 55 deletions crates/oxc_transformer/src/common/arrow_function_converter.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2017/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod async_to_generator;
pub(crate) mod async_to_generator;
mod options;

use oxc_ast::ast::{Expression, Function, Statement};
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod async_generator_functions;
pub(crate) mod async_generator_functions;
mod object_rest_spread;
mod options;

Expand Down
20 changes: 10 additions & 10 deletions tasks/coverage/snapshots/semantic_misc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ after transform: ScopeId(0): ["BrowserWorkingCopyBackupTracker", "CancellationTo
rebuilt : ScopeId(0): ["BrowserWorkingCopyBackupTracker", "DisposableStore", "EditorService", "IEditorGroupsService", "IEditorService", "IFilesConfigurationService", "ILifecycleService", "ILogService", "IWorkingCopyBackupService", "IWorkingCopyEditorService", "IWorkingCopyService", "InMemoryTestWorkingCopyBackupService", "LifecyclePhase", "Schemas", "TestServiceAccessor", "TestWorkingCopy", "URI", "UntitledTextEditorInput", "VSBuffer", "_asyncToGenerator", "assert", "bufferToReadable", "createEditorPart", "ensureNoDisposablesAreLeakedInTestSuite", "isWindows", "registerTestResourceEditor", "timeout", "toResource", "toTypedWorkingCopyId", "toUntypedWorkingCopyId", "workbenchInstantiationService", "workbenchTeardown"]
Symbol reference IDs mismatch for "URI":
after transform: SymbolId(1): [ReferenceId(109), ReferenceId(117), ReferenceId(156), ReferenceId(158), ReferenceId(160), ReferenceId(162)]
rebuilt : SymbolId(1): [ReferenceId(147), ReferenceId(149), ReferenceId(151), ReferenceId(153)]
rebuilt : SymbolId(1): [ReferenceId(150), ReferenceId(152), ReferenceId(154), ReferenceId(156)]
Symbol reference IDs mismatch for "IEditorService":
after transform: SymbolId(2): [ReferenceId(23), ReferenceId(24), ReferenceId(67), ReferenceId(184)]
rebuilt : SymbolId(2): [ReferenceId(17), ReferenceId(55), ReferenceId(174)]
rebuilt : SymbolId(2): [ReferenceId(17), ReferenceId(58), ReferenceId(177)]
Symbol reference IDs mismatch for "IEditorGroupsService":
after transform: SymbolId(4): [ReferenceId(25), ReferenceId(26), ReferenceId(57), ReferenceId(176)]
rebuilt : SymbolId(3): [ReferenceId(18), ReferenceId(46), ReferenceId(167)]
rebuilt : SymbolId(3): [ReferenceId(18), ReferenceId(49), ReferenceId(170)]
Symbol reference IDs mismatch for "EditorService":
after transform: SymbolId(5): [ReferenceId(61), ReferenceId(64), ReferenceId(178), ReferenceId(181)]
rebuilt : SymbolId(4): [ReferenceId(52), ReferenceId(171)]
rebuilt : SymbolId(4): [ReferenceId(55), ReferenceId(174)]
Symbol reference IDs mismatch for "IWorkingCopyBackupService":
after transform: SymbolId(7): [ReferenceId(11), ReferenceId(12), ReferenceId(51), ReferenceId(170)]
rebuilt : SymbolId(5): [ReferenceId(11), ReferenceId(40), ReferenceId(161)]
rebuilt : SymbolId(5): [ReferenceId(11), ReferenceId(43), ReferenceId(164)]
Symbol reference IDs mismatch for "IFilesConfigurationService":
after transform: SymbolId(10): [ReferenceId(13), ReferenceId(14)]
rebuilt : SymbolId(8): [ReferenceId(12)]
Expand All @@ -84,22 +84,22 @@ after transform: SymbolId(14): [ReferenceId(17), ReferenceId(18)]
rebuilt : SymbolId(11): [ReferenceId(14)]
Symbol reference IDs mismatch for "UntitledTextEditorInput":
after transform: SymbolId(17): [ReferenceId(38), ReferenceId(87)]
rebuilt : SymbolId(13): [ReferenceId(29)]
rebuilt : SymbolId(13): [ReferenceId(31)]
Symbol reference IDs mismatch for "InMemoryTestWorkingCopyBackupService":
after transform: SymbolId(19): [ReferenceId(43), ReferenceId(46), ReferenceId(165)]
rebuilt : SymbolId(15): [ReferenceId(35), ReferenceId(156)]
rebuilt : SymbolId(15): [ReferenceId(38), ReferenceId(159)]
Symbol reference IDs mismatch for "TestServiceAccessor":
after transform: SymbolId(21): [ReferenceId(1), ReferenceId(40), ReferenceId(71), ReferenceId(155), ReferenceId(188)]
rebuilt : SymbolId(17): [ReferenceId(59), ReferenceId(178)]
rebuilt : SymbolId(17): [ReferenceId(62), ReferenceId(181)]
Symbol reference IDs mismatch for "IWorkingCopyEditorService":
after transform: SymbolId(32): [ReferenceId(21), ReferenceId(22)]
rebuilt : SymbolId(26): [ReferenceId(16)]
Symbol reference IDs mismatch for "TestWorkingCopyBackupTracker":
after transform: SymbolId(39): [ReferenceId(42), ReferenceId(74), ReferenceId(154), ReferenceId(215)]
rebuilt : SymbolId(34): [ReferenceId(62), ReferenceId(205)]
rebuilt : SymbolId(34): [ReferenceId(65), ReferenceId(208)]
Unresolved reference IDs mismatch for "Promise":
after transform: [ReferenceId(36), ReferenceId(39), ReferenceId(82), ReferenceId(114), ReferenceId(153), ReferenceId(282)]
rebuilt : [ReferenceId(278)]
rebuilt : [ReferenceId(281)]

tasks/coverage/misc/pass/oxc-4449.ts
semantic error: Scope flags mismatch:
Expand Down
22 changes: 20 additions & 2 deletions tasks/coverage/snapshots/semantic_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20458,13 +20458,22 @@ rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/test262/test/language/expressions/object/method-definition/async-super-call-body.js
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(3): SymbolFlags(Import)
after transform: SymbolId(5): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/test262/test/language/expressions/object/method-definition/async-super-call-param.js
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(4): SymbolFlags(Import)
after transform: SymbolId(5): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
Symbol reference IDs mismatch for "_superprop_getMethod":
after transform: SymbolId(4): [ReferenceId(9)]
rebuilt : SymbolId(4): []
Reference symbol mismatch for "_superprop_getMethod":
after transform: SymbolId(4) "_superprop_getMethod"
rebuilt : <None>
Unresolved references mismatch:
after transform: ["$DONE", "Object", "assert", "require"]
rebuilt : ["$DONE", "Object", "_superprop_getMethod", "assert", "require"]

tasks/coverage/test262/test/language/expressions/object/method-definition/forbidden-ext/b1/async-gen-meth-forbidden-ext-direct-access-prop-arguments.js
semantic error: Symbol flags mismatch for "_wrapAsyncGenerator":
Expand Down Expand Up @@ -24611,6 +24620,15 @@ tasks/coverage/test262/test/language/statements/class/definition/methods-async-s
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(4): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
Symbol reference IDs mismatch for "_superprop_getMethod":
after transform: SymbolId(5): [ReferenceId(8)]
rebuilt : SymbolId(4): []
Reference symbol mismatch for "_superprop_getMethod":
after transform: SymbolId(5) "_superprop_getMethod"
rebuilt : <None>
Unresolved references mismatch:
after transform: ["$DONE", "assert", "require"]
rebuilt : ["$DONE", "_superprop_getMethod", "assert", "require"]

tasks/coverage/test262/test/language/statements/class/dstr/async-gen-meth-ary-init-iter-close.js
semantic error: Symbol flags mismatch for "_wrapAsyncGenerator":
Expand Down
16 changes: 8 additions & 8 deletions tasks/coverage/snapshots/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38180,12 +38180,12 @@ rebuilt : ["arguments", "mp", "p", "require"]

tasks/coverage/typescript/tests/cases/conformance/async/es2017/asyncMethodWithSuperConflict_es6.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(11): SymbolFlags(Import)
after transform: SymbolId(15): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(7): SymbolFlags(Import)
after transform: SymbolId(11): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts
Expand Down Expand Up @@ -38566,8 +38566,8 @@ rebuilt : ["arguments", "require"]

tasks/coverage/typescript/tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction11_es5.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(3): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
after transform: SymbolId(4): SymbolFlags(Import)
rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction1_es5.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
Expand Down Expand Up @@ -38658,7 +38658,7 @@ rebuilt : ScopeId(1): []

tasks/coverage/typescript/tests/cases/conformance/async/es5/asyncMethodWithSuper_es5.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(7): SymbolFlags(Import)
after transform: SymbolId(11): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/async/es5/asyncMultiFile_es5.ts
Expand Down Expand Up @@ -39173,10 +39173,10 @@ rebuilt : ["arguments", "mp", "p", "require"]

tasks/coverage/typescript/tests/cases/conformance/async/es6/asyncMethodWithSuper_es6.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(21): SymbolFlags(Import)
after transform: SymbolId(25): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
Symbol flags mismatch for "_wrapAsyncGenerator":
after transform: SymbolId(22): SymbolFlags(Import)
after transform: SymbolId(48): SymbolFlags(Import)
rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/async/es6/asyncMultiFile_es6.ts
Expand Down Expand Up @@ -47284,7 +47284,7 @@ rebuilt : ["o1", "o2", "o3", "o4"]

tasks/coverage/typescript/tests/cases/conformance/expressions/optionalChaining/callChain/superMethodCall.ts
semantic error: Symbol flags mismatch for "_asyncToGenerator":
after transform: SymbolId(2): SymbolFlags(Import)
after transform: SymbolId(3): SymbolFlags(Import)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/typescript/tests/cases/conformance/expressions/optionalChaining/elementAccessChain/elementAccessChain.2.ts
Expand Down
12 changes: 3 additions & 9 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 314/626
Passed: 316/626

# All Passed:
* babel-plugin-transform-class-static-block
Expand All @@ -13,7 +13,7 @@ Passed: 314/626
* babel-plugin-transform-react-jsx-source


# babel-preset-env (40/127)
# babel-preset-env (41/127)
* dynamic-import/auto-esm-unsupported-import-unsupported/input.mjs
x Output mismatch

Expand Down Expand Up @@ -107,9 +107,6 @@ x Output mismatch
* plugins-integration/issue-7527/input.mjs
x Output mismatch

* plugins-integration/issue-9935/input.js
x Output mismatch

* plugins-integration/regression-2892/input.mjs
x Output mismatch

Expand Down Expand Up @@ -463,7 +460,7 @@ x Output mismatch
x Output mismatch


# babel-plugin-transform-async-to-generator (10/24)
# babel-plugin-transform-async-to-generator (11/24)
* assumption-ignoreFunctionLength-true/basic/input.mjs
x Output mismatch

Expand All @@ -476,9 +473,6 @@ x Output mismatch
* async-to-generator/async-iife-with-regenerator-spec/input.js
x Output mismatch

* async-to-generator/object-method-with-super/input.js
x Output mismatch

* bluebird-coroutines/arrow-function/input.js
x Output mismatch

Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 80/89
Passed: 82/91

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Obj = {
value: 0,
async method() {
super.value = true;
() => {
super['value'] = true;
super.object.value = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Obj = {
value: 0,
method() {
var _superprop_getObject = () => super.object,
_superprop_set = (_prop, _value) => super[_prop] = _value,
_superprop_setValue = _value2 => super.value = _value2;
return babelHelpers.asyncToGenerator(function* () {
_superprop_setValue(true);
() => {
_superprop_set('value', true);
_superprop_getObject().value = true;
};
})();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo extends class {} {
async method() {
super['name'];
{
super['name']();
super['object']['name']();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Foo extends class {} {
method() {
var _superprop_get = _prop => super[_prop],
_this = this;
return babelHelpers.asyncToGenerator(function* () {
_superprop_get('name');
{
_superprop_get('name').call(_this);
_superprop_get('object')['name']();
}
})();
}
}

0 comments on commit ede10dc

Please sign in to comment.