id int32 0 165k | repo stringlengths 7 58 | path stringlengths 12 218 | func_name stringlengths 3 140 | original_string stringlengths 73 34.1k | language stringclasses 1 value | code stringlengths 73 34.1k | code_tokens list | docstring stringlengths 3 16k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 105 339 |
|---|---|---|---|---|---|---|---|---|---|---|---|
24,200 | google/closure-compiler | src/com/google/javascript/jscomp/ComposeWarningsGuard.java | ComposeWarningsGuard.makeEmergencyFailSafeGuard | ComposeWarningsGuard makeEmergencyFailSafeGuard() {
ComposeWarningsGuard safeGuard = new ComposeWarningsGuard();
safeGuard.demoteErrors = true;
for (WarningsGuard guard : guards.descendingSet()) {
safeGuard.addGuard(guard);
}
return safeGuard;
} | java | ComposeWarningsGuard makeEmergencyFailSafeGuard() {
ComposeWarningsGuard safeGuard = new ComposeWarningsGuard();
safeGuard.demoteErrors = true;
for (WarningsGuard guard : guards.descendingSet()) {
safeGuard.addGuard(guard);
}
return safeGuard;
} | [
"ComposeWarningsGuard",
"makeEmergencyFailSafeGuard",
"(",
")",
"{",
"ComposeWarningsGuard",
"safeGuard",
"=",
"new",
"ComposeWarningsGuard",
"(",
")",
";",
"safeGuard",
".",
"demoteErrors",
"=",
"true",
";",
"for",
"(",
"WarningsGuard",
"guard",
":",
"guards",
".",
"descendingSet",
"(",
")",
")",
"{",
"safeGuard",
".",
"addGuard",
"(",
"guard",
")",
";",
"}",
"return",
"safeGuard",
";",
"}"
] | Make a warnings guard that's the same as this one but demotes all
errors to warnings. | [
"Make",
"a",
"warnings",
"guard",
"that",
"s",
"the",
"same",
"as",
"this",
"one",
"but",
"demotes",
"all",
"errors",
"to",
"warnings",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/ComposeWarningsGuard.java#L167-L174 |
24,201 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.isSupportedCallType | private boolean isSupportedCallType(Node callNode) {
if (!callNode.getFirstChild().isName()) {
if (NodeUtil.isFunctionObjectCall(callNode)) {
if (!assumeStrictThis) {
Node thisValue = callNode.getSecondChild();
if (thisValue == null || !thisValue.isThis()) {
return false;
}
}
} else if (NodeUtil.isFunctionObjectApply(callNode)) {
return false;
}
}
return true;
} | java | private boolean isSupportedCallType(Node callNode) {
if (!callNode.getFirstChild().isName()) {
if (NodeUtil.isFunctionObjectCall(callNode)) {
if (!assumeStrictThis) {
Node thisValue = callNode.getSecondChild();
if (thisValue == null || !thisValue.isThis()) {
return false;
}
}
} else if (NodeUtil.isFunctionObjectApply(callNode)) {
return false;
}
}
return true;
} | [
"private",
"boolean",
"isSupportedCallType",
"(",
"Node",
"callNode",
")",
"{",
"if",
"(",
"!",
"callNode",
".",
"getFirstChild",
"(",
")",
".",
"isName",
"(",
")",
")",
"{",
"if",
"(",
"NodeUtil",
".",
"isFunctionObjectCall",
"(",
"callNode",
")",
")",
"{",
"if",
"(",
"!",
"assumeStrictThis",
")",
"{",
"Node",
"thisValue",
"=",
"callNode",
".",
"getSecondChild",
"(",
")",
";",
"if",
"(",
"thisValue",
"==",
"null",
"||",
"!",
"thisValue",
".",
"isThis",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"}",
"else",
"if",
"(",
"NodeUtil",
".",
"isFunctionObjectApply",
"(",
"callNode",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"return",
"true",
";",
"}"
] | Only ".call" calls and direct calls to functions are supported.
@param callNode The call evaluate.
@return Whether the call is of a type that is supported. | [
"Only",
".",
"call",
"calls",
"and",
"direct",
"calls",
"to",
"functions",
"are",
"supported",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L268-L283 |
24,202 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.inline | Node inline(Reference ref, String fnName, Node fnNode) {
checkState(compiler.getLifeCycleStage().isNormalized());
return internalInline(ref, fnName, fnNode);
} | java | Node inline(Reference ref, String fnName, Node fnNode) {
checkState(compiler.getLifeCycleStage().isNormalized());
return internalInline(ref, fnName, fnNode);
} | [
"Node",
"inline",
"(",
"Reference",
"ref",
",",
"String",
"fnName",
",",
"Node",
"fnNode",
")",
"{",
"checkState",
"(",
"compiler",
".",
"getLifeCycleStage",
"(",
")",
".",
"isNormalized",
"(",
")",
")",
";",
"return",
"internalInline",
"(",
"ref",
",",
"fnName",
",",
"fnNode",
")",
";",
"}"
] | Inline a function into the call site. | [
"Inline",
"a",
"function",
"into",
"the",
"call",
"site",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L298-L301 |
24,203 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.inlineReturnValue | private Node inlineReturnValue(Reference ref, Node fnNode) {
Node callNode = ref.callNode;
Node block = fnNode.getLastChild();
Node callParentNode = callNode.getParent();
// NOTE: As the normalize pass guarantees globals aren't being
// shadowed and an expression can't introduce new names, there is
// no need to check for conflicts.
// Create an argName -> expression map, checking for side effects.
Map<String, Node> argMap =
FunctionArgumentInjector.getFunctionCallParameterMap(
fnNode, callNode, this.safeNameIdSupplier);
Node newExpression;
if (!block.hasChildren()) {
Node srcLocation = block;
newExpression = NodeUtil.newUndefinedNode(srcLocation);
} else {
Node returnNode = block.getFirstChild();
checkArgument(returnNode.isReturn(), returnNode);
// Clone the return node first.
Node safeReturnNode = returnNode.cloneTree();
Node inlineResult = FunctionArgumentInjector.inject(
null, safeReturnNode, null, argMap);
checkArgument(safeReturnNode == inlineResult);
newExpression = safeReturnNode.removeFirstChild();
NodeUtil.markNewScopesChanged(newExpression, compiler);
}
// If the call site had a cast ensure it's persisted to the new expression that replaces it.
JSType typeBeforeCast = callNode.getJSTypeBeforeCast();
if (typeBeforeCast != null) {
newExpression.setJSTypeBeforeCast(typeBeforeCast);
newExpression.setJSType(callNode.getJSType());
}
callParentNode.replaceChild(callNode, newExpression);
NodeUtil.markFunctionsDeleted(callNode, compiler);
return newExpression;
} | java | private Node inlineReturnValue(Reference ref, Node fnNode) {
Node callNode = ref.callNode;
Node block = fnNode.getLastChild();
Node callParentNode = callNode.getParent();
// NOTE: As the normalize pass guarantees globals aren't being
// shadowed and an expression can't introduce new names, there is
// no need to check for conflicts.
// Create an argName -> expression map, checking for side effects.
Map<String, Node> argMap =
FunctionArgumentInjector.getFunctionCallParameterMap(
fnNode, callNode, this.safeNameIdSupplier);
Node newExpression;
if (!block.hasChildren()) {
Node srcLocation = block;
newExpression = NodeUtil.newUndefinedNode(srcLocation);
} else {
Node returnNode = block.getFirstChild();
checkArgument(returnNode.isReturn(), returnNode);
// Clone the return node first.
Node safeReturnNode = returnNode.cloneTree();
Node inlineResult = FunctionArgumentInjector.inject(
null, safeReturnNode, null, argMap);
checkArgument(safeReturnNode == inlineResult);
newExpression = safeReturnNode.removeFirstChild();
NodeUtil.markNewScopesChanged(newExpression, compiler);
}
// If the call site had a cast ensure it's persisted to the new expression that replaces it.
JSType typeBeforeCast = callNode.getJSTypeBeforeCast();
if (typeBeforeCast != null) {
newExpression.setJSTypeBeforeCast(typeBeforeCast);
newExpression.setJSType(callNode.getJSType());
}
callParentNode.replaceChild(callNode, newExpression);
NodeUtil.markFunctionsDeleted(callNode, compiler);
return newExpression;
} | [
"private",
"Node",
"inlineReturnValue",
"(",
"Reference",
"ref",
",",
"Node",
"fnNode",
")",
"{",
"Node",
"callNode",
"=",
"ref",
".",
"callNode",
";",
"Node",
"block",
"=",
"fnNode",
".",
"getLastChild",
"(",
")",
";",
"Node",
"callParentNode",
"=",
"callNode",
".",
"getParent",
"(",
")",
";",
"// NOTE: As the normalize pass guarantees globals aren't being",
"// shadowed and an expression can't introduce new names, there is",
"// no need to check for conflicts.",
"// Create an argName -> expression map, checking for side effects.",
"Map",
"<",
"String",
",",
"Node",
">",
"argMap",
"=",
"FunctionArgumentInjector",
".",
"getFunctionCallParameterMap",
"(",
"fnNode",
",",
"callNode",
",",
"this",
".",
"safeNameIdSupplier",
")",
";",
"Node",
"newExpression",
";",
"if",
"(",
"!",
"block",
".",
"hasChildren",
"(",
")",
")",
"{",
"Node",
"srcLocation",
"=",
"block",
";",
"newExpression",
"=",
"NodeUtil",
".",
"newUndefinedNode",
"(",
"srcLocation",
")",
";",
"}",
"else",
"{",
"Node",
"returnNode",
"=",
"block",
".",
"getFirstChild",
"(",
")",
";",
"checkArgument",
"(",
"returnNode",
".",
"isReturn",
"(",
")",
",",
"returnNode",
")",
";",
"// Clone the return node first.",
"Node",
"safeReturnNode",
"=",
"returnNode",
".",
"cloneTree",
"(",
")",
";",
"Node",
"inlineResult",
"=",
"FunctionArgumentInjector",
".",
"inject",
"(",
"null",
",",
"safeReturnNode",
",",
"null",
",",
"argMap",
")",
";",
"checkArgument",
"(",
"safeReturnNode",
"==",
"inlineResult",
")",
";",
"newExpression",
"=",
"safeReturnNode",
".",
"removeFirstChild",
"(",
")",
";",
"NodeUtil",
".",
"markNewScopesChanged",
"(",
"newExpression",
",",
"compiler",
")",
";",
"}",
"// If the call site had a cast ensure it's persisted to the new expression that replaces it.",
"JSType",
"typeBeforeCast",
"=",
"callNode",
".",
"getJSTypeBeforeCast",
"(",
")",
";",
"if",
"(",
"typeBeforeCast",
"!=",
"null",
")",
"{",
"newExpression",
".",
"setJSTypeBeforeCast",
"(",
"typeBeforeCast",
")",
";",
"newExpression",
".",
"setJSType",
"(",
"callNode",
".",
"getJSType",
"(",
")",
")",
";",
"}",
"callParentNode",
".",
"replaceChild",
"(",
"callNode",
",",
"newExpression",
")",
";",
"NodeUtil",
".",
"markFunctionsDeleted",
"(",
"callNode",
",",
"compiler",
")",
";",
"return",
"newExpression",
";",
"}"
] | Inline a function that fulfills the requirements of
canInlineReferenceDirectly into the call site, replacing only the CALL
node. | [
"Inline",
"a",
"function",
"that",
"fulfills",
"the",
"requirements",
"of",
"canInlineReferenceDirectly",
"into",
"the",
"call",
"site",
"replacing",
"only",
"the",
"CALL",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L328-L368 |
24,204 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.classifyCallSite | private CallSiteType classifyCallSite(Reference ref) {
Node callNode = ref.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.isNameDeclOrSimpleAssignLhs(callNode, parent)
&& parent.getFirstChild().isName()
// TODO(nicksantos): Remove this once everyone is using
// the CONSTANT_VAR annotation. We know how to remove that.
&& !NodeUtil.isConstantName(parent.getFirstChild())) {
// This is a simple assignment. Example: "x = foo();"
return CallSiteType.SIMPLE_ASSIGNMENT;
} else if (parent.isName()
// TODO(nicksantos): Remove this once everyone is using the CONSTANT_VAR annotation.
&& !NodeUtil.isConstantName(parent)
// Note: not let or const. See InlineFunctionsTest.testInlineFunctions35
&& grandParent.isVar()
&& 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 {
ExpressionDecomposer decomposer = getDecomposer(ref.scope);
switch (decomposer.canExposeExpression(callNode)) {
case MOVABLE:
return CallSiteType.EXPRESSION;
case DECOMPOSABLE:
return CallSiteType.DECOMPOSABLE_EXPRESSION;
case UNDECOMPOSABLE:
break;
}
}
return CallSiteType.UNSUPPORTED;
} | java | private CallSiteType classifyCallSite(Reference ref) {
Node callNode = ref.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.isNameDeclOrSimpleAssignLhs(callNode, parent)
&& parent.getFirstChild().isName()
// TODO(nicksantos): Remove this once everyone is using
// the CONSTANT_VAR annotation. We know how to remove that.
&& !NodeUtil.isConstantName(parent.getFirstChild())) {
// This is a simple assignment. Example: "x = foo();"
return CallSiteType.SIMPLE_ASSIGNMENT;
} else if (parent.isName()
// TODO(nicksantos): Remove this once everyone is using the CONSTANT_VAR annotation.
&& !NodeUtil.isConstantName(parent)
// Note: not let or const. See InlineFunctionsTest.testInlineFunctions35
&& grandParent.isVar()
&& 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 {
ExpressionDecomposer decomposer = getDecomposer(ref.scope);
switch (decomposer.canExposeExpression(callNode)) {
case MOVABLE:
return CallSiteType.EXPRESSION;
case DECOMPOSABLE:
return CallSiteType.DECOMPOSABLE_EXPRESSION;
case UNDECOMPOSABLE:
break;
}
}
return CallSiteType.UNSUPPORTED;
} | [
"private",
"CallSiteType",
"classifyCallSite",
"(",
"Reference",
"ref",
")",
"{",
"Node",
"callNode",
"=",
"ref",
".",
"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",
".",
"isNameDeclOrSimpleAssignLhs",
"(",
"callNode",
",",
"parent",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
".",
"isName",
"(",
")",
"// TODO(nicksantos): Remove this once everyone is using",
"// the CONSTANT_VAR annotation. We know how to remove that.",
"&&",
"!",
"NodeUtil",
".",
"isConstantName",
"(",
"parent",
".",
"getFirstChild",
"(",
")",
")",
")",
"{",
"// This is a simple assignment. Example: \"x = foo();\"",
"return",
"CallSiteType",
".",
"SIMPLE_ASSIGNMENT",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isName",
"(",
")",
"// TODO(nicksantos): Remove this once everyone is using the CONSTANT_VAR annotation.",
"&&",
"!",
"NodeUtil",
".",
"isConstantName",
"(",
"parent",
")",
"// Note: not let or const. See InlineFunctionsTest.testInlineFunctions35",
"&&",
"grandParent",
".",
"isVar",
"(",
")",
"&&",
"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",
"{",
"ExpressionDecomposer",
"decomposer",
"=",
"getDecomposer",
"(",
"ref",
".",
"scope",
")",
";",
"switch",
"(",
"decomposer",
".",
"canExposeExpression",
"(",
"callNode",
")",
")",
"{",
"case",
"MOVABLE",
":",
"return",
"CallSiteType",
".",
"EXPRESSION",
";",
"case",
"DECOMPOSABLE",
":",
"return",
"CallSiteType",
".",
"DECOMPOSABLE_EXPRESSION",
";",
"case",
"UNDECOMPOSABLE",
":",
"break",
";",
"}",
"}",
"return",
"CallSiteType",
".",
"UNSUPPORTED",
";",
"}"
] | Determine which, if any, of the supported types the call site is.
Constant vars are treated differently so that we don't break their
const-ness when we decompose the expression. Once the CONSTANT_VAR
annotation is used everywhere instead of coding conventions, we should just
teach this pass how to remove the annotation. | [
"Determine",
"which",
"if",
"any",
"of",
"the",
"supported",
"types",
"the",
"call",
"site",
"is",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L484-L524 |
24,205 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.maybePrepareCall | void maybePrepareCall(Reference ref) {
CallSiteType callSiteType = classifyCallSite(ref);
callSiteType.prepare(this, ref);
} | java | void maybePrepareCall(Reference ref) {
CallSiteType callSiteType = classifyCallSite(ref);
callSiteType.prepare(this, ref);
} | [
"void",
"maybePrepareCall",
"(",
"Reference",
"ref",
")",
"{",
"CallSiteType",
"callSiteType",
"=",
"classifyCallSite",
"(",
"ref",
")",
";",
"callSiteType",
".",
"prepare",
"(",
"this",
",",
"ref",
")",
";",
"}"
] | If required, rewrite the statement containing the call expression.
@see ExpressionDecomposer#canExposeExpression | [
"If",
"required",
"rewrite",
"the",
"statement",
"containing",
"the",
"call",
"expression",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L535-L538 |
24,206 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.inlineFunction | private Node inlineFunction(Reference ref, Node fnNode, String fnName) {
Node callNode = ref.callNode;
Node parent = callNode.getParent();
Node grandParent = parent.getParent();
// TODO(johnlenz): Consider storing the callSite classification in the
// reference object and passing it in here.
CallSiteType callSiteType = classifyCallSite(ref);
checkArgument(callSiteType != CallSiteType.UNSUPPORTED);
// Store the name for the result. This will be used to
// replace "return expr" statements with "resultName = expr"
// to replace
String resultName = null;
boolean needsDefaultReturnResult = true;
switch (callSiteType) {
case SIMPLE_ASSIGNMENT:
resultName = parent.getFirstChild().getString();
removeConstantVarAnnotation(ref.scope, resultName);
break;
case VAR_DECL_SIMPLE_ASSIGNMENT:
resultName = parent.getString();
removeConstantVarAnnotation(ref.scope, resultName);
break;
case SIMPLE_CALL:
resultName = null; // "foo()" doesn't need a result.
needsDefaultReturnResult = false;
break;
case EXPRESSION:
throw new IllegalStateException(
"Movable expressions must be moved before inlining.");
case DECOMPOSABLE_EXPRESSION:
throw new IllegalStateException(
"Decomposable expressions must be decomposed before inlining.");
default:
throw new IllegalStateException("Unexpected call site type.");
}
FunctionToBlockMutator mutator = new FunctionToBlockMutator(compiler, this.safeNameIdSupplier);
boolean isCallInLoop = NodeUtil.isWithinLoop(callNode);
Node newBlock = mutator.mutate(
fnName, fnNode, callNode, resultName,
needsDefaultReturnResult, isCallInLoop);
NodeUtil.markNewScopesChanged(newBlock, compiler);
// TODO(nicksantos): Create a common mutation function that
// can replace either a VAR name assignment, assignment expression or
// a EXPR_RESULT.
Node greatGrandParent = grandParent.getParent();
switch (callSiteType) {
case VAR_DECL_SIMPLE_ASSIGNMENT:
// Remove the call from the name node.
Node firstChild = parent.removeFirstChild();
NodeUtil.markFunctionsDeleted(firstChild, compiler);
Preconditions.checkState(parent.getFirstChild() == null);
// Add the call, after the VAR.
greatGrandParent.addChildAfter(newBlock, grandParent);
break;
case SIMPLE_ASSIGNMENT:
// The assignment is now part of the inline function so
// replace it completely.
Preconditions.checkState(grandParent.isExprResult());
greatGrandParent.replaceChild(grandParent, newBlock);
NodeUtil.markFunctionsDeleted(grandParent, compiler);
break;
case SIMPLE_CALL:
// If nothing is looking at the result just replace the call.
Preconditions.checkState(parent.isExprResult());
grandParent.replaceChild(parent, newBlock);
NodeUtil.markFunctionsDeleted(parent, compiler);
break;
default:
throw new IllegalStateException("Unexpected call site type.");
}
return newBlock;
} | java | private Node inlineFunction(Reference ref, Node fnNode, String fnName) {
Node callNode = ref.callNode;
Node parent = callNode.getParent();
Node grandParent = parent.getParent();
// TODO(johnlenz): Consider storing the callSite classification in the
// reference object and passing it in here.
CallSiteType callSiteType = classifyCallSite(ref);
checkArgument(callSiteType != CallSiteType.UNSUPPORTED);
// Store the name for the result. This will be used to
// replace "return expr" statements with "resultName = expr"
// to replace
String resultName = null;
boolean needsDefaultReturnResult = true;
switch (callSiteType) {
case SIMPLE_ASSIGNMENT:
resultName = parent.getFirstChild().getString();
removeConstantVarAnnotation(ref.scope, resultName);
break;
case VAR_DECL_SIMPLE_ASSIGNMENT:
resultName = parent.getString();
removeConstantVarAnnotation(ref.scope, resultName);
break;
case SIMPLE_CALL:
resultName = null; // "foo()" doesn't need a result.
needsDefaultReturnResult = false;
break;
case EXPRESSION:
throw new IllegalStateException(
"Movable expressions must be moved before inlining.");
case DECOMPOSABLE_EXPRESSION:
throw new IllegalStateException(
"Decomposable expressions must be decomposed before inlining.");
default:
throw new IllegalStateException("Unexpected call site type.");
}
FunctionToBlockMutator mutator = new FunctionToBlockMutator(compiler, this.safeNameIdSupplier);
boolean isCallInLoop = NodeUtil.isWithinLoop(callNode);
Node newBlock = mutator.mutate(
fnName, fnNode, callNode, resultName,
needsDefaultReturnResult, isCallInLoop);
NodeUtil.markNewScopesChanged(newBlock, compiler);
// TODO(nicksantos): Create a common mutation function that
// can replace either a VAR name assignment, assignment expression or
// a EXPR_RESULT.
Node greatGrandParent = grandParent.getParent();
switch (callSiteType) {
case VAR_DECL_SIMPLE_ASSIGNMENT:
// Remove the call from the name node.
Node firstChild = parent.removeFirstChild();
NodeUtil.markFunctionsDeleted(firstChild, compiler);
Preconditions.checkState(parent.getFirstChild() == null);
// Add the call, after the VAR.
greatGrandParent.addChildAfter(newBlock, grandParent);
break;
case SIMPLE_ASSIGNMENT:
// The assignment is now part of the inline function so
// replace it completely.
Preconditions.checkState(grandParent.isExprResult());
greatGrandParent.replaceChild(grandParent, newBlock);
NodeUtil.markFunctionsDeleted(grandParent, compiler);
break;
case SIMPLE_CALL:
// If nothing is looking at the result just replace the call.
Preconditions.checkState(parent.isExprResult());
grandParent.replaceChild(parent, newBlock);
NodeUtil.markFunctionsDeleted(parent, compiler);
break;
default:
throw new IllegalStateException("Unexpected call site type.");
}
return newBlock;
} | [
"private",
"Node",
"inlineFunction",
"(",
"Reference",
"ref",
",",
"Node",
"fnNode",
",",
"String",
"fnName",
")",
"{",
"Node",
"callNode",
"=",
"ref",
".",
"callNode",
";",
"Node",
"parent",
"=",
"callNode",
".",
"getParent",
"(",
")",
";",
"Node",
"grandParent",
"=",
"parent",
".",
"getParent",
"(",
")",
";",
"// TODO(johnlenz): Consider storing the callSite classification in the",
"// reference object and passing it in here.",
"CallSiteType",
"callSiteType",
"=",
"classifyCallSite",
"(",
"ref",
")",
";",
"checkArgument",
"(",
"callSiteType",
"!=",
"CallSiteType",
".",
"UNSUPPORTED",
")",
";",
"// Store the name for the result. This will be used to",
"// replace \"return expr\" statements with \"resultName = expr\"",
"// to replace",
"String",
"resultName",
"=",
"null",
";",
"boolean",
"needsDefaultReturnResult",
"=",
"true",
";",
"switch",
"(",
"callSiteType",
")",
"{",
"case",
"SIMPLE_ASSIGNMENT",
":",
"resultName",
"=",
"parent",
".",
"getFirstChild",
"(",
")",
".",
"getString",
"(",
")",
";",
"removeConstantVarAnnotation",
"(",
"ref",
".",
"scope",
",",
"resultName",
")",
";",
"break",
";",
"case",
"VAR_DECL_SIMPLE_ASSIGNMENT",
":",
"resultName",
"=",
"parent",
".",
"getString",
"(",
")",
";",
"removeConstantVarAnnotation",
"(",
"ref",
".",
"scope",
",",
"resultName",
")",
";",
"break",
";",
"case",
"SIMPLE_CALL",
":",
"resultName",
"=",
"null",
";",
"// \"foo()\" doesn't need a result.",
"needsDefaultReturnResult",
"=",
"false",
";",
"break",
";",
"case",
"EXPRESSION",
":",
"throw",
"new",
"IllegalStateException",
"(",
"\"Movable expressions must be moved before inlining.\"",
")",
";",
"case",
"DECOMPOSABLE_EXPRESSION",
":",
"throw",
"new",
"IllegalStateException",
"(",
"\"Decomposable expressions must be decomposed before inlining.\"",
")",
";",
"default",
":",
"throw",
"new",
"IllegalStateException",
"(",
"\"Unexpected call site type.\"",
")",
";",
"}",
"FunctionToBlockMutator",
"mutator",
"=",
"new",
"FunctionToBlockMutator",
"(",
"compiler",
",",
"this",
".",
"safeNameIdSupplier",
")",
";",
"boolean",
"isCallInLoop",
"=",
"NodeUtil",
".",
"isWithinLoop",
"(",
"callNode",
")",
";",
"Node",
"newBlock",
"=",
"mutator",
".",
"mutate",
"(",
"fnName",
",",
"fnNode",
",",
"callNode",
",",
"resultName",
",",
"needsDefaultReturnResult",
",",
"isCallInLoop",
")",
";",
"NodeUtil",
".",
"markNewScopesChanged",
"(",
"newBlock",
",",
"compiler",
")",
";",
"// TODO(nicksantos): Create a common mutation function that",
"// can replace either a VAR name assignment, assignment expression or",
"// a EXPR_RESULT.",
"Node",
"greatGrandParent",
"=",
"grandParent",
".",
"getParent",
"(",
")",
";",
"switch",
"(",
"callSiteType",
")",
"{",
"case",
"VAR_DECL_SIMPLE_ASSIGNMENT",
":",
"// Remove the call from the name node.",
"Node",
"firstChild",
"=",
"parent",
".",
"removeFirstChild",
"(",
")",
";",
"NodeUtil",
".",
"markFunctionsDeleted",
"(",
"firstChild",
",",
"compiler",
")",
";",
"Preconditions",
".",
"checkState",
"(",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"null",
")",
";",
"// Add the call, after the VAR.",
"greatGrandParent",
".",
"addChildAfter",
"(",
"newBlock",
",",
"grandParent",
")",
";",
"break",
";",
"case",
"SIMPLE_ASSIGNMENT",
":",
"// The assignment is now part of the inline function so",
"// replace it completely.",
"Preconditions",
".",
"checkState",
"(",
"grandParent",
".",
"isExprResult",
"(",
")",
")",
";",
"greatGrandParent",
".",
"replaceChild",
"(",
"grandParent",
",",
"newBlock",
")",
";",
"NodeUtil",
".",
"markFunctionsDeleted",
"(",
"grandParent",
",",
"compiler",
")",
";",
"break",
";",
"case",
"SIMPLE_CALL",
":",
"// If nothing is looking at the result just replace the call.",
"Preconditions",
".",
"checkState",
"(",
"parent",
".",
"isExprResult",
"(",
")",
")",
";",
"grandParent",
".",
"replaceChild",
"(",
"parent",
",",
"newBlock",
")",
";",
"NodeUtil",
".",
"markFunctionsDeleted",
"(",
"parent",
",",
"compiler",
")",
";",
"break",
";",
"default",
":",
"throw",
"new",
"IllegalStateException",
"(",
"\"Unexpected call site type.\"",
")",
";",
"}",
"return",
"newBlock",
";",
"}"
] | Inline a function which fulfills the requirements of
canInlineReferenceAsStatementBlock into the call site, replacing the
parent expression. | [
"Inline",
"a",
"function",
"which",
"fulfills",
"the",
"requirements",
"of",
"canInlineReferenceAsStatementBlock",
"into",
"the",
"call",
"site",
"replacing",
"the",
"parent",
"expression",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L545-L630 |
24,207 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.isDirectCallNodeReplacementPossible | static 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().isReturn()
&& block.getFirstFirstChild() != null) {
return true;
}
}
return false;
} | java | static 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().isReturn()
&& block.getFirstFirstChild() != null) {
return true;
}
}
return false;
} | [
"static",
"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",
"(",
")",
".",
"isReturn",
"(",
")",
"&&",
"block",
".",
"getFirstFirstChild",
"(",
")",
"!=",
"null",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Checks if the given function matches the criteria for an inlinable
function, and if so, adds it to our set of inlinable functions. | [
"Checks",
"if",
"the",
"given",
"function",
"matches",
"the",
"criteria",
"for",
"an",
"inlinable",
"function",
"and",
"if",
"so",
"adds",
"it",
"to",
"our",
"set",
"of",
"inlinable",
"functions",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L648-L666 |
24,208 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.callMeetsBlockInliningRequirements | private boolean callMeetsBlockInliningRequirements(
Reference ref, final Node fnNode, ImmutableSet<String> namesToAlias) {
// Note: functions that contain function definitions are filtered out
// in isCandidateFunction.
// TODO(johnlenz): Determining if the called function contains VARs
// or if the caller contains inner functions accounts for 20% of the
// run-time cost of this pass.
// Don't inline functions with var declarations into a scope with inner
// functions as the new vars would leak into the inner function and
// cause memory leaks.
boolean fnContainsVars = NodeUtil.has(
NodeUtil.getFunctionBody(fnNode),
new NodeUtil.MatchDeclaration(),
new NodeUtil.MatchShallowStatement());
boolean forbidTemps = false;
if (!ref.scope.getClosestHoistScope().isGlobal()) {
Node fnCallerBody = ref.scope.getClosestHoistScope().getRootNode();
// Don't allow any new vars into a scope that contains eval or one
// that contains functions (excluding the function being inlined).
Predicate<Node> match = new Predicate<Node>(){
@Override
public boolean apply(Node n) {
if (n.isName()) {
return n.getString().equals("eval");
}
if (!assumeMinimumCapture && n.isFunction()) {
return n != fnNode;
}
return false;
}
};
forbidTemps = NodeUtil.has(fnCallerBody, match, NodeUtil.MATCH_NOT_FUNCTION);
}
if (fnContainsVars && forbidTemps) {
return false;
}
// If the caller contains functions or evals, verify we aren't adding any
// additional VAR declarations because aliasing is needed.
if (forbidTemps) {
ImmutableMap<String, Node> args =
FunctionArgumentInjector.getFunctionCallParameterMap(
fnNode, ref.callNode, this.safeNameIdSupplier);
boolean hasArgs = !args.isEmpty();
if (hasArgs) {
// Limit the inlining
Set<String> allNamesToAlias = new HashSet<>(namesToAlias);
FunctionArgumentInjector.maybeAddTempsForCallArguments(
compiler, fnNode, args, allNamesToAlias, compiler.getCodingConvention());
if (!allNamesToAlias.isEmpty()) {
return false;
}
}
}
return true;
} | java | private boolean callMeetsBlockInliningRequirements(
Reference ref, final Node fnNode, ImmutableSet<String> namesToAlias) {
// Note: functions that contain function definitions are filtered out
// in isCandidateFunction.
// TODO(johnlenz): Determining if the called function contains VARs
// or if the caller contains inner functions accounts for 20% of the
// run-time cost of this pass.
// Don't inline functions with var declarations into a scope with inner
// functions as the new vars would leak into the inner function and
// cause memory leaks.
boolean fnContainsVars = NodeUtil.has(
NodeUtil.getFunctionBody(fnNode),
new NodeUtil.MatchDeclaration(),
new NodeUtil.MatchShallowStatement());
boolean forbidTemps = false;
if (!ref.scope.getClosestHoistScope().isGlobal()) {
Node fnCallerBody = ref.scope.getClosestHoistScope().getRootNode();
// Don't allow any new vars into a scope that contains eval or one
// that contains functions (excluding the function being inlined).
Predicate<Node> match = new Predicate<Node>(){
@Override
public boolean apply(Node n) {
if (n.isName()) {
return n.getString().equals("eval");
}
if (!assumeMinimumCapture && n.isFunction()) {
return n != fnNode;
}
return false;
}
};
forbidTemps = NodeUtil.has(fnCallerBody, match, NodeUtil.MATCH_NOT_FUNCTION);
}
if (fnContainsVars && forbidTemps) {
return false;
}
// If the caller contains functions or evals, verify we aren't adding any
// additional VAR declarations because aliasing is needed.
if (forbidTemps) {
ImmutableMap<String, Node> args =
FunctionArgumentInjector.getFunctionCallParameterMap(
fnNode, ref.callNode, this.safeNameIdSupplier);
boolean hasArgs = !args.isEmpty();
if (hasArgs) {
// Limit the inlining
Set<String> allNamesToAlias = new HashSet<>(namesToAlias);
FunctionArgumentInjector.maybeAddTempsForCallArguments(
compiler, fnNode, args, allNamesToAlias, compiler.getCodingConvention());
if (!allNamesToAlias.isEmpty()) {
return false;
}
}
}
return true;
} | [
"private",
"boolean",
"callMeetsBlockInliningRequirements",
"(",
"Reference",
"ref",
",",
"final",
"Node",
"fnNode",
",",
"ImmutableSet",
"<",
"String",
">",
"namesToAlias",
")",
"{",
"// Note: functions that contain function definitions are filtered out",
"// in isCandidateFunction.",
"// TODO(johnlenz): Determining if the called function contains VARs",
"// or if the caller contains inner functions accounts for 20% of the",
"// run-time cost of this pass.",
"// Don't inline functions with var declarations into a scope with inner",
"// functions as the new vars would leak into the inner function and",
"// cause memory leaks.",
"boolean",
"fnContainsVars",
"=",
"NodeUtil",
".",
"has",
"(",
"NodeUtil",
".",
"getFunctionBody",
"(",
"fnNode",
")",
",",
"new",
"NodeUtil",
".",
"MatchDeclaration",
"(",
")",
",",
"new",
"NodeUtil",
".",
"MatchShallowStatement",
"(",
")",
")",
";",
"boolean",
"forbidTemps",
"=",
"false",
";",
"if",
"(",
"!",
"ref",
".",
"scope",
".",
"getClosestHoistScope",
"(",
")",
".",
"isGlobal",
"(",
")",
")",
"{",
"Node",
"fnCallerBody",
"=",
"ref",
".",
"scope",
".",
"getClosestHoistScope",
"(",
")",
".",
"getRootNode",
"(",
")",
";",
"// Don't allow any new vars into a scope that contains eval or one",
"// that contains functions (excluding the function being inlined).",
"Predicate",
"<",
"Node",
">",
"match",
"=",
"new",
"Predicate",
"<",
"Node",
">",
"(",
")",
"{",
"@",
"Override",
"public",
"boolean",
"apply",
"(",
"Node",
"n",
")",
"{",
"if",
"(",
"n",
".",
"isName",
"(",
")",
")",
"{",
"return",
"n",
".",
"getString",
"(",
")",
".",
"equals",
"(",
"\"eval\"",
")",
";",
"}",
"if",
"(",
"!",
"assumeMinimumCapture",
"&&",
"n",
".",
"isFunction",
"(",
")",
")",
"{",
"return",
"n",
"!=",
"fnNode",
";",
"}",
"return",
"false",
";",
"}",
"}",
";",
"forbidTemps",
"=",
"NodeUtil",
".",
"has",
"(",
"fnCallerBody",
",",
"match",
",",
"NodeUtil",
".",
"MATCH_NOT_FUNCTION",
")",
";",
"}",
"if",
"(",
"fnContainsVars",
"&&",
"forbidTemps",
")",
"{",
"return",
"false",
";",
"}",
"// If the caller contains functions or evals, verify we aren't adding any",
"// additional VAR declarations because aliasing is needed.",
"if",
"(",
"forbidTemps",
")",
"{",
"ImmutableMap",
"<",
"String",
",",
"Node",
">",
"args",
"=",
"FunctionArgumentInjector",
".",
"getFunctionCallParameterMap",
"(",
"fnNode",
",",
"ref",
".",
"callNode",
",",
"this",
".",
"safeNameIdSupplier",
")",
";",
"boolean",
"hasArgs",
"=",
"!",
"args",
".",
"isEmpty",
"(",
")",
";",
"if",
"(",
"hasArgs",
")",
"{",
"// Limit the inlining",
"Set",
"<",
"String",
">",
"allNamesToAlias",
"=",
"new",
"HashSet",
"<>",
"(",
"namesToAlias",
")",
";",
"FunctionArgumentInjector",
".",
"maybeAddTempsForCallArguments",
"(",
"compiler",
",",
"fnNode",
",",
"args",
",",
"allNamesToAlias",
",",
"compiler",
".",
"getCodingConvention",
"(",
")",
")",
";",
"if",
"(",
"!",
"allNamesToAlias",
".",
"isEmpty",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"}",
"return",
"true",
";",
"}"
] | Determines whether a function can be inlined at a particular call site.
- Don't inline if the calling function contains an inner function and
inlining would introduce new globals. | [
"Determines",
"whether",
"a",
"function",
"can",
"be",
"inlined",
"at",
"a",
"particular",
"call",
"site",
".",
"-",
"Don",
"t",
"inline",
"if",
"the",
"calling",
"function",
"contains",
"an",
"inner",
"function",
"and",
"inlining",
"would",
"introduce",
"new",
"globals",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L715-L775 |
24,209 | google/closure-compiler | src/com/google/javascript/jscomp/FunctionInjector.java | FunctionInjector.inliningLowersCost | boolean inliningLowersCost(
JSModule fnModule, Node fnNode, Collection<? extends Reference> refs,
Set<String> namesToAlias, boolean isRemovable, boolean referencesThis) {
int referenceCount = refs.size();
if (referenceCount == 0) {
return true;
}
int referencesUsingBlockInlining = 0;
boolean checkModules = isRemovable && fnModule != null;
JSModuleGraph moduleGraph = compiler.getModuleGraph();
for (Reference ref : refs) {
if (ref.mode == InliningMode.BLOCK) {
referencesUsingBlockInlining++;
}
// Check if any of the references cross the module boundaries.
if (checkModules && ref.module != null) {
if (ref.module != fnModule && !moduleGraph.dependsOn(ref.module, fnModule)) {
// Calculate the cost as if the function were non-removable,
// if it still lowers the cost inline it.
isRemovable = false;
checkModules = false; // no need to check additional modules.
}
}
}
int referencesUsingDirectInlining = referenceCount - referencesUsingBlockInlining;
// Don't bother calculating the cost of function for simple functions where
// possible.
// However, when inlining a complex function, even a single reference may be
// larger than the original function if there are many returns (resulting
// in additional assignments) or many parameters that need to be aliased
// so use the cost estimating.
if (referenceCount == 1 && isRemovable && referencesUsingDirectInlining == 1) {
return true;
}
int callCost = estimateCallCost(fnNode, referencesThis);
int overallCallCost = callCost * referenceCount;
int costDeltaDirect = inlineCostDelta(fnNode, namesToAlias, InliningMode.DIRECT);
int costDeltaBlock = inlineCostDelta(fnNode, namesToAlias, InliningMode.BLOCK);
return doesLowerCost(fnNode, overallCallCost,
referencesUsingDirectInlining, costDeltaDirect,
referencesUsingBlockInlining, costDeltaBlock,
isRemovable);
} | java | boolean inliningLowersCost(
JSModule fnModule, Node fnNode, Collection<? extends Reference> refs,
Set<String> namesToAlias, boolean isRemovable, boolean referencesThis) {
int referenceCount = refs.size();
if (referenceCount == 0) {
return true;
}
int referencesUsingBlockInlining = 0;
boolean checkModules = isRemovable && fnModule != null;
JSModuleGraph moduleGraph = compiler.getModuleGraph();
for (Reference ref : refs) {
if (ref.mode == InliningMode.BLOCK) {
referencesUsingBlockInlining++;
}
// Check if any of the references cross the module boundaries.
if (checkModules && ref.module != null) {
if (ref.module != fnModule && !moduleGraph.dependsOn(ref.module, fnModule)) {
// Calculate the cost as if the function were non-removable,
// if it still lowers the cost inline it.
isRemovable = false;
checkModules = false; // no need to check additional modules.
}
}
}
int referencesUsingDirectInlining = referenceCount - referencesUsingBlockInlining;
// Don't bother calculating the cost of function for simple functions where
// possible.
// However, when inlining a complex function, even a single reference may be
// larger than the original function if there are many returns (resulting
// in additional assignments) or many parameters that need to be aliased
// so use the cost estimating.
if (referenceCount == 1 && isRemovable && referencesUsingDirectInlining == 1) {
return true;
}
int callCost = estimateCallCost(fnNode, referencesThis);
int overallCallCost = callCost * referenceCount;
int costDeltaDirect = inlineCostDelta(fnNode, namesToAlias, InliningMode.DIRECT);
int costDeltaBlock = inlineCostDelta(fnNode, namesToAlias, InliningMode.BLOCK);
return doesLowerCost(fnNode, overallCallCost,
referencesUsingDirectInlining, costDeltaDirect,
referencesUsingBlockInlining, costDeltaBlock,
isRemovable);
} | [
"boolean",
"inliningLowersCost",
"(",
"JSModule",
"fnModule",
",",
"Node",
"fnNode",
",",
"Collection",
"<",
"?",
"extends",
"Reference",
">",
"refs",
",",
"Set",
"<",
"String",
">",
"namesToAlias",
",",
"boolean",
"isRemovable",
",",
"boolean",
"referencesThis",
")",
"{",
"int",
"referenceCount",
"=",
"refs",
".",
"size",
"(",
")",
";",
"if",
"(",
"referenceCount",
"==",
"0",
")",
"{",
"return",
"true",
";",
"}",
"int",
"referencesUsingBlockInlining",
"=",
"0",
";",
"boolean",
"checkModules",
"=",
"isRemovable",
"&&",
"fnModule",
"!=",
"null",
";",
"JSModuleGraph",
"moduleGraph",
"=",
"compiler",
".",
"getModuleGraph",
"(",
")",
";",
"for",
"(",
"Reference",
"ref",
":",
"refs",
")",
"{",
"if",
"(",
"ref",
".",
"mode",
"==",
"InliningMode",
".",
"BLOCK",
")",
"{",
"referencesUsingBlockInlining",
"++",
";",
"}",
"// Check if any of the references cross the module boundaries.",
"if",
"(",
"checkModules",
"&&",
"ref",
".",
"module",
"!=",
"null",
")",
"{",
"if",
"(",
"ref",
".",
"module",
"!=",
"fnModule",
"&&",
"!",
"moduleGraph",
".",
"dependsOn",
"(",
"ref",
".",
"module",
",",
"fnModule",
")",
")",
"{",
"// Calculate the cost as if the function were non-removable,",
"// if it still lowers the cost inline it.",
"isRemovable",
"=",
"false",
";",
"checkModules",
"=",
"false",
";",
"// no need to check additional modules.",
"}",
"}",
"}",
"int",
"referencesUsingDirectInlining",
"=",
"referenceCount",
"-",
"referencesUsingBlockInlining",
";",
"// Don't bother calculating the cost of function for simple functions where",
"// possible.",
"// However, when inlining a complex function, even a single reference may be",
"// larger than the original function if there are many returns (resulting",
"// in additional assignments) or many parameters that need to be aliased",
"// so use the cost estimating.",
"if",
"(",
"referenceCount",
"==",
"1",
"&&",
"isRemovable",
"&&",
"referencesUsingDirectInlining",
"==",
"1",
")",
"{",
"return",
"true",
";",
"}",
"int",
"callCost",
"=",
"estimateCallCost",
"(",
"fnNode",
",",
"referencesThis",
")",
";",
"int",
"overallCallCost",
"=",
"callCost",
"*",
"referenceCount",
";",
"int",
"costDeltaDirect",
"=",
"inlineCostDelta",
"(",
"fnNode",
",",
"namesToAlias",
",",
"InliningMode",
".",
"DIRECT",
")",
";",
"int",
"costDeltaBlock",
"=",
"inlineCostDelta",
"(",
"fnNode",
",",
"namesToAlias",
",",
"InliningMode",
".",
"BLOCK",
")",
";",
"return",
"doesLowerCost",
"(",
"fnNode",
",",
"overallCallCost",
",",
"referencesUsingDirectInlining",
",",
"costDeltaDirect",
",",
"referencesUsingBlockInlining",
",",
"costDeltaBlock",
",",
"isRemovable",
")",
";",
"}"
] | Determine if inlining the function is likely to reduce the code size.
@param namesToAlias | [
"Determine",
"if",
"inlining",
"the",
"function",
"is",
"likely",
"to",
"reduce",
"the",
"code",
"size",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/FunctionInjector.java#L836-L887 |
24,210 | google/closure-compiler | src/com/google/debugging/sourcemap/SourceMapConsumerV3.java | SourceMapConsumerV3.search | private static int search(ArrayList<Entry> entries, int target, int start, int end) {
while (true) {
int mid = ((end - start) / 2) + start;
int compare = compareEntry(entries, mid, target);
if (compare == 0) {
return mid;
} else if (compare < 0) {
// it is in the upper half
start = mid + 1;
if (start > end) {
return end;
}
} else {
// it is in the lower half
end = mid - 1;
if (end < start) {
return end;
}
}
}
} | java | private static int search(ArrayList<Entry> entries, int target, int start, int end) {
while (true) {
int mid = ((end - start) / 2) + start;
int compare = compareEntry(entries, mid, target);
if (compare == 0) {
return mid;
} else if (compare < 0) {
// it is in the upper half
start = mid + 1;
if (start > end) {
return end;
}
} else {
// it is in the lower half
end = mid - 1;
if (end < start) {
return end;
}
}
}
} | [
"private",
"static",
"int",
"search",
"(",
"ArrayList",
"<",
"Entry",
">",
"entries",
",",
"int",
"target",
",",
"int",
"start",
",",
"int",
"end",
")",
"{",
"while",
"(",
"true",
")",
"{",
"int",
"mid",
"=",
"(",
"(",
"end",
"-",
"start",
")",
"/",
"2",
")",
"+",
"start",
";",
"int",
"compare",
"=",
"compareEntry",
"(",
"entries",
",",
"mid",
",",
"target",
")",
";",
"if",
"(",
"compare",
"==",
"0",
")",
"{",
"return",
"mid",
";",
"}",
"else",
"if",
"(",
"compare",
"<",
"0",
")",
"{",
"// it is in the upper half",
"start",
"=",
"mid",
"+",
"1",
";",
"if",
"(",
"start",
">",
"end",
")",
"{",
"return",
"end",
";",
"}",
"}",
"else",
"{",
"// it is in the lower half",
"end",
"=",
"mid",
"-",
"1",
";",
"if",
"(",
"end",
"<",
"start",
")",
"{",
"return",
"end",
";",
"}",
"}",
"}",
"}"
] | Perform a binary search on the array to find a section that covers
the target column. | [
"Perform",
"a",
"binary",
"search",
"on",
"the",
"array",
"to",
"find",
"a",
"section",
"that",
"covers",
"the",
"target",
"column",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L396-L416 |
24,211 | google/closure-compiler | src/com/google/debugging/sourcemap/SourceMapConsumerV3.java | SourceMapConsumerV3.compareEntry | private static int compareEntry(ArrayList<Entry> entries, int entry, int target) {
return entries.get(entry).getGeneratedColumn() - target;
} | java | private static int compareEntry(ArrayList<Entry> entries, int entry, int target) {
return entries.get(entry).getGeneratedColumn() - target;
} | [
"private",
"static",
"int",
"compareEntry",
"(",
"ArrayList",
"<",
"Entry",
">",
"entries",
",",
"int",
"entry",
",",
"int",
"target",
")",
"{",
"return",
"entries",
".",
"get",
"(",
"entry",
")",
".",
"getGeneratedColumn",
"(",
")",
"-",
"target",
";",
"}"
] | Compare an array entry's column value to the target column value. | [
"Compare",
"an",
"array",
"entry",
"s",
"column",
"value",
"to",
"the",
"target",
"column",
"value",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L421-L423 |
24,212 | google/closure-compiler | src/com/google/debugging/sourcemap/SourceMapConsumerV3.java | SourceMapConsumerV3.getPreviousMapping | private OriginalMapping getPreviousMapping(int lineNumber) {
do {
if (lineNumber == 0) {
return null;
}
lineNumber--;
} while (lines.get(lineNumber) == null);
ArrayList<Entry> entries = lines.get(lineNumber);
return getOriginalMappingForEntry(Iterables.getLast(entries));
} | java | private OriginalMapping getPreviousMapping(int lineNumber) {
do {
if (lineNumber == 0) {
return null;
}
lineNumber--;
} while (lines.get(lineNumber) == null);
ArrayList<Entry> entries = lines.get(lineNumber);
return getOriginalMappingForEntry(Iterables.getLast(entries));
} | [
"private",
"OriginalMapping",
"getPreviousMapping",
"(",
"int",
"lineNumber",
")",
"{",
"do",
"{",
"if",
"(",
"lineNumber",
"==",
"0",
")",
"{",
"return",
"null",
";",
"}",
"lineNumber",
"--",
";",
"}",
"while",
"(",
"lines",
".",
"get",
"(",
"lineNumber",
")",
"==",
"null",
")",
";",
"ArrayList",
"<",
"Entry",
">",
"entries",
"=",
"lines",
".",
"get",
"(",
"lineNumber",
")",
";",
"return",
"getOriginalMappingForEntry",
"(",
"Iterables",
".",
"getLast",
"(",
"entries",
")",
")",
";",
"}"
] | Returns the mapping entry that proceeds the supplied line or null if no
such entry exists. | [
"Returns",
"the",
"mapping",
"entry",
"that",
"proceeds",
"the",
"supplied",
"line",
"or",
"null",
"if",
"no",
"such",
"entry",
"exists",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L429-L438 |
24,213 | google/closure-compiler | src/com/google/debugging/sourcemap/SourceMapConsumerV3.java | SourceMapConsumerV3.getOriginalMappingForEntry | private OriginalMapping getOriginalMappingForEntry(Entry entry) {
if (entry.getSourceFileId() == UNMAPPED) {
return null;
} else {
// Adjust the line/column here to be start at 1.
Builder x = OriginalMapping.newBuilder()
.setOriginalFile(sources[entry.getSourceFileId()])
.setLineNumber(entry.getSourceLine() + 1)
.setColumnPosition(entry.getSourceColumn() + 1);
if (entry.getNameId() != UNMAPPED) {
x.setIdentifier(names[entry.getNameId()]);
}
return x.build();
}
} | java | private OriginalMapping getOriginalMappingForEntry(Entry entry) {
if (entry.getSourceFileId() == UNMAPPED) {
return null;
} else {
// Adjust the line/column here to be start at 1.
Builder x = OriginalMapping.newBuilder()
.setOriginalFile(sources[entry.getSourceFileId()])
.setLineNumber(entry.getSourceLine() + 1)
.setColumnPosition(entry.getSourceColumn() + 1);
if (entry.getNameId() != UNMAPPED) {
x.setIdentifier(names[entry.getNameId()]);
}
return x.build();
}
} | [
"private",
"OriginalMapping",
"getOriginalMappingForEntry",
"(",
"Entry",
"entry",
")",
"{",
"if",
"(",
"entry",
".",
"getSourceFileId",
"(",
")",
"==",
"UNMAPPED",
")",
"{",
"return",
"null",
";",
"}",
"else",
"{",
"// Adjust the line/column here to be start at 1.",
"Builder",
"x",
"=",
"OriginalMapping",
".",
"newBuilder",
"(",
")",
".",
"setOriginalFile",
"(",
"sources",
"[",
"entry",
".",
"getSourceFileId",
"(",
")",
"]",
")",
".",
"setLineNumber",
"(",
"entry",
".",
"getSourceLine",
"(",
")",
"+",
"1",
")",
".",
"setColumnPosition",
"(",
"entry",
".",
"getSourceColumn",
"(",
")",
"+",
"1",
")",
";",
"if",
"(",
"entry",
".",
"getNameId",
"(",
")",
"!=",
"UNMAPPED",
")",
"{",
"x",
".",
"setIdentifier",
"(",
"names",
"[",
"entry",
".",
"getNameId",
"(",
")",
"]",
")",
";",
"}",
"return",
"x",
".",
"build",
"(",
")",
";",
"}",
"}"
] | Creates an "OriginalMapping" object for the given entry object. | [
"Creates",
"an",
"OriginalMapping",
"object",
"for",
"the",
"given",
"entry",
"object",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L443-L457 |
24,214 | google/closure-compiler | src/com/google/debugging/sourcemap/SourceMapConsumerV3.java | SourceMapConsumerV3.createReverseMapping | private void createReverseMapping() {
reverseSourceMapping = new HashMap<>();
for (int targetLine = 0; targetLine < lines.size(); targetLine++) {
ArrayList<Entry> entries = lines.get(targetLine);
if (entries != null) {
for (Entry entry : entries) {
if (entry.getSourceFileId() != UNMAPPED
&& entry.getSourceLine() != UNMAPPED) {
String originalFile = sources[entry.getSourceFileId()];
if (!reverseSourceMapping.containsKey(originalFile)) {
reverseSourceMapping.put(originalFile,
new HashMap<Integer, Collection<OriginalMapping>>());
}
Map<Integer, Collection<OriginalMapping>> lineToCollectionMap =
reverseSourceMapping.get(originalFile);
int sourceLine = entry.getSourceLine();
if (!lineToCollectionMap.containsKey(sourceLine)) {
lineToCollectionMap.put(sourceLine,
new ArrayList<OriginalMapping>(1));
}
Collection<OriginalMapping> mappings =
lineToCollectionMap.get(sourceLine);
Builder builder = OriginalMapping.newBuilder().setLineNumber(
targetLine).setColumnPosition(entry.getGeneratedColumn());
mappings.add(builder.build());
}
}
}
}
} | java | private void createReverseMapping() {
reverseSourceMapping = new HashMap<>();
for (int targetLine = 0; targetLine < lines.size(); targetLine++) {
ArrayList<Entry> entries = lines.get(targetLine);
if (entries != null) {
for (Entry entry : entries) {
if (entry.getSourceFileId() != UNMAPPED
&& entry.getSourceLine() != UNMAPPED) {
String originalFile = sources[entry.getSourceFileId()];
if (!reverseSourceMapping.containsKey(originalFile)) {
reverseSourceMapping.put(originalFile,
new HashMap<Integer, Collection<OriginalMapping>>());
}
Map<Integer, Collection<OriginalMapping>> lineToCollectionMap =
reverseSourceMapping.get(originalFile);
int sourceLine = entry.getSourceLine();
if (!lineToCollectionMap.containsKey(sourceLine)) {
lineToCollectionMap.put(sourceLine,
new ArrayList<OriginalMapping>(1));
}
Collection<OriginalMapping> mappings =
lineToCollectionMap.get(sourceLine);
Builder builder = OriginalMapping.newBuilder().setLineNumber(
targetLine).setColumnPosition(entry.getGeneratedColumn());
mappings.add(builder.build());
}
}
}
}
} | [
"private",
"void",
"createReverseMapping",
"(",
")",
"{",
"reverseSourceMapping",
"=",
"new",
"HashMap",
"<>",
"(",
")",
";",
"for",
"(",
"int",
"targetLine",
"=",
"0",
";",
"targetLine",
"<",
"lines",
".",
"size",
"(",
")",
";",
"targetLine",
"++",
")",
"{",
"ArrayList",
"<",
"Entry",
">",
"entries",
"=",
"lines",
".",
"get",
"(",
"targetLine",
")",
";",
"if",
"(",
"entries",
"!=",
"null",
")",
"{",
"for",
"(",
"Entry",
"entry",
":",
"entries",
")",
"{",
"if",
"(",
"entry",
".",
"getSourceFileId",
"(",
")",
"!=",
"UNMAPPED",
"&&",
"entry",
".",
"getSourceLine",
"(",
")",
"!=",
"UNMAPPED",
")",
"{",
"String",
"originalFile",
"=",
"sources",
"[",
"entry",
".",
"getSourceFileId",
"(",
")",
"]",
";",
"if",
"(",
"!",
"reverseSourceMapping",
".",
"containsKey",
"(",
"originalFile",
")",
")",
"{",
"reverseSourceMapping",
".",
"put",
"(",
"originalFile",
",",
"new",
"HashMap",
"<",
"Integer",
",",
"Collection",
"<",
"OriginalMapping",
">",
">",
"(",
")",
")",
";",
"}",
"Map",
"<",
"Integer",
",",
"Collection",
"<",
"OriginalMapping",
">",
">",
"lineToCollectionMap",
"=",
"reverseSourceMapping",
".",
"get",
"(",
"originalFile",
")",
";",
"int",
"sourceLine",
"=",
"entry",
".",
"getSourceLine",
"(",
")",
";",
"if",
"(",
"!",
"lineToCollectionMap",
".",
"containsKey",
"(",
"sourceLine",
")",
")",
"{",
"lineToCollectionMap",
".",
"put",
"(",
"sourceLine",
",",
"new",
"ArrayList",
"<",
"OriginalMapping",
">",
"(",
"1",
")",
")",
";",
"}",
"Collection",
"<",
"OriginalMapping",
">",
"mappings",
"=",
"lineToCollectionMap",
".",
"get",
"(",
"sourceLine",
")",
";",
"Builder",
"builder",
"=",
"OriginalMapping",
".",
"newBuilder",
"(",
")",
".",
"setLineNumber",
"(",
"targetLine",
")",
".",
"setColumnPosition",
"(",
"entry",
".",
"getGeneratedColumn",
"(",
")",
")",
";",
"mappings",
".",
"add",
"(",
"builder",
".",
"build",
"(",
")",
")",
";",
"}",
"}",
"}",
"}",
"}"
] | Reverse the source map; the created mapping will allow us to quickly go
from a source file and line number to a collection of target
OriginalMappings. | [
"Reverse",
"the",
"source",
"map",
";",
"the",
"created",
"mapping",
"will",
"allow",
"us",
"to",
"quickly",
"go",
"from",
"a",
"source",
"file",
"and",
"line",
"number",
"to",
"a",
"collection",
"of",
"target",
"OriginalMappings",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L464-L502 |
24,215 | google/closure-compiler | src/com/google/javascript/jscomp/RewriteJsonToModule.java | RewriteJsonToModule.visitScript | private void visitScript(NodeTraversal t, Node n) {
if (!n.hasOneChild() || !n.getFirstChild().isExprResult()) {
compiler.report(JSError.make(n, JSON_UNEXPECTED_TOKEN));
return;
}
Node jsonObject = n.getFirstFirstChild().detach();
n.removeFirstChild();
String moduleName = t.getInput().getPath().toModuleName();
n.addChildToFront(
IR.var(IR.name(moduleName).useSourceInfoFrom(jsonObject), jsonObject)
.useSourceInfoFrom(jsonObject));
n.addChildToFront(
IR.exprResult(
IR.call(IR.getprop(IR.name("goog"), IR.string("provide")), IR.string(moduleName)))
.useSourceInfoIfMissingFromForTree(n));
String inputPath = t.getInput().getSourceFile().getOriginalPath();
if (inputPath.endsWith("/package.json") && jsonObject.isObjectLit()) {
List<String> possibleMainEntries = compiler.getOptions().getPackageJsonEntryNames();
for (String entryName : possibleMainEntries) {
Node entry = NodeUtil.getFirstPropMatchingKey(jsonObject, entryName);
if (entry != null && (entry.isString() || entry.isObjectLit())) {
String dirName = inputPath.substring(0, inputPath.length() - "package.json".length());
if (entry.isString()) {
packageJsonMainEntries.put(inputPath, dirName + entry.getString());
break;
} else if (entry.isObjectLit()) {
checkState(entryName.equals("browser"), entryName);
// don't break if we're processing a browser field that is an object literal
// because one of its entries may override the package.json main, which
// we will get in the next iteration.
processBrowserFieldAdvancedUsage(dirName, entry);
}
}
}
}
t.reportCodeChange();
} | java | private void visitScript(NodeTraversal t, Node n) {
if (!n.hasOneChild() || !n.getFirstChild().isExprResult()) {
compiler.report(JSError.make(n, JSON_UNEXPECTED_TOKEN));
return;
}
Node jsonObject = n.getFirstFirstChild().detach();
n.removeFirstChild();
String moduleName = t.getInput().getPath().toModuleName();
n.addChildToFront(
IR.var(IR.name(moduleName).useSourceInfoFrom(jsonObject), jsonObject)
.useSourceInfoFrom(jsonObject));
n.addChildToFront(
IR.exprResult(
IR.call(IR.getprop(IR.name("goog"), IR.string("provide")), IR.string(moduleName)))
.useSourceInfoIfMissingFromForTree(n));
String inputPath = t.getInput().getSourceFile().getOriginalPath();
if (inputPath.endsWith("/package.json") && jsonObject.isObjectLit()) {
List<String> possibleMainEntries = compiler.getOptions().getPackageJsonEntryNames();
for (String entryName : possibleMainEntries) {
Node entry = NodeUtil.getFirstPropMatchingKey(jsonObject, entryName);
if (entry != null && (entry.isString() || entry.isObjectLit())) {
String dirName = inputPath.substring(0, inputPath.length() - "package.json".length());
if (entry.isString()) {
packageJsonMainEntries.put(inputPath, dirName + entry.getString());
break;
} else if (entry.isObjectLit()) {
checkState(entryName.equals("browser"), entryName);
// don't break if we're processing a browser field that is an object literal
// because one of its entries may override the package.json main, which
// we will get in the next iteration.
processBrowserFieldAdvancedUsage(dirName, entry);
}
}
}
}
t.reportCodeChange();
} | [
"private",
"void",
"visitScript",
"(",
"NodeTraversal",
"t",
",",
"Node",
"n",
")",
"{",
"if",
"(",
"!",
"n",
".",
"hasOneChild",
"(",
")",
"||",
"!",
"n",
".",
"getFirstChild",
"(",
")",
".",
"isExprResult",
"(",
")",
")",
"{",
"compiler",
".",
"report",
"(",
"JSError",
".",
"make",
"(",
"n",
",",
"JSON_UNEXPECTED_TOKEN",
")",
")",
";",
"return",
";",
"}",
"Node",
"jsonObject",
"=",
"n",
".",
"getFirstFirstChild",
"(",
")",
".",
"detach",
"(",
")",
";",
"n",
".",
"removeFirstChild",
"(",
")",
";",
"String",
"moduleName",
"=",
"t",
".",
"getInput",
"(",
")",
".",
"getPath",
"(",
")",
".",
"toModuleName",
"(",
")",
";",
"n",
".",
"addChildToFront",
"(",
"IR",
".",
"var",
"(",
"IR",
".",
"name",
"(",
"moduleName",
")",
".",
"useSourceInfoFrom",
"(",
"jsonObject",
")",
",",
"jsonObject",
")",
".",
"useSourceInfoFrom",
"(",
"jsonObject",
")",
")",
";",
"n",
".",
"addChildToFront",
"(",
"IR",
".",
"exprResult",
"(",
"IR",
".",
"call",
"(",
"IR",
".",
"getprop",
"(",
"IR",
".",
"name",
"(",
"\"goog\"",
")",
",",
"IR",
".",
"string",
"(",
"\"provide\"",
")",
")",
",",
"IR",
".",
"string",
"(",
"moduleName",
")",
")",
")",
".",
"useSourceInfoIfMissingFromForTree",
"(",
"n",
")",
")",
";",
"String",
"inputPath",
"=",
"t",
".",
"getInput",
"(",
")",
".",
"getSourceFile",
"(",
")",
".",
"getOriginalPath",
"(",
")",
";",
"if",
"(",
"inputPath",
".",
"endsWith",
"(",
"\"/package.json\"",
")",
"&&",
"jsonObject",
".",
"isObjectLit",
"(",
")",
")",
"{",
"List",
"<",
"String",
">",
"possibleMainEntries",
"=",
"compiler",
".",
"getOptions",
"(",
")",
".",
"getPackageJsonEntryNames",
"(",
")",
";",
"for",
"(",
"String",
"entryName",
":",
"possibleMainEntries",
")",
"{",
"Node",
"entry",
"=",
"NodeUtil",
".",
"getFirstPropMatchingKey",
"(",
"jsonObject",
",",
"entryName",
")",
";",
"if",
"(",
"entry",
"!=",
"null",
"&&",
"(",
"entry",
".",
"isString",
"(",
")",
"||",
"entry",
".",
"isObjectLit",
"(",
")",
")",
")",
"{",
"String",
"dirName",
"=",
"inputPath",
".",
"substring",
"(",
"0",
",",
"inputPath",
".",
"length",
"(",
")",
"-",
"\"package.json\"",
".",
"length",
"(",
")",
")",
";",
"if",
"(",
"entry",
".",
"isString",
"(",
")",
")",
"{",
"packageJsonMainEntries",
".",
"put",
"(",
"inputPath",
",",
"dirName",
"+",
"entry",
".",
"getString",
"(",
")",
")",
";",
"break",
";",
"}",
"else",
"if",
"(",
"entry",
".",
"isObjectLit",
"(",
")",
")",
"{",
"checkState",
"(",
"entryName",
".",
"equals",
"(",
"\"browser\"",
")",
",",
"entryName",
")",
";",
"// don't break if we're processing a browser field that is an object literal",
"// because one of its entries may override the package.json main, which",
"// we will get in the next iteration.",
"processBrowserFieldAdvancedUsage",
"(",
"dirName",
",",
"entry",
")",
";",
"}",
"}",
"}",
"}",
"t",
".",
"reportCodeChange",
"(",
")",
";",
"}"
] | For script nodes of JSON objects, add a module variable assignment so the result is exported.
<p>If the file path ends with "/package.json", look for main entries in their specified order
in the object literal and track them as module aliases. Main entries default to "main" and can
be overridden with the `--package_json_entry_names` option. | [
"For",
"script",
"nodes",
"of",
"JSON",
"objects",
"add",
"a",
"module",
"variable",
"assignment",
"so",
"the",
"result",
"is",
"exported",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/RewriteJsonToModule.java#L120-L166 |
24,216 | google/closure-compiler | src/com/google/javascript/jscomp/Es6RewriteScriptsToModules.java | Es6RewriteScriptsToModules.forceToEs6Module | void forceToEs6Module(Node root) {
if (Es6RewriteModules.isEs6ModuleRoot(root)) {
return;
}
Node moduleNode = new Node(Token.MODULE_BODY).srcref(root);
moduleNode.addChildrenToBack(root.removeChildren());
root.addChildToBack(moduleNode);
compiler.reportChangeToChangeScope(root);
} | java | void forceToEs6Module(Node root) {
if (Es6RewriteModules.isEs6ModuleRoot(root)) {
return;
}
Node moduleNode = new Node(Token.MODULE_BODY).srcref(root);
moduleNode.addChildrenToBack(root.removeChildren());
root.addChildToBack(moduleNode);
compiler.reportChangeToChangeScope(root);
} | [
"void",
"forceToEs6Module",
"(",
"Node",
"root",
")",
"{",
"if",
"(",
"Es6RewriteModules",
".",
"isEs6ModuleRoot",
"(",
"root",
")",
")",
"{",
"return",
";",
"}",
"Node",
"moduleNode",
"=",
"new",
"Node",
"(",
"Token",
".",
"MODULE_BODY",
")",
".",
"srcref",
"(",
"root",
")",
";",
"moduleNode",
".",
"addChildrenToBack",
"(",
"root",
".",
"removeChildren",
"(",
")",
")",
";",
"root",
".",
"addChildToBack",
"(",
"moduleNode",
")",
";",
"compiler",
".",
"reportChangeToChangeScope",
"(",
"root",
")",
";",
"}"
] | Force rewriting of a script into an ES6 module, such as for imported files that contain no
"import" or "export" statements. | [
"Force",
"rewriting",
"of",
"a",
"script",
"into",
"an",
"ES6",
"module",
"such",
"as",
"for",
"imported",
"files",
"that",
"contain",
"no",
"import",
"or",
"export",
"statements",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/Es6RewriteScriptsToModules.java#L41-L49 |
24,217 | google/closure-compiler | src/com/google/javascript/jscomp/ReplacedStringsDecoder.java | ReplacedStringsDecoder.decode | public String decode(String encodedStr) {
String[] suppliedBits = encodedStr.split(ARGUMENT_PLACE_HOLDER, -1);
String originalStr = originalToNewNameMap.get(suppliedBits[0]);
if (originalStr == null) {
return encodedStr; // Failed to decode.
}
String[] originalBits = originalStr.split(ARGUMENT_PLACE_HOLDER, -1);
StringBuilder sb = new StringBuilder(originalBits[0]);
for (int i = 1; i < Math.max(originalBits.length, suppliedBits.length); i++) {
// Replace missing bits with "-". Shouldn't happen except that we aren't
// escaping the replacement token at the moment.
sb.append(i < suppliedBits.length ? suppliedBits[i] : "-");
sb.append(i < originalBits.length ? originalBits[i] : "-");
}
return sb.toString();
} | java | public String decode(String encodedStr) {
String[] suppliedBits = encodedStr.split(ARGUMENT_PLACE_HOLDER, -1);
String originalStr = originalToNewNameMap.get(suppliedBits[0]);
if (originalStr == null) {
return encodedStr; // Failed to decode.
}
String[] originalBits = originalStr.split(ARGUMENT_PLACE_HOLDER, -1);
StringBuilder sb = new StringBuilder(originalBits[0]);
for (int i = 1; i < Math.max(originalBits.length, suppliedBits.length); i++) {
// Replace missing bits with "-". Shouldn't happen except that we aren't
// escaping the replacement token at the moment.
sb.append(i < suppliedBits.length ? suppliedBits[i] : "-");
sb.append(i < originalBits.length ? originalBits[i] : "-");
}
return sb.toString();
} | [
"public",
"String",
"decode",
"(",
"String",
"encodedStr",
")",
"{",
"String",
"[",
"]",
"suppliedBits",
"=",
"encodedStr",
".",
"split",
"(",
"ARGUMENT_PLACE_HOLDER",
",",
"-",
"1",
")",
";",
"String",
"originalStr",
"=",
"originalToNewNameMap",
".",
"get",
"(",
"suppliedBits",
"[",
"0",
"]",
")",
";",
"if",
"(",
"originalStr",
"==",
"null",
")",
"{",
"return",
"encodedStr",
";",
"// Failed to decode.",
"}",
"String",
"[",
"]",
"originalBits",
"=",
"originalStr",
".",
"split",
"(",
"ARGUMENT_PLACE_HOLDER",
",",
"-",
"1",
")",
";",
"StringBuilder",
"sb",
"=",
"new",
"StringBuilder",
"(",
"originalBits",
"[",
"0",
"]",
")",
";",
"for",
"(",
"int",
"i",
"=",
"1",
";",
"i",
"<",
"Math",
".",
"max",
"(",
"originalBits",
".",
"length",
",",
"suppliedBits",
".",
"length",
")",
";",
"i",
"++",
")",
"{",
"// Replace missing bits with \"-\". Shouldn't happen except that we aren't",
"// escaping the replacement token at the moment.",
"sb",
".",
"append",
"(",
"i",
"<",
"suppliedBits",
".",
"length",
"?",
"suppliedBits",
"[",
"i",
"]",
":",
"\"-\"",
")",
";",
"sb",
".",
"append",
"(",
"i",
"<",
"originalBits",
".",
"length",
"?",
"originalBits",
"[",
"i",
"]",
":",
"\"-\"",
")",
";",
"}",
"return",
"sb",
".",
"toString",
"(",
")",
";",
"}"
] | Decodes an encoded string from the JS Compiler ReplaceStrings pass.
<p>An original string with args might look like this:
<pre> Error('Some ' + arg1 + ' error ' + arg2 + ' message.');</pre>
Which gets replaced with:
<pre> Error('key' + '`' + arg1 + '`' + arg2);</pre>
Where ` is the argument place holder. The replacement mapping would be:
<pre> key → 'Some ` error ` message.'</pre>
Where key is some arbitrary replacement string. An encoded string,
with args, from the client will look like:
<pre> 'key`arg1`arg2'</pre>
@param encodedStr An encoded string.
@return The decoded string, or the encoded string if it fails to decode.
@see com.google.javascript.jscomp.ReplaceStrings | [
"Decodes",
"an",
"encoded",
"string",
"from",
"the",
"JS",
"Compiler",
"ReplaceStrings",
"pass",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/ReplacedStringsDecoder.java#L63-L78 |
24,218 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.optimizeSubtree | @Override
@SuppressWarnings("fallthrough")
public Node optimizeSubtree(Node node) {
switch (node.getToken()) {
case ASSIGN_SUB:
return reduceSubstractionAssignment(node);
case TRUE:
case FALSE:
return reduceTrueFalse(node);
case NEW:
node = tryFoldStandardConstructors(node);
if (!node.isCall()) {
return node;
}
// Fall through on purpose because tryFoldStandardConstructors() may
// convert a NEW node into a CALL node
case CALL:
Node result = tryFoldLiteralConstructor(node);
if (result == node) {
result = tryFoldSimpleFunctionCall(node);
if (result == node) {
result = tryFoldImmediateCallToBoundFunction(node);
}
}
return result;
case RETURN:
return tryReduceReturn(node);
case COMMA:
// TODO(b/63630312): should flatten an entire comma expression in a single pass.
return trySplitComma(node);
case NAME:
return tryReplaceUndefined(node);
case ARRAYLIT:
return tryMinimizeArrayLiteral(node);
case GETPROP:
return tryMinimizeWindowRefs(node);
case TEMPLATELIT:
return tryTurnTemplateStringsToStrings(node);
case MUL:
case AND:
case OR:
case BITOR:
case BITXOR:
case BITAND:
return tryRotateAssociativeOperator(node);
default:
return node; //Nothing changed
}
} | java | @Override
@SuppressWarnings("fallthrough")
public Node optimizeSubtree(Node node) {
switch (node.getToken()) {
case ASSIGN_SUB:
return reduceSubstractionAssignment(node);
case TRUE:
case FALSE:
return reduceTrueFalse(node);
case NEW:
node = tryFoldStandardConstructors(node);
if (!node.isCall()) {
return node;
}
// Fall through on purpose because tryFoldStandardConstructors() may
// convert a NEW node into a CALL node
case CALL:
Node result = tryFoldLiteralConstructor(node);
if (result == node) {
result = tryFoldSimpleFunctionCall(node);
if (result == node) {
result = tryFoldImmediateCallToBoundFunction(node);
}
}
return result;
case RETURN:
return tryReduceReturn(node);
case COMMA:
// TODO(b/63630312): should flatten an entire comma expression in a single pass.
return trySplitComma(node);
case NAME:
return tryReplaceUndefined(node);
case ARRAYLIT:
return tryMinimizeArrayLiteral(node);
case GETPROP:
return tryMinimizeWindowRefs(node);
case TEMPLATELIT:
return tryTurnTemplateStringsToStrings(node);
case MUL:
case AND:
case OR:
case BITOR:
case BITXOR:
case BITAND:
return tryRotateAssociativeOperator(node);
default:
return node; //Nothing changed
}
} | [
"@",
"Override",
"@",
"SuppressWarnings",
"(",
"\"fallthrough\"",
")",
"public",
"Node",
"optimizeSubtree",
"(",
"Node",
"node",
")",
"{",
"switch",
"(",
"node",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"ASSIGN_SUB",
":",
"return",
"reduceSubstractionAssignment",
"(",
"node",
")",
";",
"case",
"TRUE",
":",
"case",
"FALSE",
":",
"return",
"reduceTrueFalse",
"(",
"node",
")",
";",
"case",
"NEW",
":",
"node",
"=",
"tryFoldStandardConstructors",
"(",
"node",
")",
";",
"if",
"(",
"!",
"node",
".",
"isCall",
"(",
")",
")",
"{",
"return",
"node",
";",
"}",
"// Fall through on purpose because tryFoldStandardConstructors() may",
"// convert a NEW node into a CALL node",
"case",
"CALL",
":",
"Node",
"result",
"=",
"tryFoldLiteralConstructor",
"(",
"node",
")",
";",
"if",
"(",
"result",
"==",
"node",
")",
"{",
"result",
"=",
"tryFoldSimpleFunctionCall",
"(",
"node",
")",
";",
"if",
"(",
"result",
"==",
"node",
")",
"{",
"result",
"=",
"tryFoldImmediateCallToBoundFunction",
"(",
"node",
")",
";",
"}",
"}",
"return",
"result",
";",
"case",
"RETURN",
":",
"return",
"tryReduceReturn",
"(",
"node",
")",
";",
"case",
"COMMA",
":",
"// TODO(b/63630312): should flatten an entire comma expression in a single pass.",
"return",
"trySplitComma",
"(",
"node",
")",
";",
"case",
"NAME",
":",
"return",
"tryReplaceUndefined",
"(",
"node",
")",
";",
"case",
"ARRAYLIT",
":",
"return",
"tryMinimizeArrayLiteral",
"(",
"node",
")",
";",
"case",
"GETPROP",
":",
"return",
"tryMinimizeWindowRefs",
"(",
"node",
")",
";",
"case",
"TEMPLATELIT",
":",
"return",
"tryTurnTemplateStringsToStrings",
"(",
"node",
")",
";",
"case",
"MUL",
":",
"case",
"AND",
":",
"case",
"OR",
":",
"case",
"BITOR",
":",
"case",
"BITXOR",
":",
"case",
"BITAND",
":",
"return",
"tryRotateAssociativeOperator",
"(",
"node",
")",
";",
"default",
":",
"return",
"node",
";",
"//Nothing changed",
"}",
"}"
] | Tries apply our various peephole minimizations on the passed in node. | [
"Tries",
"apply",
"our",
"various",
"peephole",
"minimizations",
"on",
"the",
"passed",
"in",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L66-L124 |
24,219 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.tryReplaceUndefined | private Node tryReplaceUndefined(Node n) {
// TODO(johnlenz): consider doing this as a normalization.
if (isASTNormalized()
&& NodeUtil.isUndefined(n)
&& !NodeUtil.isLValue(n)) {
Node replacement = NodeUtil.newUndefinedNode(n);
n.replaceWith(replacement);
reportChangeToEnclosingScope(replacement);
return replacement;
}
return n;
} | java | private Node tryReplaceUndefined(Node n) {
// TODO(johnlenz): consider doing this as a normalization.
if (isASTNormalized()
&& NodeUtil.isUndefined(n)
&& !NodeUtil.isLValue(n)) {
Node replacement = NodeUtil.newUndefinedNode(n);
n.replaceWith(replacement);
reportChangeToEnclosingScope(replacement);
return replacement;
}
return n;
} | [
"private",
"Node",
"tryReplaceUndefined",
"(",
"Node",
"n",
")",
"{",
"// TODO(johnlenz): consider doing this as a normalization.",
"if",
"(",
"isASTNormalized",
"(",
")",
"&&",
"NodeUtil",
".",
"isUndefined",
"(",
"n",
")",
"&&",
"!",
"NodeUtil",
".",
"isLValue",
"(",
"n",
")",
")",
"{",
"Node",
"replacement",
"=",
"NodeUtil",
".",
"newUndefinedNode",
"(",
"n",
")",
";",
"n",
".",
"replaceWith",
"(",
"replacement",
")",
";",
"reportChangeToEnclosingScope",
"(",
"replacement",
")",
";",
"return",
"replacement",
";",
"}",
"return",
"n",
";",
"}"
] | Use "void 0" in place of "undefined" | [
"Use",
"void",
"0",
"in",
"place",
"of",
"undefined"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L333-L344 |
24,220 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.tryReduceReturn | private Node tryReduceReturn(Node n) {
Node result = n.getFirstChild();
if (result != null) {
switch (result.getToken()) {
case VOID:
Node operand = result.getFirstChild();
if (!mayHaveSideEffects(operand)) {
n.removeFirstChild();
reportChangeToEnclosingScope(n);
}
break;
case NAME:
String name = result.getString();
if (name.equals("undefined")) {
n.removeFirstChild();
reportChangeToEnclosingScope(n);
}
break;
default:
break;
}
}
return n;
} | java | private Node tryReduceReturn(Node n) {
Node result = n.getFirstChild();
if (result != null) {
switch (result.getToken()) {
case VOID:
Node operand = result.getFirstChild();
if (!mayHaveSideEffects(operand)) {
n.removeFirstChild();
reportChangeToEnclosingScope(n);
}
break;
case NAME:
String name = result.getString();
if (name.equals("undefined")) {
n.removeFirstChild();
reportChangeToEnclosingScope(n);
}
break;
default:
break;
}
}
return n;
} | [
"private",
"Node",
"tryReduceReturn",
"(",
"Node",
"n",
")",
"{",
"Node",
"result",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"if",
"(",
"result",
"!=",
"null",
")",
"{",
"switch",
"(",
"result",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"VOID",
":",
"Node",
"operand",
"=",
"result",
".",
"getFirstChild",
"(",
")",
";",
"if",
"(",
"!",
"mayHaveSideEffects",
"(",
"operand",
")",
")",
"{",
"n",
".",
"removeFirstChild",
"(",
")",
";",
"reportChangeToEnclosingScope",
"(",
"n",
")",
";",
"}",
"break",
";",
"case",
"NAME",
":",
"String",
"name",
"=",
"result",
".",
"getString",
"(",
")",
";",
"if",
"(",
"name",
".",
"equals",
"(",
"\"undefined\"",
")",
")",
"{",
"n",
".",
"removeFirstChild",
"(",
")",
";",
"reportChangeToEnclosingScope",
"(",
"n",
")",
";",
"}",
"break",
";",
"default",
":",
"break",
";",
"}",
"}",
"return",
"n",
";",
"}"
] | Reduce "return undefined" or "return void 0" to simply "return".
@return The original node, maybe simplified. | [
"Reduce",
"return",
"undefined",
"or",
"return",
"void",
"0",
"to",
"simply",
"return",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L351-L376 |
24,221 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.tryFoldLiteralConstructor | private Node tryFoldLiteralConstructor(Node n) {
checkArgument(n.isCall() || n.isNew());
Node constructorNameNode = n.getFirstChild();
Node newLiteralNode = null;
// We require the AST to be normalized to ensure that, say,
// Object() really refers to the built-in Object constructor
// and not a user-defined constructor with the same name.
if (isASTNormalized() && constructorNameNode.isName()) {
String className = constructorNameNode.getString();
if ("RegExp".equals(className)) {
// "RegExp("boo", "g")" --> /boo/g
return tryFoldRegularExpressionConstructor(n);
} else {
boolean constructorHasArgs = constructorNameNode.getNext() != null;
if ("Object".equals(className) && !constructorHasArgs) {
// "Object()" --> "{}"
newLiteralNode = IR.objectlit();
} else if ("Array".equals(className)) {
// "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
Node arg0 = constructorNameNode.getNext();
FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);
if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
newLiteralNode = IR.arraylit();
n.removeFirstChild(); // discard the function name
Node elements = n.removeChildren();
if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
newLiteralNode.addChildrenToFront(elements);
}
}
}
if (newLiteralNode != null) {
n.replaceWith(newLiteralNode);
reportChangeToEnclosingScope(newLiteralNode);
return newLiteralNode;
}
}
}
return n;
} | java | private Node tryFoldLiteralConstructor(Node n) {
checkArgument(n.isCall() || n.isNew());
Node constructorNameNode = n.getFirstChild();
Node newLiteralNode = null;
// We require the AST to be normalized to ensure that, say,
// Object() really refers to the built-in Object constructor
// and not a user-defined constructor with the same name.
if (isASTNormalized() && constructorNameNode.isName()) {
String className = constructorNameNode.getString();
if ("RegExp".equals(className)) {
// "RegExp("boo", "g")" --> /boo/g
return tryFoldRegularExpressionConstructor(n);
} else {
boolean constructorHasArgs = constructorNameNode.getNext() != null;
if ("Object".equals(className) && !constructorHasArgs) {
// "Object()" --> "{}"
newLiteralNode = IR.objectlit();
} else if ("Array".equals(className)) {
// "Array(arg0, arg1, ...)" --> "[arg0, arg1, ...]"
Node arg0 = constructorNameNode.getNext();
FoldArrayAction action = isSafeToFoldArrayConstructor(arg0);
if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS ||
action == FoldArrayAction.SAFE_TO_FOLD_WITHOUT_ARGS) {
newLiteralNode = IR.arraylit();
n.removeFirstChild(); // discard the function name
Node elements = n.removeChildren();
if (action == FoldArrayAction.SAFE_TO_FOLD_WITH_ARGS) {
newLiteralNode.addChildrenToFront(elements);
}
}
}
if (newLiteralNode != null) {
n.replaceWith(newLiteralNode);
reportChangeToEnclosingScope(newLiteralNode);
return newLiteralNode;
}
}
}
return n;
} | [
"private",
"Node",
"tryFoldLiteralConstructor",
"(",
"Node",
"n",
")",
"{",
"checkArgument",
"(",
"n",
".",
"isCall",
"(",
")",
"||",
"n",
".",
"isNew",
"(",
")",
")",
";",
"Node",
"constructorNameNode",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"Node",
"newLiteralNode",
"=",
"null",
";",
"// We require the AST to be normalized to ensure that, say,",
"// Object() really refers to the built-in Object constructor",
"// and not a user-defined constructor with the same name.",
"if",
"(",
"isASTNormalized",
"(",
")",
"&&",
"constructorNameNode",
".",
"isName",
"(",
")",
")",
"{",
"String",
"className",
"=",
"constructorNameNode",
".",
"getString",
"(",
")",
";",
"if",
"(",
"\"RegExp\"",
".",
"equals",
"(",
"className",
")",
")",
"{",
"// \"RegExp(\"boo\", \"g\")\" --> /boo/g",
"return",
"tryFoldRegularExpressionConstructor",
"(",
"n",
")",
";",
"}",
"else",
"{",
"boolean",
"constructorHasArgs",
"=",
"constructorNameNode",
".",
"getNext",
"(",
")",
"!=",
"null",
";",
"if",
"(",
"\"Object\"",
".",
"equals",
"(",
"className",
")",
"&&",
"!",
"constructorHasArgs",
")",
"{",
"// \"Object()\" --> \"{}\"",
"newLiteralNode",
"=",
"IR",
".",
"objectlit",
"(",
")",
";",
"}",
"else",
"if",
"(",
"\"Array\"",
".",
"equals",
"(",
"className",
")",
")",
"{",
"// \"Array(arg0, arg1, ...)\" --> \"[arg0, arg1, ...]\"",
"Node",
"arg0",
"=",
"constructorNameNode",
".",
"getNext",
"(",
")",
";",
"FoldArrayAction",
"action",
"=",
"isSafeToFoldArrayConstructor",
"(",
"arg0",
")",
";",
"if",
"(",
"action",
"==",
"FoldArrayAction",
".",
"SAFE_TO_FOLD_WITH_ARGS",
"||",
"action",
"==",
"FoldArrayAction",
".",
"SAFE_TO_FOLD_WITHOUT_ARGS",
")",
"{",
"newLiteralNode",
"=",
"IR",
".",
"arraylit",
"(",
")",
";",
"n",
".",
"removeFirstChild",
"(",
")",
";",
"// discard the function name",
"Node",
"elements",
"=",
"n",
".",
"removeChildren",
"(",
")",
";",
"if",
"(",
"action",
"==",
"FoldArrayAction",
".",
"SAFE_TO_FOLD_WITH_ARGS",
")",
"{",
"newLiteralNode",
".",
"addChildrenToFront",
"(",
"elements",
")",
";",
"}",
"}",
"}",
"if",
"(",
"newLiteralNode",
"!=",
"null",
")",
"{",
"n",
".",
"replaceWith",
"(",
"newLiteralNode",
")",
";",
"reportChangeToEnclosingScope",
"(",
"newLiteralNode",
")",
";",
"return",
"newLiteralNode",
";",
"}",
"}",
"}",
"return",
"n",
";",
"}"
] | Replaces a new Array, Object, or RegExp node with a literal, unless the
call is to a local constructor function with the same name. | [
"Replaces",
"a",
"new",
"Array",
"Object",
"or",
"RegExp",
"node",
"with",
"a",
"literal",
"unless",
"the",
"call",
"is",
"to",
"a",
"local",
"constructor",
"function",
"with",
"the",
"same",
"name",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L431-L479 |
24,222 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.pickDelimiter | private static String pickDelimiter(String[] strings) {
boolean allLength1 = true;
for (String s : strings) {
if (s.length() != 1) {
allLength1 = false;
break;
}
}
if (allLength1) {
return "";
}
String[] delimiters = new String[]{" ", ";", ",", "{", "}", null};
int i = 0;
NEXT_DELIMITER: for (; delimiters[i] != null; i++) {
for (String cur : strings) {
if (cur.contains(delimiters[i])) {
continue NEXT_DELIMITER;
}
}
break;
}
return delimiters[i];
} | java | private static String pickDelimiter(String[] strings) {
boolean allLength1 = true;
for (String s : strings) {
if (s.length() != 1) {
allLength1 = false;
break;
}
}
if (allLength1) {
return "";
}
String[] delimiters = new String[]{" ", ";", ",", "{", "}", null};
int i = 0;
NEXT_DELIMITER: for (; delimiters[i] != null; i++) {
for (String cur : strings) {
if (cur.contains(delimiters[i])) {
continue NEXT_DELIMITER;
}
}
break;
}
return delimiters[i];
} | [
"private",
"static",
"String",
"pickDelimiter",
"(",
"String",
"[",
"]",
"strings",
")",
"{",
"boolean",
"allLength1",
"=",
"true",
";",
"for",
"(",
"String",
"s",
":",
"strings",
")",
"{",
"if",
"(",
"s",
".",
"length",
"(",
")",
"!=",
"1",
")",
"{",
"allLength1",
"=",
"false",
";",
"break",
";",
"}",
"}",
"if",
"(",
"allLength1",
")",
"{",
"return",
"\"\"",
";",
"}",
"String",
"[",
"]",
"delimiters",
"=",
"new",
"String",
"[",
"]",
"{",
"\" \"",
",",
"\";\"",
",",
"\",\"",
",",
"\"{\"",
",",
"\"}\"",
",",
"null",
"}",
";",
"int",
"i",
"=",
"0",
";",
"NEXT_DELIMITER",
":",
"for",
"(",
";",
"delimiters",
"[",
"i",
"]",
"!=",
"null",
";",
"i",
"++",
")",
"{",
"for",
"(",
"String",
"cur",
":",
"strings",
")",
"{",
"if",
"(",
"cur",
".",
"contains",
"(",
"delimiters",
"[",
"i",
"]",
")",
")",
"{",
"continue",
"NEXT_DELIMITER",
";",
"}",
"}",
"break",
";",
"}",
"return",
"delimiters",
"[",
"i",
"]",
";",
"}"
] | Find a delimiter that does not occur in the given strings
@param strings The strings that must be separated.
@return a delimiter string or null | [
"Find",
"a",
"delimiter",
"that",
"does",
"not",
"occur",
"in",
"the",
"given",
"strings"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L687-L711 |
24,223 | google/closure-compiler | src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java | PeepholeSubstituteAlternateSyntax.containsUnicodeEscape | static boolean containsUnicodeEscape(String s) {
String esc = REGEXP_ESCAPER.regexpEscape(s);
for (int i = -1; (i = esc.indexOf("\\u", i + 1)) >= 0;) {
int nSlashes = 0;
while (i - nSlashes > 0 && '\\' == esc.charAt(i - nSlashes - 1)) {
++nSlashes;
}
// if there are an even number of slashes before the \ u then it is a
// Unicode literal.
if (0 == (nSlashes & 1)) { return true; }
}
return false;
} | java | static boolean containsUnicodeEscape(String s) {
String esc = REGEXP_ESCAPER.regexpEscape(s);
for (int i = -1; (i = esc.indexOf("\\u", i + 1)) >= 0;) {
int nSlashes = 0;
while (i - nSlashes > 0 && '\\' == esc.charAt(i - nSlashes - 1)) {
++nSlashes;
}
// if there are an even number of slashes before the \ u then it is a
// Unicode literal.
if (0 == (nSlashes & 1)) { return true; }
}
return false;
} | [
"static",
"boolean",
"containsUnicodeEscape",
"(",
"String",
"s",
")",
"{",
"String",
"esc",
"=",
"REGEXP_ESCAPER",
".",
"regexpEscape",
"(",
"s",
")",
";",
"for",
"(",
"int",
"i",
"=",
"-",
"1",
";",
"(",
"i",
"=",
"esc",
".",
"indexOf",
"(",
"\"\\\\u\"",
",",
"i",
"+",
"1",
")",
")",
">=",
"0",
";",
")",
"{",
"int",
"nSlashes",
"=",
"0",
";",
"while",
"(",
"i",
"-",
"nSlashes",
">",
"0",
"&&",
"'",
"'",
"==",
"esc",
".",
"charAt",
"(",
"i",
"-",
"nSlashes",
"-",
"1",
")",
")",
"{",
"++",
"nSlashes",
";",
"}",
"// if there are an even number of slashes before the \\ u then it is a",
"// Unicode literal.",
"if",
"(",
"0",
"==",
"(",
"nSlashes",
"&",
"1",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | true if the JavaScript string would contain a Unicode escape when written
out as the body of a regular expression literal. | [
"true",
"if",
"the",
"JavaScript",
"string",
"would",
"contain",
"a",
"Unicode",
"escape",
"when",
"written",
"out",
"as",
"the",
"body",
"of",
"a",
"regular",
"expression",
"literal",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java#L821-L833 |
24,224 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getArrayElementStringValue | static String getArrayElementStringValue(Node n) {
return (NodeUtil.isNullOrUndefined(n) || n.isEmpty())
? "" : getStringValue(n);
} | java | static String getArrayElementStringValue(Node n) {
return (NodeUtil.isNullOrUndefined(n) || n.isEmpty())
? "" : getStringValue(n);
} | [
"static",
"String",
"getArrayElementStringValue",
"(",
"Node",
"n",
")",
"{",
"return",
"(",
"NodeUtil",
".",
"isNullOrUndefined",
"(",
"n",
")",
"||",
"n",
".",
"isEmpty",
"(",
")",
")",
"?",
"\"\"",
":",
"getStringValue",
"(",
"n",
")",
";",
"}"
] | When converting arrays to string using Array.prototype.toString or
Array.prototype.join, the rules for conversion to String are different
than converting each element individually. Specifically, "null" and
"undefined" are converted to an empty string.
@param n A node that is a member of an Array.
@return The string representation. | [
"When",
"converting",
"arrays",
"to",
"string",
"using",
"Array",
".",
"prototype",
".",
"toString",
"or",
"Array",
".",
"prototype",
".",
"join",
"the",
"rules",
"for",
"conversion",
"to",
"String",
"are",
"different",
"than",
"converting",
"each",
"element",
"individually",
".",
"Specifically",
"null",
"and",
"undefined",
"are",
"converted",
"to",
"an",
"empty",
"string",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L283-L286 |
24,225 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isImmutableValue | static boolean isImmutableValue(Node n) {
// TODO(johnlenz): rename this function. It is currently being used
// in two disjoint cases:
// 1) We only care about the result of the expression
// (in which case NOT here should return true)
// 2) We care that expression is a side-effect free and can't
// be side-effected by other expressions.
// This should only be used to say the value is immutable and
// hasSideEffects and canBeSideEffected should be used for the other case.
switch (n.getToken()) {
case STRING:
case NUMBER:
case NULL:
case TRUE:
case FALSE:
return true;
case CAST:
case NOT:
case VOID:
case NEG:
return isImmutableValue(n.getFirstChild());
case NAME:
String name = n.getString();
// We assume here that programs don't change the value of the keyword
// undefined to something other than the value undefined.
return "undefined".equals(name) || "Infinity".equals(name) || "NaN".equals(name);
case TEMPLATELIT:
for (Node child = n.getFirstChild(); child != null; child = child.getNext()) {
if (child.isTemplateLitSub()) {
if (!isImmutableValue(child.getFirstChild())) {
return false;
}
}
}
return true;
default:
// TODO(yitingwang) There are probably other tokens that shouldn't get to the default branch
checkArgument(!n.isTemplateLitString());
break;
}
return false;
} | java | static boolean isImmutableValue(Node n) {
// TODO(johnlenz): rename this function. It is currently being used
// in two disjoint cases:
// 1) We only care about the result of the expression
// (in which case NOT here should return true)
// 2) We care that expression is a side-effect free and can't
// be side-effected by other expressions.
// This should only be used to say the value is immutable and
// hasSideEffects and canBeSideEffected should be used for the other case.
switch (n.getToken()) {
case STRING:
case NUMBER:
case NULL:
case TRUE:
case FALSE:
return true;
case CAST:
case NOT:
case VOID:
case NEG:
return isImmutableValue(n.getFirstChild());
case NAME:
String name = n.getString();
// We assume here that programs don't change the value of the keyword
// undefined to something other than the value undefined.
return "undefined".equals(name) || "Infinity".equals(name) || "NaN".equals(name);
case TEMPLATELIT:
for (Node child = n.getFirstChild(); child != null; child = child.getNext()) {
if (child.isTemplateLitSub()) {
if (!isImmutableValue(child.getFirstChild())) {
return false;
}
}
}
return true;
default:
// TODO(yitingwang) There are probably other tokens that shouldn't get to the default branch
checkArgument(!n.isTemplateLitString());
break;
}
return false;
} | [
"static",
"boolean",
"isImmutableValue",
"(",
"Node",
"n",
")",
"{",
"// TODO(johnlenz): rename this function. It is currently being used",
"// in two disjoint cases:",
"// 1) We only care about the result of the expression",
"// (in which case NOT here should return true)",
"// 2) We care that expression is a side-effect free and can't",
"// be side-effected by other expressions.",
"// This should only be used to say the value is immutable and",
"// hasSideEffects and canBeSideEffected should be used for the other case.",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"STRING",
":",
"case",
"NUMBER",
":",
"case",
"NULL",
":",
"case",
"TRUE",
":",
"case",
"FALSE",
":",
"return",
"true",
";",
"case",
"CAST",
":",
"case",
"NOT",
":",
"case",
"VOID",
":",
"case",
"NEG",
":",
"return",
"isImmutableValue",
"(",
"n",
".",
"getFirstChild",
"(",
")",
")",
";",
"case",
"NAME",
":",
"String",
"name",
"=",
"n",
".",
"getString",
"(",
")",
";",
"// We assume here that programs don't change the value of the keyword",
"// undefined to something other than the value undefined.",
"return",
"\"undefined\"",
".",
"equals",
"(",
"name",
")",
"||",
"\"Infinity\"",
".",
"equals",
"(",
"name",
")",
"||",
"\"NaN\"",
".",
"equals",
"(",
"name",
")",
";",
"case",
"TEMPLATELIT",
":",
"for",
"(",
"Node",
"child",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"child",
"!=",
"null",
";",
"child",
"=",
"child",
".",
"getNext",
"(",
")",
")",
"{",
"if",
"(",
"child",
".",
"isTemplateLitSub",
"(",
")",
")",
"{",
"if",
"(",
"!",
"isImmutableValue",
"(",
"child",
".",
"getFirstChild",
"(",
")",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"}",
"return",
"true",
";",
"default",
":",
"// TODO(yitingwang) There are probably other tokens that shouldn't get to the default branch",
"checkArgument",
"(",
"!",
"n",
".",
"isTemplateLitString",
"(",
")",
")",
";",
"break",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true if this is an immutable value. | [
"Returns",
"true",
"if",
"this",
"is",
"an",
"immutable",
"value",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L568-L611 |
24,226 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isSymmetricOperation | static boolean isSymmetricOperation(Node n) {
switch (n.getToken()) {
case EQ: // equal
case NE: // not equal
case SHEQ: // exactly equal
case SHNE: // exactly not equal
case MUL: // multiply, unlike add it only works on numbers
// or results NaN if any of the operators is not a number
return true;
default:
break;
}
return false;
} | java | static boolean isSymmetricOperation(Node n) {
switch (n.getToken()) {
case EQ: // equal
case NE: // not equal
case SHEQ: // exactly equal
case SHNE: // exactly not equal
case MUL: // multiply, unlike add it only works on numbers
// or results NaN if any of the operators is not a number
return true;
default:
break;
}
return false;
} | [
"static",
"boolean",
"isSymmetricOperation",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"EQ",
":",
"// equal",
"case",
"NE",
":",
"// not equal",
"case",
"SHEQ",
":",
"// exactly equal",
"case",
"SHNE",
":",
"// exactly not equal",
"case",
"MUL",
":",
"// multiply, unlike add it only works on numbers",
"// or results NaN if any of the operators is not a number",
"return",
"true",
";",
"default",
":",
"break",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true if the operator on this node is symmetric | [
"Returns",
"true",
"if",
"the",
"operator",
"on",
"this",
"node",
"is",
"symmetric"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L616-L629 |
24,227 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isRelationalOperation | static boolean isRelationalOperation(Node n) {
switch (n.getToken()) {
case GT: // equal
case GE: // not equal
case LT: // exactly equal
case LE: // exactly not equal
return true;
default:
break;
}
return false;
} | java | static boolean isRelationalOperation(Node n) {
switch (n.getToken()) {
case GT: // equal
case GE: // not equal
case LT: // exactly equal
case LE: // exactly not equal
return true;
default:
break;
}
return false;
} | [
"static",
"boolean",
"isRelationalOperation",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"GT",
":",
"// equal",
"case",
"GE",
":",
"// not equal",
"case",
"LT",
":",
"// exactly equal",
"case",
"LE",
":",
"// exactly not equal",
"return",
"true",
";",
"default",
":",
"break",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true if the operator on this node is relational.
the returned set does not include the equalities. | [
"Returns",
"true",
"if",
"the",
"operator",
"on",
"this",
"node",
"is",
"relational",
".",
"the",
"returned",
"set",
"does",
"not",
"include",
"the",
"equalities",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L635-L646 |
24,228 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isSomeCompileTimeConstStringValue | static boolean isSomeCompileTimeConstStringValue(Node node) {
// TODO(bangert): Support constants, using a Scope argument. See ConstParamCheck
if (node.isString() || (node.isTemplateLit() && node.hasOneChild())) {
return true;
} else if (node.isAdd()) {
checkState(node.hasTwoChildren(), node);
Node left = node.getFirstChild();
Node right = node.getLastChild();
return isSomeCompileTimeConstStringValue(left) && isSomeCompileTimeConstStringValue(right);
} else if (node.isHook()) {
// Ternary operator a ? b : c
Node left = node.getSecondChild();
Node right = node.getLastChild();
return isSomeCompileTimeConstStringValue(left) && isSomeCompileTimeConstStringValue(right);
}
return false;
} | java | static boolean isSomeCompileTimeConstStringValue(Node node) {
// TODO(bangert): Support constants, using a Scope argument. See ConstParamCheck
if (node.isString() || (node.isTemplateLit() && node.hasOneChild())) {
return true;
} else if (node.isAdd()) {
checkState(node.hasTwoChildren(), node);
Node left = node.getFirstChild();
Node right = node.getLastChild();
return isSomeCompileTimeConstStringValue(left) && isSomeCompileTimeConstStringValue(right);
} else if (node.isHook()) {
// Ternary operator a ? b : c
Node left = node.getSecondChild();
Node right = node.getLastChild();
return isSomeCompileTimeConstStringValue(left) && isSomeCompileTimeConstStringValue(right);
}
return false;
} | [
"static",
"boolean",
"isSomeCompileTimeConstStringValue",
"(",
"Node",
"node",
")",
"{",
"// TODO(bangert): Support constants, using a Scope argument. See ConstParamCheck",
"if",
"(",
"node",
".",
"isString",
"(",
")",
"||",
"(",
"node",
".",
"isTemplateLit",
"(",
")",
"&&",
"node",
".",
"hasOneChild",
"(",
")",
")",
")",
"{",
"return",
"true",
";",
"}",
"else",
"if",
"(",
"node",
".",
"isAdd",
"(",
")",
")",
"{",
"checkState",
"(",
"node",
".",
"hasTwoChildren",
"(",
")",
",",
"node",
")",
";",
"Node",
"left",
"=",
"node",
".",
"getFirstChild",
"(",
")",
";",
"Node",
"right",
"=",
"node",
".",
"getLastChild",
"(",
")",
";",
"return",
"isSomeCompileTimeConstStringValue",
"(",
"left",
")",
"&&",
"isSomeCompileTimeConstStringValue",
"(",
"right",
")",
";",
"}",
"else",
"if",
"(",
"node",
".",
"isHook",
"(",
")",
")",
"{",
"// Ternary operator a ? b : c",
"Node",
"left",
"=",
"node",
".",
"getSecondChild",
"(",
")",
";",
"Node",
"right",
"=",
"node",
".",
"getLastChild",
"(",
")",
";",
"return",
"isSomeCompileTimeConstStringValue",
"(",
"left",
")",
"&&",
"isSomeCompileTimeConstStringValue",
"(",
"right",
")",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true iff the value associated with the node is a JS string literal, a concatenation
thereof or a ternary operator choosing between string literals. | [
"Returns",
"true",
"iff",
"the",
"value",
"associated",
"with",
"the",
"node",
"is",
"a",
"JS",
"string",
"literal",
"a",
"concatenation",
"thereof",
"or",
"a",
"ternary",
"operator",
"choosing",
"between",
"string",
"literals",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L779-L795 |
24,229 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isEmptyBlock | static boolean isEmptyBlock(Node block) {
if (!block.isBlock()) {
return false;
}
for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
if (!n.isEmpty()) {
return false;
}
}
return true;
} | java | static boolean isEmptyBlock(Node block) {
if (!block.isBlock()) {
return false;
}
for (Node n = block.getFirstChild(); n != null; n = n.getNext()) {
if (!n.isEmpty()) {
return false;
}
}
return true;
} | [
"static",
"boolean",
"isEmptyBlock",
"(",
"Node",
"block",
")",
"{",
"if",
"(",
"!",
"block",
".",
"isBlock",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"for",
"(",
"Node",
"n",
"=",
"block",
".",
"getFirstChild",
"(",
")",
";",
"n",
"!=",
"null",
";",
"n",
"=",
"n",
".",
"getNext",
"(",
")",
")",
"{",
"if",
"(",
"!",
"n",
".",
"isEmpty",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"return",
"true",
";",
"}"
] | Returns whether this a BLOCK node with no children.
@param block The node. | [
"Returns",
"whether",
"this",
"a",
"BLOCK",
"node",
"with",
"no",
"children",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L802-L813 |
24,230 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isBinaryOperatorType | static boolean isBinaryOperatorType(Token type) {
switch (type) {
case OR:
case AND:
case BITOR:
case BITXOR:
case BITAND:
case EQ:
case NE:
case SHEQ:
case SHNE:
case LT:
case GT:
case LE:
case GE:
case INSTANCEOF:
case IN:
case LSH:
case RSH:
case URSH:
case ADD:
case SUB:
case MUL:
case DIV:
case MOD:
case EXPONENT:
return true;
default:
return false;
}
} | java | static boolean isBinaryOperatorType(Token type) {
switch (type) {
case OR:
case AND:
case BITOR:
case BITXOR:
case BITAND:
case EQ:
case NE:
case SHEQ:
case SHNE:
case LT:
case GT:
case LE:
case GE:
case INSTANCEOF:
case IN:
case LSH:
case RSH:
case URSH:
case ADD:
case SUB:
case MUL:
case DIV:
case MOD:
case EXPONENT:
return true;
default:
return false;
}
} | [
"static",
"boolean",
"isBinaryOperatorType",
"(",
"Token",
"type",
")",
"{",
"switch",
"(",
"type",
")",
"{",
"case",
"OR",
":",
"case",
"AND",
":",
"case",
"BITOR",
":",
"case",
"BITXOR",
":",
"case",
"BITAND",
":",
"case",
"EQ",
":",
"case",
"NE",
":",
"case",
"SHEQ",
":",
"case",
"SHNE",
":",
"case",
"LT",
":",
"case",
"GT",
":",
"case",
"LE",
":",
"case",
"GE",
":",
"case",
"INSTANCEOF",
":",
"case",
"IN",
":",
"case",
"LSH",
":",
"case",
"RSH",
":",
"case",
"URSH",
":",
"case",
"ADD",
":",
"case",
"SUB",
":",
"case",
"MUL",
":",
"case",
"DIV",
":",
"case",
"MOD",
":",
"case",
"EXPONENT",
":",
"return",
"true",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | An operator with two operands that does not assign a value to either.
Once you cut through the layers of rules, these all parse similarly, taking
LeftHandSideExpression operands on either side.
Comma is not included, because it takes AssignmentExpression operands, making its syntax
different. | [
"An",
"operator",
"with",
"two",
"operands",
"that",
"does",
"not",
"assign",
"a",
"value",
"to",
"either",
".",
"Once",
"you",
"cut",
"through",
"the",
"layers",
"of",
"rules",
"these",
"all",
"parse",
"similarly",
"taking",
"LeftHandSideExpression",
"operands",
"on",
"either",
"side",
".",
"Comma",
"is",
"not",
"included",
"because",
"it",
"takes",
"AssignmentExpression",
"operands",
"making",
"its",
"syntax",
"different",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L826-L857 |
24,231 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isUnaryOperatorType | static boolean isUnaryOperatorType(Token type) {
switch (type) {
case DELPROP:
case VOID:
case TYPEOF:
case POS:
case NEG:
case BITNOT:
case NOT:
return true;
default:
return false;
}
} | java | static boolean isUnaryOperatorType(Token type) {
switch (type) {
case DELPROP:
case VOID:
case TYPEOF:
case POS:
case NEG:
case BITNOT:
case NOT:
return true;
default:
return false;
}
} | [
"static",
"boolean",
"isUnaryOperatorType",
"(",
"Token",
"type",
")",
"{",
"switch",
"(",
"type",
")",
"{",
"case",
"DELPROP",
":",
"case",
"VOID",
":",
"case",
"TYPEOF",
":",
"case",
"POS",
":",
"case",
"NEG",
":",
"case",
"BITNOT",
":",
"case",
"NOT",
":",
"return",
"true",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | An operator taking only one operand.
These all parse very similarly, taking LeftHandSideExpression operands. | [
"An",
"operator",
"taking",
"only",
"one",
"operand",
".",
"These",
"all",
"parse",
"very",
"similarly",
"taking",
"LeftHandSideExpression",
"operands",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L867-L881 |
24,232 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isAliasedConstDefinition | static boolean isAliasedConstDefinition(Node lhs) {
JSDocInfo jsdoc = getBestJSDocInfo(lhs);
if (jsdoc == null && !lhs.isFromExterns()) {
return false;
}
if (jsdoc != null && !jsdoc.hasConstAnnotation()) {
return false;
}
Node rhs = getRValueOfLValue(lhs);
if (rhs == null || !rhs.isQualifiedName()) {
return false;
}
Node parent = lhs.getParent();
return (lhs.isName() && parent.isVar())
|| (lhs.isGetProp() && lhs.isQualifiedName()
&& parent.isAssign() && parent.getParent().isExprResult());
} | java | static boolean isAliasedConstDefinition(Node lhs) {
JSDocInfo jsdoc = getBestJSDocInfo(lhs);
if (jsdoc == null && !lhs.isFromExterns()) {
return false;
}
if (jsdoc != null && !jsdoc.hasConstAnnotation()) {
return false;
}
Node rhs = getRValueOfLValue(lhs);
if (rhs == null || !rhs.isQualifiedName()) {
return false;
}
Node parent = lhs.getParent();
return (lhs.isName() && parent.isVar())
|| (lhs.isGetProp() && lhs.isQualifiedName()
&& parent.isAssign() && parent.getParent().isExprResult());
} | [
"static",
"boolean",
"isAliasedConstDefinition",
"(",
"Node",
"lhs",
")",
"{",
"JSDocInfo",
"jsdoc",
"=",
"getBestJSDocInfo",
"(",
"lhs",
")",
";",
"if",
"(",
"jsdoc",
"==",
"null",
"&&",
"!",
"lhs",
".",
"isFromExterns",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"jsdoc",
"!=",
"null",
"&&",
"!",
"jsdoc",
".",
"hasConstAnnotation",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"Node",
"rhs",
"=",
"getRValueOfLValue",
"(",
"lhs",
")",
";",
"if",
"(",
"rhs",
"==",
"null",
"||",
"!",
"rhs",
".",
"isQualifiedName",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"Node",
"parent",
"=",
"lhs",
".",
"getParent",
"(",
")",
";",
"return",
"(",
"lhs",
".",
"isName",
"(",
")",
"&&",
"parent",
".",
"isVar",
"(",
")",
")",
"||",
"(",
"lhs",
".",
"isGetProp",
"(",
")",
"&&",
"lhs",
".",
"isQualifiedName",
"(",
")",
"&&",
"parent",
".",
"isAssign",
"(",
")",
"&&",
"parent",
".",
"getParent",
"(",
")",
".",
"isExprResult",
"(",
")",
")",
";",
"}"
] | True for aliases defined with @const, not for aliases defined with @constructor/@interface. | [
"True",
"for",
"aliases",
"defined",
"with"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L950-L966 |
24,233 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isFromTypeSummary | public static boolean isFromTypeSummary(Node n) {
checkArgument(n.isScript(), n);
JSDocInfo info = n.getJSDocInfo();
return info != null && info.isTypeSummary();
} | java | public static boolean isFromTypeSummary(Node n) {
checkArgument(n.isScript(), n);
JSDocInfo info = n.getJSDocInfo();
return info != null && info.isTypeSummary();
} | [
"public",
"static",
"boolean",
"isFromTypeSummary",
"(",
"Node",
"n",
")",
"{",
"checkArgument",
"(",
"n",
".",
"isScript",
"(",
")",
",",
"n",
")",
";",
"JSDocInfo",
"info",
"=",
"n",
".",
"getJSDocInfo",
"(",
")",
";",
"return",
"info",
"!=",
"null",
"&&",
"info",
".",
"isTypeSummary",
"(",
")",
";",
"}"
] | Determine if the given SCRIPT is a @typeSummary file, like an i.js file | [
"Determine",
"if",
"the",
"given",
"SCRIPT",
"is",
"a"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L1045-L1049 |
24,234 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.mayEffectMutableState | static boolean mayEffectMutableState(Node n, AbstractCompiler compiler) {
checkNotNull(compiler);
return checkForStateChangeHelper(n, true, compiler);
} | java | static boolean mayEffectMutableState(Node n, AbstractCompiler compiler) {
checkNotNull(compiler);
return checkForStateChangeHelper(n, true, compiler);
} | [
"static",
"boolean",
"mayEffectMutableState",
"(",
"Node",
"n",
",",
"AbstractCompiler",
"compiler",
")",
"{",
"checkNotNull",
"(",
"compiler",
")",
";",
"return",
"checkForStateChangeHelper",
"(",
"n",
",",
"true",
",",
"compiler",
")",
";",
"}"
] | Returns true if the node may create new mutable state, or change existing state.
@see <a href="http://www.xkcd.org/326/">XKCD Cartoon</a> | [
"Returns",
"true",
"if",
"the",
"node",
"may",
"create",
"new",
"mutable",
"state",
"or",
"change",
"existing",
"state",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L1066-L1069 |
24,235 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.constructorCallHasSideEffects | static boolean constructorCallHasSideEffects(Node callNode) {
checkArgument(callNode.isNew(), "Expected NEW node, got %s", callNode.getToken());
if (callNode.isNoSideEffectsCall()) {
return false;
}
if (callNode.isOnlyModifiesArgumentsCall() && allArgsUnescapedLocal(callNode)) {
return false;
}
Node nameNode = callNode.getFirstChild();
return !nameNode.isName() || !CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString());
} | java | static boolean constructorCallHasSideEffects(Node callNode) {
checkArgument(callNode.isNew(), "Expected NEW node, got %s", callNode.getToken());
if (callNode.isNoSideEffectsCall()) {
return false;
}
if (callNode.isOnlyModifiesArgumentsCall() && allArgsUnescapedLocal(callNode)) {
return false;
}
Node nameNode = callNode.getFirstChild();
return !nameNode.isName() || !CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString());
} | [
"static",
"boolean",
"constructorCallHasSideEffects",
"(",
"Node",
"callNode",
")",
"{",
"checkArgument",
"(",
"callNode",
".",
"isNew",
"(",
")",
",",
"\"Expected NEW node, got %s\"",
",",
"callNode",
".",
"getToken",
"(",
")",
")",
";",
"if",
"(",
"callNode",
".",
"isNoSideEffectsCall",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"callNode",
".",
"isOnlyModifiesArgumentsCall",
"(",
")",
"&&",
"allArgsUnescapedLocal",
"(",
"callNode",
")",
")",
"{",
"return",
"false",
";",
"}",
"Node",
"nameNode",
"=",
"callNode",
".",
"getFirstChild",
"(",
")",
";",
"return",
"!",
"nameNode",
".",
"isName",
"(",
")",
"||",
"!",
"CONSTRUCTORS_WITHOUT_SIDE_EFFECTS",
".",
"contains",
"(",
"nameNode",
".",
"getString",
"(",
")",
")",
";",
"}"
] | Do calls to this constructor have side effects?
@param callNode - constructor call node | [
"Do",
"calls",
"to",
"this",
"constructor",
"have",
"side",
"effects?"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L1357-L1370 |
24,236 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.nodeTypeMayHaveSideEffects | static boolean nodeTypeMayHaveSideEffects(Node n, AbstractCompiler compiler) {
if (isAssignmentOp(n)) {
return true;
}
switch (n.getToken()) {
case DELPROP:
case DEC:
case INC:
case YIELD:
case THROW:
case AWAIT:
case FOR_IN: // assigns to a loop LHS
case FOR_OF: // assigns to a loop LHS, runs an iterator
case FOR_AWAIT_OF: // assigns to a loop LHS, runs an iterator, async operations.
return true;
case CALL:
case TAGGED_TEMPLATELIT:
return NodeUtil.functionCallHasSideEffects(n, compiler);
case NEW:
return NodeUtil.constructorCallHasSideEffects(n);
case NAME:
// A variable definition.
// TODO(b/129564961): Consider EXPORT declarations.
return n.hasChildren();
case REST:
case SPREAD:
return NodeUtil.iteratesImpureIterable(n);
default:
break;
}
return false;
} | java | static boolean nodeTypeMayHaveSideEffects(Node n, AbstractCompiler compiler) {
if (isAssignmentOp(n)) {
return true;
}
switch (n.getToken()) {
case DELPROP:
case DEC:
case INC:
case YIELD:
case THROW:
case AWAIT:
case FOR_IN: // assigns to a loop LHS
case FOR_OF: // assigns to a loop LHS, runs an iterator
case FOR_AWAIT_OF: // assigns to a loop LHS, runs an iterator, async operations.
return true;
case CALL:
case TAGGED_TEMPLATELIT:
return NodeUtil.functionCallHasSideEffects(n, compiler);
case NEW:
return NodeUtil.constructorCallHasSideEffects(n);
case NAME:
// A variable definition.
// TODO(b/129564961): Consider EXPORT declarations.
return n.hasChildren();
case REST:
case SPREAD:
return NodeUtil.iteratesImpureIterable(n);
default:
break;
}
return false;
} | [
"static",
"boolean",
"nodeTypeMayHaveSideEffects",
"(",
"Node",
"n",
",",
"AbstractCompiler",
"compiler",
")",
"{",
"if",
"(",
"isAssignmentOp",
"(",
"n",
")",
")",
"{",
"return",
"true",
";",
"}",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"DELPROP",
":",
"case",
"DEC",
":",
"case",
"INC",
":",
"case",
"YIELD",
":",
"case",
"THROW",
":",
"case",
"AWAIT",
":",
"case",
"FOR_IN",
":",
"// assigns to a loop LHS",
"case",
"FOR_OF",
":",
"// assigns to a loop LHS, runs an iterator",
"case",
"FOR_AWAIT_OF",
":",
"// assigns to a loop LHS, runs an iterator, async operations.",
"return",
"true",
";",
"case",
"CALL",
":",
"case",
"TAGGED_TEMPLATELIT",
":",
"return",
"NodeUtil",
".",
"functionCallHasSideEffects",
"(",
"n",
",",
"compiler",
")",
";",
"case",
"NEW",
":",
"return",
"NodeUtil",
".",
"constructorCallHasSideEffects",
"(",
"n",
")",
";",
"case",
"NAME",
":",
"// A variable definition.",
"// TODO(b/129564961): Consider EXPORT declarations.",
"return",
"n",
".",
"hasChildren",
"(",
")",
";",
"case",
"REST",
":",
"case",
"SPREAD",
":",
"return",
"NodeUtil",
".",
"iteratesImpureIterable",
"(",
"n",
")",
";",
"default",
":",
"break",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true if the current node's type implies side effects.
<p>This is a non-recursive version of the may have side effects
check; used to check wherever the current node's type is one of
the reasons why a subtree has side effects. | [
"Returns",
"true",
"if",
"the",
"current",
"node",
"s",
"type",
"implies",
"side",
"effects",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L1549-L1582 |
24,237 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.mayBeString | static boolean mayBeString(Node n, boolean useType) {
if (useType) {
JSType type = n.getJSType();
if (type != null) {
if (type.isStringValueType()) {
return true;
} else if (type.isNumberValueType() || type.isBooleanValueType() || type.isNullType()
|| type.isVoidType()) {
return false;
}
}
}
return mayBeString(getKnownValueType(n));
} | java | static boolean mayBeString(Node n, boolean useType) {
if (useType) {
JSType type = n.getJSType();
if (type != null) {
if (type.isStringValueType()) {
return true;
} else if (type.isNumberValueType() || type.isBooleanValueType() || type.isNullType()
|| type.isVoidType()) {
return false;
}
}
}
return mayBeString(getKnownValueType(n));
} | [
"static",
"boolean",
"mayBeString",
"(",
"Node",
"n",
",",
"boolean",
"useType",
")",
"{",
"if",
"(",
"useType",
")",
"{",
"JSType",
"type",
"=",
"n",
".",
"getJSType",
"(",
")",
";",
"if",
"(",
"type",
"!=",
"null",
")",
"{",
"if",
"(",
"type",
".",
"isStringValueType",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"else",
"if",
"(",
"type",
".",
"isNumberValueType",
"(",
")",
"||",
"type",
".",
"isBooleanValueType",
"(",
")",
"||",
"type",
".",
"isNullType",
"(",
")",
"||",
"type",
".",
"isVoidType",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"}",
"return",
"mayBeString",
"(",
"getKnownValueType",
"(",
"n",
")",
")",
";",
"}"
] | Return if the node is possibly a string.
@param n The node.
@param useType If true and the node has a primitive type, return true if that type is string
and false otherwise.
@return Whether the results is possibly a string. | [
"Return",
"if",
"the",
"node",
"is",
"possibly",
"a",
"string",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2019-L2032 |
24,238 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getEnclosingType | public static Node getEnclosingType(Node n, final Token type) {
return getEnclosingNode(
n,
new Predicate<Node>() {
@Override
public boolean apply(Node n) {
return n.getToken() == type;
}
});
} | java | public static Node getEnclosingType(Node n, final Token type) {
return getEnclosingNode(
n,
new Predicate<Node>() {
@Override
public boolean apply(Node n) {
return n.getToken() == type;
}
});
} | [
"public",
"static",
"Node",
"getEnclosingType",
"(",
"Node",
"n",
",",
"final",
"Token",
"type",
")",
"{",
"return",
"getEnclosingNode",
"(",
"n",
",",
"new",
"Predicate",
"<",
"Node",
">",
"(",
")",
"{",
"@",
"Override",
"public",
"boolean",
"apply",
"(",
"Node",
"n",
")",
"{",
"return",
"n",
".",
"getToken",
"(",
")",
"==",
"type",
";",
"}",
"}",
")",
";",
"}"
] | Gets the closest ancestor to the given node of the provided type. | [
"Gets",
"the",
"closest",
"ancestor",
"to",
"the",
"given",
"node",
"of",
"the",
"provided",
"type",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2230-L2239 |
24,239 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.referencesThis | static boolean referencesThis(Node n) {
if (n.isFunction()) {
return referencesThis(NodeUtil.getFunctionParameters(n))
|| referencesThis(NodeUtil.getFunctionBody(n));
} else {
return has(n, Node::isThis, MATCH_ANYTHING_BUT_NON_ARROW_FUNCTION);
}
} | java | static boolean referencesThis(Node n) {
if (n.isFunction()) {
return referencesThis(NodeUtil.getFunctionParameters(n))
|| referencesThis(NodeUtil.getFunctionBody(n));
} else {
return has(n, Node::isThis, MATCH_ANYTHING_BUT_NON_ARROW_FUNCTION);
}
} | [
"static",
"boolean",
"referencesThis",
"(",
"Node",
"n",
")",
"{",
"if",
"(",
"n",
".",
"isFunction",
"(",
")",
")",
"{",
"return",
"referencesThis",
"(",
"NodeUtil",
".",
"getFunctionParameters",
"(",
"n",
")",
")",
"||",
"referencesThis",
"(",
"NodeUtil",
".",
"getFunctionBody",
"(",
"n",
")",
")",
";",
"}",
"else",
"{",
"return",
"has",
"(",
"n",
",",
"Node",
"::",
"isThis",
",",
"MATCH_ANYTHING_BUT_NON_ARROW_FUNCTION",
")",
";",
"}",
"}"
] | Returns true if the shallow scope contains references to 'this' keyword | [
"Returns",
"true",
"if",
"the",
"shallow",
"scope",
"contains",
"references",
"to",
"this",
"keyword"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2344-L2351 |
24,240 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.referencesSuper | static boolean referencesSuper(Node n) {
Node curr = n.getFirstChild();
while (curr != null) {
if (containsType(curr, Token.SUPER, node -> !node.isClass())) {
return true;
}
curr = curr.getNext();
}
return false;
} | java | static boolean referencesSuper(Node n) {
Node curr = n.getFirstChild();
while (curr != null) {
if (containsType(curr, Token.SUPER, node -> !node.isClass())) {
return true;
}
curr = curr.getNext();
}
return false;
} | [
"static",
"boolean",
"referencesSuper",
"(",
"Node",
"n",
")",
"{",
"Node",
"curr",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"while",
"(",
"curr",
"!=",
"null",
")",
"{",
"if",
"(",
"containsType",
"(",
"curr",
",",
"Token",
".",
"SUPER",
",",
"node",
"->",
"!",
"node",
".",
"isClass",
"(",
")",
")",
")",
"{",
"return",
"true",
";",
"}",
"curr",
"=",
"curr",
".",
"getNext",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] | Returns true if the current scope contains references to the 'super' keyword.
Note that if there are classes declared inside the current class, super calls which
reference those classes are not reported. | [
"Returns",
"true",
"if",
"the",
"current",
"scope",
"contains",
"references",
"to",
"the",
"super",
"keyword",
".",
"Note",
"that",
"if",
"there",
"are",
"classes",
"declared",
"inside",
"the",
"current",
"class",
"super",
"calls",
"which",
"reference",
"those",
"classes",
"are",
"not",
"reported",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2358-L2367 |
24,241 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isBlockScopedDeclaration | static boolean isBlockScopedDeclaration(Node n) {
if (n.isName()) {
switch (n.getParent().getToken()) {
case LET:
case CONST:
case CATCH:
return true;
case CLASS:
return n.getParent().getFirstChild() == n;
case FUNCTION:
return isBlockScopedFunctionDeclaration(n.getParent());
default:
break;
}
}
return false;
} | java | static boolean isBlockScopedDeclaration(Node n) {
if (n.isName()) {
switch (n.getParent().getToken()) {
case LET:
case CONST:
case CATCH:
return true;
case CLASS:
return n.getParent().getFirstChild() == n;
case FUNCTION:
return isBlockScopedFunctionDeclaration(n.getParent());
default:
break;
}
}
return false;
} | [
"static",
"boolean",
"isBlockScopedDeclaration",
"(",
"Node",
"n",
")",
"{",
"if",
"(",
"n",
".",
"isName",
"(",
")",
")",
"{",
"switch",
"(",
"n",
".",
"getParent",
"(",
")",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"LET",
":",
"case",
"CONST",
":",
"case",
"CATCH",
":",
"return",
"true",
";",
"case",
"CLASS",
":",
"return",
"n",
".",
"getParent",
"(",
")",
".",
"getFirstChild",
"(",
")",
"==",
"n",
";",
"case",
"FUNCTION",
":",
"return",
"isBlockScopedFunctionDeclaration",
"(",
"n",
".",
"getParent",
"(",
")",
")",
";",
"default",
":",
"break",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Is this node the name of a block-scoped declaration?
Checks for let, const, class, or block-scoped function declarations.
@param n The node
@return True if {@code n} is the NAME of a block-scoped declaration. | [
"Is",
"this",
"node",
"the",
"name",
"of",
"a",
"block",
"-",
"scoped",
"declaration?",
"Checks",
"for",
"let",
"const",
"class",
"or",
"block",
"-",
"scoped",
"function",
"declarations",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2384-L2400 |
24,242 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isNameDeclaration | public static boolean isNameDeclaration(Node n) {
return n != null && (n.isVar() || n.isLet() || n.isConst());
} | java | public static boolean isNameDeclaration(Node n) {
return n != null && (n.isVar() || n.isLet() || n.isConst());
} | [
"public",
"static",
"boolean",
"isNameDeclaration",
"(",
"Node",
"n",
")",
"{",
"return",
"n",
"!=",
"null",
"&&",
"(",
"n",
".",
"isVar",
"(",
")",
"||",
"n",
".",
"isLet",
"(",
")",
"||",
"n",
".",
"isConst",
"(",
")",
")",
";",
"}"
] | Is this node a name declaration?
@param n The node
@return True if {@code n} is VAR, LET or CONST | [
"Is",
"this",
"node",
"a",
"name",
"declaration?"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2408-L2410 |
24,243 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getAssignedValue | public static Node getAssignedValue(Node n) {
checkState(n.isName() || n.isGetProp(), n);
Node parent = n.getParent();
if (NodeUtil.isNameDeclaration(parent)) {
return n.getFirstChild();
} else if (parent.isAssign() && parent.getFirstChild() == n) {
return n.getNext();
} else {
return null;
}
} | java | public static Node getAssignedValue(Node n) {
checkState(n.isName() || n.isGetProp(), n);
Node parent = n.getParent();
if (NodeUtil.isNameDeclaration(parent)) {
return n.getFirstChild();
} else if (parent.isAssign() && parent.getFirstChild() == n) {
return n.getNext();
} else {
return null;
}
} | [
"public",
"static",
"Node",
"getAssignedValue",
"(",
"Node",
"n",
")",
"{",
"checkState",
"(",
"n",
".",
"isName",
"(",
")",
"||",
"n",
".",
"isGetProp",
"(",
")",
",",
"n",
")",
";",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"NodeUtil",
".",
"isNameDeclaration",
"(",
"parent",
")",
")",
"{",
"return",
"n",
".",
"getFirstChild",
"(",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isAssign",
"(",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
")",
"{",
"return",
"n",
".",
"getNext",
"(",
")",
";",
"}",
"else",
"{",
"return",
"null",
";",
"}",
"}"
] | For an assignment or variable declaration get the assigned value.
@return The value node representing the new value. | [
"For",
"an",
"assignment",
"or",
"variable",
"declaration",
"get",
"the",
"assigned",
"value",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2432-L2442 |
24,244 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isLoopStructure | static boolean isLoopStructure(Node n) {
switch (n.getToken()) {
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case DO:
case WHILE:
return true;
default:
return false;
}
} | java | static boolean isLoopStructure(Node n) {
switch (n.getToken()) {
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case DO:
case WHILE:
return true;
default:
return false;
}
} | [
"static",
"boolean",
"isLoopStructure",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"FOR",
":",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"case",
"DO",
":",
"case",
"WHILE",
":",
"return",
"true",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | Determines whether the given node is a FOR, DO, or WHILE node. | [
"Determines",
"whether",
"the",
"given",
"node",
"is",
"a",
"FOR",
"DO",
"or",
"WHILE",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2483-L2495 |
24,245 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isControlStructure | public static boolean isControlStructure(Node n) {
switch (n.getToken()) {
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case DO:
case WHILE:
case WITH:
case IF:
case LABEL:
case TRY:
case CATCH:
case SWITCH:
case CASE:
case DEFAULT_CASE:
return true;
default:
return false;
}
} | java | public static boolean isControlStructure(Node n) {
switch (n.getToken()) {
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case DO:
case WHILE:
case WITH:
case IF:
case LABEL:
case TRY:
case CATCH:
case SWITCH:
case CASE:
case DEFAULT_CASE:
return true;
default:
return false;
}
} | [
"public",
"static",
"boolean",
"isControlStructure",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"FOR",
":",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"case",
"DO",
":",
"case",
"WHILE",
":",
"case",
"WITH",
":",
"case",
"IF",
":",
"case",
"LABEL",
":",
"case",
"TRY",
":",
"case",
"CATCH",
":",
"case",
"SWITCH",
":",
"case",
"CASE",
":",
"case",
"DEFAULT_CASE",
":",
"return",
"true",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node. | [
"Determines",
"whether",
"the",
"given",
"node",
"is",
"a",
"FOR",
"DO",
"WHILE",
"WITH",
"or",
"IF",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2537-L2557 |
24,246 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isControlStructureCodeBlock | static boolean isControlStructureCodeBlock(Node parent, Node n) {
switch (parent.getToken()) {
case DO:
return parent.getFirstChild() == n;
case TRY:
return parent.getFirstChild() == n || parent.getLastChild() == n;
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case WHILE:
case LABEL:
case WITH:
case CATCH:
return parent.getLastChild() == n;
case IF:
case SWITCH:
case CASE:
return parent.getFirstChild() != n;
case DEFAULT_CASE:
return true;
default:
checkState(isControlStructure(parent), parent);
return false;
}
} | java | static boolean isControlStructureCodeBlock(Node parent, Node n) {
switch (parent.getToken()) {
case DO:
return parent.getFirstChild() == n;
case TRY:
return parent.getFirstChild() == n || parent.getLastChild() == n;
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case WHILE:
case LABEL:
case WITH:
case CATCH:
return parent.getLastChild() == n;
case IF:
case SWITCH:
case CASE:
return parent.getFirstChild() != n;
case DEFAULT_CASE:
return true;
default:
checkState(isControlStructure(parent), parent);
return false;
}
} | [
"static",
"boolean",
"isControlStructureCodeBlock",
"(",
"Node",
"parent",
",",
"Node",
"n",
")",
"{",
"switch",
"(",
"parent",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"DO",
":",
"return",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
";",
"case",
"TRY",
":",
"return",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
"||",
"parent",
".",
"getLastChild",
"(",
")",
"==",
"n",
";",
"case",
"FOR",
":",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"case",
"WHILE",
":",
"case",
"LABEL",
":",
"case",
"WITH",
":",
"case",
"CATCH",
":",
"return",
"parent",
".",
"getLastChild",
"(",
")",
"==",
"n",
";",
"case",
"IF",
":",
"case",
"SWITCH",
":",
"case",
"CASE",
":",
"return",
"parent",
".",
"getFirstChild",
"(",
")",
"!=",
"n",
";",
"case",
"DEFAULT_CASE",
":",
"return",
"true",
";",
"default",
":",
"checkState",
"(",
"isControlStructure",
"(",
"parent",
")",
",",
"parent",
")",
";",
"return",
"false",
";",
"}",
"}"
] | Determines whether the given node is code node for FOR, DO,
WHILE, WITH, or IF node. | [
"Determines",
"whether",
"the",
"given",
"node",
"is",
"code",
"node",
"for",
"FOR",
"DO",
"WHILE",
"WITH",
"or",
"IF",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2563-L2588 |
24,247 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.createsBlockScope | static boolean createsBlockScope(Node n) {
switch (n.getToken()) {
case BLOCK:
Node parent = n.getParent();
// Don't create block scope for switch cases or catch blocks.
return parent != null && !isSwitchCase(parent) && !parent.isCatch();
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case SWITCH:
case CLASS:
return true;
default:
return false;
}
} | java | static boolean createsBlockScope(Node n) {
switch (n.getToken()) {
case BLOCK:
Node parent = n.getParent();
// Don't create block scope for switch cases or catch blocks.
return parent != null && !isSwitchCase(parent) && !parent.isCatch();
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
case SWITCH:
case CLASS:
return true;
default:
return false;
}
} | [
"static",
"boolean",
"createsBlockScope",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"BLOCK",
":",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"// Don't create block scope for switch cases or catch blocks.",
"return",
"parent",
"!=",
"null",
"&&",
"!",
"isSwitchCase",
"(",
"parent",
")",
"&&",
"!",
"parent",
".",
"isCatch",
"(",
")",
";",
"case",
"FOR",
":",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"case",
"SWITCH",
":",
"case",
"CLASS",
":",
"return",
"true",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | A block scope is created by a non-synthetic block node, a for loop node,
or a for-of loop node.
<p>Note: for functions, we use two separate scopes for parameters and
declarations in the body. We need to make sure default parameters cannot
reference var / function declarations in the body.
@return Whether the node creates a block scope. | [
"A",
"block",
"scope",
"is",
"created",
"by",
"a",
"non",
"-",
"synthetic",
"block",
"node",
"a",
"for",
"loop",
"node",
"or",
"a",
"for",
"-",
"of",
"loop",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2632-L2648 |
24,248 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isTryFinallyNode | static boolean isTryFinallyNode(Node parent, Node child) {
return parent.isTry() && parent.hasXChildren(3)
&& child == parent.getLastChild();
} | java | static boolean isTryFinallyNode(Node parent, Node child) {
return parent.isTry() && parent.hasXChildren(3)
&& child == parent.getLastChild();
} | [
"static",
"boolean",
"isTryFinallyNode",
"(",
"Node",
"parent",
",",
"Node",
"child",
")",
"{",
"return",
"parent",
".",
"isTry",
"(",
")",
"&&",
"parent",
".",
"hasXChildren",
"(",
"3",
")",
"&&",
"child",
"==",
"parent",
".",
"getLastChild",
"(",
")",
";",
"}"
] | Whether the child node is the FINALLY block of a try. | [
"Whether",
"the",
"child",
"node",
"is",
"the",
"FINALLY",
"block",
"of",
"a",
"try",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2725-L2728 |
24,249 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isTryCatchNodeContainer | static boolean isTryCatchNodeContainer(Node n) {
Node parent = n.getParent();
return parent.isTry()
&& parent.getSecondChild() == n;
} | java | static boolean isTryCatchNodeContainer(Node n) {
Node parent = n.getParent();
return parent.isTry()
&& parent.getSecondChild() == n;
} | [
"static",
"boolean",
"isTryCatchNodeContainer",
"(",
"Node",
"n",
")",
"{",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"return",
"parent",
".",
"isTry",
"(",
")",
"&&",
"parent",
".",
"getSecondChild",
"(",
")",
"==",
"n",
";",
"}"
] | Whether the node is a CATCH container BLOCK. | [
"Whether",
"the",
"node",
"is",
"a",
"CATCH",
"container",
"BLOCK",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2731-L2735 |
24,250 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.deleteChildren | public static void deleteChildren(Node n, AbstractCompiler compiler) {
while (n.hasChildren()) {
deleteNode(n.getFirstChild(), compiler);
}
} | java | public static void deleteChildren(Node n, AbstractCompiler compiler) {
while (n.hasChildren()) {
deleteNode(n.getFirstChild(), compiler);
}
} | [
"public",
"static",
"void",
"deleteChildren",
"(",
"Node",
"n",
",",
"AbstractCompiler",
"compiler",
")",
"{",
"while",
"(",
"n",
".",
"hasChildren",
"(",
")",
")",
"{",
"deleteNode",
"(",
"n",
".",
"getFirstChild",
"(",
")",
",",
"compiler",
")",
";",
"}",
"}"
] | Permanently delete all the children of the given node, including reporting changes. | [
"Permanently",
"delete",
"all",
"the",
"children",
"of",
"the",
"given",
"node",
"including",
"reporting",
"changes",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2779-L2783 |
24,251 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.removeChild | public static void removeChild(Node parent, Node node) {
if (isTryFinallyNode(parent, node)) {
if (NodeUtil.hasCatchHandler(getCatchBlock(parent))) {
// A finally can only be removed if there is a catch.
parent.removeChild(node);
} else {
// Otherwise, only its children can be removed.
node.detachChildren();
}
} else if (node.isCatch()) {
// The CATCH can can only be removed if there is a finally clause.
Node tryNode = node.getGrandparent();
checkState(NodeUtil.hasFinally(tryNode));
node.detach();
} else if (isTryCatchNodeContainer(node)) {
// The container node itself can't be removed, but the contained CATCH
// can if there is a 'finally' clause
Node tryNode = node.getParent();
checkState(NodeUtil.hasFinally(tryNode));
node.detachChildren();
} else if (node.isBlock()) {
// Simply empty the block. This maintains source location and
// "synthetic"-ness.
node.detachChildren();
} else if (isStatementBlock(parent)
|| isSwitchCase(node)
|| node.isMemberFunctionDef()) {
// A statement in a block or a member function can simply be removed
parent.removeChild(node);
} else if (isNameDeclaration(parent) || parent.isExprResult()) {
if (parent.hasMoreThanOneChild()) {
parent.removeChild(node);
} else {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// This would leave an empty VAR, remove the VAR itself.
removeChild(parent.getParent(), parent);
}
} else if (parent.isLabel()
&& node == parent.getLastChild()) {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// A LABEL without children can not be referred to, remove it.
removeChild(parent.getParent(), parent);
} else if (parent.isVanillaFor()) {
// Only Token.FOR can have an Token.EMPTY other control structure
// need something for the condition. Others need to be replaced
// or the structure removed.
parent.replaceChild(node, IR.empty());
} else if (parent.isObjectPattern()) {
// Remove the name from the object pattern
parent.removeChild(node);
} else if (parent.isArrayPattern()) {
if (node == parent.getLastChild()) {
parent.removeChild(node);
} else {
parent.replaceChild(node, IR.empty());
}
} else if (parent.isDestructuringLhs()) {
// Destructuring is empty so we should remove the node
parent.removeChild(node);
if (parent.getParent().hasChildren()) {
// removing the destructuring could leave an empty variable declaration node, so we would
// want to remove it from the AST
removeChild(parent.getParent(), parent);
}
} else if (parent.isRest()) {
// Rest params can only ever have one child node
parent.detach();
} else if (parent.isParamList()) {
parent.removeChild(node);
} else if (parent.isImport()) {
// An import node must always have three child nodes. Only the first can be safely removed.
if (node == parent.getFirstChild()) {
parent.replaceChild(node, IR.empty());
} else {
throw new IllegalStateException("Invalid attempt to remove: " + node + " from " + parent);
}
} else {
throw new IllegalStateException("Invalid attempt to remove node: " + node + " of " + parent);
}
} | java | public static void removeChild(Node parent, Node node) {
if (isTryFinallyNode(parent, node)) {
if (NodeUtil.hasCatchHandler(getCatchBlock(parent))) {
// A finally can only be removed if there is a catch.
parent.removeChild(node);
} else {
// Otherwise, only its children can be removed.
node.detachChildren();
}
} else if (node.isCatch()) {
// The CATCH can can only be removed if there is a finally clause.
Node tryNode = node.getGrandparent();
checkState(NodeUtil.hasFinally(tryNode));
node.detach();
} else if (isTryCatchNodeContainer(node)) {
// The container node itself can't be removed, but the contained CATCH
// can if there is a 'finally' clause
Node tryNode = node.getParent();
checkState(NodeUtil.hasFinally(tryNode));
node.detachChildren();
} else if (node.isBlock()) {
// Simply empty the block. This maintains source location and
// "synthetic"-ness.
node.detachChildren();
} else if (isStatementBlock(parent)
|| isSwitchCase(node)
|| node.isMemberFunctionDef()) {
// A statement in a block or a member function can simply be removed
parent.removeChild(node);
} else if (isNameDeclaration(parent) || parent.isExprResult()) {
if (parent.hasMoreThanOneChild()) {
parent.removeChild(node);
} else {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// This would leave an empty VAR, remove the VAR itself.
removeChild(parent.getParent(), parent);
}
} else if (parent.isLabel()
&& node == parent.getLastChild()) {
// Remove the node from the parent, so it can be reused.
parent.removeChild(node);
// A LABEL without children can not be referred to, remove it.
removeChild(parent.getParent(), parent);
} else if (parent.isVanillaFor()) {
// Only Token.FOR can have an Token.EMPTY other control structure
// need something for the condition. Others need to be replaced
// or the structure removed.
parent.replaceChild(node, IR.empty());
} else if (parent.isObjectPattern()) {
// Remove the name from the object pattern
parent.removeChild(node);
} else if (parent.isArrayPattern()) {
if (node == parent.getLastChild()) {
parent.removeChild(node);
} else {
parent.replaceChild(node, IR.empty());
}
} else if (parent.isDestructuringLhs()) {
// Destructuring is empty so we should remove the node
parent.removeChild(node);
if (parent.getParent().hasChildren()) {
// removing the destructuring could leave an empty variable declaration node, so we would
// want to remove it from the AST
removeChild(parent.getParent(), parent);
}
} else if (parent.isRest()) {
// Rest params can only ever have one child node
parent.detach();
} else if (parent.isParamList()) {
parent.removeChild(node);
} else if (parent.isImport()) {
// An import node must always have three child nodes. Only the first can be safely removed.
if (node == parent.getFirstChild()) {
parent.replaceChild(node, IR.empty());
} else {
throw new IllegalStateException("Invalid attempt to remove: " + node + " from " + parent);
}
} else {
throw new IllegalStateException("Invalid attempt to remove node: " + node + " of " + parent);
}
} | [
"public",
"static",
"void",
"removeChild",
"(",
"Node",
"parent",
",",
"Node",
"node",
")",
"{",
"if",
"(",
"isTryFinallyNode",
"(",
"parent",
",",
"node",
")",
")",
"{",
"if",
"(",
"NodeUtil",
".",
"hasCatchHandler",
"(",
"getCatchBlock",
"(",
"parent",
")",
")",
")",
"{",
"// A finally can only be removed if there is a catch.",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"{",
"// Otherwise, only its children can be removed.",
"node",
".",
"detachChildren",
"(",
")",
";",
"}",
"}",
"else",
"if",
"(",
"node",
".",
"isCatch",
"(",
")",
")",
"{",
"// The CATCH can can only be removed if there is a finally clause.",
"Node",
"tryNode",
"=",
"node",
".",
"getGrandparent",
"(",
")",
";",
"checkState",
"(",
"NodeUtil",
".",
"hasFinally",
"(",
"tryNode",
")",
")",
";",
"node",
".",
"detach",
"(",
")",
";",
"}",
"else",
"if",
"(",
"isTryCatchNodeContainer",
"(",
"node",
")",
")",
"{",
"// The container node itself can't be removed, but the contained CATCH",
"// can if there is a 'finally' clause",
"Node",
"tryNode",
"=",
"node",
".",
"getParent",
"(",
")",
";",
"checkState",
"(",
"NodeUtil",
".",
"hasFinally",
"(",
"tryNode",
")",
")",
";",
"node",
".",
"detachChildren",
"(",
")",
";",
"}",
"else",
"if",
"(",
"node",
".",
"isBlock",
"(",
")",
")",
"{",
"// Simply empty the block. This maintains source location and",
"// \"synthetic\"-ness.",
"node",
".",
"detachChildren",
"(",
")",
";",
"}",
"else",
"if",
"(",
"isStatementBlock",
"(",
"parent",
")",
"||",
"isSwitchCase",
"(",
"node",
")",
"||",
"node",
".",
"isMemberFunctionDef",
"(",
")",
")",
"{",
"// A statement in a block or a member function can simply be removed",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"if",
"(",
"isNameDeclaration",
"(",
"parent",
")",
"||",
"parent",
".",
"isExprResult",
"(",
")",
")",
"{",
"if",
"(",
"parent",
".",
"hasMoreThanOneChild",
"(",
")",
")",
"{",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"{",
"// Remove the node from the parent, so it can be reused.",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"// This would leave an empty VAR, remove the VAR itself.",
"removeChild",
"(",
"parent",
".",
"getParent",
"(",
")",
",",
"parent",
")",
";",
"}",
"}",
"else",
"if",
"(",
"parent",
".",
"isLabel",
"(",
")",
"&&",
"node",
"==",
"parent",
".",
"getLastChild",
"(",
")",
")",
"{",
"// Remove the node from the parent, so it can be reused.",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"// A LABEL without children can not be referred to, remove it.",
"removeChild",
"(",
"parent",
".",
"getParent",
"(",
")",
",",
"parent",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isVanillaFor",
"(",
")",
")",
"{",
"// Only Token.FOR can have an Token.EMPTY other control structure",
"// need something for the condition. Others need to be replaced",
"// or the structure removed.",
"parent",
".",
"replaceChild",
"(",
"node",
",",
"IR",
".",
"empty",
"(",
")",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isObjectPattern",
"(",
")",
")",
"{",
"// Remove the name from the object pattern",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isArrayPattern",
"(",
")",
")",
"{",
"if",
"(",
"node",
"==",
"parent",
".",
"getLastChild",
"(",
")",
")",
"{",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"{",
"parent",
".",
"replaceChild",
"(",
"node",
",",
"IR",
".",
"empty",
"(",
")",
")",
";",
"}",
"}",
"else",
"if",
"(",
"parent",
".",
"isDestructuringLhs",
"(",
")",
")",
"{",
"// Destructuring is empty so we should remove the node",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"if",
"(",
"parent",
".",
"getParent",
"(",
")",
".",
"hasChildren",
"(",
")",
")",
"{",
"// removing the destructuring could leave an empty variable declaration node, so we would",
"// want to remove it from the AST",
"removeChild",
"(",
"parent",
".",
"getParent",
"(",
")",
",",
"parent",
")",
";",
"}",
"}",
"else",
"if",
"(",
"parent",
".",
"isRest",
"(",
")",
")",
"{",
"// Rest params can only ever have one child node",
"parent",
".",
"detach",
"(",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isParamList",
"(",
")",
")",
"{",
"parent",
".",
"removeChild",
"(",
"node",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isImport",
"(",
")",
")",
"{",
"// An import node must always have three child nodes. Only the first can be safely removed.",
"if",
"(",
"node",
"==",
"parent",
".",
"getFirstChild",
"(",
")",
")",
"{",
"parent",
".",
"replaceChild",
"(",
"node",
",",
"IR",
".",
"empty",
"(",
")",
")",
";",
"}",
"else",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"Invalid attempt to remove: \"",
"+",
"node",
"+",
"\" from \"",
"+",
"parent",
")",
";",
"}",
"}",
"else",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"Invalid attempt to remove node: \"",
"+",
"node",
"+",
"\" of \"",
"+",
"parent",
")",
";",
"}",
"}"
] | Safely remove children while maintaining a valid node structure.
In some cases, this is done by removing the parent from the AST as well. | [
"Safely",
"remove",
"children",
"while",
"maintaining",
"a",
"valid",
"node",
"structure",
".",
"In",
"some",
"cases",
"this",
"is",
"done",
"by",
"removing",
"the",
"parent",
"from",
"the",
"AST",
"as",
"well",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2789-L2870 |
24,252 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.maybeAddFinally | static void maybeAddFinally(Node tryNode) {
checkState(tryNode.isTry());
if (!NodeUtil.hasFinally(tryNode)) {
tryNode.addChildToBack(IR.block().srcref(tryNode));
}
} | java | static void maybeAddFinally(Node tryNode) {
checkState(tryNode.isTry());
if (!NodeUtil.hasFinally(tryNode)) {
tryNode.addChildToBack(IR.block().srcref(tryNode));
}
} | [
"static",
"void",
"maybeAddFinally",
"(",
"Node",
"tryNode",
")",
"{",
"checkState",
"(",
"tryNode",
".",
"isTry",
"(",
")",
")",
";",
"if",
"(",
"!",
"NodeUtil",
".",
"hasFinally",
"(",
"tryNode",
")",
")",
"{",
"tryNode",
".",
"addChildToBack",
"(",
"IR",
".",
"block",
"(",
")",
".",
"srcref",
"(",
"tryNode",
")",
")",
";",
"}",
"}"
] | Add a finally block if one does not exist. | [
"Add",
"a",
"finally",
"block",
"if",
"one",
"does",
"not",
"exist",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2909-L2914 |
24,253 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.tryMergeBlock | public static boolean tryMergeBlock(Node block, boolean alwaysMerge) {
checkState(block.isBlock());
Node parent = block.getParent();
boolean canMerge = alwaysMerge || canMergeBlock(block);
// 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 (isStatementBlock(parent) && canMerge) {
Node previous = block;
while (block.hasChildren()) {
Node child = block.removeFirstChild();
parent.addChildAfter(child, previous);
previous = child;
}
parent.removeChild(block);
return true;
} else {
return false;
}
} | java | public static boolean tryMergeBlock(Node block, boolean alwaysMerge) {
checkState(block.isBlock());
Node parent = block.getParent();
boolean canMerge = alwaysMerge || canMergeBlock(block);
// 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 (isStatementBlock(parent) && canMerge) {
Node previous = block;
while (block.hasChildren()) {
Node child = block.removeFirstChild();
parent.addChildAfter(child, previous);
previous = child;
}
parent.removeChild(block);
return true;
} else {
return false;
}
} | [
"public",
"static",
"boolean",
"tryMergeBlock",
"(",
"Node",
"block",
",",
"boolean",
"alwaysMerge",
")",
"{",
"checkState",
"(",
"block",
".",
"isBlock",
"(",
")",
")",
";",
"Node",
"parent",
"=",
"block",
".",
"getParent",
"(",
")",
";",
"boolean",
"canMerge",
"=",
"alwaysMerge",
"||",
"canMergeBlock",
"(",
"block",
")",
";",
"// 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",
"(",
"isStatementBlock",
"(",
"parent",
")",
"&&",
"canMerge",
")",
"{",
"Node",
"previous",
"=",
"block",
";",
"while",
"(",
"block",
".",
"hasChildren",
"(",
")",
")",
"{",
"Node",
"child",
"=",
"block",
".",
"removeFirstChild",
"(",
")",
";",
"parent",
".",
"addChildAfter",
"(",
"child",
",",
"previous",
")",
";",
"previous",
"=",
"child",
";",
"}",
"parent",
".",
"removeChild",
"(",
"block",
")",
";",
"return",
"true",
";",
"}",
"else",
"{",
"return",
"false",
";",
"}",
"}"
] | Merge a block with its parent block.
@return Whether the block was removed. | [
"Merge",
"a",
"block",
"with",
"its",
"parent",
"block",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2920-L2938 |
24,254 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.canMergeBlock | public static boolean canMergeBlock(Node block) {
for (Node c = block.getFirstChild(); c != null; c = c.getNext()) {
switch (c.getToken()) {
case LABEL:
if (canMergeBlock(c)){
continue;
} else {
return false;
}
case CONST:
case LET:
case CLASS:
case FUNCTION:
return false;
default:
continue;
}
}
return true;
} | java | public static boolean canMergeBlock(Node block) {
for (Node c = block.getFirstChild(); c != null; c = c.getNext()) {
switch (c.getToken()) {
case LABEL:
if (canMergeBlock(c)){
continue;
} else {
return false;
}
case CONST:
case LET:
case CLASS:
case FUNCTION:
return false;
default:
continue;
}
}
return true;
} | [
"public",
"static",
"boolean",
"canMergeBlock",
"(",
"Node",
"block",
")",
"{",
"for",
"(",
"Node",
"c",
"=",
"block",
".",
"getFirstChild",
"(",
")",
";",
"c",
"!=",
"null",
";",
"c",
"=",
"c",
".",
"getNext",
"(",
")",
")",
"{",
"switch",
"(",
"c",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"LABEL",
":",
"if",
"(",
"canMergeBlock",
"(",
"c",
")",
")",
"{",
"continue",
";",
"}",
"else",
"{",
"return",
"false",
";",
"}",
"case",
"CONST",
":",
"case",
"LET",
":",
"case",
"CLASS",
":",
"case",
"FUNCTION",
":",
"return",
"false",
";",
"default",
":",
"continue",
";",
"}",
"}",
"return",
"true",
";",
"}"
] | A check inside a block to see if there are const, let, class, or function declarations
to be safe and not hoist them into the upper block.
@return Whether the block can be removed | [
"A",
"check",
"inside",
"a",
"block",
"to",
"see",
"if",
"there",
"are",
"const",
"let",
"class",
"or",
"function",
"declarations",
"to",
"be",
"safe",
"and",
"not",
"hoist",
"them",
"into",
"the",
"upper",
"block",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2945-L2966 |
24,255 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getFunctionBody | public static Node getFunctionBody(Node fn) {
checkArgument(fn.isFunction(), fn);
return fn.getLastChild();
} | java | public static Node getFunctionBody(Node fn) {
checkArgument(fn.isFunction(), fn);
return fn.getLastChild();
} | [
"public",
"static",
"Node",
"getFunctionBody",
"(",
"Node",
"fn",
")",
"{",
"checkArgument",
"(",
"fn",
".",
"isFunction",
"(",
")",
",",
"fn",
")",
";",
"return",
"fn",
".",
"getLastChild",
"(",
")",
";",
"}"
] | Return a BLOCK node for the given FUNCTION node. | [
"Return",
"a",
"BLOCK",
"node",
"for",
"the",
"given",
"FUNCTION",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L2979-L2982 |
24,256 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isMethodDeclaration | public static boolean isMethodDeclaration(Node n) {
if (n.isFunction()) {
Node parent = n.getParent();
switch (parent.getToken()) {
case GETTER_DEF:
case SETTER_DEF:
case MEMBER_FUNCTION_DEF:
// `({ get x() {} })`
// `({ set x(v) {} })`
// `({ f() {} })`
return true;
case COMPUTED_PROP:
// `({ [expression]() {} })`
// `({ get [expression]() {} })`
// `({ set [expression](x) {} })`
// (but not `({ [expression]: function() {} })`
// The first child is the expression, and could possibly be a function.
return parent.getLastChild() == n
&& (parent.getBooleanProp(Node.COMPUTED_PROP_METHOD)
|| parent.getBooleanProp(Node.COMPUTED_PROP_GETTER)
|| parent.getBooleanProp(Node.COMPUTED_PROP_SETTER));
default:
return false;
}
} else {
return false;
}
} | java | public static boolean isMethodDeclaration(Node n) {
if (n.isFunction()) {
Node parent = n.getParent();
switch (parent.getToken()) {
case GETTER_DEF:
case SETTER_DEF:
case MEMBER_FUNCTION_DEF:
// `({ get x() {} })`
// `({ set x(v) {} })`
// `({ f() {} })`
return true;
case COMPUTED_PROP:
// `({ [expression]() {} })`
// `({ get [expression]() {} })`
// `({ set [expression](x) {} })`
// (but not `({ [expression]: function() {} })`
// The first child is the expression, and could possibly be a function.
return parent.getLastChild() == n
&& (parent.getBooleanProp(Node.COMPUTED_PROP_METHOD)
|| parent.getBooleanProp(Node.COMPUTED_PROP_GETTER)
|| parent.getBooleanProp(Node.COMPUTED_PROP_SETTER));
default:
return false;
}
} else {
return false;
}
} | [
"public",
"static",
"boolean",
"isMethodDeclaration",
"(",
"Node",
"n",
")",
"{",
"if",
"(",
"n",
".",
"isFunction",
"(",
")",
")",
"{",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"switch",
"(",
"parent",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"GETTER_DEF",
":",
"case",
"SETTER_DEF",
":",
"case",
"MEMBER_FUNCTION_DEF",
":",
"// `({ get x() {} })`",
"// `({ set x(v) {} })`",
"// `({ f() {} })`",
"return",
"true",
";",
"case",
"COMPUTED_PROP",
":",
"// `({ [expression]() {} })`",
"// `({ get [expression]() {} })`",
"// `({ set [expression](x) {} })`",
"// (but not `({ [expression]: function() {} })`",
"// The first child is the expression, and could possibly be a function.",
"return",
"parent",
".",
"getLastChild",
"(",
")",
"==",
"n",
"&&",
"(",
"parent",
".",
"getBooleanProp",
"(",
"Node",
".",
"COMPUTED_PROP_METHOD",
")",
"||",
"parent",
".",
"getBooleanProp",
"(",
"Node",
".",
"COMPUTED_PROP_GETTER",
")",
"||",
"parent",
".",
"getBooleanProp",
"(",
"Node",
".",
"COMPUTED_PROP_SETTER",
")",
")",
";",
"default",
":",
"return",
"false",
";",
"}",
"}",
"else",
"{",
"return",
"false",
";",
"}",
"}"
] | Is this node a class or object literal member function?
<p>examples:
<pre><code>
class C {
f() {}
get x() { return this.x_; }
set x(v) { this.x_ = v; }
[someExpr]() {}
}
obj = {
f() {}
get x() { return this.x_; }
set x(v) { this.x_ = v; }
[someExpr]() {}
}
</code></pre> | [
"Is",
"this",
"node",
"a",
"class",
"or",
"object",
"literal",
"member",
"function?"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3025-L3052 |
24,257 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isFunctionExpression | static boolean isFunctionExpression(Node n) {
return n.isFunction()
&& !NodeUtil.isFunctionDeclaration(n)
&& !NodeUtil.isMethodDeclaration(n);
} | java | static boolean isFunctionExpression(Node n) {
return n.isFunction()
&& !NodeUtil.isFunctionDeclaration(n)
&& !NodeUtil.isMethodDeclaration(n);
} | [
"static",
"boolean",
"isFunctionExpression",
"(",
"Node",
"n",
")",
"{",
"return",
"n",
".",
"isFunction",
"(",
")",
"&&",
"!",
"NodeUtil",
".",
"isFunctionDeclaration",
"(",
"n",
")",
"&&",
"!",
"NodeUtil",
".",
"isMethodDeclaration",
"(",
"n",
")",
";",
"}"
] | Is a FUNCTION node a function expression?
<p>A function expression is a function that:
<ul>
<li>has either no name or a name that is not added to the current scope
<li>AND can be manipulated as an expression
(assigned to variables, passed to functions, etc.)
i.e. It is not a method declaration on a class or object literal.
</ul>
<p>Some examples of function expressions:
<pre>
(function () {})
(function f() {})()
[ function f() {} ]
var f = function f() {};
for (function f() {};;) {}
export default function() {}
() => 1
</pre>
<p>Some examples of functions that are <em>not</em> expressions:
<pre>
function f() {}
if (x); else function f() {}
for (;;) { function f() {} }
export default function f() {}
({
f() {},
set x(v) {},
get x() {},
[expr]() {}
})
class {
f() {}
set x(v) {}
get x() {}
[expr]() {}
}
</pre>
@param n A node
@return Whether n is a function used within an expression. | [
"Is",
"a",
"FUNCTION",
"node",
"a",
"function",
"expression?"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3151-L3155 |
24,258 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isNameDeclOrSimpleAssignLhs | public static boolean isNameDeclOrSimpleAssignLhs(Node n, Node parent) {
return
(parent.isAssign() && parent.getFirstChild() == n) || NodeUtil.isNameDeclaration(parent);
} | java | public static boolean isNameDeclOrSimpleAssignLhs(Node n, Node parent) {
return
(parent.isAssign() && parent.getFirstChild() == n) || NodeUtil.isNameDeclaration(parent);
} | [
"public",
"static",
"boolean",
"isNameDeclOrSimpleAssignLhs",
"(",
"Node",
"n",
",",
"Node",
"parent",
")",
"{",
"return",
"(",
"parent",
".",
"isAssign",
"(",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
")",
"||",
"NodeUtil",
".",
"isNameDeclaration",
"(",
"parent",
")",
";",
"}"
] | Determines whether this node is strictly on the left hand side of an assign or var
initialization. Notably, this does not include all L-values, only statements where the node is
used only as an L-value.
@param n The node
@param parent Parent of the node
@return True if n is the left hand of an assign | [
"Determines",
"whether",
"this",
"node",
"is",
"strictly",
"on",
"the",
"left",
"hand",
"side",
"of",
"an",
"assign",
"or",
"var",
"initialization",
".",
"Notably",
"this",
"does",
"not",
"include",
"all",
"L",
"-",
"values",
"only",
"statements",
"where",
"the",
"node",
"is",
"used",
"only",
"as",
"an",
"L",
"-",
"value",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3309-L3312 |
24,259 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isLValue | public static boolean isLValue(Node n) {
switch (n.getToken()) {
case NAME:
case GETPROP:
case GETELEM:
break;
default:
return false;
}
Node parent = n.getParent();
if (parent == null) {
return false;
}
switch (parent.getToken()) {
case IMPORT_SPEC:
return parent.getLastChild() == n;
case VAR:
case LET:
case CONST:
case REST:
case PARAM_LIST:
case IMPORT:
case INC:
case DEC:
case CATCH:
return true;
case CLASS:
case FUNCTION:
case DEFAULT_VALUE:
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
return parent.getFirstChild() == n;
case ARRAY_PATTERN:
case STRING_KEY:
case COMPUTED_PROP:
return isLhsByDestructuring(n);
default:
return NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == n;
}
} | java | public static boolean isLValue(Node n) {
switch (n.getToken()) {
case NAME:
case GETPROP:
case GETELEM:
break;
default:
return false;
}
Node parent = n.getParent();
if (parent == null) {
return false;
}
switch (parent.getToken()) {
case IMPORT_SPEC:
return parent.getLastChild() == n;
case VAR:
case LET:
case CONST:
case REST:
case PARAM_LIST:
case IMPORT:
case INC:
case DEC:
case CATCH:
return true;
case CLASS:
case FUNCTION:
case DEFAULT_VALUE:
case FOR:
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
return parent.getFirstChild() == n;
case ARRAY_PATTERN:
case STRING_KEY:
case COMPUTED_PROP:
return isLhsByDestructuring(n);
default:
return NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == n;
}
} | [
"public",
"static",
"boolean",
"isLValue",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"NAME",
":",
"case",
"GETPROP",
":",
"case",
"GETELEM",
":",
"break",
";",
"default",
":",
"return",
"false",
";",
"}",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"parent",
"==",
"null",
")",
"{",
"return",
"false",
";",
"}",
"switch",
"(",
"parent",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"IMPORT_SPEC",
":",
"return",
"parent",
".",
"getLastChild",
"(",
")",
"==",
"n",
";",
"case",
"VAR",
":",
"case",
"LET",
":",
"case",
"CONST",
":",
"case",
"REST",
":",
"case",
"PARAM_LIST",
":",
"case",
"IMPORT",
":",
"case",
"INC",
":",
"case",
"DEC",
":",
"case",
"CATCH",
":",
"return",
"true",
";",
"case",
"CLASS",
":",
"case",
"FUNCTION",
":",
"case",
"DEFAULT_VALUE",
":",
"case",
"FOR",
":",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"return",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
";",
"case",
"ARRAY_PATTERN",
":",
"case",
"STRING_KEY",
":",
"case",
"COMPUTED_PROP",
":",
"return",
"isLhsByDestructuring",
"(",
"n",
")",
";",
"default",
":",
"return",
"NodeUtil",
".",
"isAssignmentOp",
"(",
"parent",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
";",
"}",
"}"
] | Determines whether this node is used as an L-value. Notice that sometimes
names are used as both L-values and R-values.
<p>We treat "var x;" and "let x;" as an L-value because it's syntactically similar to
"var x = undefined", even though it's technically not an L-value. But it kind of makes
sense if you treat it as "assignment to 'undefined' at the top of the scope".
@param n The node
@return True if n is an L-value. | [
"Determines",
"whether",
"this",
"node",
"is",
"used",
"as",
"an",
"L",
"-",
"value",
".",
"Notice",
"that",
"sometimes",
"names",
"are",
"used",
"as",
"both",
"L",
"-",
"values",
"and",
"R",
"-",
"values",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3325-L3368 |
24,260 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isDeclarationLValue | public static boolean isDeclarationLValue(Node n) {
boolean isLValue = isLValue(n);
if (!isLValue) {
return false;
}
Node parent = n.getParent();
switch (parent.getToken()) {
case IMPORT_SPEC:
case VAR:
case LET:
case CONST:
case PARAM_LIST:
case IMPORT:
case CATCH:
case CLASS:
case FUNCTION:
return true;
case STRING_KEY:
return isNameDeclaration(parent.getParent().getGrandparent());
case OBJECT_PATTERN:
case ARRAY_PATTERN:
return isNameDeclaration(parent.getGrandparent());
default:
return false;
}
} | java | public static boolean isDeclarationLValue(Node n) {
boolean isLValue = isLValue(n);
if (!isLValue) {
return false;
}
Node parent = n.getParent();
switch (parent.getToken()) {
case IMPORT_SPEC:
case VAR:
case LET:
case CONST:
case PARAM_LIST:
case IMPORT:
case CATCH:
case CLASS:
case FUNCTION:
return true;
case STRING_KEY:
return isNameDeclaration(parent.getParent().getGrandparent());
case OBJECT_PATTERN:
case ARRAY_PATTERN:
return isNameDeclaration(parent.getGrandparent());
default:
return false;
}
} | [
"public",
"static",
"boolean",
"isDeclarationLValue",
"(",
"Node",
"n",
")",
"{",
"boolean",
"isLValue",
"=",
"isLValue",
"(",
"n",
")",
";",
"if",
"(",
"!",
"isLValue",
")",
"{",
"return",
"false",
";",
"}",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"switch",
"(",
"parent",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"IMPORT_SPEC",
":",
"case",
"VAR",
":",
"case",
"LET",
":",
"case",
"CONST",
":",
"case",
"PARAM_LIST",
":",
"case",
"IMPORT",
":",
"case",
"CATCH",
":",
"case",
"CLASS",
":",
"case",
"FUNCTION",
":",
"return",
"true",
";",
"case",
"STRING_KEY",
":",
"return",
"isNameDeclaration",
"(",
"parent",
".",
"getParent",
"(",
")",
".",
"getGrandparent",
"(",
")",
")",
";",
"case",
"OBJECT_PATTERN",
":",
"case",
"ARRAY_PATTERN",
":",
"return",
"isNameDeclaration",
"(",
"parent",
".",
"getGrandparent",
"(",
")",
")",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | Determines whether this node is used as an L-value that is a declaration.
<p><code>x = 5;</code> is an L-value but does not declare a variable. | [
"Determines",
"whether",
"this",
"node",
"is",
"used",
"as",
"an",
"L",
"-",
"value",
"that",
"is",
"a",
"declaration",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3375-L3403 |
24,261 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getDeclaringParent | public static Node getDeclaringParent(Node targetNode) {
Node rootTarget = getRootTarget(targetNode);
Node parent = rootTarget.getParent();
if (parent.isRest() || parent.isDefaultValue()) {
// e.g. `function foo(targetNode1 = default, ...targetNode2) {}`
parent = parent.getParent();
checkState(parent.isParamList(), parent);
} else if (parent.isDestructuringLhs()) {
// e.g. `let [a, b] = something;` targetNode is `[a, b]`
parent = parent.getParent();
checkState(isNameDeclaration(parent), parent);
} else if (parent.isClass() || parent.isFunction()) {
// e.g. `function targetNode() {}`
// e.g. `class targetNode {}`
checkState(targetNode == parent.getFirstChild(), targetNode);
} else if (parent.isImportSpec()) {
// e.g. `import {foo as targetNode} from './foo';
checkState(targetNode == parent.getSecondChild(), targetNode);
// import -> import_specs -> import_spec
// we want import
parent = parent.getGrandparent();
checkState(parent.isImport(), parent);
} else {
// e.g. `function foo(targetNode) {};`
// e.g. `let targetNode = something;`
// e.g. `import targetNode from './foo';
// e.g. `} catch (foo) {`
checkState(
parent.isParamList()
|| isNameDeclaration(parent)
|| parent.isImport()
|| parent.isCatch(),
parent);
}
return parent;
} | java | public static Node getDeclaringParent(Node targetNode) {
Node rootTarget = getRootTarget(targetNode);
Node parent = rootTarget.getParent();
if (parent.isRest() || parent.isDefaultValue()) {
// e.g. `function foo(targetNode1 = default, ...targetNode2) {}`
parent = parent.getParent();
checkState(parent.isParamList(), parent);
} else if (parent.isDestructuringLhs()) {
// e.g. `let [a, b] = something;` targetNode is `[a, b]`
parent = parent.getParent();
checkState(isNameDeclaration(parent), parent);
} else if (parent.isClass() || parent.isFunction()) {
// e.g. `function targetNode() {}`
// e.g. `class targetNode {}`
checkState(targetNode == parent.getFirstChild(), targetNode);
} else if (parent.isImportSpec()) {
// e.g. `import {foo as targetNode} from './foo';
checkState(targetNode == parent.getSecondChild(), targetNode);
// import -> import_specs -> import_spec
// we want import
parent = parent.getGrandparent();
checkState(parent.isImport(), parent);
} else {
// e.g. `function foo(targetNode) {};`
// e.g. `let targetNode = something;`
// e.g. `import targetNode from './foo';
// e.g. `} catch (foo) {`
checkState(
parent.isParamList()
|| isNameDeclaration(parent)
|| parent.isImport()
|| parent.isCatch(),
parent);
}
return parent;
} | [
"public",
"static",
"Node",
"getDeclaringParent",
"(",
"Node",
"targetNode",
")",
"{",
"Node",
"rootTarget",
"=",
"getRootTarget",
"(",
"targetNode",
")",
";",
"Node",
"parent",
"=",
"rootTarget",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"parent",
".",
"isRest",
"(",
")",
"||",
"parent",
".",
"isDefaultValue",
"(",
")",
")",
"{",
"// e.g. `function foo(targetNode1 = default, ...targetNode2) {}`",
"parent",
"=",
"parent",
".",
"getParent",
"(",
")",
";",
"checkState",
"(",
"parent",
".",
"isParamList",
"(",
")",
",",
"parent",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isDestructuringLhs",
"(",
")",
")",
"{",
"// e.g. `let [a, b] = something;` targetNode is `[a, b]`",
"parent",
"=",
"parent",
".",
"getParent",
"(",
")",
";",
"checkState",
"(",
"isNameDeclaration",
"(",
"parent",
")",
",",
"parent",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isClass",
"(",
")",
"||",
"parent",
".",
"isFunction",
"(",
")",
")",
"{",
"// e.g. `function targetNode() {}`",
"// e.g. `class targetNode {}`",
"checkState",
"(",
"targetNode",
"==",
"parent",
".",
"getFirstChild",
"(",
")",
",",
"targetNode",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isImportSpec",
"(",
")",
")",
"{",
"// e.g. `import {foo as targetNode} from './foo';",
"checkState",
"(",
"targetNode",
"==",
"parent",
".",
"getSecondChild",
"(",
")",
",",
"targetNode",
")",
";",
"// import -> import_specs -> import_spec",
"// we want import",
"parent",
"=",
"parent",
".",
"getGrandparent",
"(",
")",
";",
"checkState",
"(",
"parent",
".",
"isImport",
"(",
")",
",",
"parent",
")",
";",
"}",
"else",
"{",
"// e.g. `function foo(targetNode) {};`",
"// e.g. `let targetNode = something;`",
"// e.g. `import targetNode from './foo';",
"// e.g. `} catch (foo) {`",
"checkState",
"(",
"parent",
".",
"isParamList",
"(",
")",
"||",
"isNameDeclaration",
"(",
"parent",
")",
"||",
"parent",
".",
"isImport",
"(",
")",
"||",
"parent",
".",
"isCatch",
"(",
")",
",",
"parent",
")",
";",
"}",
"return",
"parent",
";",
"}"
] | Returns the node that is effectively declaring the given target.
<p>Examples:
<pre><code>
const a = 1; // getDeclaringParent(a) returns CONST
let {[expression]: [x = 3]} = obj; // getDeclaringParent(x) returns LET
function foo({a, b}) {}; // getDeclaringParent(a) returns PARAM_LIST
function foo(a = 1) {}; // getDeclaringParent(a) returns PARAM_LIST
function foo({a, b} = obj) {}; // getDeclaringParent(a) returns PARAM_LIST
function foo(...a) {}; // getDeclaringParent(a) returns PARAM_LIST
function foo() {}; // gotRootTarget(foo) returns FUNCTION
class foo {}; // gotRootTarget(foo) returns CLASS
import foo from './foo'; // getDeclaringParent(foo) returns IMPORT
import {foo} from './foo'; // getDeclaringParent(foo) returns IMPORT
import {foo as bar} from './foo'; // getDeclaringParent(bar) returns IMPORT
} catch (err) { // getDeclaringParent(err) returns CATCH
</code></pre>
@param targetNode a NAME, OBJECT_PATTERN, or ARRAY_PATTERN
@return node of type LET, CONST, VAR, FUNCTION, CLASS, PARAM_LIST, CATCH, or IMPORT
@throws IllegalStateException if targetNode is not actually used as a declaration target | [
"Returns",
"the",
"node",
"that",
"is",
"effectively",
"declaring",
"the",
"given",
"target",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3439-L3474 |
24,262 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getRootTarget | public static Node getRootTarget(Node targetNode) {
Node enclosingTarget = targetNode;
for (Node nextTarget = getEnclosingTarget(enclosingTarget);
nextTarget != null;
nextTarget = getEnclosingTarget(enclosingTarget)) {
enclosingTarget = nextTarget;
}
return enclosingTarget;
} | java | public static Node getRootTarget(Node targetNode) {
Node enclosingTarget = targetNode;
for (Node nextTarget = getEnclosingTarget(enclosingTarget);
nextTarget != null;
nextTarget = getEnclosingTarget(enclosingTarget)) {
enclosingTarget = nextTarget;
}
return enclosingTarget;
} | [
"public",
"static",
"Node",
"getRootTarget",
"(",
"Node",
"targetNode",
")",
"{",
"Node",
"enclosingTarget",
"=",
"targetNode",
";",
"for",
"(",
"Node",
"nextTarget",
"=",
"getEnclosingTarget",
"(",
"enclosingTarget",
")",
";",
"nextTarget",
"!=",
"null",
";",
"nextTarget",
"=",
"getEnclosingTarget",
"(",
"enclosingTarget",
")",
")",
"{",
"enclosingTarget",
"=",
"nextTarget",
";",
"}",
"return",
"enclosingTarget",
";",
"}"
] | Returns the outermost target enclosing the given assignment target.
<p>Returns targetNode itself if there is no enclosing target.
<p>Examples:
<pre><code>
const a = 1; // getRootTarget(a) returns a
let {[expression]: [x = 3]} = obj; // getRootTarget(x) returns {[expression]: [x = 3]}
{a = 1} = obj; // getRootTarget(a) returns {a = 1}
{[expression]: [x = 3]} = obj; // getRootTarget(x) returns {[expression]: [x = 3]}
function foo({a, b}) {}; // getRootTarget(a) returns {a, b}
function foo(a = 1) {}; // getRootTarget(a) returns a
function foo({a, b} = obj) {}; // getRootTarget(a) returns a
function foo(...a) {}; // getRootTarget(a) returns a
function foo() {}; // gotRootTarget(foo) returns foo
class foo {}; // gotRootTarget(foo) returns foo
import foo from './foo'; // getRootTarget(foo) returns foo
import {foo} from './foo'; // getRootTarget(foo) returns foo
import {foo as bar} from './foo'; // getRootTarget(bar) returns bar
</code></pre>
@param targetNode
@throws IllegalStateException if targetNode is not actually used as a target | [
"Returns",
"the",
"outermost",
"target",
"enclosing",
"the",
"given",
"assignment",
"target",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3501-L3509 |
24,263 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getEnclosingTarget | @Nullable
private static Node getEnclosingTarget(Node targetNode) {
checkState(checkNotNull(targetNode).isValidAssignmentTarget(), targetNode);
Node parent = checkNotNull(targetNode.getParent(), targetNode);
boolean targetIsFirstChild = parent.getFirstChild() == targetNode;
if (parent.isDefaultValue() || parent.isRest()) {
// in `([something = targetNode] = x)` targetNode isn't actually acting
// as a target.
checkState(targetIsFirstChild, parent);
// The DEFAULT_VALUE or REST occupies the place where the assignment target it contains would
// otherwise be in the AST, so pretend it is the target for the logic below.
targetNode = parent;
parent = checkNotNull(targetNode.getParent());
targetIsFirstChild = targetNode == parent.getFirstChild();
}
switch (parent.getToken()) {
case ARRAY_PATTERN:
// e.g. ([targetNode] = something)
return parent;
case OBJECT_PATTERN:
// e.g. ({...rest} = something);
return parent;
case COMPUTED_PROP:
// e.g. ({[expression]: targetNode} = something)
// e.g. ({[expression]: targetNode = default} = something)
// make sure the effective target (targetNode or DEFAULT_VALUE containing it)
// isn't the expression part
checkState(!targetIsFirstChild, parent);
// otherwise the same as STRING_KEY so fall through
case STRING_KEY:
// e.g. ({parent: targetNode} = something)
Node grandparent = checkNotNull(parent.getParent(), parent);
checkState(grandparent.isObjectPattern(), grandparent);
return grandparent;
case PARAM_LIST:
// e.g. `function foo(targetNode) {}`
case LET:
case CONST:
case VAR:
// non-destructured declarations
// e.g. `let targetNode = 3;`
return null;
case FUNCTION:
case CLASS:
// e.g. `function targetNode() {}`
// e.g. `class targetNode {}`
checkState(targetIsFirstChild, targetNode);
return null;
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
// e.g. `for ({length} in obj) {}` // targetNode is `{length}`
// e.g. `for ({length} of obj) {}` // targetNode is `{length}`
checkState(targetIsFirstChild, targetNode);
return null;
case DESTRUCTURING_LHS:
// destructured declarations
// e.g. `let [a] = 3`; // targetNode is `[a]`
checkState(targetIsFirstChild, targetNode);
return null;
case IMPORT:
// e.g. `import targetNode from './foo/bar';`
return null;
case IMPORT_SPEC:
// e.g. `import {bar as targetNode} from './foo/bar';`
// e.g. `import {targetNode} from './foo/bar';` // AST will have {targetNode as targetNode}
checkState(!targetIsFirstChild, parent);
return null;
case CATCH:
// e.g. `try {} catch (foo) {}`
return null;
default:
// e.g. targetNode = something
checkState(isAssignmentOp(parent) && targetIsFirstChild, parent);
return null;
}
} | java | @Nullable
private static Node getEnclosingTarget(Node targetNode) {
checkState(checkNotNull(targetNode).isValidAssignmentTarget(), targetNode);
Node parent = checkNotNull(targetNode.getParent(), targetNode);
boolean targetIsFirstChild = parent.getFirstChild() == targetNode;
if (parent.isDefaultValue() || parent.isRest()) {
// in `([something = targetNode] = x)` targetNode isn't actually acting
// as a target.
checkState(targetIsFirstChild, parent);
// The DEFAULT_VALUE or REST occupies the place where the assignment target it contains would
// otherwise be in the AST, so pretend it is the target for the logic below.
targetNode = parent;
parent = checkNotNull(targetNode.getParent());
targetIsFirstChild = targetNode == parent.getFirstChild();
}
switch (parent.getToken()) {
case ARRAY_PATTERN:
// e.g. ([targetNode] = something)
return parent;
case OBJECT_PATTERN:
// e.g. ({...rest} = something);
return parent;
case COMPUTED_PROP:
// e.g. ({[expression]: targetNode} = something)
// e.g. ({[expression]: targetNode = default} = something)
// make sure the effective target (targetNode or DEFAULT_VALUE containing it)
// isn't the expression part
checkState(!targetIsFirstChild, parent);
// otherwise the same as STRING_KEY so fall through
case STRING_KEY:
// e.g. ({parent: targetNode} = something)
Node grandparent = checkNotNull(parent.getParent(), parent);
checkState(grandparent.isObjectPattern(), grandparent);
return grandparent;
case PARAM_LIST:
// e.g. `function foo(targetNode) {}`
case LET:
case CONST:
case VAR:
// non-destructured declarations
// e.g. `let targetNode = 3;`
return null;
case FUNCTION:
case CLASS:
// e.g. `function targetNode() {}`
// e.g. `class targetNode {}`
checkState(targetIsFirstChild, targetNode);
return null;
case FOR_IN:
case FOR_OF:
case FOR_AWAIT_OF:
// e.g. `for ({length} in obj) {}` // targetNode is `{length}`
// e.g. `for ({length} of obj) {}` // targetNode is `{length}`
checkState(targetIsFirstChild, targetNode);
return null;
case DESTRUCTURING_LHS:
// destructured declarations
// e.g. `let [a] = 3`; // targetNode is `[a]`
checkState(targetIsFirstChild, targetNode);
return null;
case IMPORT:
// e.g. `import targetNode from './foo/bar';`
return null;
case IMPORT_SPEC:
// e.g. `import {bar as targetNode} from './foo/bar';`
// e.g. `import {targetNode} from './foo/bar';` // AST will have {targetNode as targetNode}
checkState(!targetIsFirstChild, parent);
return null;
case CATCH:
// e.g. `try {} catch (foo) {}`
return null;
default:
// e.g. targetNode = something
checkState(isAssignmentOp(parent) && targetIsFirstChild, parent);
return null;
}
} | [
"@",
"Nullable",
"private",
"static",
"Node",
"getEnclosingTarget",
"(",
"Node",
"targetNode",
")",
"{",
"checkState",
"(",
"checkNotNull",
"(",
"targetNode",
")",
".",
"isValidAssignmentTarget",
"(",
")",
",",
"targetNode",
")",
";",
"Node",
"parent",
"=",
"checkNotNull",
"(",
"targetNode",
".",
"getParent",
"(",
")",
",",
"targetNode",
")",
";",
"boolean",
"targetIsFirstChild",
"=",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"targetNode",
";",
"if",
"(",
"parent",
".",
"isDefaultValue",
"(",
")",
"||",
"parent",
".",
"isRest",
"(",
")",
")",
"{",
"// in `([something = targetNode] = x)` targetNode isn't actually acting",
"// as a target.",
"checkState",
"(",
"targetIsFirstChild",
",",
"parent",
")",
";",
"// The DEFAULT_VALUE or REST occupies the place where the assignment target it contains would",
"// otherwise be in the AST, so pretend it is the target for the logic below.",
"targetNode",
"=",
"parent",
";",
"parent",
"=",
"checkNotNull",
"(",
"targetNode",
".",
"getParent",
"(",
")",
")",
";",
"targetIsFirstChild",
"=",
"targetNode",
"==",
"parent",
".",
"getFirstChild",
"(",
")",
";",
"}",
"switch",
"(",
"parent",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"ARRAY_PATTERN",
":",
"// e.g. ([targetNode] = something)",
"return",
"parent",
";",
"case",
"OBJECT_PATTERN",
":",
"// e.g. ({...rest} = something);",
"return",
"parent",
";",
"case",
"COMPUTED_PROP",
":",
"// e.g. ({[expression]: targetNode} = something)",
"// e.g. ({[expression]: targetNode = default} = something)",
"// make sure the effective target (targetNode or DEFAULT_VALUE containing it)",
"// isn't the expression part",
"checkState",
"(",
"!",
"targetIsFirstChild",
",",
"parent",
")",
";",
"// otherwise the same as STRING_KEY so fall through",
"case",
"STRING_KEY",
":",
"// e.g. ({parent: targetNode} = something)",
"Node",
"grandparent",
"=",
"checkNotNull",
"(",
"parent",
".",
"getParent",
"(",
")",
",",
"parent",
")",
";",
"checkState",
"(",
"grandparent",
".",
"isObjectPattern",
"(",
")",
",",
"grandparent",
")",
";",
"return",
"grandparent",
";",
"case",
"PARAM_LIST",
":",
"// e.g. `function foo(targetNode) {}`",
"case",
"LET",
":",
"case",
"CONST",
":",
"case",
"VAR",
":",
"// non-destructured declarations",
"// e.g. `let targetNode = 3;`",
"return",
"null",
";",
"case",
"FUNCTION",
":",
"case",
"CLASS",
":",
"// e.g. `function targetNode() {}`",
"// e.g. `class targetNode {}`",
"checkState",
"(",
"targetIsFirstChild",
",",
"targetNode",
")",
";",
"return",
"null",
";",
"case",
"FOR_IN",
":",
"case",
"FOR_OF",
":",
"case",
"FOR_AWAIT_OF",
":",
"// e.g. `for ({length} in obj) {}` // targetNode is `{length}`",
"// e.g. `for ({length} of obj) {}` // targetNode is `{length}`",
"checkState",
"(",
"targetIsFirstChild",
",",
"targetNode",
")",
";",
"return",
"null",
";",
"case",
"DESTRUCTURING_LHS",
":",
"// destructured declarations",
"// e.g. `let [a] = 3`; // targetNode is `[a]`",
"checkState",
"(",
"targetIsFirstChild",
",",
"targetNode",
")",
";",
"return",
"null",
";",
"case",
"IMPORT",
":",
"// e.g. `import targetNode from './foo/bar';`",
"return",
"null",
";",
"case",
"IMPORT_SPEC",
":",
"// e.g. `import {bar as targetNode} from './foo/bar';`",
"// e.g. `import {targetNode} from './foo/bar';` // AST will have {targetNode as targetNode}",
"checkState",
"(",
"!",
"targetIsFirstChild",
",",
"parent",
")",
";",
"return",
"null",
";",
"case",
"CATCH",
":",
"// e.g. `try {} catch (foo) {}`",
"return",
"null",
";",
"default",
":",
"// e.g. targetNode = something",
"checkState",
"(",
"isAssignmentOp",
"(",
"parent",
")",
"&&",
"targetIsFirstChild",
",",
"parent",
")",
";",
"return",
"null",
";",
"}",
"}"
] | Returns the immediately enclosing target node for a given target node, or null if none found.
@see #getRootTarget(Node) for examples
@param targetNode | [
"Returns",
"the",
"immediately",
"enclosing",
"target",
"node",
"for",
"a",
"given",
"target",
"node",
"or",
"null",
"if",
"none",
"found",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3517-L3603 |
24,264 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isLhsByDestructuring | public static boolean isLhsByDestructuring(Node n) {
switch (n.getToken()) {
case NAME:
case GETPROP:
case GETELEM:
return isLhsByDestructuringHelper(n);
default:
return false;
}
} | java | public static boolean isLhsByDestructuring(Node n) {
switch (n.getToken()) {
case NAME:
case GETPROP:
case GETELEM:
return isLhsByDestructuringHelper(n);
default:
return false;
}
} | [
"public",
"static",
"boolean",
"isLhsByDestructuring",
"(",
"Node",
"n",
")",
"{",
"switch",
"(",
"n",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"NAME",
":",
"case",
"GETPROP",
":",
"case",
"GETELEM",
":",
"return",
"isLhsByDestructuringHelper",
"(",
"n",
")",
";",
"default",
":",
"return",
"false",
";",
"}",
"}"
] | Returns true if the node is a lhs value of a destructuring assignment.
<p>For example, x in {@code var [x] = [1];}, {@code var [...x] = [1];}, and {@code var {a: x} =
{a: 1}} or a.b in {@code ([a.b] = [1]);} or {@code ({key: a.b} = {key: 1});} | [
"Returns",
"true",
"if",
"the",
"node",
"is",
"a",
"lhs",
"value",
"of",
"a",
"destructuring",
"assignment",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3611-L3620 |
24,265 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getObjectLitKeyName | static String getObjectLitKeyName(Node key) {
Node keyNode = getObjectLitKeyNode(key);
if (keyNode != null) {
return keyNode.getString();
}
throw new IllegalStateException("Unexpected node type: " + key);
} | java | static String getObjectLitKeyName(Node key) {
Node keyNode = getObjectLitKeyNode(key);
if (keyNode != null) {
return keyNode.getString();
}
throw new IllegalStateException("Unexpected node type: " + key);
} | [
"static",
"String",
"getObjectLitKeyName",
"(",
"Node",
"key",
")",
"{",
"Node",
"keyNode",
"=",
"getObjectLitKeyNode",
"(",
"key",
")",
";",
"if",
"(",
"keyNode",
"!=",
"null",
")",
"{",
"return",
"keyNode",
".",
"getString",
"(",
")",
";",
"}",
"throw",
"new",
"IllegalStateException",
"(",
"\"Unexpected node type: \"",
"+",
"key",
")",
";",
"}"
] | Get the name of an object literal key.
@param key A node | [
"Get",
"the",
"name",
"of",
"an",
"object",
"literal",
"key",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3692-L3698 |
24,266 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getObjectLitKeyNode | static Node getObjectLitKeyNode(Node key) {
switch (key.getToken()) {
case STRING_KEY:
case GETTER_DEF:
case SETTER_DEF:
case MEMBER_FUNCTION_DEF:
return key;
case COMPUTED_PROP:
return key.getFirstChild().isString() ? key.getFirstChild() : null;
default:
break;
}
throw new IllegalStateException("Unexpected node type: " + key);
} | java | static Node getObjectLitKeyNode(Node key) {
switch (key.getToken()) {
case STRING_KEY:
case GETTER_DEF:
case SETTER_DEF:
case MEMBER_FUNCTION_DEF:
return key;
case COMPUTED_PROP:
return key.getFirstChild().isString() ? key.getFirstChild() : null;
default:
break;
}
throw new IllegalStateException("Unexpected node type: " + key);
} | [
"static",
"Node",
"getObjectLitKeyNode",
"(",
"Node",
"key",
")",
"{",
"switch",
"(",
"key",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"STRING_KEY",
":",
"case",
"GETTER_DEF",
":",
"case",
"SETTER_DEF",
":",
"case",
"MEMBER_FUNCTION_DEF",
":",
"return",
"key",
";",
"case",
"COMPUTED_PROP",
":",
"return",
"key",
".",
"getFirstChild",
"(",
")",
".",
"isString",
"(",
")",
"?",
"key",
".",
"getFirstChild",
"(",
")",
":",
"null",
";",
"default",
":",
"break",
";",
"}",
"throw",
"new",
"IllegalStateException",
"(",
"\"Unexpected node type: \"",
"+",
"key",
")",
";",
"}"
] | Get the Node that defines the name of an object literal key.
@param key A node | [
"Get",
"the",
"Node",
"that",
"defines",
"the",
"name",
"of",
"an",
"object",
"literal",
"key",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3705-L3718 |
24,267 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isNestedObjectPattern | static boolean isNestedObjectPattern(Node n) {
checkState(n.isObjectPattern());
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
Node value = key.getFirstChild();
if (value != null
&& (value.isObjectLit() || value.isArrayLit() || value.isDestructuringPattern())) {
return true;
}
}
return false;
} | java | static boolean isNestedObjectPattern(Node n) {
checkState(n.isObjectPattern());
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
Node value = key.getFirstChild();
if (value != null
&& (value.isObjectLit() || value.isArrayLit() || value.isDestructuringPattern())) {
return true;
}
}
return false;
} | [
"static",
"boolean",
"isNestedObjectPattern",
"(",
"Node",
"n",
")",
"{",
"checkState",
"(",
"n",
".",
"isObjectPattern",
"(",
")",
")",
";",
"for",
"(",
"Node",
"key",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"key",
"!=",
"null",
";",
"key",
"=",
"key",
".",
"getNext",
"(",
")",
")",
"{",
"Node",
"value",
"=",
"key",
".",
"getFirstChild",
"(",
")",
";",
"if",
"(",
"value",
"!=",
"null",
"&&",
"(",
"value",
".",
"isObjectLit",
"(",
")",
"||",
"value",
".",
"isArrayLit",
"(",
")",
"||",
"value",
".",
"isDestructuringPattern",
"(",
")",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Determine whether the destructuring object pattern is nested
@param n object pattern node | [
"Determine",
"whether",
"the",
"destructuring",
"object",
"pattern",
"is",
"nested"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3725-L3735 |
24,268 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isNestedArrayPattern | static boolean isNestedArrayPattern(Node n) {
checkState(n.isArrayPattern());
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
if (key.hasChildren()) {
return true;
}
}
return false;
} | java | static boolean isNestedArrayPattern(Node n) {
checkState(n.isArrayPattern());
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
if (key.hasChildren()) {
return true;
}
}
return false;
} | [
"static",
"boolean",
"isNestedArrayPattern",
"(",
"Node",
"n",
")",
"{",
"checkState",
"(",
"n",
".",
"isArrayPattern",
"(",
")",
")",
";",
"for",
"(",
"Node",
"key",
"=",
"n",
".",
"getFirstChild",
"(",
")",
";",
"key",
"!=",
"null",
";",
"key",
"=",
"key",
".",
"getNext",
"(",
")",
")",
"{",
"if",
"(",
"key",
".",
"hasChildren",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Determine whether the destructuring array pattern is nested
@param n array pattern node | [
"Determine",
"whether",
"the",
"destructuring",
"array",
"pattern",
"is",
"nested"
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3742-L3750 |
24,269 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.redeclareVarsInsideBranch | static void redeclareVarsInsideBranch(Node branch) {
Collection<Node> vars = getVarsDeclaredInBranch(branch);
if (vars.isEmpty()) {
return;
}
Node parent = getAddingRoot(branch);
for (Node nameNode : vars) {
Node var = IR.var(
IR.name(nameNode.getString())
.srcref(nameNode))
.srcref(nameNode);
copyNameAnnotations(nameNode, var.getFirstChild());
parent.addChildToFront(var);
}
} | java | static void redeclareVarsInsideBranch(Node branch) {
Collection<Node> vars = getVarsDeclaredInBranch(branch);
if (vars.isEmpty()) {
return;
}
Node parent = getAddingRoot(branch);
for (Node nameNode : vars) {
Node var = IR.var(
IR.name(nameNode.getString())
.srcref(nameNode))
.srcref(nameNode);
copyNameAnnotations(nameNode, var.getFirstChild());
parent.addChildToFront(var);
}
} | [
"static",
"void",
"redeclareVarsInsideBranch",
"(",
"Node",
"branch",
")",
"{",
"Collection",
"<",
"Node",
">",
"vars",
"=",
"getVarsDeclaredInBranch",
"(",
"branch",
")",
";",
"if",
"(",
"vars",
".",
"isEmpty",
"(",
")",
")",
"{",
"return",
";",
"}",
"Node",
"parent",
"=",
"getAddingRoot",
"(",
"branch",
")",
";",
"for",
"(",
"Node",
"nameNode",
":",
"vars",
")",
"{",
"Node",
"var",
"=",
"IR",
".",
"var",
"(",
"IR",
".",
"name",
"(",
"nameNode",
".",
"getString",
"(",
")",
")",
".",
"srcref",
"(",
"nameNode",
")",
")",
".",
"srcref",
"(",
"nameNode",
")",
";",
"copyNameAnnotations",
"(",
"nameNode",
",",
"var",
".",
"getFirstChild",
"(",
")",
")",
";",
"parent",
".",
"addChildToFront",
"(",
"var",
")",
";",
"}",
"}"
] | Given a node tree, finds all the VAR declarations in that tree that are
not in an inner scope. Then adds a new VAR node at the top of the current
scope that redeclares them, if necessary. | [
"Given",
"a",
"node",
"tree",
"finds",
"all",
"the",
"VAR",
"declarations",
"in",
"that",
"tree",
"that",
"are",
"not",
"in",
"an",
"inner",
"scope",
".",
"Then",
"adds",
"a",
"new",
"VAR",
"node",
"at",
"the",
"top",
"of",
"the",
"current",
"scope",
"that",
"redeclares",
"them",
"if",
"necessary",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3905-L3920 |
24,270 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.copyNameAnnotations | static void copyNameAnnotations(Node source, Node destination) {
if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
}
} | java | static void copyNameAnnotations(Node source, Node destination) {
if (source.getBooleanProp(Node.IS_CONSTANT_NAME)) {
destination.putBooleanProp(Node.IS_CONSTANT_NAME, true);
}
} | [
"static",
"void",
"copyNameAnnotations",
"(",
"Node",
"source",
",",
"Node",
"destination",
")",
"{",
"if",
"(",
"source",
".",
"getBooleanProp",
"(",
"Node",
".",
"IS_CONSTANT_NAME",
")",
")",
"{",
"destination",
".",
"putBooleanProp",
"(",
"Node",
".",
"IS_CONSTANT_NAME",
",",
"true",
")",
";",
"}",
"}"
] | Copy any annotations that follow a named value.
@param source
@param destination | [
"Copy",
"any",
"annotations",
"that",
"follow",
"a",
"named",
"value",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3927-L3931 |
24,271 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getAddingRoot | private static Node getAddingRoot(Node n) {
Node addingRoot = null;
Node ancestor = n;
crawl_ancestors: while (null != (ancestor = ancestor.getParent())) {
switch (ancestor.getToken()) {
case SCRIPT:
case MODULE_BODY:
addingRoot = ancestor;
break crawl_ancestors;
case FUNCTION:
addingRoot = ancestor.getLastChild();
break crawl_ancestors;
default:
continue crawl_ancestors;
}
}
// make sure that the adding root looks ok
checkState(addingRoot.isBlock() || addingRoot.isModuleBody() || addingRoot.isScript());
checkState(!addingRoot.hasChildren() || !addingRoot.getFirstChild().isScript());
return addingRoot;
} | java | private static Node getAddingRoot(Node n) {
Node addingRoot = null;
Node ancestor = n;
crawl_ancestors: while (null != (ancestor = ancestor.getParent())) {
switch (ancestor.getToken()) {
case SCRIPT:
case MODULE_BODY:
addingRoot = ancestor;
break crawl_ancestors;
case FUNCTION:
addingRoot = ancestor.getLastChild();
break crawl_ancestors;
default:
continue crawl_ancestors;
}
}
// make sure that the adding root looks ok
checkState(addingRoot.isBlock() || addingRoot.isModuleBody() || addingRoot.isScript());
checkState(!addingRoot.hasChildren() || !addingRoot.getFirstChild().isScript());
return addingRoot;
} | [
"private",
"static",
"Node",
"getAddingRoot",
"(",
"Node",
"n",
")",
"{",
"Node",
"addingRoot",
"=",
"null",
";",
"Node",
"ancestor",
"=",
"n",
";",
"crawl_ancestors",
":",
"while",
"(",
"null",
"!=",
"(",
"ancestor",
"=",
"ancestor",
".",
"getParent",
"(",
")",
")",
")",
"{",
"switch",
"(",
"ancestor",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"SCRIPT",
":",
"case",
"MODULE_BODY",
":",
"addingRoot",
"=",
"ancestor",
";",
"break",
"crawl_ancestors",
";",
"case",
"FUNCTION",
":",
"addingRoot",
"=",
"ancestor",
".",
"getLastChild",
"(",
")",
";",
"break",
"crawl_ancestors",
";",
"default",
":",
"continue",
"crawl_ancestors",
";",
"}",
"}",
"// make sure that the adding root looks ok",
"checkState",
"(",
"addingRoot",
".",
"isBlock",
"(",
")",
"||",
"addingRoot",
".",
"isModuleBody",
"(",
")",
"||",
"addingRoot",
".",
"isScript",
"(",
")",
")",
";",
"checkState",
"(",
"!",
"addingRoot",
".",
"hasChildren",
"(",
")",
"||",
"!",
"addingRoot",
".",
"getFirstChild",
"(",
")",
".",
"isScript",
"(",
")",
")",
";",
"return",
"addingRoot",
";",
"}"
] | Gets a Node at the top of the current scope where we can add new var
declarations as children. | [
"Gets",
"a",
"Node",
"at",
"the",
"top",
"of",
"the",
"current",
"scope",
"where",
"we",
"can",
"add",
"new",
"var",
"declarations",
"as",
"children",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L3937-L3958 |
24,272 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.newQName | static Node newQName(
AbstractCompiler compiler, String name, Node basisNode,
String originalName) {
Node node = newQName(compiler, name);
useSourceInfoForNewQName(node, basisNode);
if (!originalName.equals(node.getOriginalName())) {
// If basisNode already had the correct original name, then it will already be set correctly.
// Setting it again will force the QName node to have a different property list from all of
// its children, causing greater memory consumption.
node.setOriginalName(originalName);
}
return node;
} | java | static Node newQName(
AbstractCompiler compiler, String name, Node basisNode,
String originalName) {
Node node = newQName(compiler, name);
useSourceInfoForNewQName(node, basisNode);
if (!originalName.equals(node.getOriginalName())) {
// If basisNode already had the correct original name, then it will already be set correctly.
// Setting it again will force the QName node to have a different property list from all of
// its children, causing greater memory consumption.
node.setOriginalName(originalName);
}
return node;
} | [
"static",
"Node",
"newQName",
"(",
"AbstractCompiler",
"compiler",
",",
"String",
"name",
",",
"Node",
"basisNode",
",",
"String",
"originalName",
")",
"{",
"Node",
"node",
"=",
"newQName",
"(",
"compiler",
",",
"name",
")",
";",
"useSourceInfoForNewQName",
"(",
"node",
",",
"basisNode",
")",
";",
"if",
"(",
"!",
"originalName",
".",
"equals",
"(",
"node",
".",
"getOriginalName",
"(",
")",
")",
")",
"{",
"// If basisNode already had the correct original name, then it will already be set correctly.",
"// Setting it again will force the QName node to have a different property list from all of",
"// its children, causing greater memory consumption.",
"node",
".",
"setOriginalName",
"(",
"originalName",
")",
";",
"}",
"return",
"node",
";",
"}"
] | Creates a node representing a qualified name, copying over the source
location information from the basis node and assigning the given original
name to the node.
@param name A qualified name (e.g. "foo" or "foo.bar.baz")
@param basisNode The node that represents the name as currently found in
the AST.
@param originalName The original name of the item being represented by the
NAME node. Used for debugging information.
@return A NAME or GETPROP node | [
"Creates",
"a",
"node",
"representing",
"a",
"qualified",
"name",
"copying",
"over",
"the",
"source",
"location",
"information",
"from",
"the",
"basis",
"node",
"and",
"assigning",
"the",
"given",
"original",
"name",
"to",
"the",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4019-L4031 |
24,273 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.useSourceInfoForNewQName | private static void useSourceInfoForNewQName(Node newQName, Node basisNode) {
if (newQName.getStaticSourceFile() == null) {
newQName.setStaticSourceFileFrom(basisNode);
newQName.setSourceEncodedPosition(basisNode.getSourcePosition());
}
if (newQName.getOriginalName() == null) {
newQName.putProp(Node.ORIGINALNAME_PROP, basisNode.getOriginalName());
}
for (Node child = newQName.getFirstChild(); child != null; child = child.getNext()) {
useSourceInfoForNewQName(child, basisNode);
}
} | java | private static void useSourceInfoForNewQName(Node newQName, Node basisNode) {
if (newQName.getStaticSourceFile() == null) {
newQName.setStaticSourceFileFrom(basisNode);
newQName.setSourceEncodedPosition(basisNode.getSourcePosition());
}
if (newQName.getOriginalName() == null) {
newQName.putProp(Node.ORIGINALNAME_PROP, basisNode.getOriginalName());
}
for (Node child = newQName.getFirstChild(); child != null; child = child.getNext()) {
useSourceInfoForNewQName(child, basisNode);
}
} | [
"private",
"static",
"void",
"useSourceInfoForNewQName",
"(",
"Node",
"newQName",
",",
"Node",
"basisNode",
")",
"{",
"if",
"(",
"newQName",
".",
"getStaticSourceFile",
"(",
")",
"==",
"null",
")",
"{",
"newQName",
".",
"setStaticSourceFileFrom",
"(",
"basisNode",
")",
";",
"newQName",
".",
"setSourceEncodedPosition",
"(",
"basisNode",
".",
"getSourcePosition",
"(",
")",
")",
";",
"}",
"if",
"(",
"newQName",
".",
"getOriginalName",
"(",
")",
"==",
"null",
")",
"{",
"newQName",
".",
"putProp",
"(",
"Node",
".",
"ORIGINALNAME_PROP",
",",
"basisNode",
".",
"getOriginalName",
"(",
")",
")",
";",
"}",
"for",
"(",
"Node",
"child",
"=",
"newQName",
".",
"getFirstChild",
"(",
")",
";",
"child",
"!=",
"null",
";",
"child",
"=",
"child",
".",
"getNext",
"(",
")",
")",
"{",
"useSourceInfoForNewQName",
"(",
"child",
",",
"basisNode",
")",
";",
"}",
"}"
] | Custom update new QName node with source info from another node.
<p>This is very similar to {@link Node#useSourceInfoIfMissingFromForTree(Node)}, but it avoids
overwriting the length field of the nodes.
TODO(bradfordcsmith): Eliminate the need for this custom method. | [
"Custom",
"update",
"new",
"QName",
"node",
"with",
"source",
"info",
"from",
"another",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4088-L4101 |
24,274 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getRootOfQualifiedName | static Node getRootOfQualifiedName(Node qName) {
for (Node current = qName; true; current = current.getFirstChild()) {
if (current.isName() || current.isThis() || current.isSuper()) {
return current;
}
checkState(current.isGetProp(), "Not a getprop node: ", current);
}
} | java | static Node getRootOfQualifiedName(Node qName) {
for (Node current = qName; true; current = current.getFirstChild()) {
if (current.isName() || current.isThis() || current.isSuper()) {
return current;
}
checkState(current.isGetProp(), "Not a getprop node: ", current);
}
} | [
"static",
"Node",
"getRootOfQualifiedName",
"(",
"Node",
"qName",
")",
"{",
"for",
"(",
"Node",
"current",
"=",
"qName",
";",
"true",
";",
"current",
"=",
"current",
".",
"getFirstChild",
"(",
")",
")",
"{",
"if",
"(",
"current",
".",
"isName",
"(",
")",
"||",
"current",
".",
"isThis",
"(",
")",
"||",
"current",
".",
"isSuper",
"(",
")",
")",
"{",
"return",
"current",
";",
"}",
"checkState",
"(",
"current",
".",
"isGetProp",
"(",
")",
",",
"\"Not a getprop node: \"",
",",
"current",
")",
";",
"}",
"}"
] | Gets the root node of a qualified name. Must be either NAME, THIS or SUPER. | [
"Gets",
"the",
"root",
"node",
"of",
"a",
"qualified",
"name",
".",
"Must",
"be",
"either",
"NAME",
"THIS",
"or",
"SUPER",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4106-L4113 |
24,275 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isValidSimpleName | static boolean isValidSimpleName(String name) {
return TokenStream.isJSIdentifier(name)
&& !TokenStream.isKeyword(name)
// no Unicode escaped characters - some browsers are less tolerant
// of Unicode characters that might be valid according to the
// language spec.
// Note that by this point, Unicode escapes have been converted
// to UTF-16 characters, so we're only searching for character
// values, not escapes.
&& isLatin(name);
} | java | static boolean isValidSimpleName(String name) {
return TokenStream.isJSIdentifier(name)
&& !TokenStream.isKeyword(name)
// no Unicode escaped characters - some browsers are less tolerant
// of Unicode characters that might be valid according to the
// language spec.
// Note that by this point, Unicode escapes have been converted
// to UTF-16 characters, so we're only searching for character
// values, not escapes.
&& isLatin(name);
} | [
"static",
"boolean",
"isValidSimpleName",
"(",
"String",
"name",
")",
"{",
"return",
"TokenStream",
".",
"isJSIdentifier",
"(",
"name",
")",
"&&",
"!",
"TokenStream",
".",
"isKeyword",
"(",
"name",
")",
"// no Unicode escaped characters - some browsers are less tolerant",
"// of Unicode characters that might be valid according to the",
"// language spec.",
"// Note that by this point, Unicode escapes have been converted",
"// to UTF-16 characters, so we're only searching for character",
"// values, not escapes.",
"&&",
"isLatin",
"(",
"name",
")",
";",
"}"
] | Determines whether the given name is a valid variable name. | [
"Determines",
"whether",
"the",
"given",
"name",
"is",
"a",
"valid",
"variable",
"name",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4193-L4203 |
24,276 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isValidQualifiedName | public static boolean isValidQualifiedName(FeatureSet mode, String name) {
if (name.endsWith(".") || name.startsWith(".")) {
return false;
}
List<String> parts = Splitter.on('.').splitToList(name);
for (String part : parts) {
if (!isValidPropertyName(mode, part)) {
return false;
}
}
return isValidSimpleName(parts.get(0));
} | java | public static boolean isValidQualifiedName(FeatureSet mode, String name) {
if (name.endsWith(".") || name.startsWith(".")) {
return false;
}
List<String> parts = Splitter.on('.').splitToList(name);
for (String part : parts) {
if (!isValidPropertyName(mode, part)) {
return false;
}
}
return isValidSimpleName(parts.get(0));
} | [
"public",
"static",
"boolean",
"isValidQualifiedName",
"(",
"FeatureSet",
"mode",
",",
"String",
"name",
")",
"{",
"if",
"(",
"name",
".",
"endsWith",
"(",
"\".\"",
")",
"||",
"name",
".",
"startsWith",
"(",
"\".\"",
")",
")",
"{",
"return",
"false",
";",
"}",
"List",
"<",
"String",
">",
"parts",
"=",
"Splitter",
".",
"on",
"(",
"'",
"'",
")",
".",
"splitToList",
"(",
"name",
")",
";",
"for",
"(",
"String",
"part",
":",
"parts",
")",
"{",
"if",
"(",
"!",
"isValidPropertyName",
"(",
"mode",
",",
"part",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"return",
"isValidSimpleName",
"(",
"parts",
".",
"get",
"(",
"0",
")",
")",
";",
"}"
] | Determines whether the given name is a valid qualified name. | [
"Determines",
"whether",
"the",
"given",
"name",
"is",
"a",
"valid",
"qualified",
"name",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4211-L4223 |
24,277 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getVarsDeclaredInBranch | static Collection<Node> getVarsDeclaredInBranch(Node root) {
VarCollector collector = new VarCollector();
visitPreOrder(
root,
collector,
MATCH_NOT_FUNCTION);
return collector.vars.values();
} | java | static Collection<Node> getVarsDeclaredInBranch(Node root) {
VarCollector collector = new VarCollector();
visitPreOrder(
root,
collector,
MATCH_NOT_FUNCTION);
return collector.vars.values();
} | [
"static",
"Collection",
"<",
"Node",
">",
"getVarsDeclaredInBranch",
"(",
"Node",
"root",
")",
"{",
"VarCollector",
"collector",
"=",
"new",
"VarCollector",
"(",
")",
";",
"visitPreOrder",
"(",
"root",
",",
"collector",
",",
"MATCH_NOT_FUNCTION",
")",
";",
"return",
"collector",
".",
"vars",
".",
"values",
"(",
")",
";",
"}"
] | Retrieves vars declared in the current node tree, excluding descent scopes. | [
"Retrieves",
"vars",
"declared",
"in",
"the",
"current",
"node",
"tree",
"excluding",
"descent",
"scopes",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4255-L4262 |
24,278 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.findLhsNodesInNode | public static List<Node> findLhsNodesInNode(Node assigningParent) {
checkArgument(
isNameDeclaration(assigningParent)
|| assigningParent.isParamList()
|| isAssignmentOp(assigningParent)
|| assigningParent.isCatch()
|| assigningParent.isDestructuringLhs()
|| assigningParent.isDefaultValue()
|| assigningParent.isImport()
// enhanced for loops assign to loop variables
|| isEnhancedFor(assigningParent),
assigningParent);
ArrayList<Node> lhsNodes = new ArrayList<>();
getLhsNodesHelper(assigningParent, lhsNodes);
return lhsNodes;
} | java | public static List<Node> findLhsNodesInNode(Node assigningParent) {
checkArgument(
isNameDeclaration(assigningParent)
|| assigningParent.isParamList()
|| isAssignmentOp(assigningParent)
|| assigningParent.isCatch()
|| assigningParent.isDestructuringLhs()
|| assigningParent.isDefaultValue()
|| assigningParent.isImport()
// enhanced for loops assign to loop variables
|| isEnhancedFor(assigningParent),
assigningParent);
ArrayList<Node> lhsNodes = new ArrayList<>();
getLhsNodesHelper(assigningParent, lhsNodes);
return lhsNodes;
} | [
"public",
"static",
"List",
"<",
"Node",
">",
"findLhsNodesInNode",
"(",
"Node",
"assigningParent",
")",
"{",
"checkArgument",
"(",
"isNameDeclaration",
"(",
"assigningParent",
")",
"||",
"assigningParent",
".",
"isParamList",
"(",
")",
"||",
"isAssignmentOp",
"(",
"assigningParent",
")",
"||",
"assigningParent",
".",
"isCatch",
"(",
")",
"||",
"assigningParent",
".",
"isDestructuringLhs",
"(",
")",
"||",
"assigningParent",
".",
"isDefaultValue",
"(",
")",
"||",
"assigningParent",
".",
"isImport",
"(",
")",
"// enhanced for loops assign to loop variables",
"||",
"isEnhancedFor",
"(",
"assigningParent",
")",
",",
"assigningParent",
")",
";",
"ArrayList",
"<",
"Node",
">",
"lhsNodes",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"getLhsNodesHelper",
"(",
"assigningParent",
",",
"lhsNodes",
")",
";",
"return",
"lhsNodes",
";",
"}"
] | Retrieves lhs nodes declared or assigned in a given assigning parent node.
<p>An assigning parent node is one that assigns a value to one or more LHS nodes. | [
"Retrieves",
"lhs",
"nodes",
"declared",
"or",
"assigned",
"in",
"a",
"given",
"assigning",
"parent",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4327-L4342 |
24,279 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.newVarNode | static Node newVarNode(String name, Node value) {
Node lhs = IR.name(name);
if (value != null) {
lhs.srcref(value);
}
return newVarNode(lhs, value);
} | java | static Node newVarNode(String name, Node value) {
Node lhs = IR.name(name);
if (value != null) {
lhs.srcref(value);
}
return newVarNode(lhs, value);
} | [
"static",
"Node",
"newVarNode",
"(",
"String",
"name",
",",
"Node",
"value",
")",
"{",
"Node",
"lhs",
"=",
"IR",
".",
"name",
"(",
"name",
")",
";",
"if",
"(",
"value",
"!=",
"null",
")",
"{",
"lhs",
".",
"srcref",
"(",
"value",
")",
";",
"}",
"return",
"newVarNode",
"(",
"lhs",
",",
"value",
")",
";",
"}"
] | Create a VAR node containing the given name and initial value expression. | [
"Create",
"a",
"VAR",
"node",
"containing",
"the",
"given",
"name",
"and",
"initial",
"value",
"expression",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4547-L4553 |
24,280 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getNodeTypeReferenceCount | static int getNodeTypeReferenceCount(
Node node, Token type, Predicate<Node> traverseChildrenPred) {
return getCount(node, new MatchNodeType(type), traverseChildrenPred);
} | java | static int getNodeTypeReferenceCount(
Node node, Token type, Predicate<Node> traverseChildrenPred) {
return getCount(node, new MatchNodeType(type), traverseChildrenPred);
} | [
"static",
"int",
"getNodeTypeReferenceCount",
"(",
"Node",
"node",
",",
"Token",
"type",
",",
"Predicate",
"<",
"Node",
">",
"traverseChildrenPred",
")",
"{",
"return",
"getCount",
"(",
"node",
",",
"new",
"MatchNodeType",
"(",
"type",
")",
",",
"traverseChildrenPred",
")",
";",
"}"
] | Finds the number of times a type is referenced within the node tree. | [
"Finds",
"the",
"number",
"of",
"times",
"a",
"type",
"is",
"referenced",
"within",
"the",
"node",
"tree",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4647-L4650 |
24,281 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getNameReferenceCount | static int getNameReferenceCount(Node node, String name) {
return getCount(node, new MatchNameNode(name), Predicates.alwaysTrue());
} | java | static int getNameReferenceCount(Node node, String name) {
return getCount(node, new MatchNameNode(name), Predicates.alwaysTrue());
} | [
"static",
"int",
"getNameReferenceCount",
"(",
"Node",
"node",
",",
"String",
"name",
")",
"{",
"return",
"getCount",
"(",
"node",
",",
"new",
"MatchNameNode",
"(",
"name",
")",
",",
"Predicates",
".",
"alwaysTrue",
"(",
")",
")",
";",
"}"
] | Finds the number of times a simple name is referenced within the node tree. | [
"Finds",
"the",
"number",
"of",
"times",
"a",
"simple",
"name",
"is",
"referenced",
"within",
"the",
"node",
"tree",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4667-L4669 |
24,282 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.visitPreOrder | public static void visitPreOrder(Node node, Visitor visitor) {
visitPreOrder(node, visitor, Predicates.alwaysTrue());
} | java | public static void visitPreOrder(Node node, Visitor visitor) {
visitPreOrder(node, visitor, Predicates.alwaysTrue());
} | [
"public",
"static",
"void",
"visitPreOrder",
"(",
"Node",
"node",
",",
"Visitor",
"visitor",
")",
"{",
"visitPreOrder",
"(",
"node",
",",
"visitor",
",",
"Predicates",
".",
"alwaysTrue",
"(",
")",
")",
";",
"}"
] | A pre-order traversal, calling Visitor.visit for each decendent. | [
"A",
"pre",
"-",
"order",
"traversal",
"calling",
"Visitor",
".",
"visit",
"for",
"each",
"decendent",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4720-L4722 |
24,283 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.visitPreOrder | public static void visitPreOrder(
Node node, Visitor visitor, Predicate<Node> traverseChildrenPred) {
visitor.visit(node);
if (traverseChildrenPred.apply(node)) {
for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
visitPreOrder(c, visitor, traverseChildrenPred);
}
}
} | java | public static void visitPreOrder(
Node node, Visitor visitor, Predicate<Node> traverseChildrenPred) {
visitor.visit(node);
if (traverseChildrenPred.apply(node)) {
for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
visitPreOrder(c, visitor, traverseChildrenPred);
}
}
} | [
"public",
"static",
"void",
"visitPreOrder",
"(",
"Node",
"node",
",",
"Visitor",
"visitor",
",",
"Predicate",
"<",
"Node",
">",
"traverseChildrenPred",
")",
"{",
"visitor",
".",
"visit",
"(",
"node",
")",
";",
"if",
"(",
"traverseChildrenPred",
".",
"apply",
"(",
"node",
")",
")",
"{",
"for",
"(",
"Node",
"c",
"=",
"node",
".",
"getFirstChild",
"(",
")",
";",
"c",
"!=",
"null",
";",
"c",
"=",
"c",
".",
"getNext",
"(",
")",
")",
"{",
"visitPreOrder",
"(",
"c",
",",
"visitor",
",",
"traverseChildrenPred",
")",
";",
"}",
"}",
"}"
] | A pre-order traversal, calling Visitor.visit for each child matching the predicate. | [
"A",
"pre",
"-",
"order",
"traversal",
"calling",
"Visitor",
".",
"visit",
"for",
"each",
"child",
"matching",
"the",
"predicate",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4725-L4734 |
24,284 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.visitPostOrder | public static void visitPostOrder(Node node, Visitor visitor) {
visitPostOrder(node, visitor, Predicates.alwaysTrue());
} | java | public static void visitPostOrder(Node node, Visitor visitor) {
visitPostOrder(node, visitor, Predicates.alwaysTrue());
} | [
"public",
"static",
"void",
"visitPostOrder",
"(",
"Node",
"node",
",",
"Visitor",
"visitor",
")",
"{",
"visitPostOrder",
"(",
"node",
",",
"visitor",
",",
"Predicates",
".",
"alwaysTrue",
"(",
")",
")",
";",
"}"
] | A post-order traversal, calling Visitor.visit for each decendent. | [
"A",
"post",
"-",
"order",
"traversal",
"calling",
"Visitor",
".",
"visit",
"for",
"each",
"decendent",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4737-L4739 |
24,285 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isConstantByConvention | static boolean isConstantByConvention(
CodingConvention convention, Node node) {
Node parent = node.getParent();
if (parent.isGetProp() && node == parent.getLastChild()) {
return convention.isConstantKey(node.getString());
} else if (mayBeObjectLitKey(node)) {
return convention.isConstantKey(node.getString());
} else if (node.isName()) {
return convention.isConstant(node.getString());
}
return false;
} | java | static boolean isConstantByConvention(
CodingConvention convention, Node node) {
Node parent = node.getParent();
if (parent.isGetProp() && node == parent.getLastChild()) {
return convention.isConstantKey(node.getString());
} else if (mayBeObjectLitKey(node)) {
return convention.isConstantKey(node.getString());
} else if (node.isName()) {
return convention.isConstant(node.getString());
}
return false;
} | [
"static",
"boolean",
"isConstantByConvention",
"(",
"CodingConvention",
"convention",
",",
"Node",
"node",
")",
"{",
"Node",
"parent",
"=",
"node",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"parent",
".",
"isGetProp",
"(",
")",
"&&",
"node",
"==",
"parent",
".",
"getLastChild",
"(",
")",
")",
"{",
"return",
"convention",
".",
"isConstantKey",
"(",
"node",
".",
"getString",
"(",
")",
")",
";",
"}",
"else",
"if",
"(",
"mayBeObjectLitKey",
"(",
"node",
")",
")",
"{",
"return",
"convention",
".",
"isConstantKey",
"(",
"node",
".",
"getString",
"(",
")",
")",
";",
"}",
"else",
"if",
"(",
"node",
".",
"isName",
"(",
")",
")",
"{",
"return",
"convention",
".",
"isConstant",
"(",
"node",
".",
"getString",
"(",
")",
")",
";",
"}",
"return",
"false",
";",
"}"
] | Whether the given name is constant by coding convention. | [
"Whether",
"the",
"given",
"name",
"is",
"constant",
"by",
"coding",
"convention",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4847-L4858 |
24,286 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isConstantDeclaration | static boolean isConstantDeclaration(
CodingConvention convention, JSDocInfo info, Node node) {
// TODO(b/77597706): Update this method to handle destructured declarations.
if (node.isName() && node.getParent().isConst()) {
return true;
} else if (node.isName()
&& isLhsByDestructuring(node)
&& getRootTarget(node).getGrandparent().isConst()) {
return true;
}
if (info != null && info.isConstant()) {
return true;
}
if (node.getBooleanProp(Node.IS_CONSTANT_VAR)) {
return true;
}
switch (node.getToken()) {
case NAME:
return NodeUtil.isConstantByConvention(convention, node);
case GETPROP:
return node.isQualifiedName()
&& NodeUtil.isConstantByConvention(convention, node.getLastChild());
default:
break;
}
return false;
} | java | static boolean isConstantDeclaration(
CodingConvention convention, JSDocInfo info, Node node) {
// TODO(b/77597706): Update this method to handle destructured declarations.
if (node.isName() && node.getParent().isConst()) {
return true;
} else if (node.isName()
&& isLhsByDestructuring(node)
&& getRootTarget(node).getGrandparent().isConst()) {
return true;
}
if (info != null && info.isConstant()) {
return true;
}
if (node.getBooleanProp(Node.IS_CONSTANT_VAR)) {
return true;
}
switch (node.getToken()) {
case NAME:
return NodeUtil.isConstantByConvention(convention, node);
case GETPROP:
return node.isQualifiedName()
&& NodeUtil.isConstantByConvention(convention, node.getLastChild());
default:
break;
}
return false;
} | [
"static",
"boolean",
"isConstantDeclaration",
"(",
"CodingConvention",
"convention",
",",
"JSDocInfo",
"info",
",",
"Node",
"node",
")",
"{",
"// TODO(b/77597706): Update this method to handle destructured declarations.",
"if",
"(",
"node",
".",
"isName",
"(",
")",
"&&",
"node",
".",
"getParent",
"(",
")",
".",
"isConst",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"else",
"if",
"(",
"node",
".",
"isName",
"(",
")",
"&&",
"isLhsByDestructuring",
"(",
"node",
")",
"&&",
"getRootTarget",
"(",
"node",
")",
".",
"getGrandparent",
"(",
")",
".",
"isConst",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"if",
"(",
"info",
"!=",
"null",
"&&",
"info",
".",
"isConstant",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"if",
"(",
"node",
".",
"getBooleanProp",
"(",
"Node",
".",
"IS_CONSTANT_VAR",
")",
")",
"{",
"return",
"true",
";",
"}",
"switch",
"(",
"node",
".",
"getToken",
"(",
")",
")",
"{",
"case",
"NAME",
":",
"return",
"NodeUtil",
".",
"isConstantByConvention",
"(",
"convention",
",",
"node",
")",
";",
"case",
"GETPROP",
":",
"return",
"node",
".",
"isQualifiedName",
"(",
")",
"&&",
"NodeUtil",
".",
"isConstantByConvention",
"(",
"convention",
",",
"node",
".",
"getLastChild",
"(",
")",
")",
";",
"default",
":",
"break",
";",
"}",
"return",
"false",
";",
"}"
] | Temporary function to determine if a node is constant
in the old or new world. This does not check its inputs
carefully because it will go away once we switch to the new
world. | [
"Temporary",
"function",
"to",
"determine",
"if",
"a",
"node",
"is",
"constant",
"in",
"the",
"old",
"or",
"new",
"world",
".",
"This",
"does",
"not",
"check",
"its",
"inputs",
"carefully",
"because",
"it",
"will",
"go",
"away",
"once",
"we",
"switch",
"to",
"the",
"new",
"world",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4866-L4895 |
24,287 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getNodeByLineCol | public static Node getNodeByLineCol(Node ancestor, int lineNo, int columNo) {
checkArgument(ancestor.isScript());
Node current = ancestor;
Node result = null;
while (current != null) {
int currLineNo = current.getLineno();
checkState(current.getLineno() <= lineNo);
Node nextSibling = current.getNext();
if (nextSibling != null) {
int nextSiblingLineNo = nextSibling.getLineno();
int nextSiblingColumNo = getColumnNoBase1(nextSibling);
if (result != null
&& lineNo == nextSiblingLineNo && columNo == nextSiblingColumNo) {
// The cursor is in-between two nodes.
// If just one of them is a variable, a property, or a literal, return that one.
// Otherwise, return the node to the left of the cursor.
if (result.hasChildren() && !nextSibling.hasChildren()) {
return nextSibling;
}
return result;
}
// Check if the desired location is past the end of the current node,
// and if so, continue to the siblings.
if (lineNo > nextSiblingLineNo
|| (lineNo > currLineNo && lineNo == nextSiblingLineNo)
|| (lineNo == nextSiblingLineNo && columNo > nextSiblingColumNo)) {
current = nextSibling;
continue;
}
}
// The desired node is either current or one of its children.
int currColumNo = getColumnNoBase1(current);
if (currLineNo == lineNo) {
if (currColumNo > columNo) {
// current starts past the desired node, return.
return result;
}
if (currColumNo + current.getLength() >= columNo) {
result = current;
}
}
current = current.getFirstChild();
}
return result;
} | java | public static Node getNodeByLineCol(Node ancestor, int lineNo, int columNo) {
checkArgument(ancestor.isScript());
Node current = ancestor;
Node result = null;
while (current != null) {
int currLineNo = current.getLineno();
checkState(current.getLineno() <= lineNo);
Node nextSibling = current.getNext();
if (nextSibling != null) {
int nextSiblingLineNo = nextSibling.getLineno();
int nextSiblingColumNo = getColumnNoBase1(nextSibling);
if (result != null
&& lineNo == nextSiblingLineNo && columNo == nextSiblingColumNo) {
// The cursor is in-between two nodes.
// If just one of them is a variable, a property, or a literal, return that one.
// Otherwise, return the node to the left of the cursor.
if (result.hasChildren() && !nextSibling.hasChildren()) {
return nextSibling;
}
return result;
}
// Check if the desired location is past the end of the current node,
// and if so, continue to the siblings.
if (lineNo > nextSiblingLineNo
|| (lineNo > currLineNo && lineNo == nextSiblingLineNo)
|| (lineNo == nextSiblingLineNo && columNo > nextSiblingColumNo)) {
current = nextSibling;
continue;
}
}
// The desired node is either current or one of its children.
int currColumNo = getColumnNoBase1(current);
if (currLineNo == lineNo) {
if (currColumNo > columNo) {
// current starts past the desired node, return.
return result;
}
if (currColumNo + current.getLength() >= columNo) {
result = current;
}
}
current = current.getFirstChild();
}
return result;
} | [
"public",
"static",
"Node",
"getNodeByLineCol",
"(",
"Node",
"ancestor",
",",
"int",
"lineNo",
",",
"int",
"columNo",
")",
"{",
"checkArgument",
"(",
"ancestor",
".",
"isScript",
"(",
")",
")",
";",
"Node",
"current",
"=",
"ancestor",
";",
"Node",
"result",
"=",
"null",
";",
"while",
"(",
"current",
"!=",
"null",
")",
"{",
"int",
"currLineNo",
"=",
"current",
".",
"getLineno",
"(",
")",
";",
"checkState",
"(",
"current",
".",
"getLineno",
"(",
")",
"<=",
"lineNo",
")",
";",
"Node",
"nextSibling",
"=",
"current",
".",
"getNext",
"(",
")",
";",
"if",
"(",
"nextSibling",
"!=",
"null",
")",
"{",
"int",
"nextSiblingLineNo",
"=",
"nextSibling",
".",
"getLineno",
"(",
")",
";",
"int",
"nextSiblingColumNo",
"=",
"getColumnNoBase1",
"(",
"nextSibling",
")",
";",
"if",
"(",
"result",
"!=",
"null",
"&&",
"lineNo",
"==",
"nextSiblingLineNo",
"&&",
"columNo",
"==",
"nextSiblingColumNo",
")",
"{",
"// The cursor is in-between two nodes.",
"// If just one of them is a variable, a property, or a literal, return that one.",
"// Otherwise, return the node to the left of the cursor.",
"if",
"(",
"result",
".",
"hasChildren",
"(",
")",
"&&",
"!",
"nextSibling",
".",
"hasChildren",
"(",
")",
")",
"{",
"return",
"nextSibling",
";",
"}",
"return",
"result",
";",
"}",
"// Check if the desired location is past the end of the current node,",
"// and if so, continue to the siblings.",
"if",
"(",
"lineNo",
">",
"nextSiblingLineNo",
"||",
"(",
"lineNo",
">",
"currLineNo",
"&&",
"lineNo",
"==",
"nextSiblingLineNo",
")",
"||",
"(",
"lineNo",
"==",
"nextSiblingLineNo",
"&&",
"columNo",
">",
"nextSiblingColumNo",
")",
")",
"{",
"current",
"=",
"nextSibling",
";",
"continue",
";",
"}",
"}",
"// The desired node is either current or one of its children.",
"int",
"currColumNo",
"=",
"getColumnNoBase1",
"(",
"current",
")",
";",
"if",
"(",
"currLineNo",
"==",
"lineNo",
")",
"{",
"if",
"(",
"currColumNo",
">",
"columNo",
")",
"{",
"// current starts past the desired node, return.",
"return",
"result",
";",
"}",
"if",
"(",
"currColumNo",
"+",
"current",
".",
"getLength",
"(",
")",
">=",
"columNo",
")",
"{",
"result",
"=",
"current",
";",
"}",
"}",
"current",
"=",
"current",
".",
"getFirstChild",
"(",
")",
";",
"}",
"return",
"result",
";",
"}"
] | Column number 1 represents a cursor at the start of the line. | [
"Column",
"number",
"1",
"represents",
"a",
"cursor",
"at",
"the",
"start",
"of",
"the",
"line",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L4958-L5002 |
24,288 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.newCallNode | static Node newCallNode(Node callTarget, Node... parameters) {
boolean isFreeCall = !isGet(callTarget);
Node call = IR.call(callTarget);
call.putBooleanProp(Node.FREE_CALL, isFreeCall);
for (Node parameter : parameters) {
call.addChildToBack(parameter);
}
return call;
} | java | static Node newCallNode(Node callTarget, Node... parameters) {
boolean isFreeCall = !isGet(callTarget);
Node call = IR.call(callTarget);
call.putBooleanProp(Node.FREE_CALL, isFreeCall);
for (Node parameter : parameters) {
call.addChildToBack(parameter);
}
return call;
} | [
"static",
"Node",
"newCallNode",
"(",
"Node",
"callTarget",
",",
"Node",
"...",
"parameters",
")",
"{",
"boolean",
"isFreeCall",
"=",
"!",
"isGet",
"(",
"callTarget",
")",
";",
"Node",
"call",
"=",
"IR",
".",
"call",
"(",
"callTarget",
")",
";",
"call",
".",
"putBooleanProp",
"(",
"Node",
".",
"FREE_CALL",
",",
"isFreeCall",
")",
";",
"for",
"(",
"Node",
"parameter",
":",
"parameters",
")",
"{",
"call",
".",
"addChildToBack",
"(",
"parameter",
")",
";",
"}",
"return",
"call",
";",
"}"
] | A new CALL node with the "FREE_CALL" set based on call target. | [
"A",
"new",
"CALL",
"node",
"with",
"the",
"FREE_CALL",
"set",
"based",
"on",
"call",
"target",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5014-L5022 |
24,289 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getNthSibling | private static Node getNthSibling(Node first, int index) {
Node sibling = first;
while (index != 0 && sibling != null) {
sibling = sibling.getNext();
index--;
}
return sibling;
} | java | private static Node getNthSibling(Node first, int index) {
Node sibling = first;
while (index != 0 && sibling != null) {
sibling = sibling.getNext();
index--;
}
return sibling;
} | [
"private",
"static",
"Node",
"getNthSibling",
"(",
"Node",
"first",
",",
"int",
"index",
")",
"{",
"Node",
"sibling",
"=",
"first",
";",
"while",
"(",
"index",
"!=",
"0",
"&&",
"sibling",
"!=",
"null",
")",
"{",
"sibling",
"=",
"sibling",
".",
"getNext",
"(",
")",
";",
"index",
"--",
";",
"}",
"return",
"sibling",
";",
"}"
] | Given the first sibling, this returns the nth
sibling or null if no such sibling exists.
This is like "getChildAtIndex" but returns null for non-existent indexes. | [
"Given",
"the",
"first",
"sibling",
"this",
"returns",
"the",
"nth",
"sibling",
"or",
"null",
"if",
"no",
"such",
"sibling",
"exists",
".",
"This",
"is",
"like",
"getChildAtIndex",
"but",
"returns",
"null",
"for",
"non",
"-",
"existent",
"indexes",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5197-L5204 |
24,290 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getArgumentForFunction | static Node getArgumentForFunction(Node function, int index) {
checkState(function.isFunction());
return getNthSibling(
function.getSecondChild().getFirstChild(), index);
} | java | static Node getArgumentForFunction(Node function, int index) {
checkState(function.isFunction());
return getNthSibling(
function.getSecondChild().getFirstChild(), index);
} | [
"static",
"Node",
"getArgumentForFunction",
"(",
"Node",
"function",
",",
"int",
"index",
")",
"{",
"checkState",
"(",
"function",
".",
"isFunction",
"(",
")",
")",
";",
"return",
"getNthSibling",
"(",
"function",
".",
"getSecondChild",
"(",
")",
".",
"getFirstChild",
"(",
")",
",",
"index",
")",
";",
"}"
] | Given the function, this returns the nth
argument or null if no such parameter exists. | [
"Given",
"the",
"function",
"this",
"returns",
"the",
"nth",
"argument",
"or",
"null",
"if",
"no",
"such",
"parameter",
"exists",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5210-L5214 |
24,291 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getArgumentForCallOrNew | static Node getArgumentForCallOrNew(Node call, int index) {
checkState(isCallOrNew(call));
return getNthSibling(call.getSecondChild(), index);
} | java | static Node getArgumentForCallOrNew(Node call, int index) {
checkState(isCallOrNew(call));
return getNthSibling(call.getSecondChild(), index);
} | [
"static",
"Node",
"getArgumentForCallOrNew",
"(",
"Node",
"call",
",",
"int",
"index",
")",
"{",
"checkState",
"(",
"isCallOrNew",
"(",
"call",
")",
")",
";",
"return",
"getNthSibling",
"(",
"call",
".",
"getSecondChild",
"(",
")",
",",
"index",
")",
";",
"}"
] | Given the new or call, this returns the nth
argument of the call or null if no such argument exists. | [
"Given",
"the",
"new",
"or",
"call",
"this",
"returns",
"the",
"nth",
"argument",
"of",
"the",
"call",
"or",
"null",
"if",
"no",
"such",
"argument",
"exists",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5220-L5223 |
24,292 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.isInvocationTarget | static boolean isInvocationTarget(Node n) {
Node parent = n.getParent();
return parent != null && (isCallOrNew(parent) || parent.isTaggedTemplateLit())
&& parent.getFirstChild() == n;
} | java | static boolean isInvocationTarget(Node n) {
Node parent = n.getParent();
return parent != null && (isCallOrNew(parent) || parent.isTaggedTemplateLit())
&& parent.getFirstChild() == n;
} | [
"static",
"boolean",
"isInvocationTarget",
"(",
"Node",
"n",
")",
"{",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"return",
"parent",
"!=",
"null",
"&&",
"(",
"isCallOrNew",
"(",
"parent",
")",
"||",
"parent",
".",
"isTaggedTemplateLit",
"(",
")",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"==",
"n",
";",
"}"
] | Returns whether this is a target of a call or new. | [
"Returns",
"whether",
"this",
"is",
"a",
"target",
"of",
"a",
"call",
"or",
"new",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5228-L5232 |
24,293 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getDeclaredTypeExpression | @Nullable
public static JSTypeExpression getDeclaredTypeExpression(Node declaration) {
checkArgument(declaration.isName() || declaration.isStringKey());
JSDocInfo nameJsdoc = getBestJSDocInfo(declaration);
if (nameJsdoc != null) {
return nameJsdoc.getType();
}
Node parent = declaration.getParent();
if (parent.isRest() || parent.isDefaultValue()) {
parent = parent.getParent();
}
if (parent.isParamList()) {
JSDocInfo functionJsdoc = getBestJSDocInfo(parent.getParent());
if (functionJsdoc != null) {
return functionJsdoc.getParameterType(declaration.getString());
}
}
return null;
} | java | @Nullable
public static JSTypeExpression getDeclaredTypeExpression(Node declaration) {
checkArgument(declaration.isName() || declaration.isStringKey());
JSDocInfo nameJsdoc = getBestJSDocInfo(declaration);
if (nameJsdoc != null) {
return nameJsdoc.getType();
}
Node parent = declaration.getParent();
if (parent.isRest() || parent.isDefaultValue()) {
parent = parent.getParent();
}
if (parent.isParamList()) {
JSDocInfo functionJsdoc = getBestJSDocInfo(parent.getParent());
if (functionJsdoc != null) {
return functionJsdoc.getParameterType(declaration.getString());
}
}
return null;
} | [
"@",
"Nullable",
"public",
"static",
"JSTypeExpression",
"getDeclaredTypeExpression",
"(",
"Node",
"declaration",
")",
"{",
"checkArgument",
"(",
"declaration",
".",
"isName",
"(",
")",
"||",
"declaration",
".",
"isStringKey",
"(",
")",
")",
";",
"JSDocInfo",
"nameJsdoc",
"=",
"getBestJSDocInfo",
"(",
"declaration",
")",
";",
"if",
"(",
"nameJsdoc",
"!=",
"null",
")",
"{",
"return",
"nameJsdoc",
".",
"getType",
"(",
")",
";",
"}",
"Node",
"parent",
"=",
"declaration",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"parent",
".",
"isRest",
"(",
")",
"||",
"parent",
".",
"isDefaultValue",
"(",
")",
")",
"{",
"parent",
"=",
"parent",
".",
"getParent",
"(",
")",
";",
"}",
"if",
"(",
"parent",
".",
"isParamList",
"(",
")",
")",
"{",
"JSDocInfo",
"functionJsdoc",
"=",
"getBestJSDocInfo",
"(",
"parent",
".",
"getParent",
"(",
")",
")",
";",
"if",
"(",
"functionJsdoc",
"!=",
"null",
")",
"{",
"return",
"functionJsdoc",
".",
"getParameterType",
"(",
"declaration",
".",
"getString",
"(",
")",
")",
";",
"}",
"}",
"return",
"null",
";",
"}"
] | Return declared JSDoc type for the given name declaration, or null if none present. | [
"Return",
"declared",
"JSDoc",
"type",
"for",
"the",
"given",
"name",
"declaration",
"or",
"null",
"if",
"none",
"present",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5256-L5274 |
24,294 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getBestJSDocInfo | @Nullable
public static JSDocInfo getBestJSDocInfo(Node n) {
Node jsdocNode = getBestJSDocInfoNode(n);
return jsdocNode == null ? null : jsdocNode.getJSDocInfo();
} | java | @Nullable
public static JSDocInfo getBestJSDocInfo(Node n) {
Node jsdocNode = getBestJSDocInfoNode(n);
return jsdocNode == null ? null : jsdocNode.getJSDocInfo();
} | [
"@",
"Nullable",
"public",
"static",
"JSDocInfo",
"getBestJSDocInfo",
"(",
"Node",
"n",
")",
"{",
"Node",
"jsdocNode",
"=",
"getBestJSDocInfoNode",
"(",
"n",
")",
";",
"return",
"jsdocNode",
"==",
"null",
"?",
"null",
":",
"jsdocNode",
".",
"getJSDocInfo",
"(",
")",
";",
"}"
] | Find the best JSDoc for the given node. | [
"Find",
"the",
"best",
"JSDoc",
"for",
"the",
"given",
"node",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5277-L5281 |
24,295 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getBestLValue | public static Node getBestLValue(Node n) {
Node parent = n.getParent();
if (isFunctionDeclaration(n) || isClassDeclaration(n)) {
return n.getFirstChild();
} else if (n.isClassMembers()) {
return getBestLValue(parent);
} else if (parent.isName()) {
return parent;
} else if (parent.isAssign()) {
return parent.getFirstChild();
} else if (mayBeObjectLitKey(parent) || parent.isComputedProp()) {
return parent;
} else if ((parent.isHook() && parent.getFirstChild() != n)
|| parent.isOr()
|| parent.isAnd()
|| (parent.isComma() && parent.getFirstChild() != n)) {
return getBestLValue(parent);
} else if (parent.isCast()) {
return getBestLValue(parent);
}
return null;
} | java | public static Node getBestLValue(Node n) {
Node parent = n.getParent();
if (isFunctionDeclaration(n) || isClassDeclaration(n)) {
return n.getFirstChild();
} else if (n.isClassMembers()) {
return getBestLValue(parent);
} else if (parent.isName()) {
return parent;
} else if (parent.isAssign()) {
return parent.getFirstChild();
} else if (mayBeObjectLitKey(parent) || parent.isComputedProp()) {
return parent;
} else if ((parent.isHook() && parent.getFirstChild() != n)
|| parent.isOr()
|| parent.isAnd()
|| (parent.isComma() && parent.getFirstChild() != n)) {
return getBestLValue(parent);
} else if (parent.isCast()) {
return getBestLValue(parent);
}
return null;
} | [
"public",
"static",
"Node",
"getBestLValue",
"(",
"Node",
"n",
")",
"{",
"Node",
"parent",
"=",
"n",
".",
"getParent",
"(",
")",
";",
"if",
"(",
"isFunctionDeclaration",
"(",
"n",
")",
"||",
"isClassDeclaration",
"(",
"n",
")",
")",
"{",
"return",
"n",
".",
"getFirstChild",
"(",
")",
";",
"}",
"else",
"if",
"(",
"n",
".",
"isClassMembers",
"(",
")",
")",
"{",
"return",
"getBestLValue",
"(",
"parent",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isName",
"(",
")",
")",
"{",
"return",
"parent",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isAssign",
"(",
")",
")",
"{",
"return",
"parent",
".",
"getFirstChild",
"(",
")",
";",
"}",
"else",
"if",
"(",
"mayBeObjectLitKey",
"(",
"parent",
")",
"||",
"parent",
".",
"isComputedProp",
"(",
")",
")",
"{",
"return",
"parent",
";",
"}",
"else",
"if",
"(",
"(",
"parent",
".",
"isHook",
"(",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"!=",
"n",
")",
"||",
"parent",
".",
"isOr",
"(",
")",
"||",
"parent",
".",
"isAnd",
"(",
")",
"||",
"(",
"parent",
".",
"isComma",
"(",
")",
"&&",
"parent",
".",
"getFirstChild",
"(",
")",
"!=",
"n",
")",
")",
"{",
"return",
"getBestLValue",
"(",
"parent",
")",
";",
"}",
"else",
"if",
"(",
"parent",
".",
"isCast",
"(",
")",
")",
"{",
"return",
"getBestLValue",
"(",
"parent",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Find the l-value that the given r-value is being assigned to. | [
"Find",
"the",
"l",
"-",
"value",
"that",
"the",
"given",
"r",
"-",
"value",
"is",
"being",
"assigned",
"to",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5317-L5338 |
24,296 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.markNewScopesChanged | static void markNewScopesChanged(Node node, AbstractCompiler compiler) {
if (node.isFunction()) {
compiler.reportChangeToChangeScope(node);
}
for (Node child = node.getFirstChild(); child != null; child = child.getNext()) {
markNewScopesChanged(child, compiler);
}
} | java | static void markNewScopesChanged(Node node, AbstractCompiler compiler) {
if (node.isFunction()) {
compiler.reportChangeToChangeScope(node);
}
for (Node child = node.getFirstChild(); child != null; child = child.getNext()) {
markNewScopesChanged(child, compiler);
}
} | [
"static",
"void",
"markNewScopesChanged",
"(",
"Node",
"node",
",",
"AbstractCompiler",
"compiler",
")",
"{",
"if",
"(",
"node",
".",
"isFunction",
"(",
")",
")",
"{",
"compiler",
".",
"reportChangeToChangeScope",
"(",
"node",
")",
";",
"}",
"for",
"(",
"Node",
"child",
"=",
"node",
".",
"getFirstChild",
"(",
")",
";",
"child",
"!=",
"null",
";",
"child",
"=",
"child",
".",
"getNext",
"(",
")",
")",
"{",
"markNewScopesChanged",
"(",
"child",
",",
"compiler",
")",
";",
"}",
"}"
] | Recurses through a tree, marking all function nodes as changed. | [
"Recurses",
"through",
"a",
"tree",
"marking",
"all",
"function",
"nodes",
"as",
"changed",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5822-L5829 |
24,297 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.markFunctionsDeleted | public static void markFunctionsDeleted(Node node, AbstractCompiler compiler) {
if (node.isFunction()) {
compiler.reportFunctionDeleted(node);
}
for (Node child = node.getFirstChild(); child != null; child = child.getNext()) {
markFunctionsDeleted(child, compiler);
}
} | java | public static void markFunctionsDeleted(Node node, AbstractCompiler compiler) {
if (node.isFunction()) {
compiler.reportFunctionDeleted(node);
}
for (Node child = node.getFirstChild(); child != null; child = child.getNext()) {
markFunctionsDeleted(child, compiler);
}
} | [
"public",
"static",
"void",
"markFunctionsDeleted",
"(",
"Node",
"node",
",",
"AbstractCompiler",
"compiler",
")",
"{",
"if",
"(",
"node",
".",
"isFunction",
"(",
")",
")",
"{",
"compiler",
".",
"reportFunctionDeleted",
"(",
"node",
")",
";",
"}",
"for",
"(",
"Node",
"child",
"=",
"node",
".",
"getFirstChild",
"(",
")",
";",
"child",
"!=",
"null",
";",
"child",
"=",
"child",
".",
"getNext",
"(",
")",
")",
"{",
"markFunctionsDeleted",
"(",
"child",
",",
"compiler",
")",
";",
"}",
"}"
] | Recurses through a tree, marking all function nodes deleted. | [
"Recurses",
"through",
"a",
"tree",
"marking",
"all",
"function",
"nodes",
"deleted",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5832-L5840 |
24,298 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.getParentChangeScopeNodes | public static List<Node> getParentChangeScopeNodes(List<Node> scopeNodes) {
Set<Node> parentScopeNodes = new LinkedHashSet<>(scopeNodes);
for (Node scopeNode : scopeNodes) {
parentScopeNodes.add(getEnclosingChangeScopeRoot(scopeNode));
}
return new ArrayList<>(parentScopeNodes);
} | java | public static List<Node> getParentChangeScopeNodes(List<Node> scopeNodes) {
Set<Node> parentScopeNodes = new LinkedHashSet<>(scopeNodes);
for (Node scopeNode : scopeNodes) {
parentScopeNodes.add(getEnclosingChangeScopeRoot(scopeNode));
}
return new ArrayList<>(parentScopeNodes);
} | [
"public",
"static",
"List",
"<",
"Node",
">",
"getParentChangeScopeNodes",
"(",
"List",
"<",
"Node",
">",
"scopeNodes",
")",
"{",
"Set",
"<",
"Node",
">",
"parentScopeNodes",
"=",
"new",
"LinkedHashSet",
"<>",
"(",
"scopeNodes",
")",
";",
"for",
"(",
"Node",
"scopeNode",
":",
"scopeNodes",
")",
"{",
"parentScopeNodes",
".",
"add",
"(",
"getEnclosingChangeScopeRoot",
"(",
"scopeNode",
")",
")",
";",
"}",
"return",
"new",
"ArrayList",
"<>",
"(",
"parentScopeNodes",
")",
";",
"}"
] | Returns the list of scope nodes which are parents of the provided list of scope nodes. | [
"Returns",
"the",
"list",
"of",
"scope",
"nodes",
"which",
"are",
"parents",
"of",
"the",
"provided",
"list",
"of",
"scope",
"nodes",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5843-L5849 |
24,299 | google/closure-compiler | src/com/google/javascript/jscomp/NodeUtil.java | NodeUtil.removeNestedChangeScopeNodes | public static List<Node> removeNestedChangeScopeNodes(List<Node> scopeNodes) {
Set<Node> uniqueScopeNodes = new LinkedHashSet<>(scopeNodes);
for (Node scopeNode : scopeNodes) {
for (Node ancestor = scopeNode.getParent();
ancestor != null;
ancestor = ancestor.getParent()) {
if (isChangeScopeRoot(ancestor) && uniqueScopeNodes.contains(ancestor)) {
uniqueScopeNodes.remove(scopeNode);
break;
}
}
}
return new ArrayList<>(uniqueScopeNodes);
} | java | public static List<Node> removeNestedChangeScopeNodes(List<Node> scopeNodes) {
Set<Node> uniqueScopeNodes = new LinkedHashSet<>(scopeNodes);
for (Node scopeNode : scopeNodes) {
for (Node ancestor = scopeNode.getParent();
ancestor != null;
ancestor = ancestor.getParent()) {
if (isChangeScopeRoot(ancestor) && uniqueScopeNodes.contains(ancestor)) {
uniqueScopeNodes.remove(scopeNode);
break;
}
}
}
return new ArrayList<>(uniqueScopeNodes);
} | [
"public",
"static",
"List",
"<",
"Node",
">",
"removeNestedChangeScopeNodes",
"(",
"List",
"<",
"Node",
">",
"scopeNodes",
")",
"{",
"Set",
"<",
"Node",
">",
"uniqueScopeNodes",
"=",
"new",
"LinkedHashSet",
"<>",
"(",
"scopeNodes",
")",
";",
"for",
"(",
"Node",
"scopeNode",
":",
"scopeNodes",
")",
"{",
"for",
"(",
"Node",
"ancestor",
"=",
"scopeNode",
".",
"getParent",
"(",
")",
";",
"ancestor",
"!=",
"null",
";",
"ancestor",
"=",
"ancestor",
".",
"getParent",
"(",
")",
")",
"{",
"if",
"(",
"isChangeScopeRoot",
"(",
"ancestor",
")",
"&&",
"uniqueScopeNodes",
".",
"contains",
"(",
"ancestor",
")",
")",
"{",
"uniqueScopeNodes",
".",
"remove",
"(",
"scopeNode",
")",
";",
"break",
";",
"}",
"}",
"}",
"return",
"new",
"ArrayList",
"<>",
"(",
"uniqueScopeNodes",
")",
";",
"}"
] | Removes any scope nodes from the provided list that are nested within some other scope node
also in the list. Returns the modified list. | [
"Removes",
"any",
"scope",
"nodes",
"from",
"the",
"provided",
"list",
"that",
"are",
"nested",
"within",
"some",
"other",
"scope",
"node",
"also",
"in",
"the",
"list",
".",
"Returns",
"the",
"modified",
"list",
"."
] | d81e36740f6a9e8ac31a825ee8758182e1dc5aae | https://github.com/google/closure-compiler/blob/d81e36740f6a9e8ac31a825ee8758182e1dc5aae/src/com/google/javascript/jscomp/NodeUtil.java#L5855-L5868 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.