Skip to content

Commit 73f3abe

Browse files
authored
fix js_parser regex and add test (#871)
1 parent 45480f5 commit 73f3abe

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

android-static-binding-generator/project/parser/visitors/es5-visitors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ var es5_visitors = (function () {
599599
}
600600

601601
function _generateLineToWrite(classNameFromDecorator, extendClass, overriddenMethodNames, extendInfo, filePath, implementedInterfaces) {
602-
var lineToWrite = extendClass + "*" + extendInfo.replace(/[\\/\\-\\.]/g, "_") + "*" + overriddenMethodNames + "*" + classNameFromDecorator + "*" + filePath + "*" + (implementedInterfaces ? implementedInterfaces : "");
602+
var sanitizedExtendInfo = extendInfo.replace(/[-\\/\\.]/g, "_");
603+
var lineToWrite = extendClass + "*" + sanitizedExtendInfo + "*" + overriddenMethodNames + "*" + classNameFromDecorator + "*" + filePath + "*" + (implementedInterfaces ? implementedInterfaces : "");
603604
return lineToWrite;
604605
}
605606

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
android.telephony.PhoneStateListener.extend({});
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
(function() {
2+
var __extends_ts = function (d, b) {
3+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4+
function __() { this.constructor = d; }
5+
__.prototype = b.prototype;
6+
d.prototype = new __();
7+
};
8+
9+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10+
var c = arguments.length;
11+
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12+
13+
if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
14+
r = global.Reflect.decorate(decorators, target, key, desc);
15+
}
16+
else {
17+
for (var i = decorators.length - 1; i >= 0; i--) {
18+
if (d = decorators[i]) {
19+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
20+
}
21+
}
22+
}
23+
return c > 3 && r && Object.defineProperty(target, key, r), r;
24+
};
25+
26+
var __native = function(thiz) {
27+
var result = thiz.__proto__;
28+
29+
for (var prop in thiz)
30+
{
31+
if (thiz.hasOwnProperty(prop))
32+
{
33+
thiz.__proto__[prop] = thiz[prop];
34+
delete thiz[prop];
35+
}
36+
}
37+
38+
thiz.constructor = undefined;
39+
thiz.__proto__ = undefined;
40+
Object.freeze(thiz);
41+
Object.preventExtensions(thiz)
42+
return result;
43+
};
44+
45+
var __extends = function(Child, Parent) {
46+
47+
if (Parent.extend) {
48+
if (Parent.__isPrototypeImplementationObject) {
49+
throw new Error("Can not extend an already extended native object.");
50+
}
51+
52+
function extend(child, parent) {
53+
__log("TS extend called");
54+
if (!child.__extended) {
55+
child.__extended = parent.extend(child.name, child.prototype);
56+
}
57+
58+
return child.__extended;
59+
};
60+
61+
Parent.__activityExtend = function(parent, name, implementationObject) {
62+
__log("__activityExtend called");
63+
return parent.extend(name, implementationObject);
64+
};
65+
66+
Parent.call = function(thiz) {
67+
var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent);
68+
if (arguments.length > 1)
69+
{
70+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
71+
}
72+
else
73+
{
74+
thiz.__proto__ = new Extended();
75+
}
76+
};
77+
78+
Parent.apply = function(thiz, args) {
79+
var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent);
80+
if (args && args.length > 0)
81+
{
82+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
83+
}
84+
else
85+
{
86+
thiz.__proto__ = new Extended();
87+
}
88+
};
89+
}
90+
91+
__extends_ts(Child, Parent);
92+
93+
94+
if (Parent.extend) {
95+
Child.__isPrototypeImplementationObject = true;
96+
Child.__proto__ = Parent;
97+
Child.prototype.__parent = Parent;
98+
Child.prototype.__child = Child;
99+
}
100+
}
101+
102+
function JavaProxy(className) {
103+
return function (target) {
104+
var extended = target.extend(className, target.prototype)
105+
extended.name = className;
106+
return extended;
107+
};
108+
}
109+
110+
function Interfaces(interfacesArr) {
111+
return function (target) {
112+
if(interfacesArr instanceof Array) {
113+
// attach interfaces: [] to the object
114+
target.prototype.interfaces = interfacesArr;
115+
}
116+
}
117+
}
118+
119+
global.__native = __native;
120+
global.__extends = __extends;
121+
global.__decorate = __decorate;
122+
global.JavaProxy = JavaProxy;
123+
global.Interfaces = Interfaces;
124+
})()

android-static-binding-generator/tests/specs/ast-parser-tests.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,31 @@ describe("parser/js_parser tests", function () {
151151
});
152152
});
153153

154+
it("Files and folders with dashes in their names won't reflect in resulting java class names", function (done) {
155+
let input = path.normalize(prefix + "/directory_with_dashes/app"),
156+
jsFilesParameter = path.normalize(prefix + "/directory_with_dashes/jsFiles.txt"),
157+
output = prefix + "/directory_with_dashes/bindings.txt";
158+
159+
clearOutput(output, jsFilesParameter);
160+
161+
execGradle(input, output, jsFilesParameter, function (error, stdout, stderr) {
162+
if (error) {
163+
console.error(`exec error: ${error}`);
164+
return;
165+
}
166+
167+
logExecResult(stdout, stderr)
168+
169+
let bindingsContent = fs.readFileSync(output, "utf-8").toString().trim().split('\n');
170+
171+
let expectedPartialContent = "dir_with_dashes_file_with_dashes";
172+
let foundExpected = bindingsContent[0].indexOf(expectedPartialContent)
173+
174+
expect(foundExpected).toBeGreaterThan(0);
175+
done();
176+
});
177+
});
178+
154179
it("Generate valid metadata for bindings from a transpiled typescript file where multiple interfaces are implemented using a decorator", function (done) {
155180
let input = path.normalize(prefix + "/extends_with_interfaces_ts/app"),
156181
jsFilesParameter = path.normalize(prefix + "/extends_with_interfaces_ts/jsFiles.txt"),

0 commit comments

Comments
 (0)