34
34
import com .google .javascript .jscomp .SourceFile ;
35
35
import com .google .javascript .jscomp .VariableRenamingPolicy ;
36
36
import com .google .javascript .jscomp .parsing .Config .JsDocParsing ;
37
+ import com .google .javascript .jscomp .testing .TestExternsBuilder ;
37
38
import org .junit .Test ;
38
39
import org .junit .runner .RunWith ;
39
40
import org .junit .runners .JUnit4 ;
@@ -59,23 +60,42 @@ public CompilerOptions createCompilerOptions() {
59
60
60
61
private void addPolymerExterns () {
61
62
ImmutableList .Builder <SourceFile > externsList = ImmutableList .builder ();
62
- externsList .addAll (externs );
63
63
externsList .add (
64
- SourceFile .fromCode (
65
- "polymer_externs.js" ,
66
- lines (
67
- "/** @return {function(new: PolymerElement)} */" ,
68
- "var Polymer = function(descriptor) {};" ,
64
+ new TestExternsBuilder ()
65
+ .addObject ()
66
+ .addClosureExterns ()
67
+ .addPolymer ()
68
+ .addExtra (
69
+ "/**" ,
70
+ " * @see"
71
+ + " https://html.spec.whatwg.org/multipage/custom-elements.html#customelementregistry" ,
72
+ " * @constructor" ,
73
+ " */" ,
74
+ "function CustomElementRegistry() {}" ,
69
75
"" ,
70
- "/** @constructor @extends {HTMLElement} */" ,
71
- "var PolymerElement = function() {};" , // Polymer 1
76
+ "/**" ,
77
+ " * @param {string} tagName" ,
78
+ " * @param {function(new:HTMLElement)} klass" ,
79
+ " * @param {{extends: string}=} options" ,
80
+ " * @return {undefined}" ,
81
+ " */" ,
82
+ "CustomElementRegistry.prototype.define = function (tagName, klass, options) {};" ,
72
83
"" ,
73
- "/** @constructor @extends {HTMLElement} */" ,
74
- "Polymer.Element = function() {};" , // Polymer 2
84
+ "/**" ,
85
+ " * @param {string} tagName" ,
86
+ " * @return {function(new:HTMLElement)|undefined}" ,
87
+ " */" ,
88
+ "CustomElementRegistry.prototype.get = function(tagName) {};" ,
89
+ "" ,
90
+ "/**" ,
91
+ " * @param {string} tagName" ,
92
+ " * @return {!Promise<undefined>}" ,
93
+ " */" ,
94
+ "CustomElementRegistry.prototype.whenDefined = function(tagName) {};" ,
75
95
"" ,
76
- "/** @typedef {Object } */" ,
77
- "let PolymerElementProperties;" ,
78
- "" ) ));
96
+ "/** @type {!CustomElementRegistry } */" ,
97
+ "var customElements;" )
98
+ . buildExternsFile ( "polymer_externs.js" ));
79
99
externs = externsList .build ();
80
100
}
81
101
@@ -458,95 +478,14 @@ public void testConstPolymerElementAllowed() {
458
478
testNoWarnings (options , "const Foo = Polymer({ is: 'x-foo' });" );
459
479
}
460
480
461
- private void addPolymer2Externs () {
462
- ImmutableList .Builder <SourceFile > externsList = ImmutableList .builder ();
463
- externsList .addAll (externs );
464
-
465
- externsList .add (
466
- SourceFile .fromCode (
467
- "polymer_externs.js" ,
468
- lines (
469
- "" ,
470
- "/**" ,
471
- " * @param {!Object} init" ,
472
- " * @return {!function(new:HTMLElement)}" ,
473
- " */" ,
474
- "function Polymer(init) {}" ,
475
- "" ,
476
- "Polymer.ElementMixin = function(mixin) {}" ,
477
- "" ,
478
- "/** @typedef {!Object} */" ,
479
- "var PolymerElementProperties;" ,
480
- "" ,
481
- "/** @interface */" ,
482
- "function Polymer_ElementMixin() {}" ,
483
- "/** @type {string} */" ,
484
- "Polymer_ElementMixin.prototype._importPath;" ,
485
- "" ,
486
- "/**" ,
487
- "* @interface" ,
488
- "* @extends {Polymer_ElementMixin}" ,
489
- "*/" ,
490
- "function Polymer_LegacyElementMixin(){}" ,
491
- "/** @type {boolean} */" ,
492
- "Polymer_LegacyElementMixin.prototype.isAttached;" ,
493
- "/**" ,
494
- " * @constructor" ,
495
- " * @extends {HTMLElement}" ,
496
- " * @implements {Polymer_LegacyElementMixin}" ,
497
- " */" ,
498
- "var PolymerElement = function() {};" ,
499
- "" )));
500
-
501
- externsList .add (
502
- SourceFile .fromCode (
503
- "html5.js" ,
504
- lines (
505
- "/** @constructor */" ,
506
- "function Element() {}" ,
507
- "" ,
508
- "/**" ,
509
- " * @see"
510
- + " https://html.spec.whatwg.org/multipage/custom-elements.html#customelementregistry" ,
511
- " * @constructor" ,
512
- " */" ,
513
- "function CustomElementRegistry() {}" ,
514
- "" ,
515
- "/**" ,
516
- " * @param {string} tagName" ,
517
- " * @param {function(new:HTMLElement)} klass" ,
518
- " * @param {{extends: string}=} options" ,
519
- " * @return {undefined}" ,
520
- " */" ,
521
- "CustomElementRegistry.prototype.define = function (tagName, klass, options) {};" ,
522
- "" ,
523
- "/**" ,
524
- " * @param {string} tagName" ,
525
- " * @return {function(new:HTMLElement)|undefined}" ,
526
- " */" ,
527
- "CustomElementRegistry.prototype.get = function(tagName) {};" ,
528
- "" ,
529
- "/**" ,
530
- " * @param {string} tagName" ,
531
- " * @return {!Promise<undefined>}" ,
532
- " */" ,
533
- "CustomElementRegistry.prototype.whenDefined = function(tagName) {};" ,
534
- "" ,
535
- "/** @type {!CustomElementRegistry} */" ,
536
- "var customElements;" ,
537
- "" )));
538
-
539
- externs = externsList .build ();
540
- }
541
-
542
481
// Regression test for b/77650996
543
482
@ Test
544
483
public void testPolymer2b () {
545
484
CompilerOptions options = createCompilerOptions ();
546
485
options .setPolymerVersion (2 );
547
486
options .setWarningLevel (DiagnosticGroups .CHECK_TYPES , CheckLevel .ERROR );
548
487
options .setLanguageOut (LanguageMode .ECMASCRIPT5 );
549
- addPolymer2Externs ();
488
+ addPolymerExterns ();
550
489
551
490
test (
552
491
options ,
@@ -575,13 +514,6 @@ public void testPolymer2b() {
575
514
" * @extends {HTMLElement}" ,
576
515
" */" ,
577
516
" const Element = Polymer.ElementMixin(HTMLElement);" ,
578
- "" ,
579
- " /**" ,
580
- " * @constructor" ,
581
- " * @implements {Polymer_ElementMixin}" ,
582
- " * @extends {HTMLElement}" ,
583
- " */" ,
584
- " Polymer.Element = Element;" ,
585
517
"})();" ,
586
518
"" ),
587
519
},
@@ -594,7 +526,7 @@ public void testPolymer1b() {
594
526
options .setPolymerVersion (2 );
595
527
options .setWarningLevel (DiagnosticGroups .CHECK_TYPES , CheckLevel .ERROR );
596
528
options .setLanguageOut (LanguageMode .ECMASCRIPT5 );
597
- addPolymer2Externs ();
529
+ addPolymerExterns ();
598
530
599
531
test (
600
532
options ,
@@ -611,13 +543,6 @@ public void testPolymer1b() {
611
543
" * @extends {HTMLElement}" ,
612
544
" */" ,
613
545
" const Element = Polymer.ElementMixin(HTMLElement);" ,
614
- "" ,
615
- " /**" ,
616
- " * @constructor" ,
617
- " * @implements {Polymer_ElementMixin}" ,
618
- " * @extends {HTMLElement}" ,
619
- " */" ,
620
- " Polymer.Element = Element;" ,
621
546
"})();" ,
622
547
"" ),
623
548
},
@@ -820,7 +745,7 @@ public void testPolymerPropertyDeclarationsWithConstructor() {
820
745
// is injected into the externs. We need to make sure the types of the properties on this
821
746
// interface aligns with the types we declared in the constructor, or else we'll get an error.
822
747
options .setPolymerExportPolicy (PolymerExportPolicy .EXPORT_ALL );
823
- addPolymer2Externs ();
748
+ addPolymerExterns ();
824
749
825
750
Compiler compiler =
826
751
compile (
0 commit comments