44
44
import com .google .javascript .jscomp .VariableRenamingPolicy ;
45
45
import com .google .javascript .jscomp .WarningLevel ;
46
46
import com .google .javascript .jscomp .testing .JSChunkGraphBuilder ;
47
+ import com .google .javascript .jscomp .testing .JSCompCorrespondences ;
47
48
import com .google .javascript .jscomp .testing .TestExternsBuilder ;
48
49
import com .google .javascript .rhino .Node ;
49
50
import com .google .javascript .rhino .StaticSourceFile .SourceKind ;
@@ -66,6 +67,116 @@ public final class ClosureIntegrationTest extends IntegrationTestCase {
66
67
67
68
private static final String CLOSURE_COLLAPSED = lines ("var COMPILED = true;" );
68
69
70
+ @ Test
71
+ public void testReferencingMangledModuleNames_rewriteModulesBeforeTypechecking () {
72
+ CompilerOptions options = createCompilerOptions ();
73
+ options .setClosurePass (true );
74
+ options .setCheckTypes (true );
75
+ options .setBadRewriteModulesBeforeTypecheckingThatWeWantToGetRidOf (true );
76
+ compile (
77
+ options ,
78
+ new String [] {
79
+ lines (
80
+ "goog.module('foo.bar');" ,
81
+ "class Inner {}" ,
82
+ "class Outer extends Inner {}" ,
83
+ "exports.Outer = Outer;" ),
84
+ lines (
85
+ "/** @type {!module$exports$foo$bar} */ let err1;" ,
86
+ "/** @type {!module$contents$foo$bar_Inner} */ let err2;" )
87
+ });
88
+ assertThat (lastCompiler .getWarnings ())
89
+ .comparingElementsUsing (JSCompCorrespondences .DESCRIPTION_EQUALITY )
90
+ .containsExactly (
91
+ "Bad type annotation. Unknown type UnrecognizedType_module$exports$foo$bar" ,
92
+ "Bad type annotation. Unknown type UnrecognizedType_module$contents$foo$bar_Inner" );
93
+ }
94
+
95
+ @ Test
96
+ public void testReferencingMangledModuleNames_rewriteModulesAfterTypechecking () {
97
+ CompilerOptions options = createCompilerOptions ();
98
+ options .setClosurePass (true );
99
+ options .setCheckTypes (true );
100
+ options .setBadRewriteModulesBeforeTypecheckingThatWeWantToGetRidOf (false );
101
+ compile (
102
+ options ,
103
+ new String [] {
104
+ lines (
105
+ "goog.module('foo.bar');" ,
106
+ "class Inner {}" ,
107
+ "class Outer extends Inner {}" ,
108
+ "exports.Outer = Outer;" ),
109
+ lines (
110
+ "/** @type {!module$exports$foo$bar} */ let err1;" ,
111
+ "/** @type {!module$contents$foo$bar_Inner} */ let err2;" )
112
+ });
113
+ assertThat (lastCompiler .getWarnings ())
114
+ .comparingElementsUsing (JSCompCorrespondences .DESCRIPTION_EQUALITY )
115
+ .containsExactly (
116
+ "Bad type annotation. Unknown type UnrecognizedType_module$exports$foo$bar" ,
117
+ "Bad type annotation. Unknown type UnrecognizedType_module$contents$foo$bar_Inner" );
118
+ }
119
+
120
+ @ Test
121
+ public void testFunctionDeclarationInGoogScope_usingGoogModuleGetType () {
122
+ CompilerOptions options = createCompilerOptions ();
123
+ options .setClosurePass (true );
124
+ options .setCheckTypes (true );
125
+ options .setBadRewriteModulesBeforeTypecheckingThatWeWantToGetRidOf (true );
126
+
127
+ compile (
128
+ options ,
129
+ new String [] {
130
+ lines ("goog.module('foo.bar.Types');" , "exports.Klazz = class {};" ),
131
+ lines (
132
+ "goog.requireType('foo.bar.Types');" ,
133
+ "goog.scope(function() {" ,
134
+ "const fooBarTypes = goog.module.get('foo.bar.Types');" ,
135
+ "/** @param {!fooBarTypes.Klazz} k */" ,
136
+ "function f(k) {}" ,
137
+ "f(null);" , // expect a type error here
138
+ "});" )
139
+ });
140
+
141
+ assertThat (lastCompiler .getWarnings ())
142
+ .comparingElementsUsing (JSCompCorrespondences .DESCRIPTION_EQUALITY )
143
+ .containsExactly (
144
+ lines (
145
+ "actual parameter 1 of $jscomp$scope$98477071$0$f does not match formal parameter" ,
146
+ "found : null" ,
147
+ "required: module$exports$foo$bar$Types.Klazz" ));
148
+ assertThat (lastCompiler .getErrors ()).isEmpty ();
149
+ }
150
+
151
+ @ Test
152
+ public void testEsModuleInterop_esModuleUsingGoogRequireType () {
153
+ CompilerOptions options = createCompilerOptions ();
154
+ options .setClosurePass (true );
155
+ options .setCheckTypes (true );
156
+ options .setBadRewriteModulesBeforeTypecheckingThatWeWantToGetRidOf (true );
157
+
158
+ compile (
159
+ options ,
160
+ new String [] {
161
+ lines ("goog.module('foo.bar.Types');" , "exports.Klazz = class {};" ),
162
+ lines (
163
+ "const {Klazz} = goog.requireType('foo.bar.Types');" ,
164
+ "/** @param {!Klazz} k */" ,
165
+ "export function f(k) {}" ,
166
+ "f(null);" // expect a type error here
167
+ )
168
+ });
169
+
170
+ assertThat (lastCompiler .getWarnings ())
171
+ .comparingElementsUsing (JSCompCorrespondences .DESCRIPTION_EQUALITY )
172
+ .containsExactly (
173
+ lines (
174
+ "actual parameter 1 of f$$module$i1 does not match formal parameter" ,
175
+ "found : null" ,
176
+ "required: module$exports$foo$bar$Types.Klazz" ));
177
+ assertThat (lastCompiler .getErrors ()).isEmpty ();
178
+ }
179
+
69
180
@ Test
70
181
public void testProcessDefinesInModule () {
71
182
CompilerOptions options = createCompilerOptions ();
0 commit comments