Skip to content

Commit 7e1ad52

Browse files
committed
Improve test of extending logic and format code
1 parent 89849a7 commit 7e1ad52

File tree

10 files changed

+455
-497
lines changed

10 files changed

+455
-497
lines changed

test-app/app/src/main/assets/app/tests/extendedClassesTests.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,30 @@ describe("Tests extended classes ", function () {
2222
expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1);
2323
});
2424

25-
it("Having a class with static method named 'extend' and overriding it in a child class shouldn't crash the app", function (){
25+
it("Having a class with static method named 'extend' and extending it in a child class shouldn't crash the app", function (){
2626

2727
/* JS below the comment is generated from the following TS code
2828
29-
class Base{
30-
static extend(){
31-
return "expectedValue";
29+
class Base{
30+
static extend(){
31+
return "expectedValue";
32+
}
33+
}
34+
class Child extends Base{
3235
}
33-
}
3436
35-
class Child extends Base{}
37+
class SecondChild extends Child{
38+
}
3639
37-
const superProto = Object.getPrototypeOf(Child.prototype)
38-
const Super = superProto.constructor;
39-
Super.extend();
4040
41-
var child = Object.create(Child);
42-
child.extend();
43-
Child.extend();
41+
const superProto = Object.getPrototypeOf(Child.prototype)
42+
const Super = superProto.constructor;
43+
//console.log(Super.extend());
44+
45+
var child = Object.create(Child);
46+
//console.log(child.extend());
47+
48+
//console.log(Child.extend());
4449
4550
*/
4651

@@ -59,6 +64,13 @@ describe("Tests extended classes ", function () {
5964
}
6065
return Child;
6166
}(Base));
67+
var SecondChild = /** @class */ (function (_super) {
68+
__extends(SecondChild, _super);
69+
function SecondChild() {
70+
return _super !== null && _super.apply(this, arguments) || this;
71+
}
72+
return SecondChild;
73+
}(Child));
6274

6375
var superProto = Object.getPrototypeOf(Child.prototype);
6476
var Super = superProto.constructor;

test-app/app/src/main/assets/internal/ts_helpers.js

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function() {
1+
(function () {
22

33
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
44
var c = arguments.length;
@@ -18,45 +18,43 @@
1818
};
1919

2020
// For backward compatibility.
21-
var __native = function(thiz) {
22-
// we are setting the __container__ property to the base class when the super method is called
23-
// if the constructor returns the __native(this) call we will use the old implementation
24-
// copying all the properties to the result
25-
// otherwise if we are using the result from the super() method call we won't need such logic
26-
// as thiz already contains the parent properties
27-
// this way we now support both implementations in typescript generated constructors:
28-
// 1: super(); return __native(this);
29-
// 2: return super() || this;
30-
if(thiz.__container__) {
31-
var result = thiz.__proto__;
32-
33-
for (var prop in thiz)
34-
{
35-
if (thiz.hasOwnProperty(prop))
36-
{
37-
thiz.__proto__[prop] = thiz[prop];
38-
delete thiz[prop];
39-
}
40-
}
41-
42-
thiz.constructor = undefined;
43-
thiz.__proto__ = undefined;
44-
Object.freeze(thiz);
45-
Object.preventExtensions(thiz);
46-
return result;
21+
var __native = function (thiz) {
22+
// we are setting the __container__ property to the base class when the super method is called
23+
// if the constructor returns the __native(this) call we will use the old implementation
24+
// copying all the properties to the result
25+
// otherwise if we are using the result from the super() method call we won't need such logic
26+
// as thiz already contains the parent properties
27+
// this way we now support both implementations in typescript generated constructors:
28+
// 1: super(); return __native(this);
29+
// 2: return super() || this;
30+
if (thiz.__container__) {
31+
var result = thiz.__proto__;
32+
33+
for (var prop in thiz) {
34+
if (thiz.hasOwnProperty(prop)) {
35+
thiz.__proto__[prop] = thiz[prop];
36+
delete thiz[prop];
37+
}
38+
}
39+
40+
thiz.constructor = undefined;
41+
thiz.__proto__ = undefined;
42+
Object.freeze(thiz);
43+
Object.preventExtensions(thiz);
44+
return result;
4745
} else {
48-
return thiz;
46+
return thiz;
4947
}
5048
};
5149

52-
var __extends = function(Child, Parent) {
50+
var __extends = function (Child, Parent) {
5351
const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g;
5452
var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString());
55-
if (!extendNativeClass) {
53+
if (!extendNativeClass) {
5654
__extends_ts(Child, Parent);
5755
return;
5856
}
59-
if (Parent.__isPrototypeImplementationObject) {
57+
if (Parent.__isPrototypeImplementationObject) {
6058
throw new Error("Can not extend an already extended native object.");
6159
}
6260

@@ -66,41 +64,37 @@
6664
var parent = thiz.__proto__.__parent;
6765
child.__extended = parent.extend(child.name, child.prototype, true);
6866
// This will deal with "i instanceof child"
69-
child[Symbol.hasInstance] = function(instance) {
67+
child[Symbol.hasInstance] = function (instance) {
7068
return instance instanceof this.__extended;
7169
}
7270
}
7371
return child.__extended;
7472
};
7573

76-
Parent.__activityExtend = function(parent, name, implementationObject) {
74+
Parent.__activityExtend = function (parent, name, implementationObject) {
7775
__log("__activityExtend called");
7876
return parent.extend(name, implementationObject);
7977
};
8078

81-
Parent.call = function(thiz) {
79+
Parent.call = function (thiz) {
8280
var Extended = extend(thiz);
8381
thiz.__container__ = true;
84-
if (arguments.length > 1)
85-
{
82+
if (arguments.length > 1) {
8683
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
8784
}
88-
else
89-
{
85+
else {
9086
thiz.__proto__ = new Extended()
9187
}
9288
return thiz.__proto__;
9389
};
9490

95-
Parent.apply = function(thiz, args) {
91+
Parent.apply = function (thiz, args) {
9692
var Extended = extend(thiz);
9793
thiz.__container__ = true;
98-
if (args && args.length > 0)
99-
{
94+
if (args && args.length > 0) {
10095
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
10196
}
102-
else
103-
{
97+
else {
10498
thiz.__proto__ = new Extended();
10599
}
106100
return thiz.__proto__;
@@ -126,32 +120,32 @@
126120
};
127121

128122
var extendStaticFunctions =
129-
Object.setPrototypeOf
130-
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
131-
|| assignPropertiesFromParentToChild;
123+
Object.setPrototypeOf
124+
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
125+
|| assignPropertiesFromParentToChild;
132126

133-
function hasInternalProtoProperty(){
127+
function hasInternalProtoProperty() {
134128
return { __proto__: [] } instanceof Array;
135129
}
136130

137-
function assignPropertiesFromParentToChild(parent, child){
138-
for (var property in parent){
131+
function assignPropertiesFromParentToChild(parent, child) {
132+
for (var property in parent) {
139133
if (parent.hasOwnProperty(property)) {
140134
child[property] = parent[property];
141135
}
142136
}
143137
}
144138

145-
function assignPrototypeFromParentToChild(parent, child){
139+
function assignPrototypeFromParentToChild(parent, child) {
146140
function __() {
147141
this.constructor = child;
148142
}
149143

150-
if(parent === null){
144+
if (parent === null) {
151145
child.prototype = Object.create(null);
152146
} else {
153147
__.prototype = parent.prototype;
154-
child.prototype = new __();
148+
child.prototype = new __();
155149
}
156150
}
157151

@@ -166,7 +160,7 @@
166160

167161
function Interfaces(interfacesArr) {
168162
return function (target) {
169-
if(interfacesArr instanceof Array) {
163+
if (interfacesArr instanceof Array) {
170164
// attach interfaces: [] to the object
171165
target.prototype.interfaces = interfacesArr;
172166
}

0 commit comments

Comments
 (0)