Skip to content

Commit 491ad01

Browse files
blicklycopybara-github
authored andcommitted
Change more statement switches to expression switches
PiperOrigin-RevId: 786528950
1 parent 6e12063 commit 491ad01

25 files changed

+355
-521
lines changed

src/com/google/javascript/jscomp/AliasStrings.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,16 @@ public void process(Node externs, Node root) {
117117

118118
@Override
119119
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent) {
120-
switch (n.getToken()) {
121-
case TEMPLATELIT:
122-
case TAGGED_TEMPLATELIT:
123-
case TEMPLATELIT_SUB: // technically redundant, since it must be a child of the others
124-
// TODO(bradfordcsmith): Consider replacing long and/or frequently occurring substrings
125-
// within template literals with template substitutions.
126-
return false;
127-
case CALL:
128-
return !ReplaceMessagesConstants.isProtectedMessage(n);
129-
default:
130-
return true;
131-
}
120+
return switch (n.getToken()) {
121+
case TEMPLATELIT,
122+
TAGGED_TEMPLATELIT,
123+
TEMPLATELIT_SUB -> // technically redundant, since it must be a child of the others
124+
// TODO(bradfordcsmith): Consider replacing long and/or frequently occurring substrings
125+
// within template literals with template substitutions.
126+
false;
127+
case CALL -> !ReplaceMessagesConstants.isProtectedMessage(n);
128+
default -> true;
129+
};
132130
}
133131

134132
@Override

