From 8ebffcb334574bddfaee5f4204d6e859b38baa02 Mon Sep 17 00:00:00 2001 From: Vasil Trifonov Date: Tue, 6 Nov 2018 16:34:29 +0200 Subject: [PATCH 1/4] Improve extend handling --- .../src/main/assets/internal/ts_helpers.js | 126 ++++++++++-------- 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/test-app/app/src/main/assets/internal/ts_helpers.js b/test-app/app/src/main/assets/internal/ts_helpers.js index c1dc5f079..018921c6e 100644 --- a/test-app/app/src/main/assets/internal/ts_helpers.js +++ b/test-app/app/src/main/assets/internal/ts_helpers.js @@ -1,5 +1,5 @@ (function() { - var __extends_ts = function (d, b) { + var __extends_ns = function (d, b) { if (!b.extend) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; } @@ -9,6 +9,17 @@ d.prototype = new __(); }; + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + + var __extends_ts = function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; + + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; @@ -59,68 +70,65 @@ }; var __extends = function(Child, Parent) { + var extendNativeClass = !!Parent.extend && /native/.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); - } - - function extend(thiz) { - var child = thiz.__proto__.__child; - if (!child.__extended) { - var parent = thiz.__proto__.__parent; - child.__extended = parent.extend(child.name, child.prototype, true); - // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { - return instance instanceof this.__extended; - } - } - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz); - thiz.__container__ = true; - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended() - } - return thiz.__proto__; - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz); - thiz.__container__ = true; - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; } - else - { - thiz.__proto__ = new Extended(); - } - return thiz.__proto__; - }; - } + } + return child.__extended; + }; - __extends_ts(Child, Parent); + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; - } + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else + { + thiz.__proto__ = new Extended(); + } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; } function JavaProxy(className) { @@ -146,4 +154,4 @@ global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; -})() +})() \ No newline at end of file From 670dd1a2a335956cfb82f49299fe50239e81ff12 Mon Sep 17 00:00:00 2001 From: vmutafov Date: Fri, 9 Nov 2018 09:09:17 +0200 Subject: [PATCH 2/4] Refactor th_helpers.js and fix 'extend' method handling --- .../assets/app/tests/extendedClassesTests.js | 56 +++- .../src/main/assets/internal/ts_helpers.js | 67 +++-- .../internal/ts_helpers.js | 266 +++++++++++------- .../internal/ts_helpers.js | 266 +++++++++++------- .../internal/ts_helpers.js | 266 +++++++++++------- .../cases/extends/internal/ts_helpers.js | 266 +++++++++++------- .../internal/ts_helpers.js | 266 +++++++++++------- .../internal/ts_helpers.js | 266 +++++++++++------- .../internal/ts_helpers.js | 266 +++++++++++------- .../cases/mini_app/internal/ts_helpers.js | 266 +++++++++++------- 10 files changed, 1394 insertions(+), 857 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/extendedClassesTests.js b/test-app/app/src/main/assets/app/tests/extendedClassesTests.js index 3bf97ddd8..d00021dec 100644 --- a/test-app/app/src/main/assets/app/tests/extendedClassesTests.js +++ b/test-app/app/src/main/assets/app/tests/extendedClassesTests.js @@ -1,6 +1,6 @@ describe("Tests extended classes ", function () { - it("Instance_with_no_extension_shouldnt_use_previously_defined_implementation_object", function () { + it("Instance with no extension shouldn't use previously defined implementation object", function () { var MyButton = com.tns.tests.Button1.extend({ toString: function () { return "overriden toString method of chronometer instance"; @@ -21,8 +21,56 @@ describe("Tests extended classes ", function () { expect(labelToString).not.toBe(labelToString1); expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1); }); + + it("Having a class with static method named 'extend' and overriding it in a child class shouldn't crash the app", function (){ + + /* JS below the comment is generated from the following TS code + + class Base{ + static extend(){ + return "expectedValue"; + } + } + + class Child extends Base{} + + const superProto = Object.getPrototypeOf(Child.prototype) + const Super = superProto.constructor; + Super.extend(); + + var child = Object.create(Child); + child.extend(); + Child.extend(); + + */ + + var Base = /** @class */ (function () { + function Base() { + } + Base.extend = function () { + return "expectedValue"; + }; + return Base; + }()); + var Child = /** @class */ (function (_super) { + __extends(Child, _super); + function Child() { + return _super !== null && _super.apply(this, arguments) || this; + } + return Child; + }(Base)); + + var superProto = Object.getPrototypeOf(Child.prototype); + var Super = superProto.constructor; + expect(Super.extend()).toBe("expectedValue"); + + var child = Object.create(Child); + expect(child.extend()).toBe("expectedValue"); + + expect(Child.extend()).toBe("expectedValue"); + }); - it("Instance_with_extension_shouldnt_use_previously_defined_implementation_object", function () { + it("Instance with extension shouldn't use previously defined implementation object", function () { var MyButton = com.tns.tests.Button1.extend({ toString: function () { @@ -53,7 +101,7 @@ describe("Tests extended classes ", function () { expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1); }); - it("Newly_created_instances_should_behave_the_same_and_not_use_previously_defined_implementation_objects", function () { + it("Newly created instances should behave the same and not use previously defined implementation objects", function () { var button1 = new com.tns.tests.Button1(); var labelgetIMAGE_ID_PROP1 = button1.getIMAGE_ID_PROP(); @@ -74,7 +122,7 @@ describe("Tests extended classes ", function () { expect(labelgetIMAGE_ID_PROP1).toBe(labelgetIMAGE_ID_PROP2); }); - it("should not crash with no exception when incorrectly calling extended class constructor", function () { + it("Should not crash with no exception when incorrectly calling extended class constructor", function () { let MyObj = java.lang.Object.extend({ toString: () => { return "It's MyObj" } }); diff --git a/test-app/app/src/main/assets/internal/ts_helpers.js b/test-app/app/src/main/assets/internal/ts_helpers.js index 018921c6e..25d9346d5 100644 --- a/test-app/app/src/main/assets/internal/ts_helpers.js +++ b/test-app/app/src/main/assets/internal/ts_helpers.js @@ -1,24 +1,4 @@ (function() { - var __extends_ns = function (d, b) { - if (!b.extend) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - } - - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - - var extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; - - var __extends_ts = function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; - var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -70,7 +50,8 @@ }; var __extends = function(Child, Parent) { - var extendNativeClass = !!Parent.extend && /native/.test(Parent.extend.toString()); + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); if (!extendNativeClass) { __extends_ts(Child, Parent); return; @@ -131,6 +112,50 @@ Child.prototype.__child = Child; } + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); + } + + assignPrototypeFromParentToChild(parent, child); + }; + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; + } + } + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); + } + } + + function JavaProxy(className) { return function (target) { var extended = target.extend(className, target.prototype) diff --git a/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js index 2175f563f..2f4526912 100644 --- a/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js @@ -1,124 +1,182 @@ (function() { - var __extends_ts = function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); - }; - + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length; - var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - - if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { - r = global.Reflect.decorate(decorators, target, key, desc); - } - else { - for (var i = decorators.length - 1; i >= 0; i--) { - if (d = decorators[i]) { - r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function(thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if(thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) + { + if (thiz.hasOwnProperty(prop)) + { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; } } - } - return c > 3 && r && Object.defineProperty(target, key, r), r; - }; - - var __native = function(thiz) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function(Child, Parent) { + var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function(instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function(parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function(thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else + { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function(thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) + { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; + thiz.__proto__ = new Extended(); } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz) - return result; + + assignPrototypeFromParentToChild(parent, child); }; - - var __extends = function(Child, Parent) { - - if (Parent.extend) { - if (Parent.__isPrototypeImplementationObject) { - throw new Error("Can not extend an already extended native object."); + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty(){ + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child){ + for (var property in parent){ + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; } - - function extend(child, parent) { - __log("TS extend called"); - if (!child.__extended) { - child.__extended = parent.extend(child.name, child.prototype); - } - - return child.__extended; - }; - - Parent.__activityExtend = function(parent, name, implementationObject) { - __log("__activityExtend called"); - return parent.extend(name, implementationObject); - }; - - Parent.call = function(thiz) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (arguments.length > 1) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; - - Parent.apply = function(thiz, args) { - var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent); - if (args && args.length > 0) - { - thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); - } - else - { - thiz.__proto__ = new Extended(); - } - }; } - - __extends_ts(Child, Parent); - - - if (Parent.extend) { - Child.__isPrototypeImplementationObject = true; - Child.__proto__ = Parent; - Child.prototype.__parent = Parent; - Child.prototype.__child = Child; + } + + function assignPrototypeFromParentToChild(parent, child){ + function __() { + this.constructor = child; + } + + if(parent === null){ + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); } } - + + function JavaProxy(className) { - return function (target) { - var extended = target.extend(className, target.prototype) - extended.name = className; - return extended; - }; + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; } function Interfaces(interfacesArr) { - return function (target) { - if(interfacesArr instanceof Array) { - // attach interfaces: [] to the object - target.prototype.interfaces = interfacesArr; - } - } + return function (target) { + if(interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } } - - global.__native = __native; - global.__extends = __extends; - global.__decorate = __decorate; + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + global.JavaProxy = JavaProxy; global.Interfaces = Interfaces; })() \ No newline at end of file From 8a55ac04cba504144be996b7b6a03e46d85c409c Mon Sep 17 00:00:00 2001 From: vmutafov Date: Fri, 9 Nov 2018 17:14:07 +0200 Subject: [PATCH 3/4] Use gradle plugin 3.2.1 instead of 3.2.0 --- test-app/build-tools/android-metadata-generator/build.gradle | 2 +- test-app/build.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test-app/build-tools/android-metadata-generator/build.gradle b/test-app/build-tools/android-metadata-generator/build.gradle index 13d5817b6..9bcb52ff5 100644 --- a/test-app/build-tools/android-metadata-generator/build.gradle +++ b/test-app/build-tools/android-metadata-generator/build.gradle @@ -17,7 +17,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.1.4' + classpath 'com.android.tools.build:gradle:3.2.1' } } diff --git a/test-app/build.gradle b/test-app/build.gradle index 3ecb8192f..56dc9966f 100644 --- a/test-app/build.gradle +++ b/test-app/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.android.tools.build:gradle:3.2.1' } } From 7e1ad52e94267df9e407a7eebf031d8e8d16d727 Mon Sep 17 00:00:00 2001 From: vmutafov Date: Fri, 16 Nov 2018 17:45:46 +0200 Subject: [PATCH 4/4] Improve test of extending logic and format code --- .../assets/app/tests/extendedClassesTests.js | 36 ++++--- .../src/main/assets/internal/ts_helpers.js | 100 ++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../cases/extends/internal/ts_helpers.js | 102 +++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../internal/ts_helpers.js | 102 +++++++++--------- .../cases/mini_app/internal/ts_helpers.js | 102 +++++++++--------- 10 files changed, 455 insertions(+), 497 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/extendedClassesTests.js b/test-app/app/src/main/assets/app/tests/extendedClassesTests.js index d00021dec..586d049ce 100644 --- a/test-app/app/src/main/assets/app/tests/extendedClassesTests.js +++ b/test-app/app/src/main/assets/app/tests/extendedClassesTests.js @@ -22,25 +22,30 @@ describe("Tests extended classes ", function () { expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1); }); - it("Having a class with static method named 'extend' and overriding it in a child class shouldn't crash the app", function (){ + it("Having a class with static method named 'extend' and extending it in a child class shouldn't crash the app", function (){ /* JS below the comment is generated from the following TS code - class Base{ - static extend(){ - return "expectedValue"; + class Base{ + static extend(){ + return "expectedValue"; + } + } + class Child extends Base{ } - } - class Child extends Base{} + class SecondChild extends Child{ + } - const superProto = Object.getPrototypeOf(Child.prototype) - const Super = superProto.constructor; - Super.extend(); - var child = Object.create(Child); - child.extend(); - Child.extend(); + const superProto = Object.getPrototypeOf(Child.prototype) + const Super = superProto.constructor; + //console.log(Super.extend()); + + var child = Object.create(Child); + //console.log(child.extend()); + + //console.log(Child.extend()); */ @@ -59,6 +64,13 @@ describe("Tests extended classes ", function () { } return Child; }(Base)); + var SecondChild = /** @class */ (function (_super) { + __extends(SecondChild, _super); + function SecondChild() { + return _super !== null && _super.apply(this, arguments) || this; + } + return SecondChild; + }(Child)); var superProto = Object.getPrototypeOf(Child.prototype); var Super = superProto.constructor; diff --git a/test-app/app/src/main/assets/internal/ts_helpers.js b/test-app/app/src/main/assets/internal/ts_helpers.js index 25d9346d5..f5c450a65 100644 --- a/test-app/app/src/main/assets/internal/ts_helpers.js +++ b/test-app/app/src/main/assets/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { + var __extends = function (Child, Parent) { const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/decorated_double_assignment_ts/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/decorated_extends_ts/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/directory_with_dashes/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/file_names_with_dots/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; } diff --git a/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js index 2f4526912..f5c450a65 100644 --- a/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js +++ b/test-app/build-tools/jsparser/tests/cases/mini_app/internal/ts_helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length; @@ -18,45 +18,43 @@ }; // For backward compatibility. - var __native = function(thiz) { - // we are setting the __container__ property to the base class when the super method is called - // if the constructor returns the __native(this) call we will use the old implementation - // copying all the properties to the result - // otherwise if we are using the result from the super() method call we won't need such logic - // as thiz already contains the parent properties - // this way we now support both implementations in typescript generated constructors: - // 1: super(); return __native(this); - // 2: return super() || this; - if(thiz.__container__) { - var result = thiz.__proto__; - - for (var prop in thiz) - { - if (thiz.hasOwnProperty(prop)) - { - thiz.__proto__[prop] = thiz[prop]; - delete thiz[prop]; - } - } - - thiz.constructor = undefined; - thiz.__proto__ = undefined; - Object.freeze(thiz); - Object.preventExtensions(thiz); - return result; + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; } else { - return thiz; + return thiz; } }; - var __extends = function(Child, Parent) { - var NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; + var __extends = function (Child, Parent) { + const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g; var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString()); - if (!extendNativeClass) { + if (!extendNativeClass) { __extends_ts(Child, Parent); return; } - if (Parent.__isPrototypeImplementationObject) { + if (Parent.__isPrototypeImplementationObject) { throw new Error("Can not extend an already extended native object."); } @@ -66,41 +64,37 @@ var parent = thiz.__proto__.__parent; child.__extended = parent.extend(child.name, child.prototype, true); // This will deal with "i instanceof child" - child[Symbol.hasInstance] = function(instance) { + child[Symbol.hasInstance] = function (instance) { return instance instanceof this.__extended; } } return child.__extended; }; - Parent.__activityExtend = function(parent, name, implementationObject) { + Parent.__activityExtend = function (parent, name, implementationObject) { __log("__activityExtend called"); return parent.extend(name, implementationObject); }; - Parent.call = function(thiz) { + Parent.call = function (thiz) { var Extended = extend(thiz); thiz.__container__ = true; - if (arguments.length > 1) - { + if (arguments.length > 1) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); } - else - { + else { thiz.__proto__ = new Extended() } return thiz.__proto__; }; - Parent.apply = function(thiz, args) { + Parent.apply = function (thiz, args) { var Extended = extend(thiz); thiz.__container__ = true; - if (args && args.length > 0) - { + if (args && args.length > 0) { thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); } - else - { + else { thiz.__proto__ = new Extended(); } return thiz.__proto__; @@ -126,32 +120,32 @@ }; var extendStaticFunctions = - Object.setPrototypeOf - || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) - || assignPropertiesFromParentToChild; + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; - function hasInternalProtoProperty(){ + function hasInternalProtoProperty() { return { __proto__: [] } instanceof Array; } - function assignPropertiesFromParentToChild(parent, child){ - for (var property in parent){ + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { if (parent.hasOwnProperty(property)) { child[property] = parent[property]; } } } - function assignPrototypeFromParentToChild(parent, child){ + function assignPrototypeFromParentToChild(parent, child) { function __() { this.constructor = child; } - if(parent === null){ + if (parent === null) { child.prototype = Object.create(null); } else { __.prototype = parent.prototype; - child.prototype = new __(); + child.prototype = new __(); } } @@ -166,7 +160,7 @@ function Interfaces(interfacesArr) { return function (target) { - if(interfacesArr instanceof Array) { + if (interfacesArr instanceof Array) { // attach interfaces: [] to the object target.prototype.interfaces = interfacesArr; }