Skip to content

Commit

Permalink
fix(es/compat): Fix handling of objects in generator (#5861)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Sep 15, 2022
1 parent b972c7a commit 006c7fb
Show file tree
Hide file tree
Showing 27 changed files with 396 additions and 141 deletions.
25 changes: 25 additions & 0 deletions crates/swc/tests/exec/issues-5xxx/5859/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const result = [];
async function* foo() {
const input = ["hello", "swc"];

for (i of input) {
result.push({ x: i });
result.push({ y: await i });
result.push({
a: 1,
b: yield i,
});
}
}

const iter = foo();

async function main() {
let state;
for (let x = await iter.next(); !x.done; x = await iter.next(state)) {
state = `[${x.value}]`;
}
console.log(result);
}

main();
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _request() {
return _request.apply(this, arguments);
}
_async_to_generator(function() {
var obj, _tmp, _tmp1;
var obj, _tmp;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
Expand All @@ -42,8 +42,9 @@ _async_to_generator(function() {
_state.label = 3;
case 3:
obj = _tmp;
_tmp1 = {};
console.log((_tmp1.obj = obj, _tmp1));
console.log({
obj: obj
});
return [
2
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _request() {
return _request.apply(this, arguments);
}
_async_to_generator(function() {
var obj, _tmp, _tmp1;
var obj, _tmp;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
Expand All @@ -42,8 +42,9 @@ _async_to_generator(function() {
_state.label = 3;
case 3:
obj = _tmp;
_tmp1 = {};
console.log((_tmp1.obj = obj, _tmp1));
console.log({
obj: obj
});
return [
2
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ts_generator from "@swc/helpers/src/_ts_generator.mjs";
function foo() {
var val, _tmp, _tmp1;
var val, _tmp;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
Expand All @@ -23,8 +23,9 @@ function foo() {
_state.label = 3;
case 3:
val = _tmp;
_tmp1 = {};
console.log((_tmp1.val = val, _tmp1));
console.log({
val: val
});
return [
2
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ function foo() {
}
function _foo() {
_foo = _async_to_generator(function() {
var tmp, ref, ref, _tmp;
var tmp, ref, ref;
return _ts_generator(this, function(_state) {
_tmp = {};
tmp = baz.bar, ref = tmp === void 0 ? _tmp : tmp, ref = ref !== null ? ref : _throw(new TypeError("Cannot destructure undefined"));
tmp = baz.bar, ref = tmp === void 0 ? {} : tmp, ref = ref !== null ? ref : _throw(new TypeError("Cannot destructure undefined"));
return [
2
];
Expand Down
11 changes: 6 additions & 5 deletions crates/swc/tests/fixture/issues-1xxx/1455/case2/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ var obj = {
},
byPlatform: function() {
var _ref = _async_to_generator(function(platform) {
var result, _tmp;
var result;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
_tmp = {};
return [
4,
this.find((_tmp.platform = {
$eq: platform
}, _tmp))
this.find({
platform: {
$eq: platform
}
})
];
case 1:
result = _state.sent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ function _asyncToGenerator(fn) {
};
}
_asyncToGenerator(function() {
var counter, resolve, promise, iterable, _tmp, res, _i, _iter, v, oldresolve, _tmp1, _tmp2;
var counter, resolve, promise, iterable, res, _i, _iter, v, oldresolve;
return _tsGenerator(this, function(_state) {
switch(_state.label){
case 0:
counter = 0;
promise = new Promise(function(r) {
return resolve = r;
});
_tmp = {};
iterable = _defineProperty(_tmp, Symbol.asyncIterator, function() {
iterable = _defineProperty({}, Symbol.asyncIterator, function() {
return {
next: function next() {
return promise;
Expand Down Expand Up @@ -179,8 +178,10 @@ _asyncToGenerator(function() {
promise = new Promise(function(r) {
return resolve = r;
});
_tmp1 = {};
oldresolve((_tmp1.value = v, _tmp1.done = false, _tmp1));
oldresolve({
value: v,
done: false
});
_state.label = 3;
case 3:
_i++;
Expand All @@ -189,8 +190,10 @@ _asyncToGenerator(function() {
1
];
case 4:
_tmp2 = {};
resolve((_tmp2.value = undefined, _tmp2.done = true, _tmp2));
resolve({
value: undefined,
done: true
});
return [
4,
res
Expand Down
17 changes: 10 additions & 7 deletions crates/swc/tests/fixture/issues-1xxx/1918/es5/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ var _asyncToGenerator = require("@swc/helpers/lib/_async_to_generator.js").defau
var _defineProperty = require("@swc/helpers/lib/_define_property.js").default;
var _tsGenerator = require("@swc/helpers/lib/_ts_generator.js").default;
_asyncToGenerator(function() {
var counter, resolve, promise, iterable, _tmp, res, _i, _iter, v, oldresolve, _tmp1, _tmp2;
var counter, resolve, promise, iterable, res, _i, _iter, v, oldresolve;
return _tsGenerator(this, function(_state) {
switch(_state.label){
case 0:
counter = 0;
promise = new Promise(function(r) {
return resolve = r;
});
_tmp = {};
iterable = _defineProperty(_tmp, Symbol.asyncIterator, function() {
iterable = _defineProperty({}, Symbol.asyncIterator, function() {
return {
next: function next() {
return promise;
Expand Down Expand Up @@ -138,8 +137,10 @@ _asyncToGenerator(function() {
promise = new Promise(function(r) {
return resolve = r;
});
_tmp1 = {};
oldresolve((_tmp1.value = v, _tmp1.done = false, _tmp1));
oldresolve({
value: v,
done: false
});
_state.label = 3;
case 3:
_i++;
Expand All @@ -148,8 +149,10 @@ _asyncToGenerator(function() {
1
];
case 4:
_tmp2 = {};
resolve((_tmp2.value = undefined, _tmp2.done = true, _tmp2));
resolve({
value: undefined,
done: true
});
return [
4,
res
Expand Down
5 changes: 2 additions & 3 deletions crates/swc/tests/fixture/issues-2xxx/2164/es5/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ function fn() {
}
function _fn() {
_fn = _async_to_generator(function() {
var key, _tmp;
var key;
return _ts_generator(this, function(_state) {
_tmp = {};
for(var key in _tmp);
for(var key in {});
return [
2
];
Expand Down
7 changes: 4 additions & 3 deletions crates/swc/tests/fixture/issues-2xxx/2413/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
import _ts_generator from "@swc/helpers/src/_ts_generator.mjs";
var handleSubmit = useMutation(/*#__PURE__*/ _async_to_generator(function() {
var res, _tmp, param, errors;
var res, param, errors;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
Expand All @@ -11,10 +11,11 @@ var handleSubmit = useMutation(/*#__PURE__*/ _async_to_generator(function() {
,
3
]);
_tmp = {};
return [
4,
gate.register((_tmp.username = phoneNumber, _tmp))
gate.register({
username: phoneNumber
})
];
case 1:
res = _state.sent();
Expand Down
8 changes: 6 additions & 2 deletions crates/swc/tests/fixture/issues-2xxx/2717/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export var selectThemeObject = function(e) {
return e.theme;
};
export function saveTheme(t) {
var s, o, n, a;
var s, o, n;
return e(this, function(e) {
switch(e.label){
case 0:
Expand All @@ -14,7 +14,11 @@ export function saveTheme(t) {
r(selectThemeObject)
];
case 1:
return o = e.sent(), n = {}, window.localStorage.setItem("theme", JSON.stringify((n.theme = o.theme, n.mode = o.mode, n.palette = o.palette, n))), s === c && (a = window.document.querySelector("body")) && (a.classList.add("light" === o.mode ? "light" : "dark"), a.classList.remove("light" === o.mode ? "dark" : "light")), [
return o = e.sent(), window.localStorage.setItem("theme", JSON.stringify({
theme: o.theme,
mode: o.mode,
palette: o.palette
})), s === c && (n = window.document.querySelector("body")) && (n.classList.add("light" === o.mode ? "light" : "dark"), n.classList.remove("light" === o.mode ? "dark" : "light")), [
2
];
}
Expand Down

1 comment on commit 006c7fb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 006c7fb Previous: dfb01bd Ratio
es/full/minify/libraries/antd 2297059795 ns/iter (± 74760727) 2543061911 ns/iter (± 93729760) 0.90
es/full/minify/libraries/d3 412497519 ns/iter (± 28634860) 476860001 ns/iter (± 21167008) 0.87
es/full/minify/libraries/echarts 1743320496 ns/iter (± 44812295) 1990129563 ns/iter (± 111055023) 0.88
es/full/minify/libraries/jquery 101974170 ns/iter (± 3114699) 119154090 ns/iter (± 7768331) 0.86
es/full/minify/libraries/lodash 147975267 ns/iter (± 16310459) 148338265 ns/iter (± 24435256) 1.00
es/full/minify/libraries/moment 75471888 ns/iter (± 9863422) 69263290 ns/iter (± 1602318) 1.09
es/full/minify/libraries/react 26390890 ns/iter (± 3035475) 26022245 ns/iter (± 1106336) 1.01
es/full/minify/libraries/terser 342470191 ns/iter (± 4256364) 407622600 ns/iter (± 18400690) 0.84
es/full/minify/libraries/three 584648748 ns/iter (± 13266096) 664445128 ns/iter (± 83566893) 0.88
es/full/minify/libraries/typescript 4608632258 ns/iter (± 152264376) 6304686446 ns/iter (± 473009593) 0.73
es/full/minify/libraries/victory 914330618 ns/iter (± 37657028) 1168125874 ns/iter (± 125089883) 0.78
es/full/minify/libraries/vue 157794540 ns/iter (± 13855059) 217963624 ns/iter (± 45302083) 0.72
es/full/codegen/es3 33826 ns/iter (± 2285) 45383 ns/iter (± 7645) 0.75
es/full/codegen/es5 33814 ns/iter (± 1112) 47380 ns/iter (± 11698) 0.71
es/full/codegen/es2015 33749 ns/iter (± 710) 42718 ns/iter (± 11464) 0.79
es/full/codegen/es2016 34002 ns/iter (± 792) 42635 ns/iter (± 10012) 0.80
es/full/codegen/es2017 33816 ns/iter (± 619) 46418 ns/iter (± 7757) 0.73
es/full/codegen/es2018 37571 ns/iter (± 13644) 42417 ns/iter (± 7318) 0.89
es/full/codegen/es2019 34196 ns/iter (± 1937) 41999 ns/iter (± 7101) 0.81
es/full/codegen/es2020 33988 ns/iter (± 1313) 41359 ns/iter (± 5842) 0.82
es/full/all/es3 234900318 ns/iter (± 24204226) 261399151 ns/iter (± 31584831) 0.90
es/full/all/es5 212086010 ns/iter (± 24359944) 249904085 ns/iter (± 42449817) 0.85
es/full/all/es2015 169848596 ns/iter (± 14306371) 187161057 ns/iter (± 47491756) 0.91
es/full/all/es2016 170218866 ns/iter (± 17090428) 208658496 ns/iter (± 141930703) 0.82
es/full/all/es2017 168928720 ns/iter (± 14401327) 193286661 ns/iter (± 35309013) 0.87
es/full/all/es2018 168340245 ns/iter (± 20437957) 186475971 ns/iter (± 28501583) 0.90
es/full/all/es2019 164304591 ns/iter (± 19897878) 185289414 ns/iter (± 26777268) 0.89
es/full/all/es2020 158187137 ns/iter (± 13473992) 182706317 ns/iter (± 29698631) 0.87
es/full/parser 784267 ns/iter (± 62833) 891559 ns/iter (± 171113) 0.88
es/full/base/fixer 30858 ns/iter (± 5073) 37222 ns/iter (± 4178) 0.83
es/full/base/resolver_and_hygiene 104331 ns/iter (± 21551) 122893 ns/iter (± 17769) 0.85
serialization of ast node 222 ns/iter (± 5) 269 ns/iter (± 49) 0.83
serialization of serde 226 ns/iter (± 3) 275 ns/iter (± 70) 0.82

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.