Titanium JIRA Archive
Alloy (ALOY)

[ALOY-794] Alloy Conditional code not functional

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2014-01-21T22:28:19.000+0000
Affected Version/sAlloy 1.1.2
Fix Version/sAlloy 1.4.0
ComponentsRuntime
Labelsalloy, qe, qe-closed-3.3.0, qe-manualtest
ReporterMason Zhang
AssigneeTony Lukasavage
Created2013-08-17T12:43:42.000+0000
Updated2014-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);

Comments

  1. Tony Lukasavage 2013-08-17

    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:
       var aaa, bbb;
        
       if(OS_ANDROID) {
           aaa = function() {
               Ti.API.info('android aaa');
           }
       }
       else {
           bbb = function() {
               Ti.API.info('not android bbb');
           }
       }
        
       if(OS_IOS) {
           aaa = function() {
               Ti.API.info('ios aaa');
           }
       }
       else {
           bbb = function() {
               Ti.API.info('not ios bbb');
           }
       }
        
       $.index.open();
        
       aaa();
       bbb();
       
    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.
  2. Mason Zhang 2013-08-17

    The discussion on forum: http://developer.appcelerator.com/question/156082/alloy-compiler-conditional-code-not-work
  3. Mason Zhang 2013-08-18

    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
  4. Mason Zhang 2013-08-18

    You'd better increase the priority of this defect. The code will be silently wrong. This really hurt!
  5. Tony Lukasavage 2013-08-18

    [~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.
  6. Tony Lukasavage 2014-01-21

    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.
  7. Deepti Pandey 2014-05-14

    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.

JSON Source