15
15
*/
16
16
package com .google .javascript .refactoring ;
17
17
18
+ import static com .google .common .base .Preconditions .checkState ;
18
19
import static com .google .javascript .refactoring .SuggestedFix .getShortNameForRequire ;
19
20
20
- import com .google .common .base .Preconditions ;
21
21
import com .google .common .collect .ImmutableList ;
22
+ import com .google .common .collect .ImmutableSet ;
22
23
import com .google .common .collect .Iterables ;
23
24
import com .google .javascript .jscomp .AbstractCompiler ;
24
25
import com .google .javascript .jscomp .JSError ;
@@ -82,9 +83,10 @@ public static SuggestedFix getFixForJsError(JSError error, AbstractCompiler comp
82
83
case "JSC_MISSING_SEMICOLON" :
83
84
return getFixForMissingSemicolon (error , compiler );
84
85
case "JSC_REQUIRES_NOT_SORTED" :
85
- return getFixForUnsortedRequiresOrProvides ("goog.require" , error , compiler );
86
+ return getFixForUnsortedRequiresOrProvides (
87
+ error , compiler , "goog.require" , "goog.forwardDeclare" );
86
88
case "JSC_PROVIDES_NOT_SORTED" :
87
- return getFixForUnsortedRequiresOrProvides ("goog.provide" , error , compiler );
89
+ return getFixForUnsortedRequiresOrProvides (error , compiler , "goog.provide" );
88
90
case "JSC_DEBUGGER_STATEMENT_PRESENT" :
89
91
return removeNode (error , compiler );
90
92
case "JSC_USELESS_EMPTY_STATEMENT" :
@@ -114,7 +116,7 @@ public static SuggestedFix getFixForJsError(JSError error, AbstractCompiler comp
114
116
115
117
private static SuggestedFix getFixForRedeclaration (JSError error , AbstractCompiler compiler ) {
116
118
Node name = error .node ;
117
- Preconditions . checkState (name .isName (), name );
119
+ checkState (name .isName (), name );
118
120
Node parent = name .getParent ();
119
121
if (!NodeUtil .isNameDeclaration (parent )) {
120
122
return null ;
@@ -202,7 +204,7 @@ private static SuggestedFix getFixForReferenceToShortImportByLongName(
202
204
Match match = new Match (error .node , metadata );
203
205
204
206
Matcher fullNameMatcher = FULLY_QUALIFIED_NAME .matcher (error .description );
205
- Preconditions . checkState (fullNameMatcher .matches (), error .description );
207
+ checkState (fullNameMatcher .matches (), error .description );
206
208
String fullName = fullNameMatcher .group (1 );
207
209
208
210
Matcher shortNameMatcher = USE_SHORT_NAME .matcher (error .description );
@@ -294,7 +296,7 @@ private static SuggestedFix getFixForInexistentProperty(
294
296
295
297
private static SuggestedFix getFixForMissingRequire (JSError error , AbstractCompiler compiler ) {
296
298
Matcher regexMatcher = MISSING_REQUIRE .matcher (error .description );
297
- Preconditions . checkState (regexMatcher .matches (),
299
+ checkState (regexMatcher .matches (),
298
300
"Unexpected error description: %s" , error .description );
299
301
String namespaceToRequire = regexMatcher .group (1 );
300
302
NodeMetadata metadata = new NodeMetadata (compiler );
@@ -325,7 +327,7 @@ private static SuggestedFix getFixForDuplicateRequire(JSError error, AbstractCom
325
327
return null ;
326
328
}
327
329
Matcher regexMatcher = DUPLICATE_REQUIRE .matcher (error .description );
328
- Preconditions . checkState (
330
+ checkState (
329
331
regexMatcher .matches (), "Unexpected error description: %s" , error .description );
330
332
String namespace = regexMatcher .group (1 );
331
333
NodeMetadata metadata = new NodeMetadata (compiler );
@@ -344,7 +346,7 @@ private static SuggestedFix getFixForExtraRequire(JSError error, AbstractCompile
344
346
if (error .node .isStringKey ()) {
345
347
fix .delete (error .node );
346
348
} else {
347
- Preconditions . checkState (error .node .getParent ().isStringKey ());
349
+ checkState (error .node .getParent ().isStringKey (), error . node . getParent ());
348
350
fix .delete (error .node .getParent ());
349
351
}
350
352
} else {
@@ -354,11 +356,11 @@ private static SuggestedFix getFixForExtraRequire(JSError error, AbstractCompile
354
356
}
355
357
356
358
private static SuggestedFix getFixForUnsortedRequiresOrProvides (
357
- String closureFunction , JSError error , AbstractCompiler compiler ) {
359
+ JSError error , AbstractCompiler compiler , String ... closureFunctions ) {
358
360
SuggestedFix .Builder fix = new SuggestedFix .Builder ();
359
361
fix .attachMatchedNodeInfo (error .node , compiler );
360
362
Node script = NodeUtil .getEnclosingScript (error .node );
361
- RequireProvideSorter cb = new RequireProvideSorter (closureFunction );
363
+ RequireProvideSorter cb = new RequireProvideSorter (closureFunctions );
362
364
NodeTraversal .traverseEs6 (compiler , script , cb );
363
365
Node first = cb .calls .get (0 );
364
366
Node last = Iterables .getLast (cb .calls );
@@ -380,28 +382,37 @@ private static SuggestedFix getFixForUnsortedRequiresOrProvides(
380
382
381
383
private static class RequireProvideSorter extends NodeTraversal .AbstractShallowCallback
382
384
implements Comparator <Node > {
383
- private final String closureFunction ;
385
+ private final ImmutableSet < String > closureFunctions ;
384
386
private final List <Node > calls = new ArrayList <>();
385
387
386
- RequireProvideSorter (String closureFunction ) {
387
- this .closureFunction = closureFunction ;
388
+ RequireProvideSorter (String ... closureFunctions ) {
389
+ this .closureFunctions = ImmutableSet . copyOf ( closureFunctions ) ;
388
390
}
389
391
390
392
@ Override
391
393
public final void visit (NodeTraversal nodeTraversal , Node n , Node parent ) {
392
394
if (n .isCall ()
393
395
&& parent .isExprResult ()
394
- && n .getFirstChild (). matchesQualifiedName ( closureFunction )) {
396
+ && matchName ( n .getFirstChild ())) {
395
397
calls .add (parent );
396
398
} else if (NodeUtil .isNameDeclaration (parent )
397
399
&& n .hasChildren ()
398
400
&& n .getLastChild ().isCall ()
399
- && n .getLastChild ().getFirstChild (). matchesQualifiedName ( closureFunction )) {
400
- Preconditions . checkState (n .isName () || n .isDestructuringLhs ());
401
+ && matchName ( n .getLastChild ().getFirstChild ())) {
402
+ checkState (n .isName () || n .isDestructuringLhs (), n );
401
403
calls .add (parent );
402
404
}
403
405
}
404
406
407
+ private boolean matchName (Node n ) {
408
+ for (String closureFn : closureFunctions ) {
409
+ if (n .matchesQualifiedName (closureFn )) {
410
+ return true ;
411
+ }
412
+ }
413
+ return false ;
414
+ }
415
+
405
416
public void sortCallsAlphabetically () {
406
417
Collections .sort (calls , this );
407
418
}
0 commit comments