diff options
author | Jez Ng <me@jezng.com> | 2013-06-21 01:41:29 -0700 |
---|---|---|
committer | Jez Ng <me@jezng.com> | 2013-06-22 01:23:21 -0700 |
commit | 7212198353b83e2e8b638a8755708cc14d39aee6 (patch) | |
tree | bf0f97636fa224626082278dabeb57a4186edbe6 /tools | |
parent | 99fa57e08e92821e1f6f4dd230e80970ad366250 (diff) |
Make optimizer handle both strings and string-like type objects.
NodeWithToken is a string-like type produced by the parser during 'embed
tokens' mode, which allows us to track line numbers.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/eliminator/node_modules/uglify-js/lib/parse-js.js | 25 | ||||
-rw-r--r-- | tools/js-optimizer.js | 21 |
2 files changed, 35 insertions, 11 deletions
diff --git a/tools/eliminator/node_modules/uglify-js/lib/parse-js.js b/tools/eliminator/node_modules/uglify-js/lib/parse-js.js index a89163c6..2dc2ef70 100644 --- a/tools/eliminator/node_modules/uglify-js/lib/parse-js.js +++ b/tools/eliminator/node_modules/uglify-js/lib/parse-js.js @@ -679,7 +679,28 @@ function NodeWithToken(str, start, end) { this.end = end; }; -NodeWithToken.prototype.toString = function() { return this.name; }; +NodeWithToken.prototype = { + get length() { + return this.name.length; + }, + set length(v) { + return this.name.length = v; + }, + replace: function() { return this.name.replace.apply(this.name, arguments); }, + concat: function() { return this.name.concat.apply(this.name, arguments); }, + indexOf: function() { return this.name.indexOf.apply(this.name, arguments); }, + lastIndexOf: function() { return this.name.lastIndexOf.apply(this.name, arguments); }, + lastIndexOf: function() { return this.name.lastIndexOf.apply(this.name, arguments); }, + match: function() { return this.name.match.apply(this.name, arguments); }, + search: function() { return this.name.search.apply(this.name, arguments); }, + slice: function() { return this.name.slice.apply(this.name, arguments); }, + split: function() { return this.name.split.apply(this.name, arguments); }, + substr: function() { return this.name.substr.apply(this.name, arguments); }, + substring: function() { return this.name.substring.apply(this.name, arguments); }, + toString: function() { return this.name; }, + toJSON: function() { return this.name; }, + valueOf: function() { return this.name; }, +}; function parse($TEXT, exigent_mode, embed_tokens) { @@ -1314,7 +1335,7 @@ function characters(str) { function member(name, array) { for (var i = array.length; --i >= 0;) - if (array[i] === name) + if (array[i] == name) return true; return false; }; diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index f86bb0d6..07092513 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -164,7 +164,8 @@ function astToSrc(ast, compress) { function traverseChildren(node, traverse, pre, post, stack) { for (var i = 0; i < node.length; i++) { var subnode = node[i]; - if (typeof subnode == 'object' && subnode && subnode.length) { + if (typeof subnode == 'object' && subnode && subnode.length && + typeof subnode.valueOf() !== 'string') { var subresult = traverse(subnode, pre, post, stack); if (subresult == true) return true; if (subresult !== null && typeof subresult == 'object') node[i] = subresult; @@ -188,7 +189,9 @@ function traverseChildren(node, traverse, pre, post, stack) { // was stopped, true. Otherwise undefined. function traverse(node, pre, post, stack) { var type = node[0], result, len; - var relevant = typeof type == 'string'; + // valueOf() ensures that NodeWithToken (produced by uglify's parser during + // 'embed tokens' mode) gets marked as 'relevant' + var relevant = type && typeof type.valueOf() == 'string'; if (relevant) { if (stack) len = stack.length; var result = pre(node, type, stack); @@ -2006,7 +2009,7 @@ function eliminate(ast, memSafe) { // examine body and note locals var hasSwitch = false; traverse(func, function(node, type) { - if (type === 'var') { + if (type == 'var') { var node1 = node[1]; for (var i = 0; i < node1.length; i++) { var node1i = node1[i]; @@ -2020,7 +2023,7 @@ function eliminate(ast, memSafe) { if (!uses[name]) uses[name] = 0; locals[name] = true; } - } else if (type === 'name') { + } else if (type == 'name') { var name = node[1]; if (!uses[name]) uses[name] = 0; uses[name]++; @@ -2477,7 +2480,7 @@ function eliminate(ast, memSafe) { // clean up vars, and loop variable elimination traverse(func, function(node, type) { // pre - if (type === 'var') { + if (type == 'var') { node[1] = node[1].filter(function(pair) { return !varsToRemove[pair[0]] }); if (node[1].length == 0) { // wipe out an empty |var;| @@ -2599,21 +2602,21 @@ function eliminate(ast, memSafe) { this.run = function() { traverse(this.node, function(node, type) { - if (type === 'binary' && node[1] == '+') { + if (type == 'binary' && node[1] == '+') { var names = []; var num = 0; var has_num = false; var fail = false; traverse(node, function(subNode, subType) { - if (subType === 'binary') { + if (subType == 'binary') { if (subNode[1] != '+') { fail = true; return false; } - } else if (subType === 'name') { + } else if (subType == 'name') { names.push(subNode[1]); return; - } else if (subType === 'num') { + } else if (subType == 'num') { num += subNode[1]; has_num = true; return; |