Skip to content

Commit d3323fc

Browse files
blicklycopybara-github
authored andcommitted
Change to expression switch where possible
PiperOrigin-RevId: 842714786
1 parent 8f49ec0 commit d3323fc

File tree

9 files changed

+154
-181
lines changed

9 files changed

+154
-181
lines changed

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

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,20 @@ static DestructuredTarget createTarget(
174174

175175
Builder builder =
176176
new Builder(registry, destructuringChild.getParent(), destructuringPatternType);
177+
Node value;
177178
switch (destructuringChild.getToken()) {
178-
case STRING_KEY:
179+
case STRING_KEY -> {
179180
// const {objectLiteralKey: x} = ...
180181
builder.setObjectPatternKey(destructuringChild);
181-
Node value = destructuringChild.getFirstChild();
182+
value = destructuringChild.getFirstChild();
182183
if (value.isDefaultValue()) {
183184
builder.setNode(value.getFirstChild());
184185
builder.setDefaultValue(value.getSecondChild());
185186
} else {
186187
builder.setNode(value);
187188
}
188-
break;
189-
190-
case COMPUTED_PROP:
189+
}
190+
case COMPUTED_PROP -> {
191191
// const {['objectLiteralKey']: x} = ...
192192
builder.setObjectPatternKey(destructuringChild);
193193
value = destructuringChild.getSecondChild();
@@ -197,32 +197,27 @@ static DestructuredTarget createTarget(
197197
} else {
198198
builder.setNode(value);
199199
}
200-
break;
201-
202-
case OBJECT_PATTERN: // const [{x}] = ...
203-
case ARRAY_PATTERN: // const [[x]] = ...
204-
case NAME: // const [x] = ...
205-
case GETELEM: // [obj[3]] = ...
206-
case GETPROP: // [this.x] = ...
207-
builder.setNode(destructuringChild);
208-
break;
209-
210-
case DEFAULT_VALUE: // const [x = 3] = ...
200+
}
201+
case OBJECT_PATTERN, // const [{x}] = ...
202+
ARRAY_PATTERN, // const [[x]] = ...
203+
NAME, // const [x] = ...
204+
GETELEM, // [obj[3]] = ...
205+
GETPROP -> // [this.x] = ...
206+
builder.setNode(destructuringChild);
207+
case DEFAULT_VALUE -> {
208+
// const [x = 3] = ...
211209
builder.setNode(destructuringChild.getFirstChild());
212210
builder.setDefaultValue(destructuringChild.getSecondChild());
213-
break;
214-
215-
case ITER_REST:
216-
case OBJECT_REST:
211+
}
212+
case ITER_REST, OBJECT_REST -> {
217213
// const [...x] = ...
218214
// const {...x} = ...
219215
builder.setNode(destructuringChild.getFirstChild());
220216
builder.setIsRest(true);
221-
break;
222-
223-
default:
224-
throw new IllegalArgumentException(
225-
"Unexpected child of destructuring pattern " + destructuringChild);
217+
}
218+
default ->
219+
throw new IllegalArgumentException(
220+
"Unexpected child of destructuring pattern " + destructuringChild);
226221
}
227222
return builder.build();
228223
}
@@ -300,21 +295,20 @@ private JSType inferObjectPatternKeyType() {
300295
return registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
301296
}
302297
switch (objectPatternKey.getToken()) {
303-
case STRING_KEY:
298+
case STRING_KEY -> {
304299
JSType propertyType = patternType.findPropertyType(objectPatternKey.getString());
305300
return propertyType != null
306301
? propertyType
307302
: registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
308-
309-
case COMPUTED_PROP:
303+
}
304+
case COMPUTED_PROP -> {
310305
return patternType != null
311306
? patternType
312307
.getTemplateTypeMap()
313308
.getResolvedTemplateType(registry.getObjectElementKey())
314309
: registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
315-
316-
default:
317-
throw new IllegalStateException("Unexpected key " + objectPatternKey);
310+
}
311+
default -> throw new IllegalStateException("Unexpected key " + objectPatternKey);
318312
}
319313
}
320314

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

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,17 @@ private boolean exposeExpression(Node expressionRoot, Node subExpression) {
235235

236236
Node left = expressionParent.getFirstChild();
237237
switch (left.getToken()) {
238-
case ARRAY_PATTERN:
239-
// e.g. backoff from exposing expression `getObj()` in `[getObj().propName] = ...` as
240-
// the RHS must execute first
241-
case OBJECT_PATTERN:
242-
// e.g. backoff from exposing expression`getObj()` in `{a: getObj().propName} = ...`
243-
// as the RHS must execute first
244-
break;
245-
case GETELEM:
246-
case GETPROP:
247-
exposedSomething =
248-
decomposeSubExpressions(left.getFirstChild(), null, state) || exposedSomething;
249-
break;
250-
default:
251-
throw new IllegalStateException(
252-
"Expected a property access or destructuring pattern: " + left.toStringTree());
238+
case ARRAY_PATTERN, // e.g. backoff from exposing expression `getObj()` in
239+
// `[getObj().propName] = ...` as the RHS must execute first
240+
OBJECT_PATTERN -> { // e.g. backoff from exposing expression`getObj()` in `{a:
241+
// getObj().propName} = ...` as the RHS must execute first
242+
}
243+
case GETELEM, GETPROP ->
244+
exposedSomething =
245+
decomposeSubExpressions(left.getFirstChild(), null, state) || exposedSomething;
246+
default ->
247+
throw new IllegalStateException(
248+
"Expected a property access or destructuring pattern: " + left.toStringTree());
253249
}
254250
}
255251
} else if (expressionParent.isCall()
@@ -542,27 +538,27 @@ private Node extractConditional(Node expr, Node injectionPoint, boolean needResu
542538
Node trueExpr = astFactory.createBlock().srcref(expr);
543539
Node falseExpr = astFactory.createBlock().srcref(expr);
544540
switch (expr.getToken()) {
545-
case HOOK:
541+
case HOOK -> {
546542
// a = x?y:z --> if (x) {a=y} else {a=z}
547543
cond = first;
548544
trueExpr.addChildToFront(
549545
astFactory.exprResult(buildResultExpression(second, needResult, tempName)));
550546
falseExpr.addChildToFront(
551547
astFactory.exprResult(buildResultExpression(last, needResult, tempName)));
552-
break;
553-
case AND:
548+
}
549+
case AND -> {
554550
// a = x&&y --> if (a=x) {a=y} else {}
555551
cond = buildResultExpression(first, needResult, tempName);
556552
trueExpr.addChildToFront(
557553
astFactory.exprResult(buildResultExpression(last, needResult, tempName)));
558-
break;
559-
case OR:
554+
}
555+
case OR -> {
560556
// a = x||y --> if (a=x) {} else {a=y}
561557
cond = buildResultExpression(first, needResult, tempName);
562558
falseExpr.addChildToFront(
563559
astFactory.exprResult(buildResultExpression(last, needResult, tempName)));
564-
break;
565-
case COALESCE:
560+
}
561+
case COALESCE -> {
566562
// a = x ?? y --> if ((temp=x)!=null) {a=temp} else {a=y}
567563
String tempNameAssign = getTempValueName();
568564
Node tempVarNodeAssign =
@@ -580,10 +576,10 @@ private Node extractConditional(Node expr, Node injectionPoint, boolean needResu
580576
tempName)));
581577
falseExpr.addChildToFront(
582578
astFactory.exprResult(buildResultExpression(last, needResult, tempName)));
583-
break;
584-
default:
585-
// With a valid tree we should never get here.
586-
throw new IllegalStateException("Unexpected expression: " + expr);
579+
}
580+
default ->
581+
// With a valid tree we should never get here.
582+
throw new IllegalStateException("Unexpected expression: " + expr);
587583
}
588584

