/** * Transforms the given node and then sets its type to Token.STRING if it * was Token.NAME. If its type was already Token.STRING, then quotes it. * Used for properties, as the old AST uses String tokens, while the new one * uses Name tokens for unquoted strings. For example, in * var o = {'a' : 1, b: 2}; * the string 'a' is quoted, while the name b is turned into a string, but * unquoted. */ private Node processObjectLitKeyAsString( com.google.javascript.jscomp.parsing.parser.Token token) { Node ret; if (token == null) { return createMissingExpressionNode(); } else if (token.type == TokenType.IDENTIFIER) { ret = processName(token.asIdentifier(), true); } else if (token.type == TokenType.NUMBER) { ret = transformNumberAsString(token.asLiteral()); ret.putBooleanProp(Node.QUOTED_PROP, true); } else { ret = processString(token.asLiteral()); ret.putBooleanProp(Node.QUOTED_PROP, true); } checkState(ret.isString()); return ret; }
/** * Transforms the given node and then sets its type to Token.STRING if it * was Token.NAME. If its type was already Token.STRING, then quotes it. * Used for properties, as the old AST uses String tokens, while the new one * uses Name tokens for unquoted strings. For example, in * var o = {'a' : 1, b: 2}; * the string 'a' is quoted, while the name b is turned into a string, but * unquoted. */ private Node processObjectLitKeyAsString( com.google.javascript.jscomp.parsing.parser.Token token) { Node ret; if (token == null) { return createMissingExpressionNode(); } else if (token.type == TokenType.IDENTIFIER) { ret = processName(token.asIdentifier(), true); } else if (token.type == TokenType.NUMBER) { ret = transformNumberAsString(token.asLiteral()); ret.putBooleanProp(Node.QUOTED_PROP, true); } else { ret = processString(token.asLiteral()); ret.putBooleanProp(Node.QUOTED_PROP, true); } Preconditions.checkState(ret.isString()); return ret; }
return processParenthesizedExpression(node.asParenExpression()); case IDENTIFIER_EXPRESSION: return processName(node.asIdentifierExpression()); case NEW_EXPRESSION: return processNewExpression(node.asNewExpression());
Node processEnumDeclaration(EnumDeclarationTree tree) { maybeWarnTypeSyntax(tree, Feature.ENUM); Node name = processName(tree.name); Node body = newNode(Token.ENUM_MEMBERS); setSourceInfo(body, tree); for (ParseTree child : tree.members) { Node childNode = transform(child); // NOTE: we may need to "undo" the shorthand property normalization, // since this syntax has a different meaning in enums. if (childNode.isShorthandProperty()) { childNode.removeChild(childNode.getLastChild()); childNode.setShorthandProperty(false); } body.addChildToBack(childNode); } return newNode(Token.ENUM, name, body); }
Node processName(IdentifierToken identifierToken) { return processName(identifierToken, false); }
Node processName(IdentifierExpressionTree nameNode) { return processName(nameNode, false); }
Node processName(IdentifierExpressionTree nameNode, boolean asString) { return processName(nameNode.identifierToken, asString); }
Node processName(IdentifierToken identifierToken) { return processName(identifierToken, false); }
Node processName(IdentifierExpressionTree nameNode, boolean asString) { return processName(nameNode.identifierToken, asString); }
Node processInterfaceDeclaration(InterfaceDeclarationTree tree) { maybeWarnTypeSyntax(tree, Feature.INTERFACE); Node name = processName(tree.name); maybeProcessGenerics(name, tree.generics); Node superInterfaces = transformListOrEmpty(Token.INTERFACE_EXTENDS, tree.superInterfaces); Node body = newNode(Token.INTERFACE_MEMBERS); setSourceInfo(body, tree); for (ParseTree child : tree.elements) { body.addChildToBack(transform(child)); } return newNode(Token.INTERFACE, name, superInterfaces, body); }
Node processImportSpec(ImportSpecifierTree tree) { Node importedName = processName(tree.importedName, true); importedName.setType(Token.NAME); Node importSpec = newNode(Token.IMPORT_SPEC, importedName); if (tree.destinationName != null) { importSpec.addChildToBack(processName(tree.destinationName)); } return importSpec; }
Node processExportSpec(ExportSpecifierTree tree) { Node importedName = processName(tree.importedName, true); importedName.setType(Token.NAME); Node exportSpec = newNode(Token.EXPORT_SPEC, importedName); if (tree.destinationName != null) { Node destinationName = processName(tree.destinationName, true); destinationName.setType(Token.NAME); exportSpec.addChildToBack(destinationName); } return exportSpec; }
Node processEnumDeclaration(EnumDeclarationTree tree) { maybeWarnTypeSyntax(tree, Feature.ENUM); Node name = processName(tree.name); Node body = newNode(Token.ENUM_MEMBERS); setSourceInfo(body, tree); for (ParseTree child : tree.members) { body.addChildrenToBack(transform(child)); } return newNode(Token.ENUM, name, body); }
Node processImportSpec(ImportSpecifierTree tree) { Node importedName = processName(tree.importedName, true); importedName.setToken(Token.NAME); Node importSpec = newNode(Token.IMPORT_SPEC, importedName); if (tree.destinationName == null) { importSpec.setShorthandProperty(true); importSpec.addChildToBack(importedName.cloneTree()); } else { importSpec.addChildToBack(processName(tree.destinationName)); } return importSpec; }
Node processInterfaceDeclaration(InterfaceDeclarationTree tree) { maybeWarnTypeSyntax(tree, Feature.INTERFACE); Node name = processName(tree.name); maybeProcessGenerics(name, tree.generics); Node superInterfaces = transformListOrEmpty(Token.INTERFACE_EXTENDS, tree.superInterfaces); Node body = newNode(Token.INTERFACE_MEMBERS); setSourceInfo(body, tree); for (ParseTree child : tree.elements) { body.addChildToBack(transform(child)); } return newNode(Token.INTERFACE, name, superInterfaces, body); }
Node transformOrEmpty(IdentifierToken token, ParseTree parent) { if (token == null) { Node n = newNode(Token.EMPTY); setSourceInfo(n, parent); return n; } return processName(token); }
Node processName(IdentifierExpressionTree nameNode) { return processName(nameNode, false); }
private Node safeProcessName(IdentifierToken identifierToken) { if (identifierToken == null) { return createMissingExpressionNode(); } else { return processName(identifierToken); } }
Node processExportSpec(ExportSpecifierTree tree) { Node importedName = processName(tree.importedName, true); importedName.setToken(Token.NAME); Node exportSpec = newNode(Token.EXPORT_SPEC, importedName); if (tree.destinationName == null) { exportSpec.setShorthandProperty(true); exportSpec.addChildToBack(importedName.cloneTree()); } else { Node destinationName = processName(tree.destinationName, true); destinationName.setToken(Token.NAME); exportSpec.addChildToBack(destinationName); } return exportSpec; }
Node transformOrEmpty(IdentifierToken token, ParseTree parent) { if (token == null) { Node n = newNode(Token.EMPTY); setSourceInfo(n, parent); return n; } return processName(token); }