Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell the Closure compiler to, for specific types only, stop renaming properties?

This question follows: Why does Closure compiler rename properties of an extern type? John's answer to that question brings up this second question.

If I declare the extern type as suggested:

/** @interface */
function SpanishNoun() {}
/** @type {string} */
SpanishNoun.prototype.english;
/** @type {string} */
SpanishNoun.prototype.spanish;

then Javascript like:

/**
 * @param {SpanishNoun} n 
 */
exp.foo = function (n) {
    console.log(n.english, n.spanish, n['english'], n['spanish']);  
}

will compile, as desired, to:

function(a){console.log(a.english,a.spanish,a.english,a.spanish)};

The properties are not renamed as usual. Without the extern declaration, the compiled code would look like:

function(a){console.log(a.a,a.c,a.english,a.spanish)

That's all good. The problem is that the compiler has stopped renaming 'english' and 'spanish' in all places. Even if they are not on the extern type.

/**
 * @param {AnotherType}  
 */
exp.bar = function (c) {
    c.other = c.english;
}

compiles to...

function(a){a.b=a.english};

Is there a way to stop this? If not, is there a reason for this behavior?

I wanted to use extern types to handle things like JSON objects that originate from the server and do not have renamed properties. But if every time I declare an extern I'm eating away at the compiler's ability to rename and shrink the code, I will find another way. Perhaps I will take the property renaming map generated by the compiler (--property_map_output_file) and use it on the server when generating JSON responses.

like image 317
Rob N Avatar asked Nov 20 '11 13:11

Rob N


1 Answers

The Closure Compiler can rename based on types: https://github.com/google/closure-compiler/wiki/Type-Based-Property-Renaming This enhances other optimizations such as inlining and dead code removal as well. This is used internally to Google but comes with a cost as it can introduce some hard debug scenarios if you "lie" in your type declarations.

like image 58
John Avatar answered Nov 03 '22 08:11

John