src/com/google/javascript/jscomp/CheckConformance.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -607,40 +607,31 @@ private static void removeDuplicates(Requirement.Builder requirement) {
607607
*/
608608
private static @Nullable Rule initRule(AbstractCompiler compiler, Requirement requirement) {
609609
try {
610-
switch (requirement.getType()) {
611-
case CUSTOM:
612-
return new ConformanceRules.CustomRuleProxy(compiler, requirement);
613-
case NO_OP:
614-
return new ConformanceRules.NoOp(compiler, requirement);
615-
case BANNED_CODE_PATTERN:
616-
return new ConformanceRules.BannedCodePattern(compiler, requirement);
617-
case BANNED_DEPENDENCY:
618-
return new ConformanceRules.BannedDependency(compiler, requirement);
619-
case BANNED_DEPENDENCY_REGEX:
620-
return new ConformanceRules.BannedDependencyRegex(compiler, requirement);
621-
case BANNED_ENHANCE:
622-
return new ConformanceRules.BannedEnhance(compiler, requirement);
623-
case BANNED_MODS_REGEX:
624-
return new ConformanceRules.BannedModsRegex(compiler, requirement);
625-
case BANNED_NAME:
626-
case BANNED_NAME_CALL:
627-
return new ConformanceRules.BannedName(compiler, requirement);
628-
case BANNED_PROPERTY:
629-
case BANNED_PROPERTY_READ:
630-
case BANNED_PROPERTY_WRITE:
631-
case BANNED_PROPERTY_NON_CONSTANT_WRITE:
632-
case BANNED_PROPERTY_CALL:
633-
return new ConformanceRules.BannedProperty(compiler, requirement);
634-
case RESTRICTED_NAME_CALL:
635-
return new ConformanceRules.RestrictedNameCall(compiler, requirement);
636-
case RESTRICTED_METHOD_CALL:
637-
return new ConformanceRules.RestrictedMethodCall(compiler, requirement);
638-
case RESTRICTED_PROPERTY_WRITE:
639-
return new ConformanceRules.RestrictedPropertyWrite(compiler, requirement);
640-
case BANNED_STRING_REGEX:
641-
return new ConformanceRules.BannedStringRegex(compiler, requirement);
642-
}
643-
throw new AssertionError();
610+
return switch (requirement.getType()) {
611+
case CUSTOM -> new ConformanceRules.CustomRuleProxy(compiler, requirement);
612+
case NO_OP -> new ConformanceRules.NoOp(compiler, requirement);
613+
case BANNED_CODE_PATTERN -> new ConformanceRules.BannedCodePattern(compiler, requirement);
614+
case BANNED_DEPENDENCY -> new ConformanceRules.BannedDependency(compiler, requirement);
615+
case BANNED_DEPENDENCY_REGEX ->
616+
new ConformanceRules.BannedDependencyRegex(compiler, requirement);
617+
case BANNED_ENHANCE -> new ConformanceRules.BannedEnhance(compiler, requirement);
618+
case BANNED_MODS_REGEX -> new ConformanceRules.BannedModsRegex(compiler, requirement);
619+
case BANNED_NAME,
620+
BANNED_NAME_CALL ->
621+
new ConformanceRules.BannedName(compiler, requirement);
622+
case BANNED_PROPERTY,
623+
BANNED_PROPERTY_READ,
624+
BANNED_PROPERTY_WRITE,
625+
BANNED_PROPERTY_NON_CONSTANT_WRITE,
626+
BANNED_PROPERTY_CALL ->
627+
new ConformanceRules.BannedProperty(compiler, requirement);
628+
case RESTRICTED_NAME_CALL -> new ConformanceRules.RestrictedNameCall(compiler, requirement);
629+
case RESTRICTED_METHOD_CALL ->
630+
new ConformanceRules.RestrictedMethodCall(compiler, requirement);
631+
case RESTRICTED_PROPERTY_WRITE ->
632+
new ConformanceRules.RestrictedPropertyWrite(compiler, requirement);
633+
case BANNED_STRING_REGEX -> new ConformanceRules.BannedStringRegex(compiler, requirement);
634+
};
644635
} catch (InvalidRequirementSpec e) {
645636
reportInvalidRequirement(compiler, requirement, e.getMessage());
646637
return null;

src/com/google/javascript/jscomp/Compiler.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,27 +1998,19 @@ public void maybeSetTracker() {
19981998
}
19991999

20002000
void initializeModuleLoader() {
2001-
ModuleResolverFactory moduleResolverFactory = null;
20022001

2003-
switch (options.getModuleResolutionMode()) {
2004-
case BROWSER:
2005-
moduleResolverFactory = BrowserModuleResolver.FACTORY;
2006-
break;
2007-
case NODE:
2008-
// processJsonInputs requires a module loader to already be defined
2009-
// so we redefine it afterwards with the package.json inputs
2010-
moduleResolverFactory =
2011-
new NodeModuleResolver.Factory(processJsonInputs(chunkGraph.getAllInputs()));
2012-
break;
2013-
case WEBPACK:
2014-
moduleResolverFactory = new WebpackModuleResolver.Factory(inputPathByWebpackId);
2015-
break;
2016-
case BROWSER_WITH_TRANSFORMED_PREFIXES:
2017-
moduleResolverFactory =
2018-
new BrowserWithTransformedPrefixesModuleResolver.Factory(
2019-
options.getBrowserResolverPrefixReplacements());
2020-
break;
2021-
}
2002+
ModuleResolverFactory moduleResolverFactory =
2003+
switch (options.getModuleResolutionMode()) {
2004+
case BROWSER -> BrowserModuleResolver.FACTORY;
2005+
case NODE ->
2006+
// processJsonInputs requires a module loader to already be defined
2007+
// so we redefine it afterwards with the package.json inputs
2008+
new NodeModuleResolver.Factory(processJsonInputs(chunkGraph.getAllInputs()));
2009+
case WEBPACK -> new WebpackModuleResolver.Factory(inputPathByWebpackId);
2010+
case BROWSER_WITH_TRANSFORMED_PREFIXES ->
2011+
new BrowserWithTransformedPrefixesModuleResolver.Factory(
2012+
options.getBrowserResolverPrefixReplacements());
2013+
};
20222014

20232015
this.moduleLoader =
20242016
ModuleLoader.builder()
@@ -3170,22 +3162,17 @@ private List<PassFactory> getOptimizationPassesToRunInCurrentSegment(
31703162
// Create a list of passes based on which segment of optimizations we are running.
31713163
List<PassFactory> optimizationPassList = new ArrayList<>();
31723164

3173-
switch (segmentOfCompilationToRun) {
3174-
case OPTIMIZATIONS:
3175-
optimizationPassList = allOptimizationPassesToRun;
3176-
break;
3177-
case OPTIMIZATIONS_FIRST_HALF:
3178-
optimizationPassList =
3179-
passListUntil(allOptimizationPassesToRun, PassNames.OPTIMIZATIONS_HALFWAY_POINT);
3180-
break;
3181-
case OPTIMIZATIONS_SECOND_HALF:
3182-
optimizationPassList =
3183-
passListAfter(allOptimizationPassesToRun, PassNames.OPTIMIZATIONS_HALFWAY_POINT);
3184-
break;
3185-
default:
3186-
throw new IllegalArgumentException(
3187-
"Unsupported segment of optimizations to run: " + segmentOfCompilationToRun);
3188-
}
3165+
optimizationPassList =
3166+
switch (segmentOfCompilationToRun) {
3167+
case OPTIMIZATIONS -> allOptimizationPassesToRun;
3168+
case OPTIMIZATIONS_FIRST_HALF ->
3169+
passListUntil(allOptimizationPassesToRun, PassNames.OPTIMIZATIONS_HALFWAY_POINT);
3170+
case OPTIMIZATIONS_SECOND_HALF ->
3171+
passListAfter(allOptimizationPassesToRun, PassNames.OPTIMIZATIONS_HALFWAY_POINT);
3172+
default ->
3173+
throw new IllegalArgumentException(
3174+
"Unsupported segment of optimizations to run: " + segmentOfCompilationToRun);
3175+
};
31893176

31903177
return optimizationPassList;
31913178
}

src/com/google/javascript/jscomp/ConformanceRules.java

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -715,25 +715,16 @@ public boolean shouldCheck(Node n) {
715715
throw new InvalidRequirementSpec("missing value");
716716
}
717717

718-
switch (requirement.getType()) {
719-
case BANNED_PROPERTY:
720-
this.requirementPrecondition = RequirementPrecondition.BANNED_PROPERTY;
721-
break;
722-
case BANNED_PROPERTY_READ:
723-
this.requirementPrecondition = RequirementPrecondition.BANNED_PROPERTY_READ;
724-
break;
725-
case BANNED_PROPERTY_WRITE:
726-
this.requirementPrecondition = RequirementPrecondition.BANNED_PROPERTY_WRITE;
727-
break;
728-
case BANNED_PROPERTY_NON_CONSTANT_WRITE:
729-
this.requirementPrecondition = RequirementPrecondition.BANNED_PROPERTY_NON_CONSTANT_WRITE;
730-
break;
731-
case BANNED_PROPERTY_CALL:
732-
this.requirementPrecondition = RequirementPrecondition.BANNED_PROPERTY_CALL;
733-
break;
734-
default:
735-
throw new AssertionError(requirement.getType());
736-
}
718+
this.requirementPrecondition =
719+
switch (requirement.getType()) {
720+
case BANNED_PROPERTY -> RequirementPrecondition.BANNED_PROPERTY;
721+
case BANNED_PROPERTY_READ -> RequirementPrecondition.BANNED_PROPERTY_READ;
722+
case BANNED_PROPERTY_WRITE -> RequirementPrecondition.BANNED_PROPERTY_WRITE;
723+
case BANNED_PROPERTY_NON_CONSTANT_WRITE ->
724+
RequirementPrecondition.BANNED_PROPERTY_NON_CONSTANT_WRITE;
725+
case BANNED_PROPERTY_CALL -> RequirementPrecondition.BANNED_PROPERTY_CALL;
726+
default -> throw new AssertionError(requirement.getType());
727+
};
737728

738729
this.registry = compiler.getTypeRegistry();
739730

@@ -1608,22 +1599,13 @@ public BanNullDeref(AbstractCompiler compiler, Requirement requirement)
16081599

16091600
@Override
16101601
protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
1611-
boolean violation;
16121602

1613-
switch (n.getToken()) {
1614-
case GETPROP:
1615-
case GETELEM:
1616-
case NEW:
1617-
case CALL:
1618-
violation = report(n.getFirstChild());
1619-
break;
1620-
case IN:
1621-
violation = report(n.getLastChild());
1622-
break;
1623-
default:
1624-
violation = false;
1625-
break;
1626-
}
1603+
boolean violation =
1604+
switch (n.getToken()) {
1605+
case GETPROP, GETELEM, NEW, CALL -> report(n.getFirstChild());
1606+
case IN -> report(n.getLastChild());
1607+
default -> false;
1608+
};
16271609

16281610
return violation ? ConformanceResult.VIOLATION : ConformanceResult.CONFORMANCE;
16291611
}

src/com/google/javascript/jscomp/ControlFlowGraph.java

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -141,49 +141,37 @@ public final boolean shouldTraverse(NodeTraversal nodeTraversal, Node n,
141141
*/
142142
public static boolean isEnteringNewCfgNode(Node n) {
143143
Node parent = n.getParent();
144-
switch (parent.getToken()) {
145-
case BLOCK:
146-
case ROOT:
147-
case SCRIPT:
148-
case TRY:
149-
case SWITCH_BODY:
150-
return true;
151-
case FUNCTION:
152-
// A function node represents the start of a function where the name
153-
// bleeds into the local scope and parameters are assigned
154-
// to the formal argument names. The node includes the name of the
155-
// function and the PARAM_LIST since we assume the whole set up process
156-
// is atomic without change in control flow. The next change of
157-
// control is going into the function's body, represented by the second
158-
// child.
159-
return n != parent.getSecondChild();
160-
case WHILE:
161-
case DO:
162-
case IF:
163-
// These control structures are represented by a node that holds the
164-
// condition. Each of them is a branch node based on its condition.
165-
return NodeUtil.getConditionExpression(parent) != n;
166-
167-
case FOR:
168-
// The FOR(;;) node differs from other control structures in that
169-
// it has an initialization and an increment statement. Those
170-
// two statements have corresponding CFG nodes to represent them.
171-
// The FOR node only represents the condition check for each iteration.
172-
// That way the following:
173-
// for(var x = 0; x < 10; x++) { } has a graph that is isomorphic to
174-
// var x = 0; while(x<10) { x++; }
175-
return NodeUtil.getConditionExpression(parent) != n;
176-
case FOR_IN:
177-
// TODO(user): Investigate how we should handle the case where
178-
// we have a very complex expression inside the FOR-IN header.
179-
return n != parent.getFirstChild();
180-
case CASE:
181-
case CATCH:
182-
case WITH:
183-
return n != parent.getFirstChild(); // exclude the condition
184-
default:
185-
return false;
186-
}
144+
return switch (parent.getToken()) {
145+
case BLOCK, ROOT, SCRIPT, TRY, SWITCH_BODY -> true;
146+
case FUNCTION ->
147+
// A function node represents the start of a function where the name
148+
// bleeds into the local scope and parameters are assigned
149+
// to the formal argument names. The node includes the name of the
150+
// function and the PARAM_LIST since we assume the whole set up process
151+
// is atomic without change in control flow. The next change of
152+
// control is going into the function's body, represented by the second
153+
// child.
154+
n != parent.getSecondChild();
155+
case WHILE, DO, IF ->
156+
// These control structures are represented by a node that holds the
157+
// condition. Each of them is a branch node based on its condition.
158+
NodeUtil.getConditionExpression(parent) != n;
159+
case FOR ->
160+
// The FOR(;;) node differs from other control structures in that
161+
// it has an initialization and an increment statement. Those
162+
// two statements have corresponding CFG nodes to represent them.
163+
// The FOR node only represents the condition check for each iteration.
164+
// That way the following:
165+
// for(var x = 0; x < 10; x++) { } has a graph that is isomorphic to
166+
// var x = 0; while(x<10) { x++; }
167+
NodeUtil.getConditionExpression(parent) != n;
168+
case FOR_IN ->
169+
// TODO(user): Investigate how we should handle the case where
170+
// we have a very complex expression inside the FOR-IN header.
171+
n != parent.getFirstChild();
172+
case CASE, CATCH, WITH -> n != parent.getFirstChild(); // exclude the condition
173+
default -> false;
174+
};
187175
}
188176

189177
@Override

src/com/google/javascript/jscomp/DefaultPassConfig.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,20 +2454,13 @@ compiler, PassNames.EXPLOIT_ASSIGN, new ExploitAssigns()))
24542454
.setName(PassNames.EXTRACT_PROTOTYPE_MEMBER_DECLARATIONS)
24552455
.setInternalFactory(
24562456
(compiler) -> {
2457-
Pattern pattern;
2458-
switch (options.extractPrototypeMemberDeclarations) {
2459-
case USE_GLOBAL_TEMP:
2460-
pattern = Pattern.USE_GLOBAL_TEMP;
2461-
break;
2462-
case USE_CHUNK_TEMP:
2463-
pattern = Pattern.USE_CHUNK_TEMP;
2464-
break;
2465-
case USE_IIFE:
2466-
pattern = Pattern.USE_IIFE;
2467-
break;
2468-
default:
2469-
throw new IllegalStateException("unexpected");
2470-
}
2457+
Pattern pattern =
2458+
switch (options.extractPrototypeMemberDeclarations) {
2459+
case USE_GLOBAL_TEMP -> Pattern.USE_GLOBAL_TEMP;
2460+
case USE_CHUNK_TEMP -> Pattern.USE_CHUNK_TEMP;
2461+
case USE_IIFE -> Pattern.USE_IIFE;
2462+
default -> throw new IllegalStateException("unexpected");
2463+
};
24712464

24722465
return new ExtractPrototypeMemberDeclarations(compiler, pattern);
24732466
})

src/com/google/javascript/jscomp/Es6RewriteClass.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,18 @@ enum PropertyKind {
504504
* always be a new node, as this method will insert it into the returned EXPR_RESULT.
505505
*/
506506
final Node getDeclaration(AstFactory astFactory, Node toDeclareOn) {
507-
Node decl = null;
508-
509-
switch (kind()) {
510-
case QUOTED_PROPERTY:
511-
decl = astFactory.createGetElem(toDeclareOn, astFactory.createString(propertyKey()));
512-
break;
513-
case COMPUTED_PROPERTY:
514-
// No need to declare computed properties as they're unaffected by property collapsing
515-
throw new UnsupportedOperationException(this.toString());
516-
case NORMAL_PROPERTY:
517-
decl = astFactory.createGetProp(toDeclareOn, propertyKey(), type(propertyType()));
518-
break;
519-
}
507+
508+
Node decl =
509+
switch (kind()) {
510+
case QUOTED_PROPERTY ->
511+
astFactory.createGetElem(toDeclareOn, astFactory.createString(propertyKey()));
512+
case COMPUTED_PROPERTY ->
513+
// No need to declare computed properties as they're unaffected by property
514+
// collapsing
515+
throw new UnsupportedOperationException(this.toString());
516+
case NORMAL_PROPERTY ->
517+
astFactory.createGetProp(toDeclareOn, propertyKey(), type(propertyType()));
518+
};
520519

521520
decl.setJSDocInfo(jsDocInfo());
522521
decl = astFactory.exprResult(decl);

0 commit comments

Comments
 (0)