def _stringOptimizer(self, tree): # string optimization works over a list of statements, # so extract the <file>'s <statements> node statementsNode = tree.getChild("statements") stringMap = stringoptimizer.search(statementsNode) if len(stringMap) == 0: return tree stringList = stringoptimizer.sort(stringMap) stringoptimizer.replace(statementsNode, stringList) # Build JS string fragments stringStart = "(function(){" stringReplacement = stringoptimizer.replacement(stringList) stringStop = "})();" # Compile wrapper node wrapperNode = treeutil.compileString( stringStart + stringReplacement + stringStop, self.id + "||stringopt") # Reorganize structure funcStatements = (wrapperNode.getChild("operand").getChild( "group").getChild("function").getChild("body").getChild( "block").getChild("statements")) if statementsNode.hasChildren(): for child in statementsNode.children[:]: statementsNode.removeChild(child) funcStatements.addChild(child) # Add wrapper to now empty statements node statementsNode.addChild(wrapperNode) return tree
def _stringOptimizeHelper(self, tree, id, variants): stringMap = stringoptimizer.search(tree) if len(stringMap) == 0: return stringList = stringoptimizer.sort(stringMap) stringoptimizer.replace(tree, stringList) # Build JS string fragments stringStart = "(function(){" stringReplacement = stringoptimizer.replacement(stringList) stringStop = "})();" # Compile wrapper node wrapperNode = treeutil.compileString(stringStart+stringReplacement+stringStop, id + "||stringopt") # Reorganize structure funcBody = wrapperNode.getChild("operand").getChild("group").getChild("function").getChild("body").getChild("block") if tree.hasChildren(): for child in copy.copy(tree.children): tree.removeChild(child) funcBody.addChild(child) # Add wrapper to tree tree.addChild(wrapperNode)
def _optimizeStrings(tree, id): stringMap = stringoptimizer.search(tree) if len(stringMap) == 0: return stringList = stringoptimizer.sort(stringMap) stringoptimizer.replace(tree, stringList) # Build JS string fragments stringStart = "(function(){" stringReplacement = stringoptimizer.replacement(stringList) stringStop = "})();" # Compile wrapper node wrapperNode = treeutil.compileString( stringStart + stringReplacement + stringStop, id + "||stringopt") # Reorganize structure funcBody = wrapperNode.getChild("operand").getChild("group").getChild( "function").getChild("body").getChild("block") if tree.hasChildren(): for child in copy.copy(tree.children): tree.removeChild(child) funcBody.addChild(child) # Add wrapper to tree tree.addChild(wrapperNode)
def _stringOptimizer(self, tree): # string optimization works over a list of statements, # so extract the <file>'s <statements> node statementsNode = tree.getChild("statements") stringMap = stringoptimizer.search(statementsNode) if len(stringMap) == 0: return tree stringList = stringoptimizer.sort(stringMap) stringoptimizer.replace(statementsNode, stringList) # TODO: Re-write this using treeutil.ensureClosureWrapper() # Build JS string fragments stringStart = "(function(){" stringReplacement = stringoptimizer.replacement(stringList) stringStop = "})();" # Compile wrapper node wrapperNode = treeutil.compileString(stringStart+stringReplacement+stringStop, self.id + "||stringopt") # Reorganize structure funcStatements = (wrapperNode.getChild("operand").getChild("group").getChild("function") .getChild("body").getChild("block").getChild("statements")) if statementsNode.hasChildren(): for child in statementsNode.children[:]: statementsNode.removeChild(child) funcStatements.addChild(child) # Add wrapper to now empty statements node statementsNode.addChild(wrapperNode) return tree