fix(es/compat): Fix span hygiene of function naming pass (#6345)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/6344.
This commit is contained in:
Donny/강동윤 2022-11-14 15:03:09 +09:00 committed by GitHub
parent b97a59bb3f
commit 81a4bb304a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 469 additions and 112 deletions

View File

@ -0,0 +1,26 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": {
"toplevel": false,
"keep_classnames": false,
"keep_fnames": false,
"keep_private_props": false,
"ie8": false,
"safari10": false
}
}
},
"module": {
"type": "commonjs"
},
"minify": false,
"isModule": true
}

View File

@ -0,0 +1,21 @@
function a() { }
var te = function () {
function n(e) { }
var t = null;
return {
init: function (e) {
return t = new n(e);
}
}
}();
var he = function () {
function n() {
a();
};
var t = null;
return {
init: function (e) {
return t
}
}
}();

View File

@ -0,0 +1,23 @@
"use strict";
function n() {}
var r = function() {
var n = function n(n) {};
var r = null;
return {
init: function t(t) {
return r = new n(t);
}
};
}();
var t = function() {
var r = function r() {
n();
};
;
var t = null;
return {
init: function n(n) {
return t;
}
};
}();

View File

@ -0,0 +1,26 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": {
"toplevel": false,
"keep_classnames": false,
"keep_fnames": false,
"keep_private_props": false,
"ie8": false,
"safari10": false
}
}
},
"module": {
"type": "commonjs"
},
"minify": false,
"isModule": true
}

View File

@ -0,0 +1,23 @@
"use strict";
function a() { }
var te = function () {
var n = function n(e) { };
var t = null;
return {
init: function init(e) {
return t = new n(e);
}
};
}();
var he = function () {
var n = function n() {
a();
};
;
var t = null;
return {
init: function init(e) {
return t;
}
};
}();

View File

@ -0,0 +1,23 @@
"use strict";
function n() {}
var r = function() {
var n = function n(n) {};
var r = null;
return {
init: function t(t) {
return r = new n(t);
}
};
}();
var t = function() {
var r = function r() {
n();
};
;
var t = null;
return {
init: function n(n) {
return t;
}
};
}();

View File