589585
Node ifNode;
@@ -1180,15 +1176,17 @@ private boolean isSafeAssign(Node n, boolean seenSideEffects) {
11801176
if (n.isAssign()) {
11811177
Node lhs = n.getFirstChild();
11821178
switch (lhs.getToken()) {
1183-
case NAME:
1179+
case NAME -> {
11841180
return true;
1185-
case GETPROP:
1181+
}
1182+
case GETPROP -> {
11861183
return !isExpressionTreeUnsafe(lhs.getFirstChild(), seenSideEffects);
1187-
case GETELEM:
1184+
}
1185+
case GETELEM -> {
11881186
return !isExpressionTreeUnsafe(lhs.getFirstChild(), seenSideEffects)
11891187
&& !isExpressionTreeUnsafe(lhs.getLastChild(), seenSideEffects);
1190-
default:
1191-
break;
1188+
}
1189+
default -> {}
11921190
}
11931191
}
11941192
return false;

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,29 @@ public void visit(Node node) {
132132
Node parent = node.getParent();
133133
if (parent != null) {
134134
switch (parent.getToken()) {
135-
case BANG:
136-
case QMARK:
137-
case THIS: // The names inside function(this:Foo) and
138-
case NEW: // function(new:Bar) are already non-null.
139-
case TYPEOF: // Names after 'typeof' don't have nullability.
135+
case BANG,
136+
QMARK,
137+
THIS, // The names inside function(this:Foo) and
138+
NEW, // function(new:Bar) are already non-null.
139+
TYPEOF -> { // Names after 'typeof' don't have nullability.
140140
return;
141-
case PIPE:
142-
{ // Inside a union
143-
Node gp = parent.getParent();
144-
if (gp != null && gp.getToken() == Token.QMARK) {
145-
return; // Inside an explicitly nullable union
146-
}
147-
for (Node child = parent.getFirstChild();
148-
child != null;
149-
child = child.getNext()) {
150-
if ((child.isStringLit() && child.getString().equals("null"))
151-
|| child.getToken() == Token.QMARK) {
152-
return; // Inside a union that contains null or nullable type
153-
}
141+
}
142+
case PIPE -> {
143+
// Inside a union
144+
Node gp = parent.getParent();
145+
if (gp != null && gp.getToken() == Token.QMARK) {
146+
return; // Inside an explicitly nullable union
147+
}
148+
for (Node child = parent.getFirstChild();
149+
child != null;
150+
child = child.getNext()) {
151+
if ((child.isStringLit() && child.getString().equals("null"))
152+
|| child.getToken() == Token.QMARK) {
153+
return; // Inside a union that contains null or nullable type
154154
}
155-
break;
156155
}
157-
default:
158-
break;
156+
}
157+
default -> {}
159158
}
160159
}
161160
String typeName = node.getString();

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
8585
@Override
8686
public void visit(NodeTraversal t, Node n, Node parent) {
8787
switch (n.getToken()) {
88-
case ASSIGN:
88+
case ASSIGN -> {
8989
// Find whether this script contains the `$jscomp.createTemplateTagFirstArgWithRaw =
9090
// function(..) {..}` node. If yes, update the templateLitInsertionPoint.
9191
Node lhs = n.getFirstChild();
@@ -98,25 +98,21 @@ public void visit(NodeTraversal t, Node n, Node parent) {
9898
templateLitInsertionPoint = n.getParent().getNext();
9999
}
100100
}
101-
break;
102-
case OBJECTLIT:
103-
visitObject(n);
104-
break;
105-
case MEMBER_FUNCTION_DEF:
101+
}
102+
case OBJECTLIT -> visitObject(n);
103+
case MEMBER_FUNCTION_DEF -> {
106104
if (parent.isObjectLit()) {
107105
visitMemberFunctionDefInObjectLit(n);
108106
}
109-
break;
110-
case TAGGED_TEMPLATELIT:
111-
templateLiteralConverter.visitTaggedTemplateLiteral(t, n, templateLitInsertionPoint);
112-
break;
113-
case TEMPLATELIT:
107+
}
108+
case TAGGED_TEMPLATELIT ->
109+
templateLiteralConverter.visitTaggedTemplateLiteral(t, n, templateLitInsertionPoint);
110+
case TEMPLATELIT -> {
114111
if (!parent.isTaggedTemplateLit()) {
115112
templateLiteralConverter.visitTemplateLiteral(t, n);
116113
}
117-
break;
118-
default:
119-
break;
114+
}
115+
default -> {}
120116
}
121117
}
122118

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public void process(Node externs, Node root) {
151151
public void visit(NodeTraversal t, Node n, Node parent) {
152152
// TODO(johnlenz): Determine if it is necessary to support ".call" or ".apply".
153153
switch (n.getToken()) {
154-
case NEW: // e.g. new Error('msg');
155-
case CALL: // e.g. Error('msg');
156-
case TAGGED_TEMPLATELIT: // e.g. Error`msg` - not supported!
154+
case NEW, // e.g. new Error('msg');
155+
CALL, // e.g. Error('msg');
156+
TAGGED_TEMPLATELIT -> { // e.g. Error`msg` - not supported!
157157
Node calledFn = n.getFirstChild();
158158

159159
// Look for calls to static functions.
@@ -165,9 +165,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
165165
return;
166166
}
167167
}
168-
break;
169-
default:
170-
break;
168+
}
169+
default -> {}
171170
}
172171
}
173172

@@ -234,14 +233,12 @@ private Node replaceExpression(NodeTraversal t, Node expr) {
234233
String key = null;
235234
String replacementString;
236235
switch (expr.getToken()) {
237-
case STRINGLIT:
236+
case STRINGLIT -> {
238237
key = expr.getString();
239238
replacementString = getReplacement(key);
240239
replacement = IR.string(replacementString);
241-
break;
242-
case TEMPLATELIT:
243-
case ADD:
244-
case NAME:
240+
}
241+
case TEMPLATELIT, ADD, NAME -> {
245242
StringBuilder keyBuilder = new StringBuilder();
246243
Node keyNode = IR.string("");
247244
replacement = buildReplacement(t, expr, keyNode, keyBuilder);
@@ -253,11 +250,12 @@ private Node replaceExpression(NodeTraversal t, Node expr) {
253250
}
254251
replacementString = getReplacement(key);
255252
keyNode.setString(replacementString);
256-
break;
257-
default:
253+
}
254+
default -> {
258255
// This may be a function call or a variable reference. We don't
259256
// replace these.
260257
return expr;
258+
}
261259
}
262260

263261
checkNotNull(key);

0 commit comments

Comments
 (0)