mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 09:38:16 +03:00
fix(es/transform/compat): Preserve more span (#2766)
swc_ecma_transforms_compat: - `async_to_generator`: Use correct span for async methods. (Closes #2701)
This commit is contained in:
parent
58bf5a5e2c
commit
ddfc7e6e91
13
crates/swc/tests/fixture/issue-2701/1/input/.swcrc
Normal file
13
crates/swc/tests/fixture/issue-2701/1/input/.swcrc
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"jsc": {
|
||||
"externalHelpers": false,
|
||||
"parser": {
|
||||
"syntax": "ecmascript",
|
||||
"jsx": false
|
||||
},
|
||||
"target": "es2016"
|
||||
},
|
||||
"module": {
|
||||
"type": "es6"
|
||||
}
|
||||
}
|
22
crates/swc/tests/fixture/issue-2701/1/input/index.js
Normal file
22
crates/swc/tests/fixture/issue-2701/1/input/index.js
Normal file
@ -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
crates/swc/tests/fixture/issue-2701/1/output/index.js
Normal file
61
crates/swc/tests/fixture/issue-2701/1/output/index.js
Normal file
@ -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");
|
||||
})();
|
||||
}
|
||||
};
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user