Re: [closure-compiler-discuss] Name collision between default externs and unrelated code

85 views
Skip to first unread message

Ilia Mirkin

unread,
Dec 5, 2012, 7:42:27 PM12/5/12
to closure-comp...@googlegroups.com
Does this still happen if you enable type (dis)ambiguation?

http://closuretools.blogspot.com/2011/01/property-by-any-other-name-part-3.html
http://code.google.com/p/closure-compiler/wiki/ExperimentalTypeBasedPropertyRenaming

On Wed, Dec 5, 2012 at 7:17 PM, Josh <php...@gmail.com> wrote:
> I'm not sure whether the following is a bug or by design. Consider the code
> below; Closure Compiler's advanced optimizations has no problem inlining
> getSomething() and renaming Thing.someName but it won't touch getAttribute()
> or Thing.attrName unless externs are disabled. I guess this is because
> getAttribute() and the attrName property are part of the DOM externs, but I
> thought Closure Compiler would make out that this "Thing" object is not
> related to Element (the object that declares getAttribute()) and no amount
> of extra annotations seems to help.
>
> Is it a bug or just an unfortunate side-effect? Thanks for any info.
>
> /** @constructor */
> function Thing(x) {
> this.attrName = x;
> this.someName = x;
> }
> Thing.prototype = {
> getAttribute: function() { return this.attrName; },
> getSomething: function() { return this.someName; }
> }
> alert(new Thing("x").getAttribute());
> alert(new Thing("y").getSomething());
>
> # With default externs, gets compiled to
> function
> a(b){this.a=this.attrName=b}a.prototype={getAttribute:function(){return
> this.attrName}};alert((new a("x")).getAttribute());alert((new a("y")).a);
>
> # Without default externs
> function a(b){this.b=this.a=b}a.prototype={};alert((new
> a("x")).a);alert((new a("y")).b);

Josh

unread,
Dec 6, 2012, 2:49:07 PM12/6/12
to closure-comp...@googlegroups.com, imi...@alum.mit.edu
Sorry, I forgot to mention that I only use the web service and command line application so I don't have access to the Java API. On the other hand, I've found that the latest build of the command line application has a "use_types_for_optimization" switch which seems to allow those optimizations. (at any rate, the result is the same and the code is optimally minified)

And even though I had read those blog posts when they came out a couple of years ago, re-reading them allowed me to find the answer to my question as to whether those names were preserved on purpose: they are. The article mentions "If a property is listed in the externs file under the "All Unquoted" naming policy, that property will not be renamed anywhere in the program." There you go, mystery solved.

Thanks for your help.

John Lenz

unread,
Dec 14, 2012, 6:25:32 PM12/14/12
to closure-compiler, Ilia Mirkin
With the 12-12-12 release of the webservices, "use_types_for_optimization" is now available.

Chad Killingsworth

unread,
Dec 17, 2012, 2:18:15 PM12/17/12
to closure-comp...@googlegroups.com, Ilia Mirkin
And was already available from the command line. We need to update the wiki pages ...

John Lenz

unread,
Dec 17, 2012, 4:50:46 PM12/17/12
to closure-compiler
Reply all
Reply to author
Forward
0 new messages