diff --git a/crates/swc/tests/fixture/issue-2701/1/input/.swcrc b/crates/swc/tests/fixture/issue-2701/1/input/.swcrc new file mode 100644 index 000000000000..d7328379af1c --- /dev/null +++ b/crates/swc/tests/fixture/issue-2701/1/input/.swcrc @@ -0,0 +1,13 @@ +{ + "jsc": { + "externalHelpers": false, + "parser": { + "syntax": "ecmascript", + "jsx": false + }, + "target": "es2016" + }, + "module": { + "type": "es6" + } + } \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2701/1/input/index.js b/crates/swc/tests/fixture/issue-2701/1/input/index.js new file mode 100644 index 000000000000..aed69abe2031 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2701/1/input/index.js @@ -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") + } +}; \ No newline at end of file diff --git a/crates/swc/tests/fixture/issue-2701/1/output/index.js b/crates/swc/tests/fixture/issue-2701/1/output/index.js new file mode 100644 index 000000000000..8c467ba26270 --- /dev/null +++ b/crates/swc/tests/fixture/issue-2701/1/output/index.js @@ -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"); + })(); + } +}; diff --git a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs index b65b2d7a8aaf..0b6b8e28fad5 100644 --- a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs @@ -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; @@ -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 {