Skip to content

Commit d9dda31

Browse files
committed
fix: handle all swc transform cases
1 parent 6f1b7df commit d9dda31

File tree

7 files changed

+1379
-773
lines changed

7 files changed

+1379
-773
lines changed

test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts_swc/app/normal_ts_extends.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var _swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__ = {
2323

2424
var ClickListener = /*#__PURE__*/ function(_java_lang_Object) {
2525
"use strict";
26-
__swc_crazy_long_line_swc_inherit_polyfill._(ClickListener, _java_lang_Object);
26+
(0, __swc_crazy_long_line_swc_inherit_polyfill._)(ClickListener, _java_lang_Object);
2727
function ClickListener() {
2828
(0,_swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__._)(this, ClickListener);
2929
var _this;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var __swc_crazy_long_line_swc_inherit_polyfill = {
2+
_: () => {}
3+
}
4+
var _Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__ = {
5+
_: () => {}
6+
}
7+
8+
var _swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__ = {
9+
_: () => {}
10+
}
11+
12+
var _swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__ = {
13+
_: () => {}
14+
}
15+
16+
var _swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__ = {
17+
_: () => {}
18+
}
19+
20+
var _swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__ = {
21+
__decorate: () => {}
22+
}
23+
24+
var ClickListener = /*#__PURE__*/ function(_java_lang_Object) {
25+
"use strict";
26+
__swc_crazy_long_line_swc_inherit_polyfill._(ClickListener, _java_lang_Object);
27+
function ClickListener() {
28+
(0,_swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__._)(this, ClickListener);
29+
var _this;
30+
_this = (0,_Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__._)(this, ClickListener);
31+
// necessary when extending TypeScript constructors
32+
return (0,_swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__._)(_this, global.__native(_this));
33+
}
34+
(0,_swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__._)(ClickListener, [
35+
{
36+
key: "onClick",
37+
value: function onClick(view) {
38+
console.log("Button clicked!");
39+
}
40+
},
41+
{
42+
key: "onDone",
43+
value: function onClick(view) {
44+
console.log("Button clicked!");
45+
}
46+
}
47+
]);
48+
return ClickListener;
49+
}(java.lang.Object);
50+
51+
ClickListener = (0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__decorate)([
52+
NativeClass,
53+
Interfaces([
54+
android.view.View.OnClickListener
55+
]),
56+
(0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__metadata)("design:type", Function),
57+
(0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__metadata)("design:paramtypes", [])
58+
], ClickListener);
59+
var myObj = new MyObject("world");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
(function () {
2+
3+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4+
var c = arguments.length;
5+
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6+
7+
if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
8+
r = global.Reflect.decorate(decorators, target, key, desc);
9+
}
10+
else {
11+
for (var i = decorators.length - 1; i >= 0; i--) {
12+
if (d = decorators[i]) {
13+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
14+
}
15+
}
16+
}
17+
return c > 3 && r && Object.defineProperty(target, key, r), r;
18+
};
19+
20+
// For backward compatibility.
21+
var __native = function (thiz) {
22+
// we are setting the __container__ property to the base class when the super method is called
23+
// if the constructor returns the __native(this) call we will use the old implementation
24+
// copying all the properties to the result
25+
// otherwise if we are using the result from the super() method call we won't need such logic
26+
// as thiz already contains the parent properties
27+
// this way we now support both implementations in typescript generated constructors:
28+
// 1: super(); return __native(this);
29+
// 2: return super() || this;
30+
if (thiz.__container__) {
31+
var result = thiz.__proto__;
32+
33+
for (var prop in thiz) {
34+
if (thiz.hasOwnProperty(prop)) {
35+
thiz.__proto__[prop] = thiz[prop];
36+
delete thiz[prop];
37+
}
38+
}
39+
40+
thiz.constructor = undefined;
41+
thiz.__proto__ = undefined;
42+
Object.freeze(thiz);
43+
Object.preventExtensions(thiz);
44+
return result;
45+
} else {
46+
return thiz;
47+
}
48+
};
49+
50+
var __extends = function (Child, Parent) {
51+
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
52+
if (!extendNativeClass) {
53+
__extends_ts(Child, Parent);
54+
return;
55+
}
56+
if (Parent.__isPrototypeImplementationObject) {
57+
throw new Error("Can not extend an already extended native object.");
58+
}
59+
60+
function extend(thiz) {
61+
var child = thiz.__proto__.__child;
62+
if (!child.__extended) {
63+
var parent = thiz.__proto__.__parent;
64+
child.__extended = parent.extend(child.name, child.prototype, true);
65+
// This will deal with "i instanceof child"
66+
child[Symbol.hasInstance] = function (instance) {
67+
return instance instanceof this.__extended;
68+
}
69+
}
70+
return child.__extended;
71+
};
72+
73+
Parent.__activityExtend = function (parent, name, implementationObject) {
74+
__log("__activityExtend called");
75+
return parent.extend(name, implementationObject);
76+
};
77+
78+
Parent.call = function (thiz) {
79+
var Extended = extend(thiz);
80+
thiz.__container__ = true;
81+
if (arguments.length > 1) {
82+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
83+
}
84+
else {
85+
thiz.__proto__ = new Extended()
86+
}
87+
return thiz.__proto__;
88+
};
89+
90+
Parent.apply = function (thiz, args) {
91+
var Extended = extend(thiz);
92+
thiz.__container__ = true;
93+
if (args && args.length > 0) {
94+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
95+
}
96+
else {
97+
thiz.__proto__ = new Extended();
98+
}
99+
return thiz.__proto__;
100+
};
101+
__extends_ns(Child, Parent);
102+
Child.__isPrototypeImplementationObject = true;
103+
Child.__proto__ = Parent;
104+
Child.prototype.__parent = Parent;
105+
Child.prototype.__child = Child;
106+
}
107+
108+
var __extends_ts = function (child, parent) {
109+
extendStaticFunctions(child, parent);
110+
assignPrototypeFromParentToChild(parent, child);
111+
};
112+
113+
var __extends_ns = function (child, parent) {
114+
if (!parent.extend) {
115+
assignPropertiesFromParentToChild(parent, child);
116+
}
117+
118+
assignPrototypeFromParentToChild(parent, child);
119+
};
120+
121+
var extendStaticFunctions =
122+
Object.setPrototypeOf
123+
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
124+
|| assignPropertiesFromParentToChild;
125+
126+
function hasInternalProtoProperty() {
127+
return { __proto__: [] } instanceof Array;
128+
}
129+
130+
function assignPropertiesFromParentToChild(parent, child) {
131+
for (var property in parent) {
132+
if (parent.hasOwnProperty(property)) {
133+
child[property] = parent[property];
134+
}
135+
}
136+
}
137+
138+
function assignPrototypeFromParentToChild(parent, child) {
139+
function __() {
140+
this.constructor = child;
141+
}
142+
143+
if (parent === null) {
144+
child.prototype = Object.create(null);
145+
} else {
146+
__.prototype = parent.prototype;
147+
child.prototype = new __();
148+
}
149+
}
150+
151+
152+
function JavaProxy(className) {
153+
return function (target) {
154+
var extended = target.extend(className, target.prototype)
155+
extended.name = className;
156+
return extended;
157+
};
158+
}
159+
160+
function Interfaces(interfacesArr) {
161+
return function (target) {
162+
if (interfacesArr instanceof Array) {
163+
// attach interfaces: [] to the object
164+
target.prototype.interfaces = interfacesArr;
165+
}
166+
}
167+
}
168+
169+
Object.defineProperty(global, "__native", { value: __native });
170+
Object.defineProperty(global, "__extends", { value: __extends });
171+
Object.defineProperty(global, "__decorate", { value: __decorate });
172+
173+
global.JavaProxy = JavaProxy;
174+
global.Interfaces = Interfaces;
175+
})()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var __swc_crazy_long_line_swc_inherit_polyfill = () => {}
2+
3+
var _Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__ = {
4+
_: () => {}
5+
}
6+
7+
var _swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__ = {
8+
_: () => {}
9+
}
10+
11+
var _swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__ = {
12+
_: () => {}
13+
}
14+
15+
var _swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__ = {
16+
_: () => {}
17+
}
18+
19+
var _swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__ = () => {}
20+
21+
var ClickListener = /*#__PURE__*/ function(_java_lang_Object) {
22+
"use strict";
23+
__swc_crazy_long_line_swc_inherit_polyfill(ClickListener, _java_lang_Object);
24+
function ClickListener() {
25+
(0,_swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__._)(this, ClickListener);
26+
var _this;
27+
_this = (0,_Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__._)(this, ClickListener);
28+
// necessary when extending TypeScript constructors
29+
return (0,_swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__._)(_this, global.__native(_this));
30+
}
31+
(0,_swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__._)(ClickListener, [
32+
{
33+
key: "onClick",
34+
value: function onClick(view) {
35+
console.log("Button clicked!");
36+
}
37+
},
38+
{
39+
key: "onDone",
40+
value: function onClick(view) {
41+
console.log("Button clicked!");
42+
}
43+
}
44+
]);
45+
return ClickListener;
46+
}(java.lang.Object);
47+
48+
ClickListener = _swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__([
49+
NativeClass,
50+
Interfaces([
51+
android.view.View.OnClickListener
52+
]),
53+
(0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__metadata)("design:type", Function),
54+
(0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__metadata)("design:paramtypes", [])
55+
], ClickListener);
56+
var myObj = new MyObject("world");

0 commit comments

Comments
 (0)