Skip to content

Commit

Permalink
Refactor uses of JsArray inside JsfileParser.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=259074044
  • Loading branch information
tjgq authored and lauraharker committed Jul 22, 2019
1 parent fd85b3e commit 7a91459
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/com/google/javascript/jscomp/gwt/client/JsfileParser.java
Expand Up @@ -20,8 +20,9 @@
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset; import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering; import com.google.common.collect.TreeMultimap;
import com.google.common.collect.TreeMultiset; import com.google.common.collect.TreeMultiset;
import com.google.javascript.jscomp.BasicErrorManager; import com.google.javascript.jscomp.BasicErrorManager;
import com.google.javascript.jscomp.CheckLevel; import com.google.javascript.jscomp.CheckLevel;
Expand All @@ -45,6 +46,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import javax.annotation.Nullable; import javax.annotation.Nullable;
Expand Down Expand Up @@ -106,8 +108,8 @@ static final class FileInfo {
final Multiset<String> requiresCss = TreeMultiset.create(); final Multiset<String> requiresCss = TreeMultiset.create();
final Multiset<String> visibility = TreeMultiset.create(); final Multiset<String> visibility = TreeMultiset.create();


final Set<JsArray<String>> customAnnotations = assoc(); final Multimap<String, String> customAnnotations = TreeMultimap.create();
final Set<JsArray<String>> loadFlags = assoc(); final Multimap<String, String> loadFlags = TreeMultimap.create();


FileInfo(ErrorReporter reporter) { FileInfo(ErrorReporter reporter) {
this.reporter = reporter; this.reporter = reporter;
Expand Down Expand Up @@ -255,7 +257,7 @@ protected void printSummary() {}
parsed.ast.setInputId(new InputId(filename)); parsed.ast.setInputId(new InputId(filename));
String version = parsed.features.version(); String version = parsed.features.version();
if (!version.equals("es3")) { if (!version.equals("es3")) {
info.loadFlags.add(JsArray.of("lang", version)); info.loadFlags.put("lang", version);
} }


for (Comment comment : parsed.comments) { for (Comment comment : parsed.comments) {
Expand All @@ -272,9 +274,9 @@ protected void printSummary() {}
Iterables.getOnlyElement( Iterables.getOnlyElement(
compiler.getModuleMetadataMap().getModulesByPath().values()); compiler.getModuleMetadataMap().getModulesByPath().values());
if (module.isEs6Module()) { if (module.isEs6Module()) {
info.loadFlags.add(JsArray.of("module", "es6")); info.loadFlags.put("module", "es6");
} else if (module.isGoogModule()) { } else if (module.isGoogModule()) {
info.loadFlags.add(JsArray.of("module", "goog")); info.loadFlags.put("module", "goog");
} }
info.goog = module.usesClosure(); info.goog = module.usesClosure();
// If something doesn't have an external dependency on Closure, then it does not have any // If something doesn't have an external dependency on Closure, then it does not have any
Expand Down Expand Up @@ -342,19 +344,16 @@ private static void parseComment(Comment comment, FileInfo info) {
break; break;
case "@enhanceable": case "@enhanceable":
case "@pintomodule": case "@pintomodule":
info.customAnnotations.add( info.customAnnotations.put(annotation.name.substring(1), annotation.value);
JsArray.of(annotation.name.substring(1), annotation.value));
break; break;
case "@enhance": case "@enhance":
if (!annotation.value.isEmpty()) { if (!annotation.value.isEmpty()) {
info.customAnnotations.add( info.customAnnotations.put(annotation.name.substring(1), annotation.value);
JsArray.of(annotation.name.substring(1), annotation.value));
} }
break; break;
default: default:
if (fileOverview) { if (fileOverview) {
info.customAnnotations.add( info.customAnnotations.put(annotation.name.substring(1), annotation.value);
JsArray.of(annotation.name.substring(1), annotation.value));
} }
} }
} }
Expand Down Expand Up @@ -390,17 +389,26 @@ public void report(
boolean fatal, String message, String sourceName, int line, int lineOffset) {} boolean fatal, String message, String sourceName, int line, int lineOffset) {}
}; };


/** Returns an associative multimap. */
private static Set<JsArray<String>> assoc() {
return new TreeSet<>(Ordering.<String>natural().lexicographical().onResultOf(JsArray::asList));
}

/** Sparse object helper class: only adds non-trivial values. */ /** Sparse object helper class: only adds non-trivial values. */
private static class SparseObject { private static class SparseObject {
final JsObject<Object> object = new JsObject<>(); final JsObject<Object> object = new JsObject<>();


SparseObject set(String key, Iterable<?> iterable) { SparseObject set(String key, Iterable<String> iterable) {
JsArray<?> array = JsArray.copyOf(iterable); JsArray<String> array = JsArray.copyOf(iterable);
if (array.getLength() > 0) {
object.set(key, array);
}
return this;
}

SparseObject set(String key, Multimap<String, String> map) {
JsArray<JsArray<String>> array = new JsArray<>();
for (Map.Entry<String, String> entry : map.entries()) {
JsArray<String> pair = new JsArray<>();
pair.push(entry.getKey());
pair.push(entry.getValue());
array.push(pair);
}
if (array.getLength() > 0) { if (array.getLength() > 0) {
object.set(key, array); object.set(key, array);
} }
Expand Down

0 comments on commit 7a91459

Please sign in to comment.