Node processStringLiteral(LiteralExpressionTree literalTree) { LiteralToken token = literalTree.literalToken.asLiteral(); Node n = processString(token); String value = n.getString(); if (value.indexOf('\u000B') != -1) { // NOTE(nicksantos): In JavaScript, there are 3 ways to // represent a vertical tab: \v, \x0B, \u000B. // The \v notation was added later, and is not understood // on IE. So we need to preserve it as-is. This is really // obnoxious, because we do not have a good way to represent // how the original string was encoded without making the // representation of strings much more complicated. // // To handle this, we look at the original source test, and // mark the string as \v-encoded or not. If a string is // \v encoded, then all the vertical tabs in that string // will be encoded with a \v. int start = token.location.start.offset; int end = token.location.end.offset; if (start < sourceString.length() && (sourceString.substring( start, Math.min(sourceString.length(), end)).contains("\\v"))) { n.putBooleanProp(Node.SLASH_V, true); } } return n; }
Node processStringLiteral(LiteralExpressionTree literalTree) { LiteralToken token = literalTree.literalToken.asLiteral(); Node n = processString(token); String value = n.getString(); if (value.indexOf('\u000B') != -1) { // NOTE(nicksantos): In JavaScript, there are 3 ways to // represent a vertical tab: \v, \x0B, \u000B. // The \v notation was added later, and is not understood // on IE. So we need to preserve it as-is. This is really // obnoxious, because we do not have a good way to represent // how the original string was encoded without making the // representation of strings much more complicated. // // To handle this, we look at the original source test, and // mark the string as \v-encoded or not. If a string is // \v encoded, then all the vertical tabs in that string // will be encoded with a \v. int start = token.location.start.offset; int end = token.location.end.offset; if (start < sourceString.length() && (sourceString.substring( start, Math.min(sourceString.length(), end)).contains("\\v"))) { n.putBooleanProp(Node.SLASH_V, true); } } return n; }
Node processExportDecl(ExportDeclarationTree tree) { maybeWarnForFeature(tree, Feature.MODULES); Node decls = null; if (tree.isExportAll) { checkState(tree.declaration == null && tree.exportSpecifierList == null); } else if (tree.declaration != null) { checkState(tree.exportSpecifierList == null); decls = transform(tree.declaration); } else { decls = transformList(Token.EXPORT_SPECS, tree.exportSpecifierList); } if (decls == null) { decls = newNode(Token.EMPTY); } setSourceInfo(decls, tree); Node export = newNode(Token.EXPORT, decls); if (tree.from != null) { Node from = processString(tree.from); export.addChildToBack(from); } export.putBooleanProp(Node.EXPORT_ALL_FROM, tree.isExportAll); export.putBooleanProp(Node.EXPORT_DEFAULT, tree.isDefault); return export; }
Node processExportDecl(ExportDeclarationTree tree) { maybeWarnEs6Feature(tree, Feature.MODULES); Node decls = null; if (tree.isExportAll) { Preconditions.checkState( tree.declaration == null && tree.exportSpecifierList == null); } else if (tree.declaration != null) { Preconditions.checkState(tree.exportSpecifierList == null); decls = transform(tree.declaration); } else { decls = transformList(Token.EXPORT_SPECS, tree.exportSpecifierList); } if (decls == null) { decls = newNode(Token.EMPTY); } setSourceInfo(decls, tree); Node export = newNode(Token.EXPORT, decls); if (tree.from != null) { Node from = processString(tree.from); export.addChildToBack(from); } export.putBooleanProp(Node.EXPORT_ALL_FROM, tree.isExportAll); export.putBooleanProp(Node.EXPORT_DEFAULT, tree.isDefault); return export; }
/** * 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; }
/** * 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; }
Node processImportDecl(ImportDeclarationTree tree) { maybeWarnForFeature(tree, Feature.MODULES); Node firstChild = transformOrEmpty(tree.defaultBindingIdentifier, tree); Node secondChild; if (tree.nameSpaceImportIdentifier == null) { secondChild = transformListOrEmpty(Token.IMPORT_SPECS, tree.importSpecifierList); // Currently source info is "import {foo} from '...';" expression. If needed this should be // changed to use only "{foo}" part. setSourceInfo(secondChild, tree); } else { secondChild = newStringNode(Token.IMPORT_STAR, tree.nameSpaceImportIdentifier.value); setSourceInfo(secondChild, tree.nameSpaceImportIdentifier); } Node thirdChild = processString(tree.moduleSpecifier); return newNode(Token.IMPORT, firstChild, secondChild, thirdChild); }
Node processImportDecl(ImportDeclarationTree tree) { maybeWarnEs6Feature(tree, Feature.MODULES); Node firstChild = transformOrEmpty(tree.defaultBindingIdentifier, tree); Node secondChild = (tree.nameSpaceImportIdentifier != null) ? newStringNode(Token.IMPORT_STAR, tree.nameSpaceImportIdentifier.value) : transformListOrEmpty(Token.IMPORT_SPECS, tree.importSpecifierList); setSourceInfo(secondChild, tree); Node thirdChild = processString(tree.moduleSpecifier); return newNode(Token.IMPORT, firstChild, secondChild, thirdChild); }