1
Vote

Cleaner module translations

description

I like how a main focus of the TypeScript team is to keep the JS output as readable as possible. I think it may have been key in TypeScripts success so far.

One thing I've always disliked though, is the translation of:
module ns1.ns2.ns3 {
    // code
}
to:
var ns1;
(function (ns1) {
    (function (ns2) {
        (function (ns3) {
            // code
        })(ns2.ns3 || (ns2.ns3 = {}));
        var ns3 = ns2.ns3;
    })(ns1.ns2 || (ns1.ns2 = {}));
    var ns2 = ns1.ns2;
})(ns1 || (ns1 = {}));
Thought: I'm not an JS memory expert, but don't the extra closures and additions to the call stack waste memory?

But, more importantly, couldn't this be made more readable by translating it to:
var ns1;
(function( ns1, ns2, ns3) {
    // code
})( ns1 || (ns1 = {}), ns1.ns2 || (ns1.ns2 = {}), ns2.ns3 || (ns2.ns3 = {}) );
You get the advantages of a locally scoped variable, but keep the readable left-to-right-flow of the namespacing.

Now, if in the compiler parses the above TypeScript as:
module ns1 {
    module ns2 {
        module ns3 {
            // code
        }
    }
}
...I guess it's not an easy fix.

comments

danquirk wrote Nov 7, 2013 at 6:45 PM

Marking as a suggestion. There is perhaps a more optimized code gen pattern here but keeping it consistent with the fully expanded form of the module declaration which it is equivalent to is a desirable property as well.