@@ -40,29 +40,29 @@ final class CheckJSDoc extends AbstractPostOrderCallback implements CompilerPass
40
40
+ "\n Message constants must be prefixed with 'MSG_'." );
41
41
42
42
public static final DiagnosticType MISPLACED_ANNOTATION =
43
- DiagnosticType .warning ("JSC_MISPLACED_ANNOTATION" ,
44
- "Misplaced {0} annotation. {1}" );
43
+ DiagnosticType .warning ("JSC_MISPLACED_ANNOTATION" , "Misplaced {0} annotation. {1}" );
45
44
46
45
public static final DiagnosticType ANNOTATION_DEPRECATED =
47
- DiagnosticType .warning ("JSC_ANNOTATION_DEPRECATED" ,
48
- "The {0} annotation is deprecated. {1}" );
46
+ DiagnosticType .warning ("JSC_ANNOTATION_DEPRECATED" , "The {0} annotation is deprecated. {1}" );
49
47
50
48
public static final DiagnosticType DISALLOWED_MEMBER_JSDOC =
51
- DiagnosticType .warning ("JSC_DISALLOWED_MEMBER_JSDOC" ,
49
+ DiagnosticType .warning (
50
+ "JSC_DISALLOWED_MEMBER_JSDOC" ,
52
51
"Class level JSDocs (@interface, @extends, etc.) are not allowed on class members" );
53
52
54
- static final DiagnosticType ARROW_FUNCTION_AS_CONSTRUCTOR = DiagnosticType . error (
55
- "JSC_ARROW_FUNCTION_AS_CONSTRUCTOR" ,
56
- "Arrow functions cannot be used as constructors" );
53
+ static final DiagnosticType ARROW_FUNCTION_AS_CONSTRUCTOR =
54
+ DiagnosticType . error (
55
+ "JSC_ARROW_FUNCTION_AS_CONSTRUCTOR" , "Arrow functions cannot be used as constructors" );
57
56
58
57
static final DiagnosticType BAD_REST_PARAMETER_ANNOTATION =
59
58
DiagnosticType .warning (
60
59
"JSC_BAD_REST_PARAMETER_ANNOTATION" ,
61
60
"Missing \" ...\" in type annotation for rest parameter." );
62
61
63
- static final DiagnosticType DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL = DiagnosticType .error (
64
- "JSC_DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL" ,
65
- "Inline JSDoc on default parameters must be marked as optional" );
62
+ static final DiagnosticType DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL =
63
+ DiagnosticType .error (
64
+ "JSC_DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL" ,
65
+ "Inline JSDoc on default parameters must be marked as optional" );
66
66
67
67
public static final DiagnosticType INVALID_NO_SIDE_EFFECT_ANNOTATION =
68
68
DiagnosticType .error (
@@ -256,9 +256,11 @@ private void validateTemplates(Node n, JSDocInfo info) {
256
256
&& !isClassDecl (n )
257
257
&& !info .containsFunctionDeclaration ()
258
258
&& getFunctionDecl (n ) == null ) {
259
- reportMisplaced (n , "template" ,
260
- "@template is only allowed in class, constructor, interface, function "
261
- + "or method declarations" );
259
+ reportMisplaced (
260
+ n ,
261
+ "template" ,
262
+ "@template is only allowed in class, constructor, interface, function "
263
+ + "or method declarations" );
262
264
}
263
265
}
264
266
@@ -283,7 +285,8 @@ && getFunctionDecl(n) == null) {
283
285
return n .getLastChild ();
284
286
}
285
287
286
- if (n .isStringKey () && n .getGrandparent () != null
288
+ if (n .isStringKey ()
289
+ && n .getGrandparent () != null
287
290
&& ClosureRewriteClass .isGoogDefineClass (n .getGrandparent ())
288
291
&& n .getFirstChild ().isFunction ()) {
289
292
return n .getFirstChild ();
@@ -312,8 +315,7 @@ private boolean isNameInitializeWithClass(Node n) {
312
315
}
313
316
314
317
private boolean isClass (Node n ) {
315
- return n .isClass ()
316
- || (n .isCall () && compiler .getCodingConvention ().isClassFactoryCall (n ));
318
+ return n .isClass () || (n .isCall () && compiler .getCodingConvention ().isClassFactoryCall (n ));
317
319
}
318
320
319
321
private static boolean isPrototypeOrInstanceDecl (Node n ) {
@@ -331,8 +333,7 @@ private static boolean isPrototypeOrInstanceDecl(Node n) {
331
333
* Checks that class-level annotations like @interface/@extends are not used on member functions.
332
334
*/
333
335
private void validateClassLevelJsDoc (Node n , JSDocInfo info ) {
334
- if (info != null && n .isMemberFunctionDef ()
335
- && hasClassLevelJsDoc (info )) {
336
+ if (info != null && n .isMemberFunctionDef () && hasClassLevelJsDoc (info )) {
336
337
report (n , DISALLOWED_MEMBER_JSDOC );
337
338
}
338
339
}
@@ -359,7 +360,10 @@ private void validateAbstractJsDoc(Node n, JSDocInfo info) {
359
360
360
361
if (!info .isConstructor () && NodeUtil .getFunctionBody (functionNode ).hasChildren ()) {
361
362
// @abstract annotation on a function with a non-empty body
362
- report (n , MISPLACED_ANNOTATION , "@abstract" ,
363
+ report (
364
+ n ,
365
+ MISPLACED_ANNOTATION ,
366
+ "@abstract" ,
363
367
"function with a non-empty body cannot be abstract" );
364
368
return ;
365
369
}
@@ -406,8 +410,8 @@ private static boolean hasClassLevelJsDoc(JSDocInfo info) {
406
410
}
407
411
408
412
/**
409
- * Warns when nocollapse annotations are present on nodes
410
- * which are not eligible for property collapsing.
413
+ * Warns when nocollapse annotations are present on nodes which are not eligible for property
414
+ * collapsing.
411
415
*/
412
416
private void validateNoCollapse (Node n , JSDocInfo info ) {
413
417
if (info == null || !info .isNoCollapse ()) {
@@ -552,19 +556,17 @@ private static boolean isValidMsgName(Node nameNode) {
552
556
}
553
557
}
554
558
555
- /**
556
- * Check that JSDoc with a {@code @type} annotation is in a valid place.
557
- */
559
+ /** Check that JSDoc with a {@code @type} annotation is in a valid place. */
558
560
private void validateTypeAnnotations (Node n , JSDocInfo info ) {
559
561
if (info != null && info .hasType ()) {
560
562
boolean valid = false ;
561
563
switch (n .getToken ()) {
562
- // Function declarations are valid
564
+ // Function declarations are valid
563
565
case FUNCTION :
564
566
valid = NodeUtil .isFunctionDeclaration (n );
565
567
break ;
566
- // Object literal properties, catch declarations and variable
567
- // initializers are valid.
568
+ // Object literal properties, catch declarations and variable
569
+ // initializers are valid.
568
570
case NAME :
569
571
valid = isTypeAnnotationAllowedForName (n );
570
572
break ;
@@ -575,7 +577,7 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
575
577
// function f(/** !Array */ [x]) {}
576
578
valid = n .getParent ().isParamList ();
577
579
break ;
578
- // Casts, exports, and Object literal properties are valid.
580
+ // Casts, exports, and Object literal properties are valid.
579
581
case CAST :
580
582
case EXPORT :
581
583
case STRING_KEY :
@@ -585,22 +587,23 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
585
587
case COMPUTED_FIELD_DEF :
586
588
valid = true ;
587
589
break ;
588
- // Declarations are valid iff they only contain simple names
589
- // /** @type {number} */ var x = 3; // ok
590
- // /** @type {number} */ var {x} = obj; // forbidden
590
+ // Declarations are valid iff they only contain simple names
591
+ // /** @type {number} */ var x = 3; // ok
592
+ // /** @type {number} */ var {x} = obj; // forbidden
591
593
case VAR :
592
594
case LET :
593
595
case CONST :
594
596
valid = !NodeUtil .isDestructuringDeclaration (n );
595
597
break ;
596
- // Property assignments are valid, if at the root of an expression.
597
- case ASSIGN : {
598
- Node lvalue = n .getFirstChild ();
598
+ // Property assignments are valid, if at the root of an expression.
599
+ case ASSIGN :
600
+ {
601
+ Node lvalue = n .getFirstChild ();
599
602
valid =
600
603
n .getParent ().isExprResult ()
601
604
&& (lvalue .isGetProp () || lvalue .isGetElem () || lvalue .matchesName ("exports" ));
602
- break ;
603
- }
605
+ break ;
606
+ }
604
607
case GETPROP :
605
608
valid = n .getParent ().isExprResult () && n .isQualifiedName ();
606
609
break ;
@@ -612,8 +615,8 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
612
615
}
613
616
614
617
if (!valid ) {
615
- reportMisplaced (n , "type" , "Type annotations are not allowed here. "
616
- + "Are you missing parentheses?" );
618
+ reportMisplaced (
619
+ n , "type" , "Type annotations are not allowed here. " + "Are you missing parentheses?" );
617
620
}
618
621
}
619
622
}
@@ -632,17 +635,14 @@ private static boolean isTypeAnnotationAllowedForName(Node n) {
632
635
}
633
636
634
637
private void reportMisplaced (Node n , String annotationName , String note ) {
635
- compiler .report (JSError .make (n , MISPLACED_ANNOTATION ,
636
- annotationName , note ));
638
+ compiler .report (JSError .make (n , MISPLACED_ANNOTATION , annotationName , note ));
637
639
}
638
640
639
641
private void report (Node n , DiagnosticType type , String ... arguments ) {
640
642
compiler .report (JSError .make (n , type , arguments ));
641
643
}
642
644
643
- /**
644
- * Check that an arrow function is not annotated with {@constructor}.
645
- */
645
+ /** Check that an arrow function is not annotated with {@constructor}. */
646
646
private void validateArrowFunction (Node n ) {
647
647
if (n .isArrowFunction ()) {
648
648
JSDocInfo info = NodeUtil .getBestJSDocInfo (n );
@@ -686,8 +686,8 @@ private void validateRestParameter(Node restParam) {
686
686
}
687
687
688
688
/**
689
- * Check that a parameter with a default value is marked as optional.
690
- * TODO(bradfordcsmith): This is redundant. We shouldn't require it.
689
+ * Check that a parameter with a default value is marked as optional. TODO(bradfordcsmith): This
690
+ * is redundant. We shouldn't require it.
691
691
*/
692
692
private void validateDefaultValue (Node n ) {
693
693
if (n .isDefaultValue () && n .getParent ().isParamList ()) {
@@ -725,9 +725,7 @@ private void validateNoSideEffects(Node n, JSDocInfo info) {
725
725
}
726
726
}
727
727
728
- /**
729
- * Check that a let declaration is not used with {@defines}
730
- */
728
+ /** Check that a let declaration is not used with {@defines} */
731
729
private void validateDefinesDeclaration (Node n , JSDocInfo info ) {
732
730
if (info != null && info .isDefine () && n .isLet ()) {
733
731
report (n , INVALID_DEFINE_ON_LET );
@@ -775,7 +773,7 @@ private void validateTsType(Node n, JSDocInfo info) {
775
773
}
776
774
777
775
private static boolean isFromTs (Node n ) {
778
- return n .getSourceFileName ().endsWith ( ".closure.js" );
776
+ return n .getStaticSourceFile ().isTypeScriptSource ( );
779
777
}
780
778
781
779
private final CheckJsdocTypes checkJsDocTypesVisitor = new CheckJsdocTypes ();
0 commit comments