@ -7,7 +7,7 @@ function C1() {
}
var c1_v1 = C1();
var c1_v2 = new C1();
var C2 = function() {
var C2 = function C21() {
if (!_instanceof(this, C2)) return new C2();
this.x = 1;
};
@ -18,7 +18,7 @@ var c2_v2 = new C2();
}
var c3_v1 = C3(); // error: @class tag requires 'new'
var c3_v2 = new C3();
/** @class */ var C4 = function() {
/** @class */ var C4 = function C41() {
if (!_instanceof(this, C4)) return new C4();
};
var c4_v1 = C4(); // error: @class tag requires 'new'

View File

@ -0,0 +1,21 @@
function a() { }
var te = function () {
function n(e) { }
var t = null;
return {
init: function (e) {
return t = new n(e);
}
}
}();
var he = function () {
function n() {
a();
};
var t = null;
return {
init: function (e) {
return t
}
}
}();

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,21 @@
function n() {}
var t = function() {
function n(n) {}
var t = null;
return {
init: function(u) {
return t = new n(u);
}
};
}();
var u = function() {
function t() {
n();
}
var u = null;
return {
init: function(n) {
return u;
}
};
}();

View File

@ -0,0 +1,23 @@
"use strict";
function a() { }
var te = function () {
var n = function n(e) { };
var t = null;
return {
init: function init(e) {
return t = new n(e);
}
};
}();
var he = function () {
var n = function n() {
a();
};
;
var t = null;
return {
init: function init(e) {
return t;
}
};
}();

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,22 @@
"use strict";
function n() {}
var r = function() {
var n = function n(n) {};
var r = null;
return {
init: function t(t) {
return r = new n(t);
}
};
}();
var t = function() {
var r = function r() {
n();
};
var t = null;
return {
init: function n(n) {
return t;
}
};
}();

View File

@ -0,0 +1,3 @@
{
"defaults": false
}

View File

@ -0,0 +1,23 @@
"use strict";
function n() { }
var t = function () {
function n(n) { }
var t = null;
return {
init: function (u) {
return t = new n(u);
}
};
}();
var u = function () {
function t() {
n();
}
;
var u = null;
return {
init: function (n) {
return u;
}
};
}();

View File

@ -0,0 +1,23 @@
"use strict";
function n() {}
var t = function() {
function n(n) {}
var t = null;
return {
init: function(u) {
return t = new n(u);
}
};
}();
var u = function() {
function t() {
n();
}
;
var u = null;
return {
init: function(n) {
return u;
}
};
}();

View File

@ -110,7 +110,7 @@ expect(Foo.name).toBe('Foo');
"#,
r#"
var Foo = function() {
var Foo = function() {
var Foo = function Foo1() {
_classCallCheck(this, Foo);
};
_defineProperty(Foo, 'num', 0);
@ -137,7 +137,7 @@ test!(
return extendStatics(d, b);
};",
"var extendStatics = function (d, b) {
"var extendStatics = function extendStatics1(d, b) {
extendStatics = Object.setPrototypeOf || ({
__proto__: []
}) instanceof Array && function (d, b) {

View File

@ -48,7 +48,7 @@ fn prepare(i: Ident) -> Ident {
return private_ident!(i.span, format!("_{}", i.sym));
}
i
private_ident!(i.span, i.sym)
}
#[swc_trace]

View File

@ -1,7 +1,7 @@
{
var _$a = 1;
var _$b = 1;
var c = 1;
var _$a__4 = 1;
var _$b__5 = 1;
var c__1 = 1;
}console.log(a);
console.log(b);
console.log(c);
console.log(c__1);

View File

@ -1,16 +1,16 @@
var res = [];
var a = 2;
res.push(a === 2);
var res__1 = [];
var a__1 = 2;
res__1.push(a__1 === 2);
{
var _$b = 1;
}res.push(typeof b === "undefined");
var _$b__7 = 1;
}res__1.push(typeof b === "undefined");
if (true) {
var _$b1 = 0;
var _$b__8 = 0;
}
res.push(typeof b === "undefined");
for(var _$b2 = 0; _$b2 < 10; _$b2++){}
res.push(typeof b === "undefined");
function test() {
var _$b = 7;
res__1.push(typeof b === "undefined");
for(var _$b__9 = 0; _$b__9 < 10; _$b__9++){}
res__1.push(typeof b === "undefined");
function test__1() {
var _$b__10 = 7;
}
res.push(typeof b === "undefined");
res__1.push(typeof b === "undefined");

View File

@ -1,24 +1,24 @@
var _loop = function(i) {
var _loop = function(j) {
if (i < 2) return "continue|out";
var _loop__7 = function(i__1) {
var _loop__5 = function(j__2) {
if (i__1 < 2) return "continue|out";
[
1
].forEach((_)=>{
console.log(i, j);
].forEach((___3)=>{
console.log(i__1, j__2);
});
};
for(var j = 0; j < 4; ++j){
var _ret = _loop(j);
switch(_ret){
for(var j__2 = 0; j__2 < 4; ++j__2){
var _ret__6 = _loop__5(j__2);
switch(_ret__6){
case "continue|out":
return "continue|out";
}
}
};
out: for(var i = 0; i < 4; i++){
var _ret = _loop(i);
switch(_ret){
out__0: for(var i__1 = 0; i__1 < 4; i__1++){
var _ret__8 = _loop__7(i__1);
switch(_ret__8){
case "continue|out":
continue out;
continue out__0;
}
}

View File

@ -1,12 +1,12 @@
var _loop = function(i) {
var _loop = function(j) {
if (i < 2) return "continue";
var _loop__6 = function(i__1) {
var _loop__5 = function(j__2) {
if (i__1 < 2) return "continue";
[
1
].forEach((_)=>{
console.log(i, j);
].forEach((___3)=>{
console.log(i__1, j__2);
});
};
for(var j = 0; j < 4; ++j)_loop(j);
for(var j__2 = 0; j__2 < 4; ++j__2)_loop__5(j__2);
};
for(var i = 0; i < 4; i++)_loop(i);
for(var i__1 = 0; i__1 < 4; i__1++)_loop__6(i__1);

View File

@ -1,24 +1,24 @@
var _loop = function(i) {
var _loop = function(j) {
if (i > 2) return "break|out";
var _loop__7 = function(i__1) {
var _loop__5 = function(j__2) {
if (i__1 > 2) return "break|out";
[
1
].forEach((_)=>{
console.log(i, j);
].forEach((___3)=>{
console.log(i__1, j__2);
});
};
for(var j = 0; j < 4; ++j){
var _ret = _loop(j);
switch(_ret){
for(var j__2 = 0; j__2 < 4; ++j__2){
var _ret__6 = _loop__5(j__2);
switch(_ret__6){
case "break|out":
return "break|out";
}
}
};
out: for(var i = 0; i < 4; i++){
var _ret = _loop(i);
switch(_ret){
out__0: for(var i__1 = 0; i__1 < 4; i__1++){
var _ret__8 = _loop__7(i__1);
switch(_ret__8){
case "break|out":
break out;
break out__0;
}
}

View File

@ -1,15 +1,15 @@
var _loop = function(i) {
var _loop = function(j) {
if (i > 2) return "break";
var _loop__7 = function(i__1) {
var _loop__5 = function(j__2) {
if (i__1 > 2) return "break";
[
1
].forEach((_)=>{
console.log(i, j);
].forEach((___3)=>{
console.log(i__1, j__2);
});
};
for(var j = 0; j < 4; ++j){
var _ret = _loop(j);
if (_ret === "break") break;
for(var j__2 = 0; j__2 < 4; ++j__2){
var _ret__6 = _loop__5(j__2);
if (_ret__6 === "break") break;
}
};
for(var i = 0; i < 4; i++)_loop(i);
for(var i__1 = 0; i__1 < 4; i__1++)_loop__7(i__1);

View File

@ -1,18 +1,18 @@
var _loop = function(i) {
var _loop = function(j) {
if (i < 2) return "continue|b";
var _loop__7 = function(i__1) {
var _loop__5 = function(j__2) {
if (i__1 < 2) return "continue|b";
[
1
].forEach((_)=>{
console.log(i, j);
].forEach((___3)=>{
console.log(i__1, j__2);
});
};
b: for(var j = 0; j < 4; ++j){
var _ret = _loop(j);
switch(_ret){
b__0: for(var j__2 = 0; j__2 < 4; ++j__2){
var _ret__6 = _loop__5(j__2);
switch(_ret__6){
case "continue|b":
continue b;
continue b__0;
}
}
};
for(var i = 0; i < 4; i++)_loop(i);
for(var i__1 = 0; i__1 < 4; i__1++)_loop__7(i__1);

View File

@ -1,15 +1,15 @@
console.log([
...function*() {
var _loop = function*(i) {
var _loop__3 = function*(i__2) {
Promise.resolve().then(()=>{
console.log(`async: ${i}`);
console.log(`async: ${i__2}`);
});
yield i;
yield i__2;
};
for (var i of [
for (var i__2 of [
1,
2,
3
])yield* _loop(i);
}(),
])yield* _loop__3(i__2);
}()
]);

View File

@ -1,10 +1,10 @@
var _loop = function(i) {
var _loop__3 = function(i__1) {
Promise.resolve().then(()=>{
console.log(`async: ${i}`);
console.log(`async: ${i__1}`);
});
};
for (var i of [
for (var i__1 of [
1,
2,
3
])_loop(i);
])_loop__3(i__1);

View File

@ -1,23 +1,23 @@
class C {
class C__1 {
mount() {
var _this = this, _loop = function(v) {
var _this__6 = this, _loop__7 = function(v__3) {
//when you put this inside the for...of loop, the 'this' inside function declare will lose
var overrideTarget = _this.$cardsBox;
var origiFuc = overrideTarget[v];
overrideTarget[v] = function() {
if (this.width > cardWidth) {
this.scaleX = this.scaleY = (cardWidth / this.width).toFixed(2);
var overrideTarget__4 = _this__6.$cardsBox;
var origiFuc__4 = overrideTarget__4[v__3];
overrideTarget__4[v__3] = function() {
if (this.width > cardWidth__2) {
this.scaleX = this.scaleY = (cardWidth__2 / this.width).toFixed(2);
}
origiFuc.apply(this, arguments);
origiFuc__4.apply(this, arguments);
};
};
var overrideFucNames = [
var overrideFucNames__2 = [
"addChild",
"addChildAt",
"removeChild",
"removeChildAt",
"removeChildAt"
];
var cardWidth = 1275;
for (var v of overrideFucNames)_loop(v);
var cardWidth__2 = 1275;
for (var v__3 of overrideFucNames__2)_loop__7(v__3);
}
}

View File

@ -1,14 +1,14 @@
class C extends D {
class C__1 extends D {
mount() {
var _this = this, _superprop_get_mount = ()=>super.mount, _loop = function(v) {
setTimeout(()=>_superprop_get_mount().call(_this, v));
var _this__6 = this, _superprop_get_mount__5 = ()=>super.mount, _loop__7 = function(v__4) {
setTimeout(()=>_superprop_get_mount__5().call(_this__6, v__4));
};
var overrideFucNames = [
var overrideFucNames__3 = [
"addChild",
"addChildAt",
"removeChild",
"removeChildAt",
"removeChildAt"
];
for (var v of overrideFucNames)_loop(v);
for (var v__4 of overrideFucNames__3)_loop__7(v__4);
}
}

View File

@ -1,5 +1,5 @@
var _loop = function(i) {
var j = Math.random();
setTimeout(()=>console.log(j), 0);
var _loop__4 = function(i__1) {
var j__2 = Math.random();
setTimeout(()=>console.log(j__2), 0);
};
for(var i = 0; i < 5; i++)_loop(i);
for(var i__1 = 0; i__1 < 5; i__1++)_loop__4(i__1);

View File

@ -1,3 +1,3 @@
for(var a = 0; a < 10; a++){
console.log(()=>a);
for(var a__1 = 0; a__1 < 10; a__1++){
console.log(()=>a__1);
}

View File

@ -1,5 +1,5 @@
var _loop = function() {
var j = Math.random();
setTimeout(()=>console.log(j), 0);
var _loop__3 = function() {
var j__2 = Math.random();
setTimeout(()=>console.log(j__2), 0);
};
while(foo())_loop();
while(foo())_loop__3();

View File

@ -0,0 +1,23 @@
"use strict";
function a() { }
var te = function () {
var n = function n(e) { };
var t = null;
return {
init: function init(e) {
return t = new n(e);
}
};
}();
var he = function () {
var n = function n() {
a();
};
;
var t = null;
return {
init: function init(e) {
return t;
}
};
}();

View File

@ -0,0 +1,23 @@
"use strict";
function a__1() {}
var te__1 = function() {
var n__2 = function n__3(e__4) {};
var t__2 = null;
return {
init: function init__5(e__6) {
return t__2 = new n__2(e__6);
}
};
}();
var he__1 = function() {
var n__7 = function n__8() {
a__1();
};
;
var t__7 = null;
return {
init: function init__9(e__10) {
return t__7;
}
};
}();

View File

@ -1,6 +1,7 @@
use std::{fs::read_to_string, path::PathBuf};
use swc_common::{chain, comments::SingleThreadedComments, Mark};
use swc_common::{chain, comments::SingleThreadedComments, Mark, SyntaxContext};
use swc_ecma_ast::{Ident, PropName, TsQualifiedName};
use swc_ecma_parser::Syntax;
use swc_ecma_transforms_base::resolver;
use swc_ecma_transforms_compat::{
@ -9,7 +10,7 @@ use swc_ecma_transforms_compat::{
es2017::async_to_generator,
};
use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester};
use swc_ecma_visit::Fold;
use swc_ecma_visit::{as_folder, visit_mut_obj_and_computed, Fold, VisitMut, VisitMutWith};
fn tr() -> impl Fold {
let unresolved_mark = Mark::new();
@ -1108,7 +1109,8 @@ fn fixture(input: PathBuf) {
let unresolved_mark = Mark::new();
chain!(
resolver(unresolved_mark, Mark::new(), false),
block_scoping(unresolved_mark)
block_scoping(unresolved_mark),
as_folder(TsHygiene { unresolved_mark })
)
},
&input,
@ -1116,3 +1118,32 @@ fn fixture(input: PathBuf) {
Default::default(),
);
}
struct TsHygiene {
unresolved_mark: Mark,
}
impl VisitMut for TsHygiene {
visit_mut_obj_and_computed!();
fn visit_mut_ident(&mut self, i: &mut Ident) {
if SyntaxContext::empty().apply_mark(self.unresolved_mark) == i.span.ctxt {
println!("ts_hygiene: {} is unresolved", i.sym);
return;
}
let ctxt = format!("{:?}", i.span.ctxt).replace('#', "");
i.sym = format!("{}__{}", i.sym, ctxt).into();
i.span = i.span.with_ctxt(SyntaxContext::empty());
}
fn visit_mut_prop_name(&mut self, n: &mut PropName) {
if let PropName::Computed(n) = n {
n.visit_mut_with(self);
}
}
fn visit_mut_ts_qualified_name(&mut self, q: &mut TsQualifiedName) {
q.left.visit_mut_with(self);
}
}