def __init__(self, filename, logger=None): self.filename = filename content = filetool.read(filename) self.tree = treegenerator.createSyntaxTree( tokenizer.parseStream(content)) self.script = Script(self.tree, self.filename) if not logger: self.logger = ConsoleLogger() else: self.logger = logger
def search(node): def updateOccurences(var, newname): # Replace variable definition for node in var.nodes: update(node, newname) # Replace variable references for varUse in var.uses: # varUse is a VariableUse object update(varUse.node, newname) def getAllVarOccurrences(script): # Collect the set of all used and declared ('var' + params) variables varset = set(()) for scope in script.iterScopes(): varset.update( (x.name for x in scope.arguments + scope.variables + scope.uses)) return varset global counter counter = 0 # reset repl name generation script = Script(node) checkSet = getAllVarOccurrences( script) # set of var names already in use, to check new names against # loop through declared vars of scopes for scope in script.iterScopes(): allvars = scope.arguments + scope.variables for var in allvars: if var.name in reservedWords or len(var.name) < 2: continue # get replacement name newname = mapper(var.name, checkSet) # update all occurrences in scope updateOccurences(var, newname) # if var is param, patch local vars of same name in one go if (var in scope.arguments): # get declared vars of same name lvars = [x for x in scope.variables if x.name == var.name] for lvar in lvars: updateOccurences(lvar, newname) # don't re-process allvars.remove(lvar)
def getScript(node, fileId, ): # TODO: checking the root nodes is a fix, as they sometimes differ (prob. caching) # -- looking up nodes in a Script() uses object identity for comparison; sometimes, the # tree _analyzeClassDepsNode works on and the tree Script is built from are not the # same in memory, e.g. when the tree is re-read from disk; then those comparisons # fail (although the nodes are semantically the same); hence we have to # re-calculate the Script (which is expensive!) when the root node object changes; # using __memo allows at least to re-use the existing script when a class is worked # on and this method is called successively for the same tree. rootNode = node.getRoot() #if _memo1_[0] == fileId: # replace with '_memo1_[0] == rootNode', to make it more robust, but slightly less performant if _memo1_[0] == rootNode: script = _memo1_[1] else: # TODO: disentagle use of ecmascript.frontend.Script and generator.code.Script script = Script(rootNode, fileId) _memo1_[0], _memo1_[1] = rootNode, script return script