本文整理汇总了Java中com.google.javascript.rhino.Node.hasOneChild方法的典型用法代码示例。如果您正苦于以下问题:Java Node.hasOneChild方法的具体用法?Java Node.hasOneChild怎么用?Java Node.hasOneChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.hasOneChild方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryMergeBlock
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Merge a block with its parent block.
* @return Whether the block was removed.
*/
static boolean tryMergeBlock(Node block) {
Preconditions.checkState(block.getType() == Token.BLOCK);
Node parent = block.getParent();
// Try to remove the block if its parent is a block/script or if its
// parent is label and it has exactly one child.
if (NodeUtil.isStatementBlock(parent)) {
Node previous = block;
while (block.hasChildren()) {
Node child = block.removeFirstChild();
parent.addChildAfter(child, previous);
previous = child;
}
parent.removeChild(block);
return true;
} else if (parent.getType() == Token.LABEL && block.hasOneChild()) {
parent.replaceChild(block, block.removeFirstChild());
return true;
} else {
return false;
}
}
示例2: getInfoForNameNode
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @param nameNode A name node
* @return The JSDocInfo for the name node
*/
static JSDocInfo getInfoForNameNode(Node nameNode) {
JSDocInfo info = null;
Node parent = null;
if (nameNode != null) {
info = nameNode.getJSDocInfo();
parent = nameNode.getParent();
}
if (info == null && parent != null &&
((parent.getType() == Token.VAR && parent.hasOneChild()) ||
parent.getType() == Token.FUNCTION)) {
info = parent.getJSDocInfo();
}
return info;
}
示例3: returnedExpression
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Return the node that represents the expression returned
* by the method, given a FUNCTION node.
*/
private static Node returnedExpression(Node fn) {
Node expectedBlock = getMethodBlock(fn);
if (!expectedBlock.hasOneChild()) {
return null;
}
Node expectedReturn = expectedBlock.getFirstChild();
if (expectedReturn.getType() != Token.RETURN) {
return null;
}
if (!expectedReturn.hasOneChild()) {
return null;
}
return expectedReturn.getLastChild();
}
示例4: isDirectCallNodeReplacementPossible
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Checks if the given function matches the criteria for an inlinable
* function, and if so, adds it to our set of inlinable functions.
*/
boolean isDirectCallNodeReplacementPossible(Node fnNode) {
// Only inline single-statement functions
Node block = NodeUtil.getFunctionBody(fnNode);
// Check if this function is suitable for direct replacement of a CALL node:
// a function that consists of single return that returns an expression.
if (!block.hasChildren()) {
// special case empty functions.
return true;
} else if (block.hasOneChild()) {
// Only inline functions that return something.
if (block.getFirstChild().getType() == Token.RETURN
&& block.getFirstChild().getFirstChild() != null) {
return true;
}
}
return false;
}
示例5: maybeGetSingleReturnRValue
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return function return value node if function body contains a
* single return statement. Otherwise, null.
*/
protected final Node maybeGetSingleReturnRValue(Node functionNode) {
Node body = functionNode.getLastChild();
if (!body.hasOneChild()) {
return null;
}
Node statement = body.getFirstChild();
if (statement.getType() == Token.RETURN) {
return statement.getFirstChild();
}
return null;
}
示例6: getSetPropertyName
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Checks if the function matches the pattern:
* function(<value>, <rest>) {this.<name> = <value>}
* and returns <name> if a match is found.
*
* @return STRING node that is the rhs of a this property get; or null.
*/
private Node getSetPropertyName(Node functionNode) {
Node body = functionNode.getLastChild();
if (!body.hasOneChild()) {
return null;
}
Node argList = functionNode.getFirstChild().getNext();
Node paramNode = argList.getFirstChild();
if (paramNode == null) {
return null;
}
Node statement = body.getFirstChild();
if (!NodeUtil.isExprAssign(statement)) {
return null;
}
Node assign = statement.getFirstChild();
Node lhs = assign.getFirstChild();
if (NodeUtil.isGetProp(lhs) && NodeUtil.isThis(lhs.getFirstChild())) {
Node rhs = assign.getLastChild();
if (NodeUtil.isName(rhs) &&
rhs.getString().equals(paramNode.getString())) {
Node propertyName = lhs.getLastChild();
return propertyName;
}
}
return null;
}
示例7: collectDefines
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Finds all defines, and creates a {@link DefineInfo} data structure for
* each one.
* @return A map of {@link DefineInfo} structures, keyed by name.
*/
private Map<String, DefineInfo> collectDefines(Node root,
GlobalNamespace namespace) {
// Find all the global names with a @define annotation
List<Name> allDefines = Lists.newArrayList();
for (Name name : namespace.getNameIndex().values()) {
if (name.docInfo != null && name.docInfo.isDefine()) {
allDefines.add(name);
} else if (name.refs != null) {
for (Ref ref : name.refs) {
Node n = ref.node;
Node parent = ref.node.getParent();
JSDocInfo info = n.getJSDocInfo();
if (info == null &&
parent.getType() == Token.VAR && parent.hasOneChild()) {
info = parent.getJSDocInfo();
}
if (info != null && info.isDefine()) {
allDefines.add(name);
break;
}
}
}
}
CollectDefines pass = new CollectDefines(compiler, allDefines);
NodeTraversal.traverse(compiler, root, pass);
return pass.getAllDefines();
}
示例8: isExpressBlock
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return Whether the node is a block with a single statement that is
* an expression.
*/
private boolean isExpressBlock(Node n) {
if (n.getType() == Token.BLOCK) {
if (n.hasOneChild()) {
return NodeUtil.isExpressionNode(n.getFirstChild());
}
}
return false;
}
示例9: isReturnExpressBlock
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return Whether the node is a block with a single statement that is
* an return.
*/
private boolean isReturnExpressBlock(Node n) {
if (n.getType() == Token.BLOCK) {
if (n.hasOneChild()) {
Node first = n.getFirstChild();
if (first.getType() == Token.RETURN) {
return first.hasOneChild();
}
}
}
return false;
}
示例10: isVarBlock
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* @return Whether the node is a block with a single statement that is
* a VAR declaration of a single variable.
*/
private boolean isVarBlock(Node n) {
if (n.getType() == Token.BLOCK) {
if (n.hasOneChild()) {
Node first = n.getFirstChild();
if (first.getType() == Token.VAR) {
return first.hasOneChild();
}
}
}
return false;
}
示例11: findNamedFunctions
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
public void findNamedFunctions(NodeTraversal t, Node n, Node parent) {
if (!NodeUtil.isStatement(n)) {
// There aren't any interesting functions here.
return;
}
switch (n.getType()) {
// Anonymous functions in the form of:
// var fooFn = function(x) { return ... }
case Token.VAR:
// TODO(johnlenz): Make this a Preconditions check.
// Currently this fails for some targets.
if (n.hasOneChild()) {
// Only look at declarations in the global scope.
Node nameNode = n.getFirstChild();
if (nameNode.getType() == Token.NAME && nameNode.hasChildren()
&& nameNode.getFirstChild().getType() == Token.FUNCTION) {
maybeAddFunction(new FunctionVar(n), t.getModule());
}
}
break;
// Named functions
// function Foo(x) { return ... }
case Token.FUNCTION:
Preconditions.checkState(NodeUtil.isStatementBlock(parent)
|| parent.getType() == Token.LABEL);
Function fn = new NamedFunction(n);
String name = fn.getName();
if (!name.isEmpty()) {
maybeAddFunction(fn, t.getModule());
}
break;
}
}
示例12: visitVar
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Visits a VAR node.
*
* @param t The node traversal object that supplies context, such as the
* scope chain to use in name lookups as well as error reporting.
* @param n The node being visited.
*/
private void visitVar(NodeTraversal t, Node n) {
// TODO(nicksantos): Fix this so that the doc info always shows up
// on the NAME node. We probably want to wait for the parser
// merge to fix this.
JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
for (Node name : n.children()) {
Node value = name.getFirstChild();
// A null var would indicate a bug in the scope creation logic.
Var var = t.getScope().getVar(name.getString());
if (value != null) {
JSType valueType = getJSType(value);
JSType nameType = var.getType();
nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;
JSDocInfo info = name.getJSDocInfo();
if (info == null) {
info = varInfo;
}
if (info != null && info.hasEnumParameterType()) {
// var.getType() can never be null, this would indicate a bug in the
// scope creation logic.
checkEnumInitializer(
t, value, info.getEnumParameterType().evaluate(t.getScope()));
} else if (var.isTypeInferred()) {
ensureTyped(t, name, valueType);
} else {
validator.expectCanAssignTo(
t, value, valueType, nameType, "initializing variable");
}
}
}
}
示例13: classifyCallSite
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Determine which, if any, of the supported types the call site is.
*/
private CallSiteType classifyCallSite(Node callNode) {
Node parent = callNode.getParent();
Node grandParent = parent.getParent();
// Verify the call site:
if (NodeUtil.isExprCall(parent)) {
// This is a simple call? Example: "foo();".
return CallSiteType.SIMPLE_CALL;
} else if (NodeUtil.isExprAssign(grandParent)
&& !NodeUtil.isLhs(callNode, parent)
&& parent.getFirstChild().getType() == Token.NAME
&& !NodeUtil.isConstantName(parent.getFirstChild())) {
// This is a simple assignment. Example: "x = foo();"
return CallSiteType.SIMPLE_ASSIGNMENT;
} else if (parent.getType() == Token.NAME
&& !NodeUtil.isConstantName(parent)
&& grandParent.getType() == Token.VAR
&& grandParent.hasOneChild()) {
// This is a var declaration. Example: "var x = foo();"
// TODO(johnlenz): Should we be checking for constants on the
// left-hand-side of the assignments (and handling them as EXPRESSION?
return CallSiteType.VAR_DECL_SIMPLE_ASSIGNMENT;
} else {
Node expressionRoot = ExpressionDecomposer.findExpressionRoot(callNode);
if (expressionRoot != null) {
ExpressionDecomposer decomposer = new ExpressionDecomposer(
compiler, safeNameIdSupplier, knownConstants);
DecompositionType type = decomposer.canExposeExpression(
callNode);
if (type == DecompositionType.MOVABLE) {
return CallSiteType.EXPRESSION;
} else if (type == DecompositionType.DECOMPOSABLE) {
return CallSiteType.DECOMPOSABLE_EXPRESSION;
} else {
Preconditions.checkState(type == DecompositionType.UNDECOMPOSABLE);
}
}
}
return CallSiteType.UNSUPPORTED;
}
示例14: visit
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {
if (!inExterns && hasNoSideEffectsAnnotation(node)) {
traversal.report(node, INVALID_NO_SIDE_EFFECT_ANNOTATION);
}
if (NodeUtil.isGetProp(node)) {
if (NodeUtil.isExpressionNode(parent) &&
hasNoSideEffectsAnnotation(node)) {
noSideEffectFunctionNames.add(node);
}
} else if (NodeUtil.isFunction(node)) {
// The annotation may attached to the function node, the
// variable declaration or assignment expression.
boolean hasAnnotation = hasNoSideEffectsAnnotation(node);
List<Node> nameNodes = Lists.newArrayList();
nameNodes.add(node.getFirstChild());
Node nameNode = null;
if (NodeUtil.isName(parent)) {
Node gramp = parent.getParent();
if (NodeUtil.isVar(gramp) &&
gramp.hasOneChild() &&
hasNoSideEffectsAnnotation(gramp)) {
hasAnnotation = true;
}
nameNodes.add(parent);
} else if (NodeUtil.isAssign(parent)) {
if (hasNoSideEffectsAnnotation(parent)) {
hasAnnotation = true;
}
nameNodes.add(parent.getFirstChild());
}
if (hasAnnotation) {
noSideEffectFunctionNames.addAll(nameNodes);
}
}
}