public static Node quotedStringKey(String s, Node value) { Node k = stringKey(s, value); k.putBooleanProp(Node.QUOTED_PROP, true); return k; }
public static Node stringKey(String s, Node value) { checkState(mayBeExpression(value)); Node stringKey = stringKey(s); stringKey.addChildToFront(value); return stringKey; }
public static Node stringKey(String s, Node value) { Preconditions.checkState(mayBeExpression(value)); Node stringKey = stringKey(s); stringKey.addChildToFront(value); return stringKey; }
/** * Represents a structural type. * Closure calls this a Record Type and accepts the syntax * {@code {myNum: number, myObject}} * * <p>Example: * <pre> * RECORD_TYPE * STRING_KEY myNum * NUMBER_TYPE * STRING_KEY myObject * </pre> * @param properties a map from property name to property type * @return a new node representing the record type */ public static TypeDeclarationNode recordType( LinkedHashMap<String, TypeDeclarationNode> properties) { TypeDeclarationNode node = new TypeDeclarationNode(Token.RECORD_TYPE); for (Map.Entry<String, TypeDeclarationNode> prop : properties.entrySet()) { Node stringKey = IR.stringKey(prop.getKey()); node.addChildToBack(stringKey); if (prop.getValue() != null) { stringKey.addChildToFront(prop.getValue()); } } return node; }
/** * Represents a structural type. * Closure calls this a Record Type and accepts the syntax * {@code {myNum: number, myObject}} * * <p>Example: * <pre> * RECORD_TYPE * STRING_KEY myNum * NUMBER_TYPE * STRING_KEY myObject * </pre> * @param properties a map from property name to property type * @return a new node representing the record type */ public static TypeDeclarationNode recordType( LinkedHashMap<String, TypeDeclarationNode> properties) { TypeDeclarationNode node = new TypeDeclarationNode(Token.RECORD_TYPE); for (Map.Entry<String, TypeDeclarationNode> prop : properties.entrySet()) { Node stringKey = IR.stringKey(prop.getKey()); node.addChildToBack(stringKey); if (prop.getValue() != null) { stringKey.addChildToFront(prop.getValue()); } } return node; }
Node createStringKey(String key, Node value) { Node result = IR.stringKey(key, value); if (isAddingTypes()) { result.setJSType(value.getJSType()); } return result; }
/** Adds an ES5 getter to the given object literal to use an an export. */ private void addExport(Node definePropertiesLit, String exportedName, LocalQName localQName) { Node exportedValue = NodeUtil.newQName(compiler, localQName.qName); Node getterFunction = IR.function(IR.name(""), IR.paramList(), IR.block(IR.returnNode(exportedValue))); getterFunction.useSourceInfoFromForTree(localQName.nodeForSourceInfo); Node objLit = IR.objectlit( IR.stringKey("enumerable", IR.trueNode()), IR.stringKey("get", getterFunction)); definePropertiesLit.addChildToBack(IR.stringKey(exportedName, objLit)); compiler.reportChangeToChangeScope(getterFunction); }
/** * Creates a JS object that holds a map of tweakId -> default value override. */ private Node createCompilerDefaultValueOverridesVarNode( Node sourceInformationNode) { Node objNode = IR.objectlit().srcref(sourceInformationNode); for (Entry<String, Node> entry : compilerDefaultValueOverrides.entrySet()) { Node objKeyNode = IR.stringKey(entry.getKey()) .useSourceInfoIfMissingFrom(sourceInformationNode); Node objValueNode = entry.getValue().cloneNode() .useSourceInfoIfMissingFrom(sourceInformationNode); objKeyNode.addChildToBack(objValueNode); objNode.addChildToBack(objKeyNode); } return objNode; }
/** * Creates a JS object that holds a map of tweakId -> default value override. */ private Node createCompilerDefaultValueOverridesVarNode( Node sourceInformationNode) { Node objNode = IR.objectlit().srcref(sourceInformationNode); for (Entry<String, Node> entry : compilerDefaultValueOverrides.entrySet()) { Node objKeyNode = IR.stringKey(entry.getKey()) .useSourceInfoIfMissingFrom(sourceInformationNode); Node objValueNode = entry.getValue().cloneNode() .useSourceInfoIfMissingFrom(sourceInformationNode); objKeyNode.addChildToBack(objValueNode); objNode.addChildToBack(objKeyNode); } return objNode; }
private void rewriteMemberDefInObjLit(Node memberDef, Node objLit) { String name = memberDef.getString(); Node stringKey = IR.stringKey(name, memberDef.getFirstChild().detachFromParent()); objLit.replaceChild(memberDef, stringKey); stringKey.setQuotedString(); compiler.reportCodeChange(); }
/** * Converts a member definition in an object literal to an ES3 key/value pair. * Member definitions in classes are handled in {@link #visitClass}. */ private void visitMemberFunctionDefInObjectLit(Node n, Node parent) { String name = n.getString(); Node stringKey = IR.stringKey(name, n.getFirstChild().detachFromParent()); stringKey.setJSDocInfo(n.getJSDocInfo()); parent.replaceChild(n, stringKey); compiler.reportCodeChange(); }
/** Converts a member function into a quoted string key to avoid property renaming */ private void rewriteMemberDefInObjLit(Node memberDef, Node objLit) { String name = memberDef.getString(); Node stringKey = IR.stringKey(name, memberDef.getFirstChild().detach()); objLit.replaceChild(memberDef, stringKey); stringKey.setQuotedString(); stringKey.setJSDocInfo(memberDef.getJSDocInfo()); compiler.reportChangeToEnclosingScope(objLit); }
/** * Converts a member definition in an object literal to an ES3 key/value pair. * Member definitions in classes are handled in {@link Es6RewriteClass}. */ private void visitMemberFunctionDefInObjectLit(Node n, Node parent) { String name = n.getString(); Node nameNode = n.getFirstFirstChild(); Node stringKey = withType(IR.stringKey(name, n.getFirstChild().detach()), n.getJSType()); stringKey.setJSDocInfo(n.getJSDocInfo()); parent.replaceChild(n, stringKey); stringKey.useSourceInfoFrom(nameNode); compiler.reportChangeToEnclosingScope(stringKey); }
prop = IR.objectlit( IR.stringKey("configurable", IR.trueNode()), IR.stringKey("enumerable", IR.trueNode())); if (member.isComputedProp()) { obj.addChildToBack(IR.computedProp(member.getFirstChild().cloneTree(), prop)); } else { obj.addChildToBack(IR.stringKey(member.getString(), prop)); Token.BANG, IR.string(metadata.fullClassName)), member.getSourceFileName())); Node stringKey = IR.stringKey( (member.isGetterDef() || member.getBooleanProp(Node.COMPUTED_PROP_GETTER)) ? "get"
/** * Given an object literal to export, create an object lit with all its * string properties. We don't care what the values of those properties * are because they are not checked. */ private Node createExternObjectLit(Node exportedObjectLit) { Node lit = IR.objectlit(); lit.setJSType(exportedObjectLit.getJSType()); // This is an indirect way of telling the typed code generator // "print the type of this" lit.setJSDocInfo(buildEmptyJSDoc()); int index = 1; for (Node child = exportedObjectLit.getFirstChild(); child != null; child = child.getNext()) { // TODO(dimvar): handle getters or setters? if (child.isStringKey()) { lit.addChildToBack( IR.propdef( IR.stringKey(child.getString()), IR.number(index++))); } } return lit; }
/** * Given an object literal to export, create an object lit with all its * string properties. We don't care what the values of those properties * are because they are not checked. */ private Node createExternObjectLit(Node exportedObjectLit) { Node lit = IR.objectlit(); lit.setJSType(exportedObjectLit.getJSType()); // This is an indirect way of telling the typed code generator // "print the type of this" lit.setJSDocInfo(buildEmptyJSDoc()); int index = 1; for (Node child = exportedObjectLit.getFirstChild(); child != null; child = child.getNext()) { // TODO(dimvar): handle getters or setters? if (child.isStringKey()) { lit.addChildToBack( IR.propdef( IR.stringKey(child.getString()), IR.number(index++))); } } return lit; }
initModule.setJSDocInfo(builder.build()); if (directAssignments == 0 || (!exports.isEmpty() && !moduleExports.isEmpty())) { Node defaultProp = IR.stringKey(EXPORT_PROPERTY_NAME); defaultProp.putBooleanProp(Node.MODULE_EXPORT, true); defaultProp.addChildToFront(IR.objectlit());
Node newProperty = IR.stringKey(propertyName) .useSourceInfoIfMissingFrom(property);
Node newProperty = IR.stringKey(propertyName) .useSourceInfoIfMissingFrom(property);