HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

goog.structs.TreeNode blocks property renaming #1195

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.  Use TreeNode.getParent() (TreeNode in Closure Library)

What is the expected output? What do you see instead?
TreeNode.getParent() should be optimized as just a.b, instead it becomes 
something like a.getParent()
There are actually quite a few of property/namespace/function names that cannot 
be optimized because of this, and end up being permanently preserved by the 
compiler.

What version of the product are you using? On what operating system?
Latest Closure Compiler from trunk, Closure Library from trunk

Please provide any additional information below.
This happened relatively recently.  Older versions of Closure Compiler did not 
exhibit this problem.  Appears to clash with FileAPI externs.

Sample Code for Closure Compiler Service:
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

goog.provide('ns.x');

/** @constructor */
ns.x = function(){this._x = 1;};

/** 
 * @private
 * @type {number}
 */
ns.x.prototype._x

/** @returns {number} */
ns.x.prototype.getParent = function(){
return this._x;
}
var x = new ns.x().getParent();
console.log(x);

----------
becomes
----------

function a(){this.a=1}a.prototype.getParent=function(){return this.a};var 
b=(new a).getParent();console.log(b);

Original issue reported on code.google.com by jhiswin on 13 Jan 2014 at 3:33

GoogleCodeExporter commented 9 years ago
Closure Library API reference:
http://docs.closure-library.googlecode.com/git/closure_goog_structs_treenode.js.
source.html#line94

Original comment by jhiswin on 13 Jan 2014 at 3:34

GoogleCodeExporter commented 9 years ago
This is by design and addressed in the 
FAQ:https://code.google.com/p/closure-compiler/wiki/FAQ#Some_of_my_properties_ar
e_getting_renamed,_but_some_aren't.

When you add the use_types_for_optimization flag, the output of your example is:

console.log(1);

For the web service, your header should look like:
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @use_types_for_optimization true
// ==/ClosureCompiler==

Original comment by chadkill...@missouristate.edu on 13 Jan 2014 at 4:04

GoogleCodeExporter commented 9 years ago
Hi,
I'm using Closure Compiler from trunk locally, and for some odd reason it is 
not using types for optimization.
It seems to be, as you say, fine on the Closure Compiler service.

Do you happen to know what might be the problem?  This problem only happened 
recently, and my build scripts are already set up to include -f 
"--use-types-for-optimization" on the command line.

Original comment by jhiswin on 13 Jan 2014 at 6:25

GoogleCodeExporter commented 9 years ago
Hi, so here is an example that uses Closure Library's TreeNode.  I'm not sure 
if this is a problem with Closure Library or Closure Compiler, but this causes 
all references to getParent to be unoptimized.

goog.provide('ns.x');

goog.require('goog.structs.TreeNode');

/** @constructor */
ns.x = function(){this._x = 1;};

/** 
 * @private
 * @type {number}
 */
ns.x.prototype._x

/** @returns {number} */
ns.x.prototype.getParent = function(){
return this._x;
}
var x = new ns.x().getParent();
console.log(x);

  var node1 = new goog.structs.TreeNode(1, '1');
  var node2 = new goog.structs.TreeNode(2, '2');
  var node3 = new goog.structs.TreeNode(3, '3');
  node1.addChild(node2);
  node2.addChild(node3);

  var node4 = new goog.structs.TreeNode(4, '4');
  node4.setParent(node3);

  var clone = node2.clone();
  console.log(clone.getKey()+clone.getParent()+node3.getParent()+node2.getChildren());

Original comment by jhiswin on 13 Jan 2014 at 6:49

GoogleCodeExporter commented 9 years ago
Hi, so I've managed to create a version that can be pasted into the Closure 
Compiler service.  The use of goog.structs.TreeNode appears to cause all 
references to the name getParent to be no longer optimizable:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @use_types_for_optimization true
// @code_url https://closure-library.googlecode.com/git/closure/goog/base.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/structs/node.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/structs/treenode.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/dom/nodetype.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/debug/error.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/string/string.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/asserts/asserts.js
// @code_url 
https://closure-library.googlecode.com/git/closure/goog/array/array.js
// @formatting pretty_print
// ==/ClosureCompiler==

goog.provide('ns.x');

goog.require('goog.structs.TreeNode');

/** @constructor */
ns.x = function(){this._x = 1;};

/**
* @private
* @type {number}
*/
ns.x.prototype._x

/** @returns {number} */
ns.x.prototype.getParent = function(){
return this._x;
}
var x = new ns.x().getParent();
console.log(x);

var node1 = new goog.structs.TreeNode(1, '1');
var node2 = new goog.structs.TreeNode(2, '2');
var node3 = new goog.structs.TreeNode(3, '3');
node1.addChild(node2);
node2.addChild(node3);

var node4 = new goog.structs.TreeNode(4, '4');
node4.setParent(node3);

var clone = node2.clone();
console.log(clone.getKey()+clone.getParent()+node3.getParent()+node2.getChildren
());

Original comment by jhiswin on 13 Jan 2014 at 6:59

GoogleCodeExporter commented 9 years ago
John: Is this because TreeNode uses @template?

Original comment by chadkill...@missouristate.edu on 13 Jan 2014 at 10:52

GoogleCodeExporter commented 9 years ago
Here's another example that may not be related.  "name" property doesn't get 
renamed.
Though I'm not sure this is valid since "name" is reserved.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @use_types_for_optimization true
// ==/ClosureCompiler==

/** @type {Object} */
var x = {name: 1};

console.log(x);
console.log(x.name);

Original comment by jhiswin on 14 Jan 2014 at 4:35

GoogleCodeExporter commented 9 years ago
#7 is working as intended.

Original comment by concavel...@gmail.com on 14 Jan 2014 at 5:39

GoogleCodeExporter commented 9 years ago
Issue tracking has been migrated to github. Please make any further comments on 
this issue through https://github.com/google/closure-compiler/issues

Original comment by blic...@google.com on 1 May 2014 at 6:31

GoogleCodeExporter commented 9 years ago

Original comment by blic...@google.com on 1 May 2014 at 6:34