@@ -470,7 +470,6 @@ public void testNoOptimizeClosureUnawareCode_parsesCodeWithNonClosureJsDoc() {
470
470
CompilerOptions options = createCompilerOptions ();
471
471
CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
472
472
CompilationLevel .ADVANCED_OPTIMIZATIONS .setTypeBasedOptimizationOptions (options );
473
- options .setLanguageOut (LanguageMode .ECMASCRIPT_2018 );
474
473
475
474
externs =
476
475
ImmutableList .<SourceFile >builder ()
@@ -492,12 +491,180 @@ public void testNoOptimizeClosureUnawareCode_parsesCodeWithNonClosureJsDoc() {
492
491
"(function() {" ,
493
492
" /**" ,
494
493
" * @prop {number} a - scale x" ,
494
+ // @defaults isn't a valid jsdoc tag, but within the closure-unaware portions of the
495
+ // AST we should not raise a warning.
496
+ " * @defaults" ,
497
+ " */" ,
498
+ " const x = 5;" ,
499
+ "}).call(globalThis);" ),
500
+ lines ("(function() {" , " const x = 5;" , "}).call(globalThis);" ));
501
+ }
502
+
503
+ @ Test
504
+ public void
505
+ testNoOptimizeClosureUnawareCode_parsesCodeWithNonClosureJsDoc_whenNotAttachedToNode () {
506
+ CompilerOptions options = createCompilerOptions ();
507
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
508
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setTypeBasedOptimizationOptions (options );
509
+
510
+ externs =
511
+ ImmutableList .<SourceFile >builder ()
512
+ .addAll (externs )
513
+ .add (
514
+ SourceFile .fromCode (
515
+ "globalthis.js" , lines ("/**" , " * @type {?}" , " */" , "var globalThis;" )))
516
+ .build ();
517
+
518
+ // this test-case is trying to ensure that even when there are jsdoc comments in the
519
+ // closure-unaware portions of the AST that aren't attached to specific nodes / expressions
520
+ // that we can still ignore invalid jsdoc contents.
521
+ // @defaults isn't a valid jsdoc tag, and outside of the closure-unaware portions of the AST we
522
+ // should raise a warning.
523
+
524
+ test (
525
+ options ,
526
+ lines (
527
+ "/**" ,
528
+ " * @fileoverview" ,
529
+ " * @closureUnaware" ,
530
+ " */" ,
531
+ "goog.module('a.b');" ,
532
+ "/** @closureUnaware */" ,
533
+ "(function() {" ,
534
+ " /** @defaults */" , // JSDoc comment not attached to any node
535
+ " /**" ,
536
+ " * @prop {number} a - scale x" ,
537
+ " * @defaults" ,
495
538
" */" ,
496
539
" const x = 5;" ,
497
540
"}).call(globalThis);" ),
498
541
lines ("(function() {" , " const x = 5;" , "}).call(globalThis);" ));
499
542
}
500
543
544
+ @ Test
545
+ public void
546
+ testNoOptimizeClosureUnawareCode_parsesCodeWithNonClosureJsDoc_whenNotAttachedToNode_typedJsDoc () {
547
+ CompilerOptions options = createCompilerOptions ();
548
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
549
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setTypeBasedOptimizationOptions (options );
550
+
551
+ externs =
552
+ ImmutableList .<SourceFile >builder ()
553
+ .addAll (externs )
554
+ .add (
555
+ SourceFile .fromCode (
556
+ "globalthis.js" , lines ("/**" , " * @type {?}" , " */" , "var globalThis;" )))
557
+ .build ();
558
+
559
+ // this test-case is trying to ensure that even when there are jsdoc comments in the
560
+ // closure-unaware portions of the AST that aren't attached to specific nodes / expressions
561
+ // that we can still ignore invalid jsdoc contents.
562
+ // @type [string] does not parse as a valid "typed jsdoc" tag, and this follows a different
563
+ // parsing codepath than non-type jsdoc tags and raises a different error
564
+ // than an entirely unknown jsdoc tag.
565
+
566
+ test (
567
+ options ,
568
+ lines (
569
+ "/**" ,
570
+ " * @fileoverview" ,
571
+ " * @closureUnaware" ,
572
+ " */" ,
573
+ "goog.module('a.b');" ,
574
+ "/** @closureUnaware */" ,
575
+ "(function() {" ,
576
+ " /** @type [string] */" ,
577
+ "" ,
578
+ " /**" ,
579
+ " * @prop {number} a - scale x" ,
580
+ " * @defaults" ,
581
+ " */" ,
582
+ " const x = 5;" ,
583
+ "}).call(globalThis);" ),
584
+ lines ("(function() {" , " const x = 5;" , "}).call(globalThis);" ));
585
+ }
586
+
587
+ @ Test
588
+ public void
589
+ testNoOptimizeClosureUnawareCode_doesntSuppressParseErrorsOutsideClosureUnawareBlocks () {
590
+ CompilerOptions options = createCompilerOptions ();
591
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
592
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setTypeBasedOptimizationOptions (options );
593
+
594
+ externs =
595
+ ImmutableList .<SourceFile >builder ()
596
+ .addAll (externs )
597
+ .add (
598
+ SourceFile .fromCode (
599
+ "globalthis.js" , lines ("/**" , " * @type {?}" , " */" , "var globalThis;" )))
600
+ .build ();
601
+
602
+ // this test-case is trying to ensure that even when the special handling for JSDoc comments in
603
+ // closureUnaware code we can still report jsdoc errors for code that is not within the
604
+ // closure-unaware subsection of the AST.
605
+ test (
606
+ options ,
607
+ lines (
608
+ "/**" ,
609
+ " * @fileoverview" ,
610
+ " * @closureUnaware" ,
611
+ " */" ,
612
+ "goog.module('a.b');" ,
613
+ // This is invalid JSDoc, but it isn't within the subtree of a node annotated as
614
+ // `@closureUnaware`.
615
+ // NOTE: The `@closureUnaware` in the `@fileoverview` comment does not apply this
616
+ // subtree suppression
617
+ // mechanism to the whole script - it is only to indicate that the file contains some
618
+ // closure-unaware code
619
+ // (e.g. a performance optimization).
620
+ "/**" ,
621
+ " * @prop {number} a - scale x" ,
622
+ " */" ,
623
+ "/** @closureUnaware */" ,
624
+ "(function() {" ,
625
+ " const x = 5;" ,
626
+ "}).call(globalThis);" ),
627
+ DiagnosticGroups .NON_STANDARD_JSDOC );
628
+ }
629
+
630
+ @ Test
631
+ public void
632
+ testNoOptimizeClosureUnawareCode_doesntSuppressParseErrorsOutsideClosureUnawareBlocks_afterRange () {
633
+ CompilerOptions options = createCompilerOptions ();
634
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
635
+ CompilationLevel .ADVANCED_OPTIMIZATIONS .setTypeBasedOptimizationOptions (options );
636
+
637
+ externs =
638
+ ImmutableList .<SourceFile >builder ()
639
+ .addAll (externs )
640
+ .add (
641
+ SourceFile .fromCode (
642
+ "globalthis.js" , lines ("/**" , " * @type {?}" , " */" , "var globalThis;" )))
643
+ .build ();
644
+
645
+ // this test-case is trying to ensure that even when the special handling for JSDoc comments in
646
+ // closureUnaware code we can still report jsdoc errors for code that is not within the
647
+ // closure-unaware subsection of the AST.
648
+ // Specifically this test validates that any comments that come after a closure-unaware
649
+ // subsection of the AST don't have their jsdoc parse errors suppressed.
650
+ test (
651
+ options ,
652
+ lines (
653
+ "/**" ,
654
+ " * @fileoverview" ,
655
+ " * @closureUnaware" ,
656
+ " */" ,
657
+ "goog.module('a.b');" ,
658
+ "/** @closureUnaware */" ,
659
+ "(function() {" ,
660
+ " const x = 5;" ,
661
+ "}).call(globalThis);" ,
662
+ "/**" ,
663
+ " * @prop {number} a - scale x" ,
664
+ " */" ,
665
+ "" ),
666
+ DiagnosticGroups .NON_STANDARD_JSDOC );
667
+ }
501
668
// TODO how can I test whether source info is being properly retained?
502
669
// TODO if there is a sourcemap comment in the IIFE, can we use that info somehow?
503
670
}
0 commit comments