Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/transform/compat) fix early return caused by comment above async func in object #2766

13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issue-2701/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"jsc": {
"externalHelpers": false,
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2016"
},
"module": {
"type": "es6"
}
}
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issue-2701/1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const x = {
// i am some comment1
async hello() {
console.log("Hello")
},
// i am some comment2
async "hello"(){
console.log("Hello")
},
// i am some comment3
async 1(){
console.log("Hello")
},
// i am some comment4
async [Date.now()](){
console.log("Hello")
},
// i am some comment5
async 1n() {
console.log("Hello")
}
};
61 changes: 61 additions & 0 deletions crates/swc/tests/fixture/issue-2701/1/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
const x = {
// i am some comment1
hello () {
return _asyncToGenerator(function*() {
console.log("Hello");
})();
},
// i am some comment2
"hello" () {
return _asyncToGenerator(function*() {
console.log("Hello");
})();
},
// i am some comment3
1 () {
return _asyncToGenerator(function*() {
console.log("Hello");
})();
},
// i am some comment4
[Date.now()] () {
return _asyncToGenerator(function*() {
console.log("Hello");
})();
},
// i am some comment5
1n () {
return _asyncToGenerator(function*() {
console.log("Hello");
})();
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{iter, mem::replace};
use swc_common::{util::take::Take, Mark, Span, Spanned, DUMMY_SP};
use swc_common::{util::take::Take, BytePos, Mark, Span, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::{helper, perf::Check};
use swc_ecma_transforms_macros::fast_path;
Expand Down Expand Up @@ -429,9 +429,28 @@ impl VisitMut for Actual {
})
};

let func_span_lo = {
let key_span_lo = match &prop.key {
PropName::Ident(ident) => ident.span().lo(),
PropName::Str(str) => str.span().lo(),
PropName::Num(num) => num.span().lo(),
PropName::Computed(computed) => computed.span().lo(),
PropName::BigInt(bigint) => bigint.span().lo(),
};

// sub length of "async " from prop's key span
key_span_lo - BytePos(6)
};

let func_span = Span::new(
func_span_lo,
func_span_lo + BytePos(1), // dummy pos
SyntaxContext::empty(),
);

prop.function = Function {
params: original_fn_params,
span: prop_method_span,
span: func_span,
is_async: false,
is_generator: false,
body: Some(BlockStmt {
Expand Down