@@ -364,7 +364,7 @@ namespace ts {
364
364
}
365
365
366
366
function isGlobalSourceFile(node: Node) {
367
- return node.kind === SyntaxKind.SourceFile && !isExternalModule (<SourceFile>node);
367
+ return node.kind === SyntaxKind.SourceFile && !isExternalOrCommonJsModule (<SourceFile>node);
368
368
}
369
369
370
370
function getSymbol(symbols: SymbolTable, name: string, meaning: SymbolFlags): Symbol {
@@ -480,7 +480,7 @@ namespace ts {
480
480
}
481
481
switch (location.kind) {
482
482
case SyntaxKind.SourceFile:
483
- if (!isExternalModule (<SourceFile>location)) break;
483
+ if (!isExternalOrCommonJsModule (<SourceFile>location)) break;
484
484
case SyntaxKind.ModuleDeclaration:
485
485
const moduleExports = getSymbolOfNode(location).exports;
486
486
if (location.kind === SyntaxKind.SourceFile ||
@@ -990,11 +990,16 @@ namespace ts {
990
990
991
991
// Module names are escaped in our symbol table. However, string literal values aren't.
992
992
// Escape the name in the "require(...)" clause to ensure we find the right symbol.
993
- const moduleName = escapeIdentifier(moduleReferenceLiteral.text);
993
+ let moduleName = escapeIdentifier(moduleReferenceLiteral.text);
994
994
995
995
if (moduleName === undefined) {
996
996
return;
997
997
}
998
+
999
+ if (moduleName.indexOf("!") >= 0) {
1000
+ moduleName = moduleName.substr(0, moduleName.indexOf("!"));
1001
+ }
1002
+
998
1003
const isRelative = isExternalModuleNameRelative(moduleName);
999
1004
if (!isRelative) {
1000
1005
const symbol = getSymbol(globals, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
@@ -1205,7 +1210,7 @@ namespace ts {
1205
1210
}
1206
1211
switch (location.kind) {
1207
1212
case SyntaxKind.SourceFile:
1208
- if (!isExternalModule (<SourceFile>location)) {
1213
+ if (!isExternalOrCommonJsModule (<SourceFile>location)) {
1209
1214
break;
1210
1215
}
1211
1216
case SyntaxKind.ModuleDeclaration:
@@ -1387,7 +1392,7 @@ namespace ts {
1387
1392
1388
1393
function hasExternalModuleSymbol(declaration: Node) {
1389
1394
return (declaration.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>declaration).name.kind === SyntaxKind.StringLiteral) ||
1390
- (declaration.kind === SyntaxKind.SourceFile && isExternalModule (<SourceFile>declaration));
1395
+ (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule (<SourceFile>declaration));
1391
1396
}
1392
1397
1393
1398
function hasVisibleDeclarations(symbol: Symbol): SymbolVisibilityResult {
@@ -2061,7 +2066,7 @@ namespace ts {
2061
2066
}
2062
2067
}
2063
2068
else if (node.kind === SyntaxKind.SourceFile) {
2064
- return isExternalModule (<SourceFile>node) ? node : undefined;
2069
+ return isExternalOrCommonJsModule (<SourceFile>node) ? node : undefined;
2065
2070
}
2066
2071
}
2067
2072
Debug.fail("getContainingModule cant reach here");
@@ -2182,7 +2187,7 @@ namespace ts {
2182
2187
case SyntaxKind.SourceFile:
2183
2188
return true;
2184
2189
2185
- // Export assignements do not create name bindings outside the module
2190
+ // Export assignments do not create name bindings outside the module
2186
2191
case SyntaxKind.ExportAssignment:
2187
2192
return false;
2188
2193
@@ -2567,6 +2572,14 @@ namespace ts {
2567
2572
if (declaration.kind === SyntaxKind.ExportAssignment) {
2568
2573
return links.type = checkExpression((<ExportAssignment>declaration).expression);
2569
2574
}
2575
+ // Handle module.exports = expr
2576
+ if (declaration.kind === SyntaxKind.BinaryExpression) {
2577
+ return links.type = checkExpression((<BinaryExpression>declaration).right);
2578
+ }
2579
+ // Handle exports.p = expr
2580
+ if (declaration.kind === SyntaxKind.PropertyAccessExpression) {
2581
+ return checkExpressionCached((<BinaryExpression>declaration.parent).right);
2582
+ }
2570
2583
// Handle variable, parameter or property
2571
2584
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
2572
2585
return unknownType;
@@ -3841,6 +3854,18 @@ namespace ts {
3841
3854
return result;
3842
3855
}
3843
3856
3857
+ function resolveExternalModuleTypeByLiteral(name: StringLiteral) {
3858
+ const moduleSym = resolveExternalModuleName(name, name);
3859
+ if (moduleSym) {
3860
+ const resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym);
3861
+ if (resolvedModuleSymbol) {
3862
+ return getTypeOfSymbol(resolvedModuleSymbol);
3863
+ }
3864
+ }
3865
+
3866
+ return anyType;
3867
+ }
3868
+
3844
3869
function getReturnTypeOfSignature(signature: Signature): Type {
3845
3870
if (!signature.resolvedReturnType) {
3846
3871
if (!pushTypeResolution(signature, TypeSystemPropertyName.ResolvedReturnType)) {
@@ -9426,6 +9451,12 @@ namespace ts {
9426
9451
return anyType;
9427
9452
}
9428
9453
}
9454
+
9455
+ // In JavaScript files, calls to any identifier 'require' are treated as external module imports
9456
+ if (isInJavaScriptFile(node) && isRequireCall(node)) {
9457
+ return resolveExternalModuleTypeByLiteral(<StringLiteral>node.arguments[0]);
9458
+ }
9459
+
9429
9460
return getReturnTypeOfSignature(signature);
9430
9461
}
9431
9462
@@ -12067,7 +12098,7 @@ namespace ts {
12067
12098
12068
12099
// In case of variable declaration, node.parent is variable statement so look at the variable statement's parent
12069
12100
const parent = getDeclarationContainer(node);
12070
- if (parent.kind === SyntaxKind.SourceFile && isExternalModule (<SourceFile>parent)) {
12101
+ if (parent.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule (<SourceFile>parent)) {
12071
12102
// If the declaration happens to be in external module, report error that require and exports are reserved keywords
12072
12103
error(name, Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module,
12073
12104
declarationNameToString(name), declarationNameToString(name));
@@ -14150,7 +14181,7 @@ namespace ts {
14150
14181
forEach(node.statements, checkSourceElement);
14151
14182
checkFunctionAndClassExpressionBodies(node);
14152
14183
14153
- if (isExternalModule (node)) {
14184
+ if (isExternalOrCommonJsModule (node)) {
14154
14185
checkExternalModuleExports(node);
14155
14186
}
14156
14187
@@ -14253,7 +14284,7 @@ namespace ts {
14253
14284
14254
14285
switch (location.kind) {
14255
14286
case SyntaxKind.SourceFile:
14256
- if (!isExternalModule (<SourceFile>location)) {
14287
+ if (!isExternalOrCommonJsModule (<SourceFile>location)) {
14257
14288
break;
14258
14289
}
14259
14290
case SyntaxKind.ModuleDeclaration:
@@ -14999,16 +15030,16 @@ namespace ts {
14999
15030
15000
15031
// Initialize global symbol table
15001
15032
forEach(host.getSourceFiles(), file => {
15002
- if (!isExternalModule (file)) {
15033
+ if (!isExternalOrCommonJsModule (file)) {
15003
15034
mergeSymbolTable(globals, file.locals);
15004
15035
}
15005
15036
});
15006
15037
15007
- // Initialize special symbols
15008
15038
getSymbolLinks(undefinedSymbol).type = undefinedType;
15009
15039
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments");
15010
15040
getSymbolLinks(unknownSymbol).type = unknownType;
15011
15041
globals[undefinedSymbol.name] = undefinedSymbol;
15042
+
15012
15043
// Initialize special types
15013
15044
globalArrayType = <GenericType>getGlobalType("Array", /*arity*/ 1);
15014
15045
globalObjectType = getGlobalType("Object");
0 commit comments