本文整理汇总了Java中com.google.javascript.rhino.Node.newString方法的典型用法代码示例。如果您正苦于以下问题:Java Node.newString方法的具体用法?Java Node.newString怎么用?Java Node.newString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.newString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testParse
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public void testParse() {
Node a = Node.newString(Token.NAME, "a");
a.addChildToFront(Node.newString(Token.NAME, "b"));
List<ParserResult> testCases = ImmutableList.of(
new ParserResult(
"3;",
createScript(new Node(Token.EXPR_RESULT, Node.newNumber(3.0)))),
new ParserResult(
"var a = b;",
createScript(new Node(Token.VAR, a))),
new ParserResult(
"\"hell\\\no\\ world\\\n\\\n!\"",
createScript(new Node(Token.EXPR_RESULT,
Node.newString(Token.STRING, "hello world!")))));
for (ParserResult testCase : testCases) {
assertNodeEquality(testCase.node, parse(testCase.code));
}
}
示例2: addAliasDeclarationNodes
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Creates a var declaration for each aliased string. Var declarations are
* inserted as close to the first use of the string as possible.
*/
private void addAliasDeclarationNodes() {
for (Entry<String, StringInfo> entry : stringInfoMap.entrySet()) {
StringInfo info = entry.getValue();
if (!info.isAliased) {
continue;
}
String alias = info.getVariableName(entry.getKey());
Node value = Node.newString(Token.STRING, entry.getKey());
Node name = Node.newString(Token.NAME, alias);
name.addChildToBack(value);
Node var = new Node(Token.VAR);
var.addChildToBack(name);
if (info.siblingToInsertVarDeclBefore == null) {
info.parentForNewVarDecl.addChildToFront(var);
} else {
info.parentForNewVarDecl.addChildBefore(
var, info.siblingToInsertVarDeclBefore);
}
compiler.reportCodeChange();
}
}
示例3: createFunction
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/** Creates a fake function with the given description. */
private ConcreteFunctionType createFunction(
String name, String... paramNames) {
Node args = new Node(Token.LP);
for (int i = 0; i < paramNames.length; ++i) {
args.addChildToBack(Node.newString(Token.NAME, paramNames[i]));
}
Node decl = new Node(Token.FUNCTION,
Node.newString(Token.NAME, name),
args,
new Node(Token.BLOCK));
JSType[] paramTypes = new JSType[paramNames.length];
Arrays.fill(paramTypes, unknownType);
decl.setJSType(
typeRegistry.createConstructorType(name, decl, args, unknownType));
return new ConcreteFunctionType(factory, decl, null);
}
示例4: makeForwardSlashBracketSafe
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* returns a string node that can safely be rendered inside /brackets/.
*/
private static Node makeForwardSlashBracketSafe(Node n) {
String s = n.getString();
// sb contains everything in s[0:pos]
StringBuilder sb = null;
int pos = 0;
for (int i = 0; i < s.length(); ++i) {
switch (s.charAt(i)) {
case '\\': // skip over the next char after a '\\'.
++i;
break;
case '/': // escape it
if (null == sb) { sb = new StringBuilder(s.length() + 16); }
sb.append(s, pos, i).append('\\');
pos = i;
break;
}
}
// don't discard useful line-number info if there were no changes
if (null == sb) { return n.cloneTree(); }
sb.append(s, pos, s.length());
return Node.newString(sb.toString());
}
示例5: makeVarDeclNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Creates a simple namespace variable declaration
* (e.g. <code>var foo = {};</code>).
*
* @param namespace A simple namespace (must be a valid js identifier)
* @param sourceNode The node to get source information from.
*/
private Node makeVarDeclNode(String namespace, Node sourceNode) {
Node name = Node.newString(Token.NAME, namespace);
name.addChildToFront(new Node(Token.OBJECTLIT));
Node decl = new Node(Token.VAR, name);
decl.putBooleanProp(Node.IS_NAMESPACE, true);
// TODO(nicksantos): ew ew ew. Create a mutator package.
if (compiler.getCodingConvention().isConstant(namespace)) {
name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
}
Preconditions.checkState(isNamespacePlaceholder(decl));
decl.copyInformationFromForTree(sourceNode);
return decl;
}
示例6: testVarAndOptionalParams
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void testVarAndOptionalParams() {
Node args = new Node(Token.LP,
Node.newString(Token.NAME, "a"),
Node.newString(Token.NAME, "b"));
Node optArgs = new Node(Token.LP,
Node.newString(Token.NAME, "opt_a"),
Node.newString(Token.NAME, "opt_b"));
assertFalse(conv.isVarArgsParameter(args.getFirstChild()));
assertFalse(conv.isVarArgsParameter(args.getLastChild()));
assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild()));
assertFalse(conv.isVarArgsParameter(optArgs.getLastChild()));
assertFalse(conv.isOptionalParameter(args.getFirstChild()));
assertFalse(conv.isOptionalParameter(args.getLastChild()));
assertFalse(conv.isOptionalParameter(optArgs.getFirstChild()));
assertFalse(conv.isOptionalParameter(optArgs.getLastChild()));
}
示例7: replaceAccessor
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
private void replaceAccessor(Node getPropNode) {
/*
* BEFORE
getprop
NODE...
string length
AFTER
getelem
NODE...
name PROP_length
*/
Node propNameNode = getPropNode.getLastChild();
String propName = propNameNode.getString();
if (props.get(propName).aliasAccessor) {
Node propSrc = getPropNode.getFirstChild();
getPropNode.removeChild(propSrc);
Node newNameNode =
Node.newString(Token.NAME, getArrayNotationNameFor(propName));
Node elemNode = new Node(Token.GETELEM, propSrc, newNameNode);
replaceNode(getPropNode.getParent(), getPropNode, elemNode);
compiler.reportCodeChange();
}
}
示例8: addAccessorPropName
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Adds a string that can be used to reference properties by array []
* notation.
*
* PROP_prototype = 'prototype';
*
* @param propName Name of property
* @param root Root of output tree that function can be added to
*/
private void addAccessorPropName(String propName, Node root) {
/*
* Target:
var 1
name PROP_length
string length
*/
Node propValue = Node.newString(Token.STRING, propName);
Node propNameNode =
Node.newString(Token.NAME, getArrayNotationNameFor(propName));
propNameNode.addChildToFront(propValue);
Node var = new Node(Token.VAR, propNameNode);
root.addChildToFront(var);
compiler.reportCodeChange();
}
示例9: testNameNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Type checks a NAME node and retrieve its type.
*/
private JSType testNameNode(String name) {
Node node = Node.newString(Token.NAME, name);
Node parent = new Node(Token.SCRIPT, node);
Node externs = new Node(Token.BLOCK);
Node externAndJsRoot = new Node(Token.BLOCK, externs, parent);
externAndJsRoot.setIsSyntheticBlock(true);
makeTypeCheck().processForTesting(null, parent);
return node.getJSType();
}
示例10: testIllegalArgumentIfNotHook
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void testIllegalArgumentIfNotHook() throws Exception {
Node nameNode = Node.newString(Token.NAME, "foo");
try {
checkKeepSimplifiedHookExpr(nameNode,
true,
true,
ImmutableList.<String>of());
fail("Expected exception");
} catch (IllegalArgumentException e) {
// ignore
}
}
示例11: doExtraction
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Declares the temp variable to point to prototype objects and iterates
* through all ExtractInstance and performs extraction there.
*/
private void doExtraction(GatherExtractionInfo info) {
// First declare the temp variable.
Node var = new Node(Token.VAR, Node.newString(Token.NAME, prototypeAlias));
compiler.getNodeForCodeInsertion(null).addChildrenToFront(var);
// Go through all extraction instances and extract each of them.
for (ExtractionInstance instance : info.instances) {
extractInstance(instance);
}
}
示例12: buildResultExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Create an expression tree for an expression.
* If the result of the expression is needed, then:
* ASSIGN
* tempName
* expr
* otherwise, simply:
* expr
*/
private static Node buildResultExpression(
Node expr, boolean needResult, String tempName) {
if (needResult) {
return new Node(Token.ASSIGN,
Node.newString(Token.NAME, tempName),
expr);
} else {
return expr;
}
}
示例13: extractExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @param expr The expression to extract.
* @param injectionPoint The node before which to added the extracted
* expression.
* @return The extract statement node.
*/
private Node extractExpression(Node expr, Node injectionPoint) {
Node parent = expr.getParent();
// The temp value is known to be constant.
String tempName = getTempConstantValueName();
// Replace the expression with the temporary name.
Node replacementValueNode = Node.newString(Token.NAME, tempName);
parent.replaceChild(expr, replacementValueNode);
// Re-add the expression in the declaration of the temporary name.
Node tempNameNode = Node.newString(Token.NAME, tempName);
tempNameNode.addChildrenToBack(expr);
Node tempVarNode = new Node(Token.VAR, tempNameNode);
Node injectionPointParent = injectionPoint.getParent();
injectionPointParent.addChildBefore(tempVarNode, injectionPoint);
// If it is ASSIGN_XXX we need to assign it back to the original value.
// Note that calling the temp constant is a lie in this case, but we do know
// that it is not modified until after the exposed expression.
if (NodeUtil.isAssignmentOp(parent) && !NodeUtil.isAssign(parent)) {
Node gParent = parent.getParent();
Node assignBack = new Node(Token.ASSIGN,
expr.cloneTree(),
tempNameNode.cloneNode());
if (NodeUtil.isExpressionNode(gParent)) {
gParent.getParent().addChildAfter(
NodeUtil.newExpr(assignBack), gParent);
} else {
// TODO(user): Use comma here sucks. We might close some accuracy
// in flow sensitive passes but as far as I know it is unavoidable.
Node comma = new Node(Token.COMMA);
gParent.replaceChild(parent, comma);
comma.addChildrenToFront(assignBack);
comma.addChildrenToFront(parent);
}
}
return tempVarNode;
}
示例14: addStubsForUndeclaredProperties
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Adds global variable "stubs" for any properties of a global name that are
* only set in a local scope or read but never set.
*
* @param n An object representing a global name (e.g. "a", "a.b.c")
* @param alias The flattened name of the object whose properties we are
* adding stubs for (e.g. "a$b$c")
* @param parent The node to which new global variables should be added
* as children
* @param addAfter The child of after which new
* variables should be added (may be null)
* @return The number of variables added
*/
private int addStubsForUndeclaredProperties(
Name n, String alias, Node parent, Node addAfter) {
Preconditions.checkArgument(NodeUtil.isStatementBlock(parent));
int numStubs = 0;
if (n.props != null) {
for (Name p : n.props) {
if (p.needsToBeStubbed()) {
String propAlias = appendPropForAlias(alias, p.name);
Node nameNode = Node.newString(Token.NAME, propAlias);
Node newVar = new Node(Token.VAR, nameNode);
if (addAfter == null) {
parent.addChildToFront(newVar);
} else {
parent.addChildAfter(newVar, addAfter);
addAfter = newVar;
}
numStubs++;
compiler.reportCodeChange();
// Determine if this is a constant var by checking the first
// reference to it. Don't check the declaration, as it might be null.
if (p.refs.get(0).node.getLastChild().getBooleanProp(
Node.IS_CONSTANT_NAME)) {
nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
}
}
}
}
return numStubs;
}
示例15: testUndefinedNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void testUndefinedNode() throws Exception {
Node p = new Node(Token.ADD);
Node n = Node.newString(Token.NAME, "undefined");
p.addChildToBack(n);
p.addChildToBack(Node.newNumber(5));
typeCheck(p);
assertEquals(VOID_TYPE, n.getJSType());
}