Skip to content

Commit

Permalink
Make the Token class into an enum
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124639427
  • Loading branch information
blickly committed Jun 13, 2016
1 parent 6a26c4a commit 62f4c44
Show file tree
Hide file tree
Showing 58 changed files with 373 additions and 569 deletions.
Expand Up @@ -131,7 +131,7 @@ private void nameObjectLiteralMethods(Node objectLiteral, String context) {
// concatenate the context and key name to get a new qualified name.
String name = namer.getCombinedName(context, namer.getName(keyNode));

Token.Kind type = valueNode.getType();
Token type = valueNode.getType();
if (type == Token.FUNCTION) {
// set name if function is anonymous
Node functionNameNode = valueNode.getFirstChild();
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/AstChangeProxy.java
Expand Up @@ -102,7 +102,7 @@ final void replaceWith(Node parent, Node node, List<Node> replacements) {
return;
}

Token.Kind parentType = parent.getType();
Token parentType = parent.getType();

Preconditions.checkState(size == 1 ||
parentType == Token.BLOCK ||
Expand Down
20 changes: 10 additions & 10 deletions src/com/google/javascript/jscomp/AstValidator.java
Expand Up @@ -768,7 +768,7 @@ private void validateParametersEs6(Node n) {
}
}

private void validateDefaultValue(Token.Kind type, Node n) {
private void validateDefaultValue(Token type, Node n) {
validateAssignmentExpression(n);
Node lhs = n.getFirstChild();

Expand All @@ -795,7 +795,7 @@ private void validateCall(Node n) {
* appropriately for a descendant of a {@link Node} of this type.
* @param n
*/
private void validateRest(Token.Kind contextType, Node n) {
private void validateRest(Token contextType, Node n) {
validateNodeType(Token.REST, n);
validateChildCount(n);
validateLHS(contextType, n.getFirstChild());
Expand Down Expand Up @@ -827,14 +827,14 @@ private void validateNew(Node n) {
}
}

private void validateNameDeclarationHelper(Token.Kind type, Node n) {
private void validateNameDeclarationHelper(Token type, Node n) {
validateMinimumChildCount(n, 1);
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateNameDeclarationChild(type, c);
}
}

private void validateNameDeclarationChild(Token.Kind type, Node n) {
private void validateNameDeclarationChild(Token type, Node n) {
if (n.isName()) {
validateLHS(type, n);
} else if (n.isDestructuringLhs()) {
Expand All @@ -849,7 +849,7 @@ private void validateNameDeclarationChild(Token.Kind type, Node n) {
* appropriately for a descendant of a {@link Node} of this type.
* @param n
*/
private void validateLHS(Token.Kind contextType, Node n) {
private void validateLHS(Token contextType, Node n) {
if (n.isName()) {
// Don't use validateName here since this NAME node may have
// a child.
Expand All @@ -871,7 +871,7 @@ private void validateLHS(Token.Kind contextType, Node n) {
}
}

private void validateArrayPattern(Token.Kind type, Node n) {
private void validateArrayPattern(Token type, Node n) {
validateNodeType(Token.ARRAY_PATTERN, n);
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
// When the array pattern is a direct child of a var/let/const node,
Expand All @@ -890,7 +890,7 @@ private void validateArrayPattern(Token.Kind type, Node n) {
}
}

private void validateObjectPattern(Token.Kind type, Node n) {
private void validateObjectPattern(Token type, Node n) {
validateNodeType(Token.OBJECT_PATTERN, n);
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
// When the object pattern is a direct child of a var/let/const node,
Expand Down Expand Up @@ -1243,7 +1243,7 @@ private void validateObjectLitStringKey(Node n) {
}
}

private void validateObjectPatternStringKey(Token.Kind type, Node n) {
private void validateObjectPatternStringKey(Token type, Node n) {
validateNodeType(Token.STRING_KEY, n);
validateObjectLiteralKeyName(n);
validateChildCountIn(n, 0, 1);
Expand All @@ -1260,7 +1260,7 @@ private void validateObjectLitComputedPropKey(Node n) {
validateExpression(n.getLastChild());
}

private void validateObjectPatternComputedPropKey(Token.Kind type, Node n) {
private void validateObjectPatternComputedPropKey(Token type, Node n) {
validateNodeType(Token.COMPUTED_PROP, n);
validateChildCount(n);
validateExpression(n.getFirstChild());
Expand Down Expand Up @@ -1394,7 +1394,7 @@ private void violation(String message, Node n) {
violationHandler.handleViolation(message, n);
}

private void validateNodeType(Token.Kind type, Node n) {
private void validateNodeType(Token type, Node n) {
if (n.getType() != type) {
violation("Expected " + type + " but was " + n.getType(), n);
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/CallGraph.java
Expand Up @@ -228,7 +228,7 @@ private void createFunctionsAndCallsites(Node jsRoot,
NodeTraversal.traverseEs6(compiler, jsRoot, new AbstractPostOrderCallback() {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
Token.Kind nodeType = n.getType();
Token nodeType = n.getType();

if (nodeType == Token.CALL || nodeType == Token.NEW) {
Callsite callsite = createCallsite(n);
Expand Down Expand Up @@ -365,7 +365,7 @@ private void fillInFunctionInformation(DefinitionProvider provider) {
*/
private void updateFunctionForUse(Function function, Node useNode) {
Node useParent = useNode.getParent();
Token.Kind parentType = useParent.getType();
Token parentType = useParent.getType();

if ((parentType == Token.CALL || parentType == Token.NEW)
&& useParent.getFirstChild() == useNode) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CheckGlobalThis.java
Expand Up @@ -105,7 +105,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
// function a() {} // or
// a.x = function() {}; // or
// var a = {x: function() {}};
Token.Kind pType = parent.getType();
Token pType = parent.getType();
if (!(pType == Token.BLOCK ||
pType == Token.SCRIPT ||
pType == Token.NAME ||
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CheckRegExp.java
Expand Up @@ -70,7 +70,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
if (NodeUtil.isReferenceName(n)) {
String name = n.getString();
if (name.equals("RegExp") && t.getScope().getVar(name) == null) {
Token.Kind parentType = parent.getType();
Token parentType = parent.getType();
boolean first = (n == parent.getFirstChild());
if (!((parentType == Token.NEW && first)
|| (parentType == Token.CALL && first)
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/CodeGenerator.java
Expand Up @@ -113,7 +113,7 @@ void add(Node n, Context context) {
}
}

Token.Kind type = n.getType();
Token type = n.getType();
String opstr = NodeUtil.opToStr(type);
int childCount = n.getChildCount();
Node first = n.getFirstChild();
Expand Down Expand Up @@ -1354,7 +1354,7 @@ private void maybeAddOptional(Node n) {
* We assume nodes are left-recursive.
*/
private void unrollBinaryOperator(
Node n, Token.Kind op, String opStr, Context context,
Node n, Token op, String opStr, Context context,
Context rhsContext, int leftPrecedence, int rightPrecedence) {
Node firstNonOperator = n.getFirstChild();
while (firstNonOperator.getType() == op) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CodePrinter.java
Expand Up @@ -382,7 +382,7 @@ boolean breakAfterBlockFor(Node n, boolean isStatementContext) {
Preconditions.checkState(n.isBlock(), n);
Node parent = n.getParent();
if (parent != null) {
Token.Kind type = parent.getType();
Token type = parent.getType();
switch (type) {
case DO:
// Don't break before 'while' in DO-WHILE statements.
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CollapseProperties.java
Expand Up @@ -607,7 +607,7 @@ private void flattenNameRefAtDepth(String alias, Node n, int depth,
// This method has to work for both GETPROP chains and, in rare cases,
// OBJLIT keys, possibly nested. That's why we check for children before
// proceeding. In the OBJLIT case, we don't need to do anything.
Token.Kind nType = n.getType();
Token nType = n.getType();
boolean isQName = nType == Token.NAME || nType == Token.GETPROP;
boolean isObjKey = NodeUtil.isObjectLitKey(n);
Preconditions.checkState(isObjKey || isQName);
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/ControlFlowAnalysis.java
Expand Up @@ -876,9 +876,9 @@ private void connectToPossibleExceptionHandler(Node cfgNode, Node target) {
/**
* Get the next sibling (including itself) of one of the given types.
*/
private static Node getNextSiblingOfType(Node first, Token.Kind ... types) {
private static Node getNextSiblingOfType(Node first, Token ... types) {
for (Node c = first; c != null; c = c.getNext()) {
for (Token.Kind type : types) {
for (Token type : types) {
if (c.getType() == type) {
return c;
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/DefinitionsRemover.java
Expand Up @@ -163,7 +163,7 @@ public boolean isExtern() {
* RHS is missing.
*/
abstract static class IncompleteDefinition extends Definition {
private static final Set<Token.Kind> ALLOWED_TYPES =
private static final Set<Token> ALLOWED_TYPES =
ImmutableSet.of(Token.NAME, Token.GETPROP, Token.GETELEM);
private final Node lValue;

Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Denormalize.java
Expand Up @@ -134,7 +134,7 @@ private void maybeCollapseAssignShorthand(Node n, Node parent) {
&& NodeUtil.hasCorrespondingAssignmentOp(n.getLastChild())
&& n.getLastChild().getFirstChild().isName()) {
Node op = n.getLastChild();
Token.Kind assignOp = NodeUtil.getAssignOpFromOp(op);
Token assignOp = NodeUtil.getAssignOpFromOp(op);
if (n.getFirstChild().getString().equals(op.getFirstChild().getString())) {
op.setType(assignOp);
Node opDetached = op.detachFromParent();
Expand Down
Expand Up @@ -438,7 +438,7 @@ private void visitDestructuringPatternInEnhancedFor(Node pattern) {
Node block = forNode.getLastChild();
declarationNode.replaceChild(
destructuringLhs, IR.name(tempVarName).useSourceInfoFrom(pattern));
Token.Kind declarationType = declarationNode.getType();
Token declarationType = declarationNode.getType();
Node decl = IR.declaration(pattern.detachFromParent(), IR.name(tempVarName), declarationType);
decl.useSourceInfoIfMissingFromForTree(pattern);
block.addChildToFront(decl);
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Expand Up @@ -917,7 +917,7 @@ private boolean controlCanExit(Node n) {
/**
* Finds the only child of the {@code node} of the given type.
*/
private Node getUnique(Node node, Token.Kind type) {
private Node getUnique(Node node, Token type) {
List<Node> matches = new ArrayList<>();
insertAll(node, type, matches);
Preconditions.checkState(matches.size() == 1, matches);
Expand All @@ -927,7 +927,7 @@ private Node getUnique(Node node, Token.Kind type) {
/**
* Adds all children of the {@code node} of the given type to given list.
*/
private void insertAll(Node node, Token.Kind type, List<Node> matchingNodes) {
private void insertAll(Node node, Token type, List<Node> matchingNodes) {
if (node.getType() == type) {
matchingNodes.add(node);
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/Es6ToEs3Converter.java
Expand Up @@ -250,7 +250,7 @@ private void visitForOf(Node node, Node parent) {
iterName.makeNonIndexable();
Node getNext = IR.call(IR.getprop(iterName.cloneTree(), IR.string("next")));
String variableName;
Token.Kind declType;
Token declType;
if (variable.isName()) {
declType = Token.NAME;
variableName = variable.getQualifiedName();
Expand Down Expand Up @@ -502,7 +502,7 @@ private void visitObjectWithComputedProperty(Node obj) {
}
Node val = propdef.removeFirstChild();
propdef.setType(Token.STRING);
Token.Kind type = propdef.isQuotedString() ? Token.GETELEM : Token.GETPROP;
Token type = propdef.isQuotedString() ? Token.GETELEM : Token.GETPROP;
Node access = new Node(type, IR.name(objName), propdef);
result = IR.comma(IR.assign(access, val), result);
}
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/ExpressionDecomposer.java
Expand Up @@ -166,7 +166,7 @@ private void exposeExpression(Node expressionRoot, Node subExpression) {
grandchild = child,
child = parent,
parent = child.getParent()) {
Token.Kind parentType = parent.getType();
Token parentType = parent.getType();
Preconditions.checkState(
!isConditionalOp(parent) || child == parent.getFirstChild());
if (parentType == Token.ASSIGN) {
Expand All @@ -184,7 +184,7 @@ private void exposeExpression(Node expressionRoot, Node subExpression) {
} else {
// Alias "next()" in "next().foo"
Node left = parent.getFirstChild();
Token.Kind type = left.getType();
Token type = left.getType();
if (left != child) {
Preconditions.checkState(NodeUtil.isGet(left));
if (type == Token.GETELEM) {
Expand Down Expand Up @@ -660,7 +660,7 @@ private static boolean isConditionalOp(Node n) {
static Node findExpressionRoot(Node subExpression) {
Node child = subExpression;
for (Node parent : child.getAncestors()) {
Token.Kind parentType = parent.getType();
Token parentType = parent.getType();
switch (parentType) {
// Supported expression roots:
// SWITCH and IF can have multiple children, but the CASE, DEFAULT,
Expand Down
Expand Up @@ -226,7 +226,7 @@ private static Set<String> findModifiedParameters(
* @param parent The parent of the node.
*/
private static boolean canNameValueChange(Node n, Node parent) {
Token.Kind type = parent.getType();
Token type = parent.getType();
return (type == Token.VAR || type == Token.INC || type == Token.DEC ||
(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == n) ||
(NodeUtil.isForIn(parent)));
Expand Down Expand Up @@ -480,7 +480,7 @@ public void visit(Node n) {
*/
private boolean hasNonLocalSideEffect(Node n) {
boolean sideEffect = false;
Token.Kind type = n.getType();
Token type = n.getType();
// Note: Only care about changes to non-local names, specifically
// ignore VAR declaration assignments.
if (NodeUtil.isAssignmentOp(n)
Expand Down
Expand Up @@ -146,7 +146,7 @@ public void keepSimplifiedHookExpression(Node hook,
hook.addChildToBack(simplifyShortCircuitBranch(elseBranch));
keepSubTree(hook);
} else if (thenHasSideEffects || elseHasSideEffects) {
Token.Kind type = thenHasSideEffects ? Token.AND : Token.OR;
Token type = thenHasSideEffects ? Token.AND : Token.OR;
Node body = thenHasSideEffects ? thenBranch : elseBranch;
Node simplified = new Node(
type, condition.detachFromParent(),
Expand Down Expand Up @@ -186,7 +186,7 @@ private Node simplifyShortCircuitBranch(Node node) {
}
}

private static final Set<Token.Kind> FORBIDDEN_TYPES = ImmutableSet.of(
private static final Set<Token> FORBIDDEN_TYPES = ImmutableSet.of(
Token.BLOCK, Token.SCRIPT, Token.VAR, Token.EXPR_RESULT, Token.RETURN);
private final AbstractCompiler compiler;
private final SideEffectAccumulator accumulator;
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/InstrumentFunctions.java
Expand Up @@ -312,7 +312,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
} else {
Node beforeChild = n;
for (Node ancestor : n.getAncestors()) {
Token.Kind type = ancestor.getType();
Token type = ancestor.getType();
if (type == Token.BLOCK || type == Token.SCRIPT) {
addingRoot = ancestor;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/JsMessageVisitor.java
Expand Up @@ -933,7 +933,7 @@ static String toLowerCamelCaseWithNumericSuffixes(String input) {
*
* @throws MalformedException if the node is null or the wrong type
*/
protected void checkNode(@Nullable Node node, Token.Kind type) throws MalformedException {
protected void checkNode(@Nullable Node node, Token type) throws MalformedException {
if (node == null) {
throw new MalformedException(
"Expected node type " + type + "; found: null", node);
Expand Down
Expand Up @@ -156,7 +156,7 @@ public static TypeDeclarationNode convert(@Nullable JSTypeExpression typeExpr) {
// representation directly, and delete this function.
@Nullable
public static TypeDeclarationNode convertTypeNodeAST(Node n) {
Token.Kind token = n.getType();
Token token = n.getType();
switch (token) {
case STAR:
case EMPTY: // for function types that don't declare a return type
Expand Down
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/MinimizeExitPoints.java
Expand Up @@ -115,7 +115,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
* @param labelName If parent is a label the name of the label to look for,
* null otherwise. Non-null only for breaks within labels.
*/
void tryMinimizeExits(Node n, Token.Kind exitType, @Nullable String labelName) {
void tryMinimizeExits(Node n, Token exitType, @Nullable String labelName) {

// Just an 'exit'.
if (matchingExitNode(n, exitType, labelName)) {
Expand Down Expand Up @@ -212,7 +212,7 @@ void tryMinimizeExits(Node n, Token.Kind exitType, @Nullable String labelName) {
}
}

void tryMinimizeSwitchExits(Node n, Token.Kind exitType, @Nullable String labelName) {
void tryMinimizeSwitchExits(Node n, Token exitType, @Nullable String labelName) {
Preconditions.checkState(n.isSwitch());
// Skipping the switch condition, visit all the children.
for (Node c = n.getSecondChild(); c != null; c = c.getNext()) {
Expand All @@ -229,7 +229,7 @@ void tryMinimizeSwitchExits(Node n, Token.Kind exitType, @Nullable String labelN
* Attempt to remove explicit exits from switch cases that also occur implicitly
* after the switch.
*/
void tryMinimizeSwitchCaseExits(Node n, Token.Kind exitType, @Nullable String labelName) {
void tryMinimizeSwitchCaseExits(Node n, Token exitType, @Nullable String labelName) {
Preconditions.checkState(NodeUtil.isSwitchCase(n));

Preconditions.checkState(n != n.getParent().getLastChild());
Expand Down Expand Up @@ -267,7 +267,7 @@ void tryMinimizeSwitchCaseExits(Node n, Token.Kind exitType, @Nullable String la
* named-break associated with a label.
*/
private void tryMinimizeIfBlockExits(Node srcBlock, Node destBlock,
Node ifNode, Token.Kind exitType, @Nullable String labelName) {
Node ifNode, Token exitType, @Nullable String labelName) {
Node exitNodeParent = null;
Node exitNode = null;

Expand Down Expand Up @@ -326,7 +326,7 @@ private void tryMinimizeIfBlockExits(Node srcBlock, Node destBlock,
* non-null only for breaks associated with labels.
* @return Whether the node matches the specified block-exit type.
*/
private static boolean matchingExitNode(Node n, Token.Kind type, @Nullable String labelName) {
private static boolean matchingExitNode(Node n, Token type, @Nullable String labelName) {
if (n.getType() == type) {
if (type == Token.RETURN) {
// only returns without expressions.
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/MinimizedCondition.java
Expand Up @@ -181,8 +181,8 @@ private static MinimizedCondition computeMinimizedCondition(Node n) {
}
case AND:
case OR: {
Token.Kind opType = n.getType();
Token.Kind complementType = opType == Token.AND ? Token.OR : Token.AND;
Token opType = n.getType();
Token complementType = opType == Token.AND ? Token.OR : Token.AND;
MinimizedCondition leftSubtree =
computeMinimizedCondition(n.getFirstChild().detachFromParent());
MinimizedCondition rightSubtree =
Expand Down

0 comments on commit 62f4c44

Please sign in to comment.