diff --git a/src/com/google/javascript/jscomp/AbstractCompiler.java b/src/com/google/javascript/jscomp/AbstractCompiler.java index f7edc889670..3591285c225 100644 --- a/src/com/google/javascript/jscomp/AbstractCompiler.java +++ b/src/com/google/javascript/jscomp/AbstractCompiler.java @@ -22,7 +22,6 @@ import com.google.common.base.Optional; import com.google.common.base.Splitter; import com.google.common.base.Strings; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -52,6 +51,7 @@ import java.util.EnumSet; import java.util.List; import java.util.Set; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; /** @@ -81,7 +81,7 @@ void afterPass(String passName) {} // Many of them are just accessors that should be passed to the // CompilerPass's constructor. - abstract java.util.function.Supplier getTypedAstDeserializer(SourceFile file); + abstract Supplier getTypedAstDeserializer(SourceFile file); /** Looks up an input (possibly an externs input) by input id. May return null. */ @Override diff --git a/src/com/google/javascript/jscomp/ExpressionDecomposer.java b/src/com/google/javascript/jscomp/ExpressionDecomposer.java index 4c1a357c24a..e6aac892200 100644 --- a/src/com/google/javascript/jscomp/ExpressionDecomposer.java +++ b/src/com/google/javascript/jscomp/ExpressionDecomposer.java @@ -23,7 +23,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.MakeDeclaredNamesUnique.ContextualRenamer; import com.google.javascript.jscomp.colors.StandardColors; @@ -35,6 +34,7 @@ import com.google.javascript.rhino.jstype.JSTypeNative; import java.util.ArrayDeque; import java.util.EnumSet; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; /** diff --git a/src/com/google/javascript/jscomp/FunctionArgumentInjector.java b/src/com/google/javascript/jscomp/FunctionArgumentInjector.java index 31e1f2e51eb..48408567eba 100644 --- a/src/com/google/javascript/jscomp/FunctionArgumentInjector.java +++ b/src/com/google/javascript/jscomp/FunctionArgumentInjector.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkState; import com.google.common.base.Predicate; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.NodeUtil.Visitor; @@ -30,10 +29,11 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; /** - * A nifty set of functions to deal with the issues of replacing function - * parameters with a set of call argument expressions. + * A nifty set of functions to deal with the issues of replacing function parameters with a set of + * call argument expressions. */ class FunctionArgumentInjector { @@ -174,11 +174,8 @@ ImmutableMap getFunctionCallParameterMap( return argMap.buildOrThrow(); } - /** - * Parameter names will be name unique when at a later time. - */ - private static String getUniqueAnonymousParameterName( - Supplier safeNameIdSupplier) { + /** Parameter names will be name unique when at a later time. */ + private static String getUniqueAnonymousParameterName(Supplier safeNameIdSupplier) { return "JSCompiler_inline_anon_param_" + safeNameIdSupplier.get(); } @@ -202,16 +199,12 @@ Set findModifiedParameters(Node fnNode) { } /** - * Check for uses of the named value that imply a pass-by-value - * parameter is expected. This is used to prevent cases like: + * Check for uses of the named value that imply a pass-by-value parameter is expected. This is + * used to prevent cases like: * - * function (x) { - * x=2; - * return x; - * } + *

function (x) { x=2; return x; } * - * We don't want "undefined" to be substituted for "x", and get - * undefined=2 + *

We don't want "undefined" to be substituted for "x", and get undefined=2 * * @param n The node in question. * @param names The set of names to check. @@ -242,12 +235,11 @@ private static Set findModifiedParameters( } /** - * This is similar to NodeUtil.isLValue except that object properties and - * array member modification aren't important ("o" in "o.a = 2" is still "o" - * after assignment, where in as "o = x", "o" is now "x"). + * This is similar to NodeUtil.isLValue except that object properties and array member + * modification aren't important ("o" in "o.a = 2" is still "o" after assignment, where in as "o = + * x", "o" is now "x"). * - * This also looks for the redefinition of a name. - * function (x) {var x;} + *

This also looks for the redefinition of a name. function (x) {var x;} * * @param n The NAME node in question. */ @@ -282,14 +274,15 @@ void maybeAddTempsForCallArguments( int argCount = argMap.size(); // We limit the "trivial" bodies to those where there is a single expression or // return, the expression is - boolean isTrivialBody = (!block.hasChildren() - || (block.hasOneChild() && !bodyMayHaveConditionalCode(block.getLastChild()))); - boolean hasMinimalParameters = NodeUtil.isUndefined(argMap.get(THIS_MARKER)) - && argCount <= 2; // this + one parameter + boolean isTrivialBody = + (!block.hasChildren() + || (block.hasOneChild() && !bodyMayHaveConditionalCode(block.getLastChild()))); + boolean hasMinimalParameters = + NodeUtil.isUndefined(argMap.get(THIS_MARKER)) && argCount <= 2; // this + one parameter // Get the list of parameters that may need temporaries due to side-effects. - ImmutableSet namesAfterSideEffects = findParametersReferencedAfterSideEffect( - argMap.keySet(), block); + ImmutableSet namesAfterSideEffects = + findParametersReferencedAfterSideEffect(argMap.keySet(), block); // Check for arguments that are evaluated more than once. for (Map.Entry entry : argMap.entrySet()) { @@ -304,7 +297,8 @@ void maybeAddTempsForCallArguments( boolean argSideEffects = compiler.getAstAnalyzer().mayHaveSideEffects(cArg); if (!argSideEffects && references == 0) { safe = true; - } else if (isTrivialBody && hasMinimalParameters + } else if (isTrivialBody + && hasMinimalParameters && references == 1 && !(NodeUtil.canBeSideEffected(cArg) && namesAfterSideEffects.contains(argName))) { // For functions with a trivial body, and where the parameter evaluation order @@ -323,7 +317,7 @@ void maybeAddTempsForCallArguments( // x( [] ); // // The parameter in the call to foo should not become "[]". - safe = false; + safe = false; } else if (argSideEffects) { // Even if there are no references, we still need to evaluate the // expression if it has side-effects. @@ -409,12 +403,9 @@ private ImmutableSet findParametersReferencedAfterSideEffect( Set locals = new LinkedHashSet<>(parameters); gatherLocalNames(root, locals); - ReferencedAfterSideEffect collector = new ReferencedAfterSideEffect( - parameters, ImmutableSet.copyOf(locals)); - NodeUtil.visitPostOrder( - root, - collector, - collector); + ReferencedAfterSideEffect collector = + new ReferencedAfterSideEffect(parameters, ImmutableSet.copyOf(locals)); + NodeUtil.visitPostOrder(root, collector, collector); return collector.getResults(); } @@ -513,9 +504,7 @@ private boolean hasNonLocalSideEffect(Node n) { Token type = n.getToken(); // Note: Only care about changes to non-local names, specifically // ignore VAR declaration assignments. - if (NodeUtil.isAssignmentOp(n) - || type == Token.INC - || type == Token.DEC) { + if (NodeUtil.isAssignmentOp(n) || type == Token.INC || type == Token.DEC) { Node lhs = n.getFirstChild(); // Ignore changes to local names. if (!isLocalName(lhs)) { @@ -544,9 +533,7 @@ private boolean isLocalName(Node node) { } } - /** - * Gather any names declared in the local scope. - */ + /** Gather any names declared in the local scope. */ private static void gatherLocalNames(Node n, Set names) { if (n.isFunction()) { if (NodeUtil.isFunctionDeclaration(n)) { @@ -572,15 +559,13 @@ private static void gatherLocalNames(Node n, Set names) { } } - /** - * Get a set of function parameter names. - */ + /** Get a set of function parameter names. */ private static ImmutableSet getFunctionParameterSet(Node fnNode) { ImmutableSet.Builder builder = ImmutableSet.builder(); for (Node n = NodeUtil.getFunctionParameters(fnNode).getFirstChild(); n != null; n = n.getNext()) { - if (n.isRest()){ + if (n.isRest()) { builder.add(REST_MARKER); } else if (n.isDefaultValue() || n.isObjectPattern() || n.isArrayPattern()) { throw new IllegalStateException("Not supported: " + n); @@ -590,5 +575,4 @@ private static ImmutableSet getFunctionParameterSet(Node fnNode) { } return builder.build(); } - } diff --git a/src/com/google/javascript/jscomp/FunctionInjector.java b/src/com/google/javascript/jscomp/FunctionInjector.java index 4b276604d45..d93cd465f39 100644 --- a/src/com/google/javascript/jscomp/FunctionInjector.java +++ b/src/com/google/javascript/jscomp/FunctionInjector.java @@ -22,7 +22,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -34,6 +33,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Set; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; /** @@ -44,6 +44,7 @@ class FunctionInjector { /** Sentinel value indicating that the key contains no functions. */ private static final Node NO_FUNCTIONS = new Node(Token.FUNCTION); + /** Sentinel value indicating that the key contains multiple distinct functions. */ private static final Node MULTIPLE_FUNCTIONS = new Node(Token.FUNCTION); @@ -960,7 +961,9 @@ boolean inliningLowersCost( isRemovable); } - /** @return Whether inlining will lower cost. */ + /** + * @return Whether inlining will lower cost. + */ private static boolean doesLowerCost( Node fnNode, int callCost, @@ -1014,7 +1017,9 @@ private static int estimateCallCost(Node fnNode, boolean referencesThis) { return callCost; } - /** @return The difference between the function definition cost and inline cost. */ + /** + * @return The difference between the function definition cost and inline cost. + */ private static int inlineCostDelta(Node fnNode, Set namesToAlias, InliningMode mode) { // The part of the function that is never inlined: // "function xx(xx,xx){}" (15 + (param count * 3) -1; diff --git a/src/com/google/javascript/jscomp/FunctionToBlockMutator.java b/src/com/google/javascript/jscomp/FunctionToBlockMutator.java index d4914ef07a4..4666548a0b3 100644 --- a/src/com/google/javascript/jscomp/FunctionToBlockMutator.java +++ b/src/com/google/javascript/jscomp/FunctionToBlockMutator.java @@ -21,7 +21,6 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.javascript.jscomp.FunctionArgumentInjector.THIS_MARKER; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.javascript.jscomp.MakeDeclaredNamesUnique.InlineRenamer; @@ -34,20 +33,17 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; -/** - * A class to transform the body of a function into a generic block suitable - * for inlining. - */ +/** A class to transform the body of a function into a generic block suitable for inlining. */ class FunctionToBlockMutator { private final AbstractCompiler compiler; private final Supplier safeNameIdSupplier; private final FunctionArgumentInjector functionArgumentInjector; - FunctionToBlockMutator( - AbstractCompiler compiler, Supplier safeNameIdSupplier) { + FunctionToBlockMutator(AbstractCompiler compiler, Supplier safeNameIdSupplier) { this.compiler = checkNotNull(compiler); this.safeNameIdSupplier = safeNameIdSupplier; this.functionArgumentInjector = new FunctionArgumentInjector(compiler.getAstAnalyzer()); @@ -59,13 +55,18 @@ class FunctionToBlockMutator { * @param callNode The call node that will be replaced. * @param resultName Function results should be assigned to this name. * @param needsDefaultResult Whether the result value must be set. - * @param isCallInLoop Whether the function body must be prepared to be - * injected into the body of a loop. - * @return A clone of the function body mutated to be suitable for injection - * as a statement into another code block. + * @param isCallInLoop Whether the function body must be prepared to be injected into the body of + * a loop. + * @return A clone of the function body mutated to be suitable for injection as a statement into + * another code block. */ - Node mutate(String fnName, Node fnNode, Node callNode, - String resultName, boolean needsDefaultResult, boolean isCallInLoop) { + Node mutate( + String fnName, + Node fnNode, + Node callNode, + String resultName, + boolean needsDefaultResult, + boolean isCallInLoop) { return mutateInternal( fnName, fnNode, @@ -174,8 +175,7 @@ private Node mutateInternal( } String labelName = getLabelNameForFunction(fnName); - Node injectableBlock = replaceReturns( - newBlock, resultName, labelName, needsDefaultResult); + Node injectableBlock = replaceReturns(newBlock, resultName, labelName, needsDefaultResult); checkState(injectableBlock != null); return injectableBlock; @@ -225,10 +225,7 @@ private Node mutateInternal( return null; } - /** - * For all VAR node with uninitialized declarations, set - * the values to be "undefined". - */ + /** For all VAR node with uninitialized declarations, set the values to be "undefined". */ private static void fixUninitializedVarDeclarations(Node n, Node containingBlock) { // Inner loop structure must already have logic to initialize its // variables. In particular FOR-IN structures must not be modified. @@ -254,6 +251,7 @@ private static void fixUninitializedVarDeclarations(Node n, Node containingBlock /** * Fix-up all local names to be unique for this subtree. + * * @param fnNode A mutable instance of the function to be inlined. */ private void makeLocalNamesUnique(Node fnNode, boolean isCallInLoop) { @@ -289,32 +287,27 @@ static class LabelNameSupplier implements Supplier { @Override public String get() { - return "JSCompiler_inline_label_" + idSupplier.get(); + return "JSCompiler_inline_label_" + idSupplier.get(); } } - /** - * Create a unique label name. - */ + /** Create a unique label name. */ private String getLabelNameForFunction(String fnName) { String name = (isNullOrEmpty(fnName)) ? "anon" : fnName; return "JSCompiler_inline_label_" + name + "_" + safeNameIdSupplier.get(); } - /** - * Create a unique "this" name. - */ + /** Create a unique "this" name. */ private String getUniqueThisName() { return "JSCompiler_inline_this_" + safeNameIdSupplier.get(); } /** - * Inlines the arguments within the node tree using the given argument map, - * replaces "unsafe" names with local aliases. + * Inlines the arguments within the node tree using the given argument map, replaces "unsafe" + * names with local aliases. * - * The aliases for unsafe require new VAR declarations, so this function - * can not be used in for direct CALL node replacement as VAR nodes can not be - * created there. + *

The aliases for unsafe require new VAR declarations, so this function can not be used in for + * direct CALL node replacement as VAR nodes can not be created there. * * @return The node or its replacement. */ @@ -383,37 +376,24 @@ private Node aliasAndInlineArguments( } /** - * Convert returns to assignments and breaks, as needed. - * For example, with a labelName of 'foo': - * { - * return a; - * } - * becomes: - * foo: { - * a; - * break foo; - * } - * or - * foo: { - * resultName = a; - * break foo; - * } + * Convert returns to assignments and breaks, as needed. For example, with a labelName of 'foo': { + * return a; } becomes: foo: { a; break foo; } or foo: { resultName = a; break foo; } * * @param resultMustBeSet Whether the result must always be set to a value. - * @return The node containing the transformed block, this may be different - * than the passed in node 'block'. + * @return The node containing the transformed block, this may be different than the passed in + * node 'block'. */ private static Node replaceReturns( - Node block, String resultName, String labelName, - boolean resultMustBeSet) { + Node block, String resultName, String labelName, boolean resultMustBeSet) { checkNotNull(block); checkNotNull(labelName); Node root = block; boolean hasReturnAtExit = false; - int returnCount = NodeUtil.getNodeTypeReferenceCount( - block, Token.RETURN, new NodeUtil.MatchShallowStatement()); + int returnCount = + NodeUtil.getNodeTypeReferenceCount( + block, Token.RETURN, new NodeUtil.MatchShallowStatement()); if (returnCount > 0) { hasReturnAtExit = hasReturnAtExit(block); // TODO(johnlenz): Simpler not to special case this, @@ -436,7 +416,6 @@ private static Node replaceReturns( Node newRoot = IR.block().srcref(block); newRoot.addChildToBack(label); - // The label is now the root. root = newRoot; } @@ -455,10 +434,7 @@ private static Node replaceReturns( * Functions following here are general node transformation functions **********************************************************************/ - /** - * Example: - * a = (void) 0; - */ + /** Example: a = (void) 0; */ private static void addDummyAssignment(Node node, String resultName) { checkArgument(node.isBlock()); @@ -488,10 +464,7 @@ private static void convertLastReturnToStatement(Node block, String resultName) } } - /** - * Create a valid statement Node containing an assignment to name of the - * given expression. - */ + /** Create a valid statement Node containing an assignment to name of the given expression. */ private static Node createAssignStatementNode(String name, Node expression) { // Create 'name = result-expression;' statement. // EXPR (ASSIGN (NAME, EXPRESSION)) @@ -501,16 +474,11 @@ private static Node createAssignStatementNode(String name, Node expression) { } /** - * Replace the 'return' statement with its child expression. - * If the result is needed (resultName != null): - * "return foo()" becomes "resultName = foo()" - * "return" becomes "resultName = void 0". - * Otherwise: - * "return foo()" becomes "foo()" - * "return", null is returned. + * Replace the 'return' statement with its child expression. If the result is needed (resultName + * != null): "return foo()" becomes "resultName = foo()" "return" becomes "resultName = void 0". + * Otherwise: "return foo()" becomes "foo()" "return", null is returned. */ - private static Node getReplacementReturnStatement( - Node node, String resultName) { + private static Node getReplacementReturnStatement(Node node, String resultName) { Node resultNode = null; Node retVal = null; @@ -554,8 +522,7 @@ private static boolean hasReturnAtExit(Node block) { private static Node replaceReturnWithBreak( Node current, @Nullable Node parent, String resultName, String labelName) { - if (current.isFunction() - || current.isExprResult()) { + if (current.isFunction() || current.isExprResult()) { // Don't recurse into functions definitions, and expressions can't // contain RETURN nodes. return current; diff --git a/src/com/google/javascript/jscomp/InlineFunctions.java b/src/com/google/javascript/jscomp/InlineFunctions.java index 721ce2c0e95..4a78f2b6f96 100644 --- a/src/com/google/javascript/jscomp/InlineFunctions.java +++ b/src/com/google/javascript/jscomp/InlineFunctions.java @@ -22,7 +22,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Predicate; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.CompilerOptions.Reach; @@ -40,6 +39,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; /** @@ -392,7 +392,9 @@ private boolean isCandidateFunction(Function fn) { return injector.doesFunctionMeetMinimumRequirements(fnName, fnNode); } - /** @see CallVisitor */ + /** + * @see CallVisitor + */ private interface CallVisitorCallback { public void visitCallSite(NodeTraversal t, Node callNode, FunctionState functionState); } @@ -451,7 +453,9 @@ public void visit(NodeTraversal t, Node n, Node parent) { } } - /** @return Whether the name is used in a way that might be a candidate for inlining. */ + /** + * @return Whether the name is used in a way that might be a candidate for inlining. + */ static boolean isCandidateUsage(Node name) { Node parent = name.getParent(); checkState(name.isName()); @@ -689,7 +693,9 @@ private boolean minimizeCost(FunctionState functionState) { return true; } - /** @return Whether inlining the function reduces code size. */ + /** + * @return Whether inlining the function reduces code size. + */ private boolean inliningLowersCost(FunctionState functionState) { return injector.inliningLowersCost( functionState.getModule(), @@ -731,7 +737,9 @@ private static boolean hasNonInlinableParam(Node node) { return NodeUtil.has(node, pred, alwaysTrue()); } - /** @see #resolveInlineConflicts */ + /** + * @see #resolveInlineConflicts + */ private void resolveInlineConflictsForFunction(FunctionState functionState) { // Functions that aren't referenced don't cause conflicts. if (!functionState.hasReferences() || !functionState.canInline()) { @@ -767,7 +775,9 @@ private Set findCalledFunctions(Node node) { return changed; } - /** @see #findCalledFunctions(Node) */ + /** + * @see #findCalledFunctions(Node) + */ private static void findCalledFunctions(Node node, Set changed) { checkArgument(changed != null); // For each referenced function, add a new reference diff --git a/src/com/google/javascript/jscomp/InlineObjectLiterals.java b/src/com/google/javascript/jscomp/InlineObjectLiterals.java index 99865de7726..1e4de763156 100644 --- a/src/com/google/javascript/jscomp/InlineObjectLiterals.java +++ b/src/com/google/javascript/jscomp/InlineObjectLiterals.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkState; -import com.google.common.base.Supplier; import com.google.common.collect.Lists; import com.google.javascript.jscomp.ReferenceCollector.Behavior; import com.google.javascript.rhino.IR; @@ -33,6 +32,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; /** * Using the infrastructure provided by {@link ReferenceCollector}, identify variables that are only diff --git a/src/com/google/javascript/jscomp/J2clPass.java b/src/com/google/javascript/jscomp/J2clPass.java index f3e6d2f4303..57b5baff442 100644 --- a/src/com/google/javascript/jscomp/J2clPass.java +++ b/src/com/google/javascript/jscomp/J2clPass.java @@ -15,7 +15,6 @@ */ package com.google.javascript.jscomp; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.FunctionInjector.InliningMode; import com.google.javascript.jscomp.FunctionInjector.Reference; @@ -24,6 +23,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; /** A normalization pass to inline some J2CL calls to enable other optimizations. */ public class J2clPass implements CompilerPass { diff --git a/src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java b/src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java index e47d57ea936..c89d89cd824 100644 --- a/src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java +++ b/src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import com.google.common.base.Supplier; import com.google.common.collect.HashMultiset; import com.google.common.collect.ImmutableList; import com.google.common.collect.Multiset; @@ -35,6 +34,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import org.jspecify.nullness.Nullable; /** diff --git a/src/com/google/javascript/jscomp/PolymerClassDefinition.java b/src/com/google/javascript/jscomp/PolymerClassDefinition.java index 8c00ba3a460..c9e40596257 100644 --- a/src/com/google/javascript/jscomp/PolymerClassDefinition.java +++ b/src/com/google/javascript/jscomp/PolymerClassDefinition.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkState; import com.google.common.base.CaseFormat; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.javascript.jscomp.PolymerBehaviorExtractor.BehaviorDefinition; import com.google.javascript.jscomp.PolymerPass.MemberDefinition; @@ -35,6 +34,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import java.util.stream.Collectors; import org.jspecify.nullness.Nullable; diff --git a/src/com/google/javascript/jscomp/RenameLabels.java b/src/com/google/javascript/jscomp/RenameLabels.java index 794f157b7f6..fc23f6c1d2a 100644 --- a/src/com/google/javascript/jscomp/RenameLabels.java +++ b/src/com/google/javascript/jscomp/RenameLabels.java @@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkState; -import com.google.common.base.Supplier; import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; import com.google.javascript.rhino.Node; import java.util.ArrayDeque; @@ -27,21 +26,19 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.function.Supplier; /** - * RenameLabels renames all the labels so that they have short names, to reduce - * code size and also to obfuscate the code. + * RenameLabels renames all the labels so that they have short names, to reduce code size and also + * to obfuscate the code. * - * Label names have a unique namespace, so variable or function names clashes - * are not a concern, but keywords clashes are. + *

Label names have a unique namespace, so variable or function names clashes are not a concern, + * but keywords clashes are. * - * Additionally, labels names are only within the statements include in the - * label and do not cross function boundaries. This means that it is possible to - * create one label name that is used for labels at any given depth of label - * nesting. Typically, the name "a" will be used for all top-level labels, "b" - * for the next nested label, and so on. For example: - * - * + *

Additionally, labels names are only within the statements include in the label and do not + * cross function boundaries. This means that it is possible to create one label name that is used + * for labels at any given depth of label nesting. Typically, the name "a" will be used for all + * top-level labels, "b" for the next nested label, and so on. For example: * function bar() { * a: { * b: { @@ -53,16 +50,12 @@ * b: break a; * } * } - * - * - * The general processes is as follows: process() is the entry point for the - * CompilerPass, and from there a standard "ScopedCallback" traversal is done, - * where "shouldTraverse" is called when descending the tree, and the "visit" is - * called in a depth first manner. The name for the label is selected during the - * decent in "shouldTraverse", and the references to the label name are renamed - * as they are encountered during the "visit". This means that if the label is - * unreferenced, it is known when the label node is visited, and, if so, can be - * safely removed. + * The general processes is as follows: process() is the entry point for the CompilerPass, + * and from there a standard "ScopedCallback" traversal is done, where "shouldTraverse" is called + * when descending the tree, and the "visit" is called in a depth first manner. The name for the + * label is selected during the decent in "shouldTraverse", and the references to the label name are + * renamed as they are encountered during the "visit". This means that if the label is unreferenced, + * it is known when the label node is visited, and, if so, can be safely removed. */ final class RenameLabels implements CompilerPass { private final AbstractCompiler compiler; @@ -99,9 +92,7 @@ public String get() { } } - /** - * Iterate through the nodes, renaming all the labels. - */ + /** Iterate through the nodes, renaming all the labels. */ class ProcessLabels implements ScopedCallback { private final boolean markChanges; @@ -120,7 +111,6 @@ class ProcessLabels implements ScopedCallback { // the second "b", etc. final ArrayList names = new ArrayList<>(); - @Override public void enterScope(NodeTraversal nodeTraversal) { // Start a new namespace for label names. @@ -137,14 +127,13 @@ public void exitScope(NodeTraversal nodeTraversal) { } /** - * shouldTraverse is call when descending into the Node tree, so it is used - * here to build the context for label renames. + * shouldTraverse is call when descending into the Node tree, so it is used here to build the + * context for label renames. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override - public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, - Node parent) { + public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, Node parent) { if (node.isLabel()) { // Determine the new name for this label. LabelNamespace current = namespaceStack.peek(); @@ -166,10 +155,9 @@ public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node, } /** - * Delegate the actual processing of the node to visitLabel and - * visitBreakOrContinue. + * Delegate the actual processing of the node to visitLabel and visitBreakOrContinue. * - * {@inheritDoc} + *

{@inheritDoc} */ @Override public void visit(NodeTraversal t, Node node, Node parent) { @@ -189,6 +177,7 @@ public void visit(NodeTraversal t, Node node, Node parent) { /** * Rename label references in breaks and continues. + * * @param node The break or continue node. */ private void visitBreakOrContinue(NodeTraversal t, Node node) { @@ -252,8 +241,8 @@ private void visitLabel(NodeTraversal t, Node node) { } /** - * @param id The id, which is the depth of the label in the current context, - * for which to get a short name. + * @param id The id, which is the depth of the label in the current context, for which to get a + * short name. * @return The short name of the identified label. */ String getNameForId(int id) { @@ -284,9 +273,7 @@ private static class LabelInfo { } } - private static class LabelNamespace { final Map renameMap = new LinkedHashMap<>(); } - } diff --git a/src/com/google/javascript/jscomp/RewriteNullishCoalesceOperator.java b/src/com/google/javascript/jscomp/RewriteNullishCoalesceOperator.java index 464fcdb9233..fc14a93bf9c 100644 --- a/src/com/google/javascript/jscomp/RewriteNullishCoalesceOperator.java +++ b/src/com/google/javascript/jscomp/RewriteNullishCoalesceOperator.java @@ -17,11 +17,11 @@ import static com.google.javascript.jscomp.AstFactory.type; -import com.google.common.base.Supplier; import com.google.javascript.jscomp.parsing.parser.FeatureSet; import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature; import com.google.javascript.rhino.IR; import com.google.javascript.rhino.Node; +import java.util.function.Supplier; /** Replaces the ES2020 `??` operator with conditional (?:). */ public final class RewriteNullishCoalesceOperator implements NodeTraversal.Callback, CompilerPass {