-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es/modules): Handle top level this (#5159)
- Loading branch information
1 parent
8b62d47
commit 451f346
Showing
41 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...es/swc_ecma_transforms_module/tests/fixture/common/class-properties/live-rewrite/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
test1, | ||
test2, | ||
test3, | ||
test4, | ||
test5, | ||
test6, | ||
test7, | ||
test8, | ||
test9, | ||
} from "anywhere"; | ||
|
||
class Example { | ||
#test1 = test1; | ||
test2 = test2; | ||
#test3() { | ||
return test3; | ||
} | ||
test4() { | ||
return test4; | ||
} | ||
get #test5() { | ||
return test5; | ||
} | ||
get test6() { | ||
return test6; | ||
} | ||
|
||
#test7 = this.#test1; | ||
#test8() { | ||
return this.#test3(); | ||
} | ||
get #test9() { | ||
return this.#test5(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c_ecma_transforms_module/tests/fixture/common/class-properties/live-rewrite/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
define([ | ||
"require", | ||
"exports", | ||
"anywhere" | ||
], function(require, exports, _anywhere) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
class Example { | ||
#test1 = _anywhere.test1; | ||
test2 = _anywhere.test2; | ||
#test3() { | ||
return _anywhere.test3; | ||
} | ||
test4() { | ||
return _anywhere.test4; | ||
} | ||
get #test5() { | ||
return _anywhere.test5; | ||
} | ||
get test6() { | ||
return _anywhere.test6; | ||
} | ||
#test7 = this.#test1; | ||
#test8() { | ||
return this.#test3(); | ||
} | ||
get #test9() { | ||
return this.#test5(); | ||
} | ||
} | ||
}); |
28 changes: 28 additions & 0 deletions
28
.../swc_ecma_transforms_module/tests/fixture/common/class-properties/live-rewrite/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
const _anywhere = require("anywhere"); | ||
class Example { | ||
#test1 = _anywhere.test1; | ||
test2 = _anywhere.test2; | ||
#test3() { | ||
return _anywhere.test3; | ||
} | ||
test4() { | ||
return _anywhere.test4; | ||
} | ||
get #test5() { | ||
return _anywhere.test5; | ||
} | ||
get test6() { | ||
return _anywhere.test6; | ||
} | ||
#test7 = this.#test1; | ||
#test8() { | ||
return this.#test3(); | ||
} | ||
get #test9() { | ||
return this.#test5(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...c_ecma_transforms_module/tests/fixture/common/class-properties/live-rewrite/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("anywhere")); | ||
else if (typeof define === "function" && define.amd) define([ | ||
"exports", | ||
"anywhere" | ||
], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}, global.anywhere); | ||
})(this, function(exports, _anywhere) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
class Example { | ||
#test1 = _anywhere.test1; | ||
test2 = _anywhere.test2; | ||
#test3() { | ||
return _anywhere.test3; | ||
} | ||
test4() { | ||
return _anywhere.test4; | ||
} | ||
get #test5() { | ||
return _anywhere.test5; | ||
} | ||
get test6() { | ||
return _anywhere.test6; | ||
} | ||
#test7 = this.#test1; | ||
#test8() { | ||
return this.#test3(); | ||
} | ||
get #test9() { | ||
return this.#test5(); | ||
} | ||
} | ||
}); |
5 changes: 5 additions & 0 deletions
5
.../swc_ecma_transforms_module/tests/fixture/common/class-properties/private-method/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Example { | ||
#method() { | ||
console.log(this); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ecma_transforms_module/tests/fixture/common/class-properties/private-method/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
define([ | ||
"require" | ||
], function(require) { | ||
"use strict"; | ||
class Example { | ||
#method() { | ||
console.log(this); | ||
} | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
...wc_ecma_transforms_module/tests/fixture/common/class-properties/private-method/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
class Example { | ||
#method() { | ||
console.log(this); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ecma_transforms_module/tests/fixture/common/class-properties/private-method/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(); | ||
else if (typeof define === "function" && define.amd) define([], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(); | ||
})(this, function() { | ||
"use strict"; | ||
class Example { | ||
#method() { | ||
console.log(this); | ||
} | ||
} | ||
}); |
3 changes: 3 additions & 0 deletions
3
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/private/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Example { | ||
#property = this; | ||
} |
8 changes: 8 additions & 0 deletions
8
...es/swc_ecma_transforms_module/tests/fixture/common/class-properties/private/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
define([ | ||
"require" | ||
], function(require) { | ||
"use strict"; | ||
class Example { | ||
#property = this; | ||
} | ||
}); |
4 changes: 4 additions & 0 deletions
4
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/private/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
class Example { | ||
#property = this; | ||
} |
10 changes: 10 additions & 0 deletions
10
...es/swc_ecma_transforms_module/tests/fixture/common/class-properties/private/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(); | ||
else if (typeof define === "function" && define.amd) define([], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(); | ||
})(this, function() { | ||
"use strict"; | ||
class Example { | ||
#property = this; | ||
} | ||
}); |
3 changes: 3 additions & 0 deletions
3
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/public/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Example { | ||
property = this; | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/public/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
define([ | ||
"require" | ||
], function(require) { | ||
"use strict"; | ||
class Example { | ||
property = this; | ||
} | ||
}); |
4 changes: 4 additions & 0 deletions
4
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/public/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
class Example { | ||
property = this; | ||
} |
10 changes: 10 additions & 0 deletions
10
crates/swc_ecma_transforms_module/tests/fixture/common/class-properties/public/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(); | ||
else if (typeof define === "function" && define.amd) define([], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(); | ||
})(this, function() { | ||
"use strict"; | ||
class Example { | ||
property = this; | ||
} | ||
}); |
14 changes: 14 additions & 0 deletions
14
crates/swc_ecma_transforms_module/tests/fixture/common/issue-5157/1/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const foo = { | ||
get prop1() { | ||
return 1; | ||
}, | ||
get prop2() { | ||
return this.prop1 + 1; | ||
}, | ||
set prop3(v) { | ||
this.x = v; | ||
}, | ||
method() { | ||
return this.prop1; | ||
}, | ||
}; |
27 changes: 27 additions & 0 deletions
27
crates/swc_ecma_transforms_module/tests/fixture/common/issue-5157/1/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
define([ | ||
"require", | ||
"exports" | ||
], function(require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "foo", { | ||
enumerable: true, | ||
get: ()=>foo | ||
}); | ||
const foo = { | ||
get prop1 () { | ||
return 1; | ||
}, | ||
get prop2 () { | ||
return this.prop1 + 1; | ||
}, | ||
set prop3 (v){ | ||
this.x = v; | ||
}, | ||
method () { | ||
return this.prop1; | ||
} | ||
}; | ||
}); |
22 changes: 22 additions & 0 deletions
22
crates/swc_ecma_transforms_module/tests/fixture/common/issue-5157/1/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "foo", { | ||
enumerable: true, | ||
get: ()=>foo | ||
}); | ||
const foo = { | ||
get prop1 () { | ||
return 1; | ||
}, | ||
get prop2 () { | ||
return this.prop1 + 1; | ||
}, | ||
set prop3 (v){ | ||
this.x = v; | ||
}, | ||
method () { | ||
return this.prop1; | ||
} | ||
}; |
30 changes: 30 additions & 0 deletions
30
crates/swc_ecma_transforms_module/tests/fixture/common/issue-5157/1/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(exports); | ||
else if (typeof define === "function" && define.amd) define([ | ||
"exports" | ||
], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}); | ||
})(this, function(exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "foo", { | ||
enumerable: true, | ||
get: ()=>foo | ||
}); | ||
const foo = { | ||
get prop1 () { | ||
return 1; | ||
}, | ||
get prop2 () { | ||
return this.prop1 + 1; | ||
}, | ||
set prop3 (v){ | ||
this.x = v; | ||
}, | ||
method () { | ||
return this.prop1; | ||
} | ||
}; | ||
}); |
5 changes: 5 additions & 0 deletions
5
crates/swc_ecma_transforms_module/tests/fixture/common/misc/class-static-block/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class foo { | ||
static { | ||
this; // should not be replaced by undefined | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
crates/swc_ecma_transforms_module/tests/fixture/common/misc/class-static-block/output.amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
define([ | ||
"require" | ||
], function(require) { | ||
"use strict"; | ||
class foo { | ||
static{ | ||
this; // should not be replaced by undefined | ||
} | ||
} | ||
}); |
6 changes: 6 additions & 0 deletions
6
crates/swc_ecma_transforms_module/tests/fixture/common/misc/class-static-block/output.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
class foo { | ||
static{ | ||
this; // should not be replaced by undefined | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
crates/swc_ecma_transforms_module/tests/fixture/common/misc/class-static-block/output.umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(function(global, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") factory(); | ||
else if (typeof define === "function" && define.amd) define([], factory); | ||
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(); | ||
})(this, function() { | ||
"use strict"; | ||
class foo { | ||
static{ | ||
this; // should not be replaced by undefined | ||
} | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
...ansforms_module/tests/fixture/common/misc/undefined-this-computed-class-method-1/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class C { [this.name]() {} } |
Oops, something went wrong.
451f346
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
es/full/minify/libraries/antd
1652853443
ns/iter (± 35852042
)1830891880
ns/iter (± 36447081
)0.90
es/full/minify/libraries/d3
415369277
ns/iter (± 12468625
)462425198
ns/iter (± 23967247
)0.90
es/full/minify/libraries/echarts
1667385520
ns/iter (± 171997926
)1838771080
ns/iter (± 155091451
)0.91
es/full/minify/libraries/jquery
100324122
ns/iter (± 7542226
)114835362
ns/iter (± 31612979
)0.87
es/full/minify/libraries/lodash
141058079
ns/iter (± 10071839
)154654443
ns/iter (± 16441581
)0.91
es/full/minify/libraries/moment
60100871
ns/iter (± 2479905
)65873456
ns/iter (± 6877746
)0.91
es/full/minify/libraries/react
19092057
ns/iter (± 1743158
)21668845
ns/iter (± 6151153
)0.88
es/full/minify/libraries/terser
631676402
ns/iter (± 8912932
)648206930
ns/iter (± 11353098
)0.97
es/full/minify/libraries/three
582049754
ns/iter (± 11887848
)580002673
ns/iter (± 5800464
)1.00
es/full/minify/libraries/typescript
3638965444
ns/iter (± 66056848
)3820196049
ns/iter (± 128314494
)0.95
es/full/minify/libraries/victory
744798561
ns/iter (± 11486945
)752849851
ns/iter (± 25512025
)0.99
es/full/minify/libraries/vue
150361652
ns/iter (± 2048959
)149463181
ns/iter (± 6104004
)1.01
es/full/codegen/es3
32574
ns/iter (± 1816
)31865
ns/iter (± 1162
)1.02
es/full/codegen/es5
32720
ns/iter (± 528
)32522
ns/iter (± 4438
)1.01
es/full/codegen/es2015
32768
ns/iter (± 309
)32445
ns/iter (± 692
)1.01
es/full/codegen/es2016
32720
ns/iter (± 819
)32198
ns/iter (± 1567
)1.02
es/full/codegen/es2017
32751
ns/iter (± 963
)32077
ns/iter (± 812
)1.02
es/full/codegen/es2018
32269
ns/iter (± 235
)32077
ns/iter (± 1514
)1.01
es/full/codegen/es2019
32166
ns/iter (± 410
)32160
ns/iter (± 777
)1.00
es/full/codegen/es2020
32176
ns/iter (± 740
)32069
ns/iter (± 694
)1.00
es/full/all/es3
186380470
ns/iter (± 10362352
)216674885
ns/iter (± 12958622
)0.86
es/full/all/es5
174063229
ns/iter (± 19475353
)202310419
ns/iter (± 14564596
)0.86
es/full/all/es2015
140416177
ns/iter (± 9988106
)148937928
ns/iter (± 8292919
)0.94
es/full/all/es2016
142268717
ns/iter (± 7470858
)148957049
ns/iter (± 10032063
)0.96
es/full/all/es2017
141985677
ns/iter (± 12245599
)151381356
ns/iter (± 9246616
)0.94
es/full/all/es2018
139278237
ns/iter (± 7287309
)149673703
ns/iter (± 9131160
)0.93
es/full/all/es2019
139044874
ns/iter (± 7906559
)147239461
ns/iter (± 12466750
)0.94
es/full/all/es2020
134786140
ns/iter (± 6336512
)138177621
ns/iter (± 10580563
)0.98
es/full/parser
723624
ns/iter (± 34380
)717030
ns/iter (± 67285
)1.01
es/full/base/fixer
29533
ns/iter (± 1042
)29657
ns/iter (± 778
)1.00
es/full/base/resolver_and_hygiene
87738
ns/iter (± 4342
)88573
ns/iter (± 2432
)0.99
serialization of ast node
207
ns/iter (± 2
)205
ns/iter (± 4
)1.01
serialization of serde
224
ns/iter (± 3
)223
ns/iter (± 5
)1.00
This comment was automatically generated by workflow using github-action-benchmark.