Skip to content

Commit

Permalink
fix(es/compat): Correctly handle this in arrow function parameters (#…
Browse files Browse the repository at this point in the history
…8489)

**Related issue:**
- Closes #8488
  • Loading branch information
magic-akari committed Jan 9, 2024
1 parent 46cfc5e commit 52a8f05
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es5",
"transform": {
"react": {
"runtime": "automatic",
"useBuiltins": true
}
},
"minify": {
"compress": false,
"mangle": false
},
"loose": false
},
"module": {
"type": "es6"
},
"minify": false
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
bar(v = this.a?.b?.c) {}
}

new Foo().bar();
22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/1/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
var Foo = function() {
"use strict";
function Foo() {
_class_call_check(this, Foo);
}
_create_class(Foo, [
{
key: "bar",
value: function bar() {
var _this = this;
var v = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
var _this_a_b, _this_a;
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;
}();
}
}
]);
return Foo;
}();
new Foo().bar();
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"target": "es5"
}
}
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/2/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function f1(x = this) {}
function f2(x = () => this) {}
function f3(
x = () => {
return this;
},
) {}

function bar() {
function b1(x = this) {}
function b2(x = () => this) {}
function b3(
x = () => {
return this;
},
) {}
}
32 changes: 32 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8488/2/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function f1() {
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
}
function f2() {
var _this = this;
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
return _this;
};
}
function f3() {
var _this = this;
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
return _this;
};
}
function bar() {
function b1() {
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this;
}
function b2() {
var _this = this;
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
return _this;
};
}
function b3() {
var _this = this;
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : function() {
return _this;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ export var SnippetSession = /*#__PURE__*/ function() {
{
key: "merge",
value: function merge(template) {
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : _defaultOptions;
var _this = this;
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : _defaultOptions;
if (!this._editor.hasModel()) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2015/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ where
Optional::new(object_super(), !c.typescript),
shorthand(),
function_name(),
exprs(unresolved_mark),
for_of(c.for_of),
// Should come before parameters
// See: https://github.com/swc-project/swc/issues/1036
parameters(c.parameters, unresolved_mark),
exprs(unresolved_mark),
computed_properties(c.computed_props),
destructuring(c.destructuring),
block_scoping(unresolved_mark),
Expand Down
10 changes: 5 additions & 5 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ where
);
let pass = add!(pass, ObjectSuper, es2015::object_super());
let pass = add!(pass, FunctionName, es2015::function_name());
let pass = add!(pass, ArrowFunctions, es2015::arrow(unresolved_mark));
let pass = add!(pass, DuplicateKeys, es2015::duplicate_keys());
let pass = add!(pass, StickyRegex, es2015::sticky_regex());
// TODO: InstanceOf,
let pass = add!(pass, TypeOfSymbol, es2015::typeof_symbol());
let pass = add!(pass, ShorthandProperties, es2015::shorthand());
let pass = add!(
pass,
Expand All @@ -263,6 +258,11 @@ where
unresolved_mark
)
);
let pass = add!(pass, ArrowFunctions, es2015::arrow(unresolved_mark));
let pass = add!(pass, DuplicateKeys, es2015::duplicate_keys());
let pass = add!(pass, StickyRegex, es2015::sticky_regex());
// TODO: InstanceOf,
let pass = add!(pass, TypeOfSymbol, es2015::typeof_symbol());
let pass = add!(
pass,
ForOf,
Expand Down

0 comments on commit 52a8f05

Please sign in to comment.