Java Code Examples for com.google.javascript.rhino.Node#addChildrenToFront()
The following examples show how to use
com.google.javascript.rhino.Node#addChildrenToFront() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtractPrototypeMemberDeclarations.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Declares the temp variable to point to prototype objects and iterates * through all ExtractInstance and performs extraction there. */ private void doExtraction(GatherExtractionInfo info) { // Insert a global temp if we are using the USE_GLOBAL_TEMP pattern. if (pattern == Pattern.USE_GLOBAL_TEMP) { Node injectionPoint = compiler.getNodeForCodeInsertion(null); Node var = NodeUtil.newVarNode(prototypeAlias, null) .copyInformationFromForTree(injectionPoint); injectionPoint.addChildrenToFront(var); } // Go through all extraction instances and extract each of them. for (ExtractionInstance instance : info.instances) { extractInstance(instance); } }
Example 2
Source File: ReorderConstantExpression.java From astor with GNU General Public License v2.0 | 6 votes |
@Override Node optimizeSubtree(Node subtree) { // if the operator is symmetric if (NodeUtil.isSymmetricOperation(subtree) || NodeUtil.isRelationalOperation(subtree)) { // right value is immutable and left is not if (NodeUtil.isImmutableValue(subtree.getLastChild()) && !NodeUtil.isImmutableValue(subtree.getFirstChild())) { // if relational, get the inverse operator. if (NodeUtil.isRelationalOperation(subtree)){ int inverseOperator = NodeUtil.getInverseOperator(subtree.getType()); subtree.setType(inverseOperator); } // swap them Node firstNode = subtree.getFirstChild().detachFromParent(); Node lastNode = subtree.getLastChild().detachFromParent(); subtree.addChildrenToFront(lastNode); subtree.addChildrenToBack(firstNode); reportCodeChange(); } } return subtree; }
Example 3
Source File: PrepareAst.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Add blocks to IF, WHILE, DO, etc. */ private void normalizeBlocks(Node n) { if (NodeUtil.isControlStructure(n) && !n.isLabel() && !n.isSwitch()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { if (NodeUtil.isControlStructureCodeBlock(n,c) && !c.isBlock()) { Node newBlock = IR.block().srcref(n); n.replaceChild(c, newBlock); if (!c.isEmpty()) { newBlock.addChildrenToFront(c); } else { newBlock.setWasEmptyNode(true); } c = newBlock; reportChange(); } } } }
Example 4
Source File: Closure_129_PrepareAst_s.java From coming with MIT License | 6 votes |
/** * Add blocks to IF, WHILE, DO, etc. */ private void normalizeBlocks(Node n) { if (NodeUtil.isControlStructure(n) && !n.isLabel() && !n.isSwitch()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { if (NodeUtil.isControlStructureCodeBlock(n,c) && !c.isBlock()) { Node newBlock = IR.block().srcref(n); n.replaceChild(c, newBlock); if (!c.isEmpty()) { newBlock.addChildrenToFront(c); } else { newBlock.setWasEmptyNode(true); } c = newBlock; reportChange(); } } } }
Example 5
Source File: Closure_129_PrepareAst_t.java From coming with MIT License | 6 votes |
/** * Add blocks to IF, WHILE, DO, etc. */ private void normalizeBlocks(Node n) { if (NodeUtil.isControlStructure(n) && !n.isLabel() && !n.isSwitch()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { if (NodeUtil.isControlStructureCodeBlock(n,c) && !c.isBlock()) { Node newBlock = IR.block().srcref(n); n.replaceChild(c, newBlock); if (!c.isEmpty()) { newBlock.addChildrenToFront(c); } else { newBlock.setWasEmptyNode(true); } c = newBlock; reportChange(); } } } }
Example 6
Source File: Closure_18_Compiler_s.java From coming with MIT License | 5 votes |
@Override Node ensureLibraryInjected(String resourceName) { if (injectedLibraries.containsKey(resourceName)) { return null; } // All libraries depend on js/base.js boolean isBase = "base".equals(resourceName); if (!isBase) { ensureLibraryInjected("base"); } Node firstChild = loadLibraryCode(resourceName).removeChildren(); Node lastChild = firstChild.getLastSibling(); Node parent = getNodeForCodeInsertion(null); if (isBase) { parent.addChildrenToFront(firstChild); } else { parent.addChildrenAfter( firstChild, injectedLibraries.get("base")); } reportCodeChange(); injectedLibraries.put(resourceName, lastChild); return lastChild; }
Example 7
Source File: Compiler.java From astor with GNU General Public License v2.0 | 5 votes |
@Override Node ensureLibraryInjected(String resourceName) { if (injectedLibraries.containsKey(resourceName)) { return null; } // All libraries depend on js/base.js boolean isBase = "base".equals(resourceName); if (!isBase) { ensureLibraryInjected("base"); } Node firstChild = loadLibraryCode(resourceName).removeChildren(); Node lastChild = firstChild.getLastSibling(); Node parent = getNodeForCodeInsertion(null); if (isBase) { parent.addChildrenToFront(firstChild); } else { parent.addChildrenAfter( firstChild, injectedLibraries.get("base")); } reportCodeChange(); injectedLibraries.put(resourceName, lastChild); return lastChild; }
Example 8
Source File: AliasKeywords.java From astor with GNU General Public License v2.0 | 5 votes |
@Override /** * Create the alias declaration (e.g. var $$ALIAS_VOID=void 0;). */ protected void insertAliasDeclaration(Node codeRoot) { Node varNode = new Node(Token.VAR); Node value = IR.voidNode(IR.number(0)); Node name = NodeUtil.newName( compiler.getCodingConvention(), getAliasName(), varNode, getAliasName()); name.addChildToBack(value); varNode.addChildToBack(name); codeRoot.addChildrenToFront(varNode); }
Example 9
Source File: AliasKeywords.java From astor with GNU General Public License v2.0 | 5 votes |
@Override /** * Create the alias declaration (e.g. var $$ALIAS_NULL=null;). */ protected void insertAliasDeclaration(Node codeRoot) { Node varNode = new Node(Token.VAR); Node value = new Node(getTokenId()); Node name = NodeUtil.newName( compiler.getCodingConvention(), getAliasName(), varNode, getAliasName()); name.addChildToBack(value); varNode.addChildToBack(name); codeRoot.addChildrenToFront(varNode); }
Example 10
Source File: Closure_31_Compiler_t.java From coming with MIT License | 5 votes |
@Override Node ensureLibraryInjected(String resourceName) { if (injectedLibraries.containsKey(resourceName)) { return null; } // All libraries depend on js/base.js boolean isBase = "base".equals(resourceName); if (!isBase) { ensureLibraryInjected("base"); } Node firstChild = loadLibraryCode(resourceName).removeChildren(); Node lastChild = firstChild.getLastSibling(); Node parent = getNodeForCodeInsertion(null); if (isBase) { parent.addChildrenToFront(firstChild); } else { parent.addChildrenAfter( firstChild, injectedLibraries.get("base")); } reportCodeChange(); injectedLibraries.put(resourceName, lastChild); return lastChild; }
Example 11
Source File: Closure_31_Compiler_s.java From coming with MIT License | 5 votes |
@Override Node ensureLibraryInjected(String resourceName) { if (injectedLibraries.containsKey(resourceName)) { return null; } // All libraries depend on js/base.js boolean isBase = "base".equals(resourceName); if (!isBase) { ensureLibraryInjected("base"); } Node firstChild = loadLibraryCode(resourceName).removeChildren(); Node lastChild = firstChild.getLastSibling(); Node parent = getNodeForCodeInsertion(null); if (isBase) { parent.addChildrenToFront(firstChild); } else { parent.addChildrenAfter( firstChild, injectedLibraries.get("base")); } reportCodeChange(); injectedLibraries.put(resourceName, lastChild); return lastChild; }
Example 12
Source File: Closure_18_Compiler_t.java From coming with MIT License | 5 votes |
@Override Node ensureLibraryInjected(String resourceName) { if (injectedLibraries.containsKey(resourceName)) { return null; } // All libraries depend on js/base.js boolean isBase = "base".equals(resourceName); if (!isBase) { ensureLibraryInjected("base"); } Node firstChild = loadLibraryCode(resourceName).removeChildren(); Node lastChild = firstChild.getLastSibling(); Node parent = getNodeForCodeInsertion(null); if (isBase) { parent.addChildrenToFront(firstChild); } else { parent.addChildrenAfter( firstChild, injectedLibraries.get("base")); } reportCodeChange(); injectedLibraries.put(resourceName, lastChild); return lastChild; }
Example 13
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java From coming with MIT License | 4 votes |
/** * Replaces a new Array or Object node with an object literal, unless the * call to Array or Object is to a local function with the same name. */ private Node tryFoldLiteralConstructor(Node n) { Preconditions.checkArgument(n.isCall() || n.isNew()); Node constructorNameNode = n.getFirstChild(); Node newLiteralNode = null; // We require the AST to be normalized to ensure that, say, // Object() really refers to the built-in Object constructor // and not a user-defined constructor with the same name. if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) { String className = constructorNameNode.getString(); if ("RegExp".equals(className)) { // "RegExp("boo", "g")" --> /boo/g return tryFoldRegularExpressionConstructor(n); } else { boolean constructorHasArgs = constructorNameNode.getNext() != null; if ("Object".equals(className) && !constructorHasArgs) { // "Object()" --> "{}" newLiteralNode = IR.objectlit(); } else if ("Array".equals(className)) { // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]" Node arg0 = constructorNameNode.getNext(); FoldArrayAction action = isSafeToFoldArrayConstructor(arg0); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS || action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) { newLiteralNode = IR.arraylit(); n.removeChildren(); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) { newLiteralNode.addChildrenToFront(arg0); } } } if (newLiteralNode != null) { n.getParent().replaceChild(n, newLiteralNode); reportCodeChange(); return newLiteralNode; } } } return n; }
Example 14
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java From coming with MIT License | 4 votes |
/** * Replaces a new Array or Object node with an object literal, unless the * call to Array or Object is to a local function with the same name. */ private Node tryFoldLiteralConstructor(Node n) { Preconditions.checkArgument(n.isCall() || n.isNew()); Node constructorNameNode = n.getFirstChild(); Node newLiteralNode = null; // We require the AST to be normalized to ensure that, say, // Object() really refers to the built-in Object constructor // and not a user-defined constructor with the same name. if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) { String className = constructorNameNode.getString(); if ("RegExp".equals(className)) { // "RegExp("boo", "g")" --> /boo/g return tryFoldRegularExpressionConstructor(n); } else { boolean constructorHasArgs = constructorNameNode.getNext() != null; if ("Object".equals(className) && !constructorHasArgs) { // "Object()" --> "{}" newLiteralNode = IR.objectlit(); } else if ("Array".equals(className)) { // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]" Node arg0 = constructorNameNode.getNext(); FoldArrayAction action = isSafeToFoldArrayConstructor(arg0); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS || action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) { newLiteralNode = IR.arraylit(); n.removeChildren(); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) { newLiteralNode.addChildrenToFront(arg0); } } } if (newLiteralNode != null) { n.getParent().replaceChild(n, newLiteralNode); reportCodeChange(); return newLiteralNode; } } } return n; }
Example 15
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java From coming with MIT License | 4 votes |
/** * Replaces a new Array or Object node with an object literal, unless the * call to Array or Object is to a local function with the same name. */ private Node tryFoldLiteralConstructor(Node n) { Preconditions.checkArgument(n.isCall() || n.isNew()); Node constructorNameNode = n.getFirstChild(); Node newLiteralNode = null; // We require the AST to be normalized to ensure that, say, // Object() really refers to the built-in Object constructor // and not a user-defined constructor with the same name. if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) { String className = constructorNameNode.getString(); if ("RegExp".equals(className)) { // "RegExp("boo", "g")" --> /boo/g return tryFoldRegularExpressionConstructor(n); } else { boolean constructorHasArgs = constructorNameNode.getNext() != null; if ("Object".equals(className) && !constructorHasArgs) { // "Object()" --> "{}" newLiteralNode = IR.objectlit(); } else if ("Array".equals(className)) { // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]" Node arg0 = constructorNameNode.getNext(); FoldArrayAction action = isSafeToFoldArrayConstructor(arg0); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS || action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) { newLiteralNode = IR.arraylit(); n.removeChildren(); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) { newLiteralNode.addChildrenToFront(arg0); } } } if (newLiteralNode != null) { n.getParent().replaceChild(n, newLiteralNode); reportCodeChange(); return newLiteralNode; } } } return n; }
Example 16
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java From coming with MIT License | 4 votes |
/** * Replaces a new Array or Object node with an object literal, unless the * call to Array or Object is to a local function with the same name. */ private Node tryFoldLiteralConstructor(Node n) { Preconditions.checkArgument(n.isCall() || n.isNew()); Node constructorNameNode = n.getFirstChild(); Node newLiteralNode = null; // We require the AST to be normalized to ensure that, say, // Object() really refers to the built-in Object constructor // and not a user-defined constructor with the same name. if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) { String className = constructorNameNode.getString(); if ("RegExp".equals(className)) { // "RegExp("boo", "g")" --> /boo/g return tryFoldRegularExpressionConstructor(n); } else { boolean constructorHasArgs = constructorNameNode.getNext() != null; if ("Object".equals(className) && !constructorHasArgs) { // "Object()" --> "{}" newLiteralNode = IR.objectlit(); } else if ("Array".equals(className)) { // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]" Node arg0 = constructorNameNode.getNext(); FoldArrayAction action = isSafeToFoldArrayConstructor(arg0); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS || action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) { newLiteralNode = IR.arraylit(); n.removeChildren(); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) { newLiteralNode.addChildrenToFront(arg0); } } } if (newLiteralNode != null) { n.getParent().replaceChild(n, newLiteralNode); reportCodeChange(); return newLiteralNode; } } } return n; }
Example 17
Source File: SpecializeModule.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Adds a copy of the original versions of specialized/removed functions * to each of the dependents of module. * * Currently we add all of these functions to all dependents; it * would be more efficient to only add the functions that could be used. * * TODO(dcc): Only add fixup functions where needed. */ private void addOriginalFunctionVersionsToDependentModules(JSModule module) { for (JSModule directDependent : getDirectDependents(module)) { CompilerInput firstInput = directDependent.getInputs().get(0); Node firstInputRootNode = firstInput.getAstRoot(compiler); // We don't iterate specializedFunctions directly because want to maintain // and specializedFunctions in source order, rather than // in the order that some optimization specialized the function. // So since we're adding to the front of the module each time, we // have to iterate in reverse source order. List<Node> possiblyModifiedFunctions = Lists.newArrayList(functionInfoBySpecializedFunctionNode.keySet()); Collections.reverse(possiblyModifiedFunctions); for (Node modifiedFunction : possiblyModifiedFunctions) { boolean declarationWasSpecialized = specializationState.getSpecializedFunctions() .contains(modifiedFunction); boolean declarationWasRemoved = specializationState.getRemovedFunctions() .contains(modifiedFunction); if (declarationWasSpecialized || declarationWasRemoved) { OriginalFunctionInformation originalInfo = functionInfoBySpecializedFunctionNode.get(modifiedFunction); // Don't add unspecialized versions of anonymous functions if (originalInfo.name != null) { Node newDefinition = originalInfo.generateFixupDefinition(); firstInputRootNode.addChildrenToFront(newDefinition); } } } } }
Example 18
Source File: PeepholeSubstituteAlternateSyntax.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Replaces a new Array or Object node with an object literal, unless the * call to Array or Object is to a local function with the same name. */ private Node tryFoldLiteralConstructor(Node n) { Preconditions.checkArgument(n.isCall() || n.isNew()); Node constructorNameNode = n.getFirstChild(); Node newLiteralNode = null; // We require the AST to be normalized to ensure that, say, // Object() really refers to the built-in Object constructor // and not a user-defined constructor with the same name. if (isASTNormalized() && Token.NAME == constructorNameNode.getType()) { String className = constructorNameNode.getString(); if ("RegExp".equals(className)) { // "RegExp("boo", "g")" --> /boo/g return tryFoldRegularExpressionConstructor(n); } else { boolean constructorHasArgs = constructorNameNode.getNext() != null; if ("Object".equals(className) && !constructorHasArgs) { // "Object()" --> "{}" newLiteralNode = IR.objectlit(); } else if ("Array".equals(className)) { // "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]" Node arg0 = constructorNameNode.getNext(); FoldArrayAction action = isSafeToFoldArrayConstructor(arg0); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS || action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) { newLiteralNode = IR.arraylit(); n.removeChildren(); if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) { newLiteralNode.addChildrenToFront(arg0); } } } if (newLiteralNode != null) { n.getParent().replaceChild(n, newLiteralNode); reportCodeChange(); return newLiteralNode; } } } return n; }
Example 19
Source File: ExpandJqueryAliases.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Expand jQuery.extend (and derivative) calls into direct object assignments * Example: jQuery.extend(obj1, {prop1: val1, prop2: val2}) -> * obj1.prop1 = val1; * obj1.prop2 = val2; */ private void maybeExpandJqueryExtendCall(Node n) { Node callTarget = n.getFirstChild(); Node objectToExtend = callTarget.getNext(); // first argument Node extendArg = objectToExtend.getNext(); // second argument boolean ensureObjectDefined = true; if (extendArg == null) { // Only one argument was specified, so extend jQuery namespace extendArg = objectToExtend; objectToExtend = callTarget.getFirstChild(); ensureObjectDefined = false; } else if (objectToExtend.isGetProp() && (objectToExtend.getLastChild().getString().equals("prototype") || convention.isPrototypeAlias(objectToExtend))) { ensureObjectDefined = false; } // Check for an empty object literal if (!extendArg.hasChildren()) { return; } // Since we are expanding jQuery.extend calls into multiple statements, // encapsulate the new statements in a new block. Node fncBlock = IR.block().srcref(n); if (ensureObjectDefined) { Node assignVal = IR.or(objectToExtend.cloneTree(), IR.objectlit().srcref(n)).srcref(n); Node assign = IR.assign(objectToExtend.cloneTree(), assignVal).srcref(n); fncBlock.addChildrenToFront(IR.exprResult(assign).srcref(n)); } while (extendArg.hasChildren()) { Node currentProp = extendArg.removeFirstChild(); currentProp.setType(Token.STRING); Node propValue = currentProp.removeFirstChild(); Node newProp; if(currentProp.isQuotedString()) { newProp = IR.getelem(objectToExtend.cloneTree(), currentProp).srcref(currentProp); } else { newProp = IR.getprop(objectToExtend.cloneTree(), currentProp).srcref(currentProp); } Node assignNode = IR.assign(newProp, propValue).srcref(currentProp); fncBlock.addChildToBack(IR.exprResult(assignNode).srcref(currentProp)); } // Check to see if the return value is used. If not, replace the original // call with new block. Otherwise, wrap the statements in an // immediately-called anonymous function. if (n.getParent().isExprResult()) { Node parent = n.getParent(); parent.getParent().replaceChild(parent, fncBlock); } else { Node targetVal; if ("jQuery.prototype".equals(objectToExtend.getQualifiedName())) { // When extending the jQuery prototype, return the jQuery namespace. // This is not commonly used. targetVal = objectToExtend.removeFirstChild(); } else { targetVal = objectToExtend.detachFromParent(); } fncBlock.addChildToBack(IR.returnNode(targetVal).srcref(targetVal)); Node fnc = IR.function(IR.name("").srcref(n), IR.paramList().srcref(n), fncBlock); n.replaceChild(callTarget, fnc); n.putBooleanProp(Node.FREE_CALL, true); // remove any other pre-existing call arguments while(fnc.getNext() != null) { n.removeChildAfter(fnc); } } compiler.reportCodeChange(); }