Skip to content

Commit 24a5352

Browse files
lauraharkercopybara-github
authored andcommitted
Add TypedAstIntegrationTest to reproduce a Polymer crash
Tests are copied from the existing PolymerIntegrationTest class PiperOrigin-RevId: 621276538
1 parent d8268b7 commit 24a5352

File tree

3 files changed

+263
-111
lines changed

3 files changed

+263
-111
lines changed

src/com/google/javascript/jscomp/testing/TestExternsBuilder.java

+67
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,52 @@ public static String getClosureExternsAsSource() {
958958
"/** @type {string} */",
959959
"RegExp.$1;");
960960

961+
private static final String HTML_ELEMENT_EXTERNS =
962+
lines(
963+
"/** @constructor */",
964+
"function Node() {}",
965+
"/** @constructor @extends {Node} */",
966+
"function Element() {}",
967+
"/** @constructor @extends {Element} */",
968+
"function HTMLElement() {}");
969+
970+
private static final String POLYMER_EXTERNS =
971+
lines(
972+
"",
973+
"/**",
974+
" * @param {!Object} init",
975+
" * @return {!function(new:HTMLElement)}",
976+
" */",
977+
"function Polymer(init) {}",
978+
"",
979+
"Polymer.ElementMixin = function(mixin) {}",
980+
"",
981+
"/** @typedef {!Object} */",
982+
"var PolymerElementProperties;",
983+
"",
984+
"/** @interface */",
985+
"function Polymer_ElementMixin() {}",
986+
"/** @type {string} */",
987+
"Polymer_ElementMixin.prototype._importPath;",
988+
"",
989+
"/**",
990+
"* @interface",
991+
"* @extends {Polymer_ElementMixin}",
992+
"*/",
993+
"function Polymer_LegacyElementMixin(){}",
994+
"/** @type {boolean} */",
995+
"Polymer_LegacyElementMixin.prototype.isAttached;",
996+
"/**",
997+
" * @constructor",
998+
" * @extends {HTMLElement}",
999+
" * @implements {Polymer_LegacyElementMixin}",
1000+
" */",
1001+
"var PolymerElement = function() {};", // Polymer 1
1002+
"",
1003+
"/** @constructor @extends {HTMLElement} */",
1004+
"Polymer.Element = function() {};", // Polymer 2
1005+
"");
1006+
9611007
private boolean includeBigIntExterns = false;
9621008
private boolean includeIterableExterns = false;
9631009
private boolean includeStringExterns = false;
@@ -976,6 +1022,8 @@ public static String getClosureExternsAsSource() {
9761022
private boolean includeJSCompLibraries = false;
9771023
private boolean includeMathExterns = false;
9781024
private boolean includeRegExpExterns = false;
1025+
private boolean includeHtmlElementExterns = false;
1026+
private boolean includePolymerExterns = false;
9791027
private final List<String> extraExterns = new ArrayList<>();
9801028

9811029
@CanIgnoreReturnValue
@@ -1110,6 +1158,19 @@ public TestExternsBuilder addRegExp() {
11101158
return this;
11111159
}
11121160

1161+
@CanIgnoreReturnValue
1162+
public TestExternsBuilder addHtmlElement() {
1163+
includeHtmlElementExterns = true;
1164+
return this;
1165+
}
1166+
1167+
@CanIgnoreReturnValue
1168+
public TestExternsBuilder addPolymer() {
1169+
addHtmlElement();
1170+
includePolymerExterns = true;
1171+
return this;
1172+
}
1173+
11131174
@CanIgnoreReturnValue
11141175
public TestExternsBuilder addExtra(String... lines) {
11151176
Collections.addAll(extraExterns, lines);
@@ -1179,6 +1240,12 @@ public String build() {
11791240
if (includeJSCompLibraries) {
11801241
externSections.add(JSCOMP_LIBRARIES);
11811242
}
1243+
if (includeHtmlElementExterns) {
1244+
externSections.add(HTML_ELEMENT_EXTERNS);
1245+
}
1246+
if (includePolymerExterns) {
1247+
externSections.add(POLYMER_EXTERNS);
1248+
}
11821249
externSections.addAll(extraExterns);
11831250
return LINE_JOINER.join(externSections);
11841251
}

test/com/google/javascript/jscomp/integration/PolymerIntegrationTest.java

+36-111
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.javascript.jscomp.SourceFile;
3535
import com.google.javascript.jscomp.VariableRenamingPolicy;
3636
import com.google.javascript.jscomp.parsing.Config.JsDocParsing;
37+
import com.google.javascript.jscomp.testing.TestExternsBuilder;
3738
import org.junit.Test;
3839
import org.junit.runner.RunWith;
3940
import org.junit.runners.JUnit4;
@@ -59,23 +60,42 @@ public CompilerOptions createCompilerOptions() {
5960

6061
private void addPolymerExterns() {
6162
ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
62-
externsList.addAll(externs);
6363
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() {}",
6975
"",
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) {};",
7283
"",
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) {};",
7595
"",
76-
"/** @typedef {Object} */",
77-
"let PolymerElementProperties;",
78-
"")));
96+
"/** @type {!CustomElementRegistry} */",
97+
"var customElements;")
98+
.buildExternsFile("polymer_externs.js"));
7999
externs = externsList.build();
80100
}
81101

@@ -458,95 +478,14 @@ public void testConstPolymerElementAllowed() {
458478
testNoWarnings(options, "const Foo = Polymer({ is: 'x-foo' });");
459479
}
460480

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-
542481
// Regression test for b/77650996
543482
@Test
544483
public void testPolymer2b() {
545484
CompilerOptions options = createCompilerOptions();
546485
options.setPolymerVersion(2);
547486
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
548487
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
549-
addPolymer2Externs();
488+
addPolymerExterns();
550489

551490
test(
552491
options,
@@ -575,13 +514,6 @@ public void testPolymer2b() {
575514
" * @extends {HTMLElement}",
576515
" */",
577516
" const Element = Polymer.ElementMixin(HTMLElement);",
578-
"",
579-
" /**",
580-
" * @constructor",
581-
" * @implements {Polymer_ElementMixin}",
582-
" * @extends {HTMLElement}",
583-
" */",
584-
" Polymer.Element = Element;",
585517
"})();",
586518
""),
587519
},
@@ -594,7 +526,7 @@ public void testPolymer1b() {
594526
options.setPolymerVersion(2);
595527
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
596528
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
597-
addPolymer2Externs();
529+
addPolymerExterns();
598530

599531
test(
600532
options,
@@ -611,13 +543,6 @@ public void testPolymer1b() {
611543
" * @extends {HTMLElement}",
612544
" */",
613545
" const Element = Polymer.ElementMixin(HTMLElement);",
614-
"",
615-
" /**",
616-
" * @constructor",
617-
" * @implements {Polymer_ElementMixin}",
618-
" * @extends {HTMLElement}",
619-
" */",
620-
" Polymer.Element = Element;",
621546
"})();",
622547
""),
623548
},
@@ -820,7 +745,7 @@ public void testPolymerPropertyDeclarationsWithConstructor() {
820745
// is injected into the externs. We need to make sure the types of the properties on this
821746
// interface aligns with the types we declared in the constructor, or else we'll get an error.
822747
options.setPolymerExportPolicy(PolymerExportPolicy.EXPORT_ALL);
823-
addPolymer2Externs();
748+
addPolymerExterns();
824749

825750
Compiler compiler =
826751
compile(

0 commit comments

Comments
 (0)