Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit fa8d632

Browse files
committed
Remove string switches.
1 parent 6ef9eba commit fa8d632

20 files changed

+384
-499
lines changed

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,16 @@ private static void checkRequirementList(AbstractCompiler compiler, Requirement
192192
}
193193

194194
private static List<String> getRequirementList(Requirement requirement, String field) {
195-
switch (field) {
196-
case "whitelist":
197-
return requirement.getWhitelistList();
198-
case "whitelist_regexp":
199-
return requirement.getWhitelistRegexpList();
200-
case "only_apply_to":
201-
return requirement.getOnlyApplyToList();
202-
case "only_apply_to_regexp":
203-
return requirement.getOnlyApplyToRegexpList();
204-
default:
205-
throw new AssertionError("Unrecognized field: " + field);
206-
}
195+
if (field.equals("whitelist"))
196+
return requirement.getWhitelistList();
197+
else if (field.equals("whitelist_regexp"))
198+
return requirement.getWhitelistRegexpList();
199+
else if (field.equals("only_apply_to"))
200+
return requirement.getOnlyApplyToList();
201+
else if (field.equals("only_apply_to_regexp"))
202+
return requirement.getOnlyApplyToRegexpList();
203+
else
204+
throw new AssertionError("Unrecognized field: " + field);
207205
}
208206

