Skip to content

Commit

Permalink
fix(es/transforms/compat): Fix syntax context of super classes (swc-p…
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Apr 18, 2021
1 parent 46c3d62 commit d7ea5ae
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 80 deletions.
4 changes: 2 additions & 2 deletions ecmascript/transforms/compat/src/es2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ class B extends A {
}]);
return A;
}();
var B = function(A) {
var B = function(A1) {
'use strict';
_inherits(B, A);
_inherits(B, A1);
function B(num) {
_classCallCheck(this, B);
var _this;
Expand Down
13 changes: 8 additions & 5 deletions ecmascript/transforms/compat/src/es2015/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_transforms_base::native::is_native;
use swc_ecma_utils::quote_expr;
use swc_ecma_utils::quote_ident;
use swc_ecma_utils::quote_str;
use swc_ecma_utils::{
alias_if_required, default_constructor, prepend, prop_name_to_expr, ExprFactory, IsDirective,
ModuleItemLike, StmtLike,
};
use swc_ecma_utils::{private_ident, quote_ident};
use swc_ecma_visit::noop_visit_type;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, Node, Visit, VisitWith};

Expand Down Expand Up @@ -260,11 +260,13 @@ impl Classes {
.as_ref()
.map(|e| alias_if_required(e, "_super").0);
let has_super = super_ident.is_some();
let (params, args) = if let Some(ref super_ident) = super_ident {
let (params, args, super_ident) = if let Some(ref super_ident) = super_ident {
// Param should have a separate syntax context from arg.
let super_param = private_ident!(super_ident.sym.clone());
let params = vec![Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: Pat::Ident(super_ident.clone().into()),
pat: Pat::Ident(super_param.clone().into()),
}];

let super_class = class.super_class.clone().unwrap();
Expand All @@ -282,12 +284,13 @@ impl Classes {
type_args: Default::default(),
}
.as_arg()],
Some(super_param),
)
} else {
(params, vec![super_class.as_arg()])
(params, vec![super_class.as_arg()], Some(super_param))
}
} else {
(vec![], vec![])
(vec![], vec![], None)
};

let mut stmts = self.class_to_stmts(class_name, super_ident, class);
Expand Down
145 changes: 99 additions & 46 deletions ecmascript/transforms/compat/tests/es2015_classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -437,9 +437,9 @@ Base.prototype.test = 1;
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -599,9 +599,9 @@ let value = 2;
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -687,9 +687,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1082,9 +1082,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1211,9 +1211,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1346,9 +1346,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1507,9 +1507,9 @@ const proper = {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1598,9 +1598,9 @@ function () {
return Hello;
}();
var Outer = function (Hello) {
var Outer = function (Hello1) {
'use strict';
_inherits(Outer, Hello);
_inherits(Outer, Hello1);
function Outer() {
_classCallCheck(this, Outer);
Expand Down Expand Up @@ -1904,9 +1904,9 @@ Object.defineProperty(Base.prototype, 'test', {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -1973,9 +1973,9 @@ let Base = function Base() {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -2127,9 +2127,9 @@ function Point() {
let ColorPoint =
/*#__PURE__*/
function (Point) {
function (Point1) {
'use strict';
_inherits(ColorPoint, Point);
_inherits(ColorPoint, Point1);
function ColorPoint() {
_classCallCheck(this, ColorPoint);
Expand Down Expand Up @@ -2354,9 +2354,9 @@ var Hello = function Hello() {
};
};
var Outer = function (Hello) {
var Outer = function (Hello1) {
'use strict';
_inherits(Outer, Hello);
_inherits(Outer, Hello1);
function Outer() {
_classCallCheck(this, Outer);
Expand Down Expand Up @@ -2412,9 +2412,9 @@ let Base = function Base() {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -2767,9 +2767,9 @@ _classCallCheck(this, A);
let B =
/*#__PURE__*/
function (A) {
function (A1) {
'use strict';
_inherits(B, A);
_inherits(B, A1);
function B() {
_classCallCheck(this, B);
Expand Down Expand Up @@ -3017,9 +3017,9 @@ var Hello = function Hello() {
};
};
var Outer = function (Hello) {
var Outer = function (Hello1) {
'use strict';
_inherits(Outer, Hello);
_inherits(Outer, Hello1);
function Outer() {
_classCallCheck(this, Outer);
Expand Down Expand Up @@ -3501,9 +3501,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -3575,9 +3575,9 @@ let Base = function Base() {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -3681,9 +3681,9 @@ function () {
var Outer =
/*#__PURE__*/
function (Hello) {
function (Hello1) {
'use strict';
_inherits(Outer, Hello);
_inherits(Outer, Hello1);
function Outer() {
_classCallCheck(this, Outer);
Expand Down Expand Up @@ -3831,9 +3831,9 @@ function () {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -4062,9 +4062,9 @@ let Base = function Base() {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -4174,9 +4174,9 @@ const proper = {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -4548,9 +4548,9 @@ let Base = function Base() {
let Obj =
/*#__PURE__*/
function (Base) {
function (Base1) {
'use strict';
_inherits(Obj, Base);
_inherits(Obj, Base1);
function Obj() {
_classCallCheck(this, Obj);
Expand Down Expand Up @@ -6130,3 +6130,56 @@ test!(
}(CanvasElement);
"
);

test!(
syntax(),
|_| classes(),
super_binding,
"
class Foo {}
class Test extends Foo {
foo() {
console.log(Foo)
}
}
",
"
let Foo = function Foo() {
'use strict';
_classCallCheck(this, Foo);
};
let Test = function(Foo1) {
'use strict';
_inherits(Test, Foo1);
function Test() {
_classCallCheck(this, Test);
return _possibleConstructorReturn(this, _getPrototypeOf(Test).apply(this, arguments));
}
_createClass(Test, [
{
key: 'foo',
value: function foo() {
console.log(Foo);
}
}
]);
return Test;
}(Foo);
"
);

test_exec!(
syntax(),
|_| classes(),
super_binding_exec,
"
class Foo {}
class Test extends Foo {
foo() {
return Foo;
}
}
Foo = 3;
expect(new Test().foo()).toBe(3);
"
);
Loading

0 comments on commit d7ea5ae

Please sign in to comment.