Skip to content

Commit

Permalink
test(transformer/class-properties): override fixtures (#7689)
Browse files Browse the repository at this point in the history
Override some transform conformance test fixtures for class properties transform, where:

* Our output differs from Babel in cosmetic manner only.
* Our transform intentionally works differently from Babel.
* Babel's fixtures enable arrow functions transform, which malfunctions in our implementation. But we're not trying to test arrow functions transform here, so disable it.
  • Loading branch information
overlookmotel committed Dec 8, 2024
1 parent efaaa97 commit 5fd1361
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var _bar = /*#__PURE__*/new WeakMap();
class Foo extends Bar {
constructor() {
var _super = (..._args) => {
super(..._args);
babelHelpers.classPrivateFieldInitSpec(this, _bar, "foo");
return this;
};

if (condition) {
_super();
} else {
_super();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var _myAsyncMethod2;
var _myAsyncMethod = new WeakMap();
class MyClass {
constructor() {
var _this = this;
babelHelpers.classPrivateFieldInitSpec(this, _myAsyncMethod, babelHelpers.asyncToGenerator(function* () {
console.log(_this);
}));
}
}
_myAsyncMethod2 = new WeakMap(), class MyClass2 {
constructor() {
var _this2 = this;
babelHelpers.classPrivateFieldInitSpec(this, _myAsyncMethod2, babelHelpers.asyncToGenerator(function* () {
console.log(_this2);
}));
}
};
var _myAsyncMethod3 = new WeakMap();
export default class MyClass3 {
constructor() {
var _this3 = this;
babelHelpers.classPrivateFieldInitSpec(this, _myAsyncMethod3, babelHelpers.asyncToGenerator(function* () {
console.log(_this3);
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
[
"transform-class-properties",
{
"loose": true
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disable arrow functions transform in `options.json` because it malfunctions.
But these fixtures aren't to test arrow functions transform.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo extends Bar {
constructor() {
var _super = (..._args) => {
super(..._args);
this.bar = "foo";
return this;
};
foo(_super());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var _Class;
const foo = { [Symbol.toPrimitive]: () => "foo" };
expect((_Class = class {}, babelHelpers.defineProperty(_Class, foo, 0), _Class).foo).toBe(0);
expect(class {
static [foo]() {
return 0;
}
}.foo()).toBe(0);
expect(class {
static get [foo]() {
return 0;
}
}.foo).toBe(0);
expect(class {
static set [foo](v) {
return v;
}
}.foo = 0).toBe(0);
expect(new class {
constructor() {
babelHelpers.defineProperty(this, foo, 0);
}
}().foo).toBe(0);
const arrayLike = { [Symbol.toPrimitive]: () => [] };
expect(() => {
var _Class2;
return _Class2 = class {}, babelHelpers.defineProperty(_Class2, arrayLike, 0), _Class2;
}).toThrow("@@toPrimitive must return a primitive value.");
expect(() => class {
static [arrayLike]() {
return 0;
}
}).toThrow("@@toPrimitive must return a primitive value.");
expect(() => class {
static get [arrayLike]() {
return 0;
}
}).toThrow("@@toPrimitive must return a primitive value.");
expect(() => class {
static set [arrayLike](v) {
return v;
}
}).toThrow("@@toPrimitive must return a primitive value.");
expect(() => new class {
constructor() {
babelHelpers.defineProperty(this, arrayLike, 0);
}
}()).toThrow("@@toPrimitive must return a primitive value.");
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Foo extends Bar {
constructor() {
var _super = (..._args) => {
super(..._args);
babelHelpers.defineProperty(this, "bar", "foo");
return this;
};

if (condition) {
_super();
} else {
_super();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let _super = function() {
babelHelpers.defineProperty(this, "bar", "foo");
return this;
};
class Foo extends Bar {
constructor(x = test ? _super.call(super()) : 0) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let _super = function() {
babelHelpers.defineProperty(this, "bar", "foo");
return this;
};
class Foo extends Bar {
constructor(x = () => {
check(_super.call(super()));
}) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let _super = function() {
babelHelpers.defineProperty(this, "bar", "foo");
return this;
};
class Foo extends Bar {
constructor(x = _super.call(super())) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo extends Bar {
constructor() {
var _super = (..._args) => {
super(..._args);
babelHelpers.defineProperty(this, "bar", "foo");
return this;
};
foo(_super());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"transform-class-properties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"use strict";

class C {}

class A extends C {
constructor() {
super();
babelHelpers.defineProperty(this, "field", 1);
class B extends C {
constructor() {
super();
expect(this.field).toBeUndefined();
}
}
expect(this.field).toBe(1);
new B();
}
}
new A();

class Obj {
constructor() {
return {};
}
}

// ensure superClass is still transformed
class SuperClass extends Obj {
constructor() {
var _super = (..._args) => {
super(..._args);
babelHelpers.defineProperty(this, "field", 1);
return this;
};
class B extends (_super(), Obj) {
constructor() {
super();
expect(this.field).toBeUndefined();
}
}
expect(this.field).toBe(1);
new B();
}
}
new SuperClass();

// ensure ComputedKey Method is still transformed
class ComputedMethod extends Obj {
constructor() {
var _super2 = (..._args2) => {
super(..._args2);
babelHelpers.defineProperty(this, "field", 1);
return this;
};
class B extends Obj {
constructor() {
super();
expect(this.field).toBeUndefined();
}
[_super2()]() {}
}
expect(this.field).toBe(1);
new B();
}
}
new ComputedMethod();

// ensure ComputedKey Field is still transformed
class ComputedField extends Obj {
constructor() {
let _super4;
var _super3 = (..._args3) => {
super(..._args3);
babelHelpers.defineProperty(this, "field", 1);
return this;
};
_super4 = _super3();
class B extends Obj {
constructor() {
super();
babelHelpers.defineProperty(this, _super4, 1);
expect(this.field).toBeUndefined();
}
}
expect(this.field).toBe(1);
new B();
}
}
new ComputedField();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disable arrow functions transform in `options.json` because it malfunctions.
But these fixtures aren't to test arrow functions transform.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class A extends B {
constructor() {
var _super = (..._args) => {
super(..._args);
babelHelpers.defineProperty(this, "x", 2);
return this;
};
x ? _super(a) : _super(b);
}
}
Loading

0 comments on commit 5fd1361

Please sign in to comment.