209207
private static Rule initRule(

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

+20-23
Original file line numberDiff line numberDiff line change
@@ -2604,29 +2604,26 @@ Node ensureLibraryInjected(String resourceName, boolean force) {
26042604
node = ast.getFirstChild()) {
26052605
String directive = node.getFirstChild().getString();
26062606
List<String> words = Splitter.on(' ').splitToList(directive);
2607-
switch (words.get(0)) {
2608-
case "use":
2609-
// 'use strict' is ignored (and deleted).
2610-
break;
2611-
case "require":
2612-
// 'require lib1 lib2'; pulls in the named libraries before this one.
2613-
for (String dependency : words.subList(1, words.size())) {
2614-
ensureLibraryInjected(dependency, force);
2615-
}
2616-
break;
2617-
case "declare":
2618-
// 'declare name1 name2'; adds the names to the externs (with no type information).
2619-
// Note that we could simply add the entire externs library, but that leads to
2620-
// potentially-surprising behavior when the externs that are present depend on
2621-
// whether or not a polyfill is used.
2622-
for (String extern : words.subList(1, words.size())) {
2623-
getSynthesizedExternsInputAtEnd()
2624-
.getAstRoot(this)
2625-
.addChildToBack(IR.var(IR.name(extern)));
2626-
}
2627-
break;
2628-
default:
2629-
throw new RuntimeException("Bad directive: " + directive);
2607+
String word = words.get(0);
2608+
if (word.equals("use")) {
2609+
// 'use strict' is ignored (and deleted).
2610+
} else if (word.equals("require")) {
2611+
// 'require lib1 lib2'; pulls in the named libraries before this one.
2612+
for (String dependency : words.subList(1, words.size())) {
2613+
ensureLibraryInjected(dependency, force);
2614+
}
2615+
} else if (word.equals("declare")) {
2616+
// 'declare name1 name2'; adds the names to the externs (with no type information).
2617+
// Note that we could simply add the entire externs library, but that leads to
2618+
// potentially-surprising behavior when the externs that are present depend on
2619+
// whether or not a polyfill is used.
2620+
for (String extern : words.subList(1, words.size())) {
2621+
getSynthesizedExternsInputAtEnd()
2622+
.getAstRoot(this)
2623+
.addChildToBack(IR.var(IR.name(extern)));
2624+
}
2625+
} else {
2626+
throw new RuntimeException("Bad directive: " + directive);
26302627
}
26312628
ast.removeChild(node);
26322629
}

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

+13-20
Original file line numberDiff line numberDiff line change
@@ -2495,26 +2495,19 @@ public static LanguageMode fromString(String value) {
24952495
if (value == null) {
24962496
return null;
24972497
}
2498-
switch (value) {
2499-
case "ECMASCRIPT6_STRICT":
2500-
case "ES6_STRICT":
2501-
return LanguageMode.ECMASCRIPT6_STRICT;
2502-
case "ECMASCRIPT6":
2503-
case "ES6":
2504-
return LanguageMode.ECMASCRIPT6;
2505-
case "ECMASCRIPT5_STRICT":
2506-
case "ES5_STRICT":
2507-
return LanguageMode.ECMASCRIPT5_STRICT;
2508-
case "ECMASCRIPT5":
2509-
case "ES5":
2510-
return LanguageMode.ECMASCRIPT5;
2511-
case "ECMASCRIPT3":
2512-
case "ES3":
2513-
return LanguageMode.ECMASCRIPT3;
2514-
case "ECMASCRIPT6_TYPED":
2515-
case "ES6_TYPED":
2516-
return LanguageMode.ECMASCRIPT6_TYPED;
2517-
}
2498+
if (value.equals("ECMASCRIPT6_STRICT") || value.equals("ES6_STRICT"))
2499+
return LanguageMode.ECMASCRIPT6_STRICT;
2500+
else if (value.equals("ECMASCRIPT6") || value.equals("ES6"))
2501+
return LanguageMode.ECMASCRIPT6;
2502+
else if (value.equals("ECMASCRIPT5_STRICT") || value.equals("ES5_STRICT"))
2503+
return LanguageMode.ECMASCRIPT5_STRICT;
2504+
else if (value.equals("ECMASCRIPT5") || value.equals("ES5"))
2505+
return LanguageMode.ECMASCRIPT5;
2506+
else if (value.equals("ECMASCRIPT3") || value.equals("ES3"))
2507+
return LanguageMode.ECMASCRIPT3;
2508+
else if (value.equals("ECMASCRIPT6_TYPED") || value.equals("ES6_TYPED"))
2509+
return LanguageMode.ECMASCRIPT6_TYPED;
2510+
25182511
return null;
25192512
}
25202513
}

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

+18-29
Original file line numberDiff line numberDiff line change
@@ -1054,35 +1054,24 @@ private void maybeRecordNominalType(
10541054

10551055
private void maybeRecordBuiltinType(
10561056
String name, RawNominalType rawType) {
1057-
switch (name) {
1058-
case "Arguments":
1059-
commonTypes.setArgumentsType(rawType);
1060-
break;
1061-
case "Function":
1062-
commonTypes.setFunctionType(rawType);
1063-
break;
1064-
case "Object":
1065-
commonTypes.setObjectType(rawType);
1066-
break;
1067-
case "Number":
1068-
commonTypes.setNumberInstance(rawType.getInstanceAsJSType());
1069-
break;
1070-
case "String":
1071-
commonTypes.setStringInstance(rawType.getInstanceAsJSType());
1072-
break;
1073-
case "Boolean":
1074-
commonTypes.setBooleanInstance(rawType.getInstanceAsJSType());
1075-
break;
1076-
case "RegExp":
1077-
commonTypes.setRegexpInstance(rawType.getInstanceAsJSType());
1078-
break;
1079-
case "Array":
1080-
commonTypes.setArrayType(rawType);
1081-
break;
1082-
case "IObject":
1083-
commonTypes.setIObjectType(rawType);
1084-
break;
1085-
}
1057+
if (name.equals("Arguments"))
1058+
commonTypes.setArgumentsType(rawType);
1059+
else if (name.equals("Function"))
1060+
commonTypes.setFunctionType(rawType);
1061+
else if (name.equals("Object"))
1062+
commonTypes.setObjectType(rawType);
1063+
else if (name.equals("Number"))
1064+
commonTypes.setNumberInstance(rawType.getInstanceAsJSType());
1065+
else if (name.equals("String"))
1066+
commonTypes.setStringInstance(rawType.getInstanceAsJSType());
1067+
else if (name.equals("Boolean"))
1068+
commonTypes.setBooleanInstance(rawType.getInstanceAsJSType());
1069+
else if (name.equals("RegExp"))
1070+
commonTypes.setRegexpInstance(rawType.getInstanceAsJSType());
1071+
else if (name.equals("Array"))
1072+
commonTypes.setArrayType(rawType);
1073+
else if (name.equals("IObject"))
1074+
commonTypes.setIObjectType(rawType);
10861075
}
10871076

10881077
private void visitAliasedNamespace(Node lhs) {

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,15 @@ public void visit(NodeTraversal t, Node n, Node parent) {
200200
if (!innerKey.isStringKey()) {
201201
continue;
202202
}
203-
switch (innerKey.getString()) {
204-
case "get":
205-
if (matchesJ2clGetKeySignature(classNameString, innerKey)) {
206-
getKey = innerKey;
207-
}
208-
break;
209-
case "set":
210-
if (matchesJ2clSetKeySignature(classNameString, innerKey)) {
211-
setKey = innerKey;
212-
}
213-
break;
203+
String innerKeyString = innerKey.getString();
204+
if (innerKeyString.equals("get")) {
205+
if (matchesJ2clGetKeySignature(classNameString, innerKey)) {
206+
getKey = innerKey;
207+
}
208+
} else if (innerKeyString.equals("set")) {
209+
if (matchesJ2clSetKeySignature(classNameString, innerKey)) {
210+
setKey = innerKey;
211+
}
214212
}
215213
}
216214
if (getKey != null && setKey != null) {

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

+21-23
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,29 @@ public static TypeDeclarationNode convertTypeNodeAST(Node n) {
168168
return convertTypeNodeAST(n.getFirstChild());
169169
case Token.STRING:
170170
String typeName = n.getString();
171-
switch (typeName) {
172-
case "boolean":
173-
return booleanType();
174-
case "number":
175-
return numberType();
176-
case "string":
177-
return stringType();
178-
case "null":
179-
case "undefined":
180-
case "void":
181-
return null;
182-
default:
183-
TypeDeclarationNode root = namedType(typeName);
184-
if (n.getChildCount() > 0 && n.getFirstChild().isBlock()) {
185-
Node block = n.getFirstChild();
186-
if ("Array".equals(typeName)) {
187-
return arrayType(convertTypeNodeAST(block.getFirstChild()));
188-
}
189-
return parameterizedType(root,
190-
Iterables.filter(
191-
Iterables.transform(block.children(), CONVERT_TYPE_NODE),
192-
Predicates.notNull()));
171+
if (typeName.equals("boolean"))
172+
return booleanType();
173+
else if (typeName.equals("number"))
174+
return numberType();
175+
else if (typeName.equals("string"))
176+
return stringType();
177+
else if (typeName.equals("null") || typeName.equals("undefined") || typeName.equals("void"))
178+
return null;
179+
else {
180+
TypeDeclarationNode root = namedType(typeName);
181+
if (n.getChildCount() > 0 && n.getFirstChild().isBlock()) {
182+
Node block = n.getFirstChild();
183+
if ("Array".equals(typeName)) {
184+
return arrayType(convertTypeNodeAST(block.getFirstChild()));
193185
}
194-
return root;
186+
return parameterizedType(root,
187+
Iterables.filter(
188+
Iterables.transform(block.children(), CONVERT_TYPE_NODE),
189+
Predicates.notNull()));
190+
}
191+
return root;
195192
}
193+
196194
case Token.QMARK:
197195
Node child = n.getFirstChild();
198196
return child == null

0 commit comments

Comments
 (0)