[ALOY-794] Alloy Conditional code not functional
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2014-01-21T22:28:19.000+0000 |
Affected Version/s | Alloy 1.1.2 |
Fix Version/s | Alloy 1.4.0 |
Components | Runtime |
Labels | alloy, qe, qe-closed-3.3.0, qe-manualtest |
Reporter | Mason Zhang |
Assignee | Tony Lukasavage |
Created | 2013-08-17T12:43:42.000+0000 |
Updated | 2014-11-20T18:20:27.000+0000 |
Description
Q&A: http://developer.appcelerator.com/question/156082/alloy-compiler-conditional-code-not-work
Add code below in a controller code:
if(OS_ANDROID) {
function aaa() {
Ti.API.info('android aaa');
}
}
else {
function bbb() {
Ti.API.info('not android bbb');
}
}
if(OS_IOS) {
function aaa() {
Ti.API.info('ios aaa');
}
}
else {
function bbb() {
Ti.API.info('not ios bbb');
}
}
setTimeout(aaa, 0);
setTimeout(bbb, 0);
The compiled code is:
function aaa() {
Ti.API.info("android aaa");
}
function bbb() {
Ti.API.info("not android bbb");
}
function aaa() {
Ti.API.info("ios aaa");
}
function bbb() {
Ti.API.info("not ios bbb");
}
setTimeout(aaa, 0);
setTimeout(bbb, 0);
As noted in the Q&A, this is an issue with putting function declarations inside a block, which is a bad practice in Javascript as function declarations have top-level scope. The fix for the issue is to properly structure the blocks using function expressions instead, like this:
Seeing as how these are supposed to be "compiler" conditionals, though, I will look into the possibility of having this work. I'm hesitant, though, and unlikely to change it as it could inadvertantly promote a bad Javascript practice.
The discussion on forum: http://developer.appcelerator.com/question/156082/alloy-compiler-conditional-code-not-work
More to add: I took some time to change all the function definition to function expression. But got other issues. Here are sample code: still on Mac OS X 10.7, iOS simulator, TI 3.1.2.GA, Alloy 1.2.0 view.xml
You'd better increase the priority of this defect. The code will be silently wrong. This really hurt!
[~aladdina] please try to use the appopriate code formatting tags. Your responses are very hard to read. I bumped up the priority a bit, but as this issue is pretty easy to workaround just by slight restructuring of your code, it will likely wait til the next minor release of alloy.
The original issue of nesting function declarations inside conditionals in Javascript is not going to be addressed for the reasons already mentioned. Even further, under the condition of "use strict" in Javascript, this code is not even valid. Concerning the multiple instances of event handlers showing up in code when you refactored to avoid the aforementioned issue, I have addressed it. It was resolved as a result of ALOY-851.
Verified as FIXED using : Mac :10.9.2 Appcelerator Studio, build: 3.3.0.201405121247 SDK - 3.3.0.v20140513191712 acs-1.0.14 alloy-1.4.0-dev npm-1.3.2 titanium-3.3.0-dev titanium-code-processor-1.1.1 Xcode :5.1.1 Alloy conditional code are functional and working as expected. Hence closing as FIXED.