@@ -364,7 +364,7 @@ namespace ts {
364364        }
365365
366366        function isGlobalSourceFile(node: Node) {
367-             return node.kind === SyntaxKind.SourceFile && !isExternalModule (<SourceFile>node);
367+             return node.kind === SyntaxKind.SourceFile && !isExternalOrCommonJsModule (<SourceFile>node);
368368        }
369369
370370        function getSymbol(symbols: SymbolTable, name: string, meaning: SymbolFlags): Symbol {
@@ -480,7 +480,7 @@ namespace ts {
480480                }
481481                switch (location.kind) {
482482                    case SyntaxKind.SourceFile:
483-                         if (!isExternalModule (<SourceFile>location)) break;
483+                         if (!isExternalOrCommonJsModule (<SourceFile>location)) break;
484484                    case SyntaxKind.ModuleDeclaration:
485485                        const moduleExports = getSymbolOfNode(location).exports;
486486                        if (location.kind === SyntaxKind.SourceFile ||
@@ -990,11 +990,16 @@ namespace ts {
990990
991991            // Module names are escaped in our symbol table.  However, string literal values aren't.
992992            // 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);
994994
995995            if (moduleName === undefined) {
996996                return;
997997            }
998+ 
999+             if (moduleName.indexOf("!") >= 0) {
1000+                 moduleName = moduleName.substr(0, moduleName.indexOf("!"));
1001+             }
1002+ 
9981003            const isRelative = isExternalModuleNameRelative(moduleName);
9991004            if (!isRelative) {
10001005                const symbol = getSymbol(globals, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
@@ -1205,7 +1210,7 @@ namespace ts {
12051210                }
12061211                switch (location.kind) {
12071212                    case SyntaxKind.SourceFile:
1208-                         if (!isExternalModule (<SourceFile>location)) {
1213+                         if (!isExternalOrCommonJsModule (<SourceFile>location)) {
12091214                            break;
12101215                        }
12111216                    case SyntaxKind.ModuleDeclaration:
@@ -1387,7 +1392,7 @@ namespace ts {
13871392
13881393        function hasExternalModuleSymbol(declaration: Node) {
13891394            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));
13911396        }
13921397
13931398        function hasVisibleDeclarations(symbol: Symbol): SymbolVisibilityResult {
@@ -2061,7 +2066,7 @@ namespace ts {
20612066                        }
20622067                    }
20632068                    else if (node.kind === SyntaxKind.SourceFile) {
2064-                         return isExternalModule (<SourceFile>node) ? node : undefined;
2069+                         return isExternalOrCommonJsModule (<SourceFile>node) ? node : undefined;
20652070                    }
20662071                }
20672072                Debug.fail("getContainingModule cant reach here");
@@ -2182,7 +2187,7 @@ namespace ts {
21822187                    case SyntaxKind.SourceFile:
21832188                        return true;
21842189
2185-                     // Export assignements  do not create name bindings outside the module
2190+                     // Export assignments  do not create name bindings outside the module
21862191                    case SyntaxKind.ExportAssignment:
21872192                        return false;
21882193
@@ -2567,6 +2572,14 @@ namespace ts {
25672572                if (declaration.kind === SyntaxKind.ExportAssignment) {
25682573                    return links.type = checkExpression((<ExportAssignment>declaration).expression);
25692574                }
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+                 }
25702583                // Handle variable, parameter or property
25712584                if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
25722585                    return unknownType;
@@ -3841,6 +3854,18 @@ namespace ts {
38413854            return result;
38423855        }
38433856
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+ 
38443869        function getReturnTypeOfSignature(signature: Signature): Type {
38453870            if (!signature.resolvedReturnType) {
38463871                if (!pushTypeResolution(signature, TypeSystemPropertyName.ResolvedReturnType)) {
@@ -9426,6 +9451,12 @@ namespace ts {
94269451                    return anyType;
94279452                }
94289453            }
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+ 
94299460            return getReturnTypeOfSignature(signature);
94309461        }
94319462
@@ -12067,7 +12098,7 @@ namespace ts {
1206712098
1206812099            // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent
1206912100            const parent = getDeclarationContainer(node);
12070-             if (parent.kind === SyntaxKind.SourceFile && isExternalModule (<SourceFile>parent)) {
12101+             if (parent.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule (<SourceFile>parent)) {
1207112102                // If the declaration happens to be in external module, report error that require and exports are reserved keywords
1207212103                error(name, Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module,
1207312104                    declarationNameToString(name), declarationNameToString(name));
@@ -14150,7 +14181,7 @@ namespace ts {
1415014181                forEach(node.statements, checkSourceElement);
1415114182                checkFunctionAndClassExpressionBodies(node);
1415214183
14153-                 if (isExternalModule (node)) {
14184+                 if (isExternalOrCommonJsModule (node)) {
1415414185                    checkExternalModuleExports(node);
1415514186                }
1415614187
@@ -14253,7 +14284,7 @@ namespace ts {
1425314284
1425414285                    switch (location.kind) {
1425514286                        case SyntaxKind.SourceFile:
14256-                             if (!isExternalModule (<SourceFile>location)) {
14287+                             if (!isExternalOrCommonJsModule (<SourceFile>location)) {
1425714288                                break;
1425814289                            }
1425914290                        case SyntaxKind.ModuleDeclaration:
@@ -14999,16 +15030,16 @@ namespace ts {
1499915030
1500015031            // Initialize global symbol table
1500115032            forEach(host.getSourceFiles(), file => {
15002-                 if (!isExternalModule (file)) {
15033+                 if (!isExternalOrCommonJsModule (file)) {
1500315034                    mergeSymbolTable(globals, file.locals);
1500415035                }
1500515036            });
1500615037
15007-             // Initialize special symbols
1500815038            getSymbolLinks(undefinedSymbol).type = undefinedType;
1500915039            getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments");
1501015040            getSymbolLinks(unknownSymbol).type = unknownType;
1501115041            globals[undefinedSymbol.name] = undefinedSymbol;
15042+ 
1501215043            // Initialize special types
1501315044            globalArrayType = <GenericType>getGlobalType("Array", /*arity*/ 1);
1501415045            globalObjectType = getGlobalType("Object");
0 commit comments