Skip to content

Commit 52a8f05

Browse files
authoredJan 9, 2024
fix(es/compat): Correctly handle this in arrow function parameters (#8489)
**Related issue:** - Closes #8488
1 parent 46cfc5e commit 52a8f05

File tree

9 files changed

+115
-7
lines changed

9 files changed

+115
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": false
6+
},
7+
"target": "es5",
8+
"transform": {
9+
"react": {
10+
"runtime": "automatic",
11+
"useBuiltins": true
12+
}
13+
},
14+
"minify": {
15+
"compress": false,
16+
"mangle": false
17+
},
18+
"loose": false
19+
},
20+
"module": {
21+
"type": "es6"
22+
},
23+
"minify": false
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Foo {
2+
bar(v = this.a?.b?.c) {}
3+
}
4+
5+
new Foo().bar();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
3+
var Foo = function() {
4+
"use strict";
5+
function Foo() {
6+
_class_call_check(this, Foo);
7+
}
8+
_create_class(Foo, [
9+
{
10+
key: "bar",
11+
value: function bar() {
12+
var _this = this;
13+
var v = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
14+
var _this_a_b, _this_a;
15+
return (_this_a = _this.a) === null || _this_a === void 0 ? void 0 : (_this_a_b = _this_a.b) === null || _this_a_b === void 0 ? void 0 : _this_a_b.c;
16+
}();
17+
}
18+
}
19+
]);
20+
return Foo;
21+
}();
22+
new Foo().bar();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript"
5+
},
6+
"target": "es5"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function f1(x = this) {}
2+
function f2(x = () => this) {}
3+
function f3(
4+
x = () => {
5+
return this;
6+
},
7+
) {}
8+
9+
function bar() {
10+
function b1(x = this) {}
11+
function b2(x = () => this) {}
12+
function b3(
13+
x = () => {
14+
return this;
15+
},
16+
) {}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function f1() {
2+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
3+
}
4+
function f2() {
5+
var _this = this;
6+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
7+
return _this;
8+
};
9+
}
10+
function f3() {
11+
var _this = this;
12+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
13+
return _this;
14+
};
15+
}
16+
function bar() {
17+
function b1() {
18+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
19+
}
20+
function b2() {
21+
var _this = this;
22+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
23+
return _this;
24+
};
25+
}
26+
function b3() {
27+
var _this = this;
28+
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
29+
return _this;
30+
};
31+
}
32+
}

‎crates/swc/tests/vercel/loader-only/next-39460/output/snippetSession.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ export var SnippetSession = /*#__PURE__*/ function() {
513513
{
514514
key: "merge",
515515
value: function merge(template) {
516-
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : _defaultOptions;
517516
var _this = this;
517+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : _defaultOptions;
518518
if (!this._editor.hasModel()) {
519519
return;
520520
}

‎crates/swc_ecma_compat_es2015/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ where
7878
Optional::new(object_super(), !c.typescript),
7979
shorthand(),
8080
function_name(),
81-
exprs(unresolved_mark),
8281
for_of(c.for_of),
8382
// Should come before parameters
8483
// See: https://github.com/swc-project/swc/issues/1036
8584
parameters(c.parameters, unresolved_mark),
85+
exprs(unresolved_mark),
8686
computed_properties(c.computed_props),
8787
destructuring(c.destructuring),
8888
block_scoping(unresolved_mark),

‎crates/swc_ecma_preset_env/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ where
247247
);
248248
let pass = add!(pass, ObjectSuper, es2015::object_super());
249249
let pass = add!(pass, FunctionName, es2015::function_name());
250-
let pass = add!(pass, ArrowFunctions, es2015::arrow(unresolved_mark));
251-
let pass = add!(pass, DuplicateKeys, es2015::duplicate_keys());
252-
let pass = add!(pass, StickyRegex, es2015::sticky_regex());
253-
// TODO: InstanceOf,
254-
let pass = add!(pass, TypeOfSymbol, es2015::typeof_symbol());
255250
let pass = add!(pass, ShorthandProperties, es2015::shorthand());
256251
let pass = add!(
257252
pass,
@@ -263,6 +258,11 @@ where
263258
unresolved_mark
264259
)
265260
);
261+
let pass = add!(pass, ArrowFunctions, es2015::arrow(unresolved_mark));
262+
let pass = add!(pass, DuplicateKeys, es2015::duplicate_keys());
263+
let pass = add!(pass, StickyRegex, es2015::sticky_regex());
264+
// TODO: InstanceOf,
265+
let pass = add!(pass, TypeOfSymbol, es2015::typeof_symbol());
266266
let pass = add!(
267267
pass,
268268
ForOf,

0 commit comments

Comments
 (0)
Please sign in to comment.