Skip to content

Commit

Permalink
Get rid of unnecessary anonymous classes
Browse files Browse the repository at this point in the history
These can generally be replaced with functions or inlined completely.

PiperOrigin-RevId: 601809673
  • Loading branch information
blickly authored and copybara-github committed Jan 26, 2024
1 parent 62745bb commit eec68e4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 54 deletions.
37 changes: 10 additions & 27 deletions src/com/google/javascript/jscomp/ConformanceRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,17 +588,14 @@ static final class BannedName extends AbstractRule {
}

private static final Precondition IS_CANDIDATE_NODE =
new Precondition() {
@Override
public boolean shouldCheck(Node n) {
switch (n.getToken()) {
case GETPROP:
return n.getFirstChild().isQualifiedName();
case NAME:
return !n.getString().isEmpty();
default:
return false;
}
(Node n) -> {
switch (n.getToken()) {
case GETPROP:
return n.getFirstChild().isQualifiedName();
case NAME:
return !n.getString().isEmpty();
default:
return false;
}
};

Expand Down Expand Up @@ -1953,17 +1950,10 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
return ConformanceResult.CONFORMANCE;
}

private static final Precondition IS_SCRIPT_NODE =
new Precondition() {
@Override
public boolean shouldCheck(Node n) {
return n.isScript();
}
};

@Override
public final Precondition getPrecondition() {
return IS_SCRIPT_NODE;
return Node::isScript;
}
}

Expand Down Expand Up @@ -1996,17 +1986,10 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
return ConformanceResult.CONFORMANCE;
}

private static final Precondition IS_SCRIPT_NODE =
new Precondition() {
@Override
public boolean shouldCheck(Node n) {
return n.isScript();
}
};

@Override
public final Precondition getPrecondition() {
return IS_SCRIPT_NODE;
return Node::isScript;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,7 @@ private boolean inLoop(Node n) {
}

private static final Predicate<Node> isLoopOrFunction =
new Predicate<Node>() {
@Override
public boolean apply(Node n) {
return n.isFunction() || NodeUtil.isLoopStructure(n);
}
};
(n) -> n.isFunction() || NodeUtil.isLoopStructure(n);

private static void extractInlineJSDoc(Node srcDeclaration, Node srcName, Node destDeclaration) {
JSDocInfo existingInfo = srcDeclaration.getJSDocInfo();
Expand Down
24 changes: 10 additions & 14 deletions src/com/google/javascript/jscomp/RenameVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,17 @@ void incCount(String name) {
}

/**
* Sorts Assignment objects by their count, breaking ties by their order of
* occurrence in the source to ensure a deterministic total ordering.
* Sorts Assignment objects by their count, breaking ties by their order of occurrence in the
* source to ensure a deterministic total ordering.
*/
private static final Comparator<Assignment> FREQUENCY_COMPARATOR =
new Comparator<Assignment>() {
@Override
public int compare(Assignment a1, Assignment a2) {
if (a1.count != a2.count) {
return a2.count - a1.count;
}
// Break a tie using the order in which the variable first appears in
// the source.
return ORDER_OF_OCCURRENCE_COMPARATOR.compare(a1, a2);
private static int frequencyComparator(Assignment a1, Assignment a2) {
if (a1.count != a2.count) {
return a2.count - a1.count;
}
};
// Break a tie using the order in which the variable first appears in
// the source.
return ORDER_OF_OCCURRENCE_COMPARATOR.compare(a1, a2);
}

/** Sorts Assignment objects by the order the variable name first appears in the source. */
private static final Comparator<Assignment> ORDER_OF_OCCURRENCE_COMPARATOR =
Expand All @@ -325,7 +321,7 @@ public void process(Node externs, Node root) {
reservedNames.addAll(externNames);

// Rename vars, sorted by frequency of occurrence to minimize code size.
SortedSet<Assignment> varsByFrequency = new TreeSet<>(FREQUENCY_COMPARATOR);
SortedSet<Assignment> varsByFrequency = new TreeSet<>(RenameVars::frequencyComparator);
varsByFrequency.addAll(assignments.values());

// First try to reuse names from an earlier compilation.
Expand Down
8 changes: 1 addition & 7 deletions src/com/google/javascript/jscomp/Xid.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ public interface HashFunction {
int hashCode(String value);
}

private static final HashFunction DEFAULT = new HashFunction() {
@Override public int hashCode(String value) {
return value.hashCode();
}
};

public Xid() {
this.hasher = DEFAULT;
this.hasher = String::hashCode;
}

public Xid(HashFunction hasher) {
Expand Down

0 comments on commit eec68e4

Please sign in to comment.