Skip to content

Commit 5ac4b78

Browse files
committed
Merge branch 'master' into typesCleanup
2 parents 79b7146 + 75364b6 commit 5ac4b78

File tree

76 files changed

+1983
-464
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1983
-464
lines changed

src/compiler/binder.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference path="utilities.ts"/>
12
/// <reference path="parser.ts"/>
23

34
/* @internal */
@@ -6,8 +7,8 @@ namespace ts {
67

78
export const enum ModuleInstanceState {
89
NonInstantiated = 0,
9-
Instantiated = 1,
10-
ConstEnumOnly = 2
10+
Instantiated = 1,
11+
ConstEnumOnly = 2
1112
}
1213

1314
const enum Reachability {
@@ -188,6 +189,11 @@ namespace ts {
188189
}
189190
if (node.name.kind === SyntaxKind.ComputedPropertyName) {
190191
const nameExpression = (<ComputedPropertyName>node.name).expression;
192+
// treat computed property names where expression is string/numeric literal as just string/numeric literal
193+
if (isStringOrNumericLiteral(nameExpression.kind)) {
194+
return (<LiteralExpression>nameExpression).text;
195+
}
196+
191197
Debug.assert(isWellKnownSymbolSyntactically(nameExpression));
192198
return getPropertyNameForKnownSymbolName((<PropertyAccessExpression>nameExpression).name.text);
193199
}
@@ -208,6 +214,9 @@ namespace ts {
208214
return "__export";
209215
case SyntaxKind.ExportAssignment:
210216
return (<ExportAssignment>node).isExportEquals ? "export=" : "default";
217+
case SyntaxKind.BinaryExpression:
218+
// Binary expression case is for JS module 'module.exports = expr'
219+
return "export=";
211220
case SyntaxKind.FunctionDeclaration:
212221
case SyntaxKind.ClassDeclaration:
213222
return node.flags & NodeFlags.Default ? "default" : undefined;
@@ -1069,7 +1078,7 @@ namespace ts {
10691078
return "__" + indexOf((<SignatureDeclaration>node.parent).parameters, node);
10701079
}
10711080

1072-
function bind(node: Node) {
1081+
function bind(node: Node): void {
10731082
if (!node) {
10741083
return;
10751084
}
@@ -1145,9 +1154,18 @@ namespace ts {
11451154

11461155
function bindWorker(node: Node) {
11471156
switch (node.kind) {
1157+
/* Strict mode checks */
11481158
case SyntaxKind.Identifier:
11491159
return checkStrictModeIdentifier(<Identifier>node);
11501160
case SyntaxKind.BinaryExpression:
1161+
if (isInJavaScriptFile(node)) {
1162+
if (isExportsPropertyAssignment(node)) {
1163+
bindExportsPropertyAssignment(<BinaryExpression>node);
1164+
}
1165+
else if (isModuleExportsAssignment(node)) {
1166+
bindModuleExportsAssignment(<BinaryExpression>node);
1167+
}
1168+
}
11511169
return checkStrictModeBinaryExpression(<BinaryExpression>node);
11521170
case SyntaxKind.CatchClause:
11531171
return checkStrictModeCatchClause(<CatchClause>node);
@@ -1213,6 +1231,14 @@ namespace ts {
12131231
checkStrictModeFunctionName(<FunctionExpression>node);
12141232
const bindingName = (<FunctionExpression>node).name ? (<FunctionExpression>node).name.text : "__function";
12151233
return bindAnonymousDeclaration(<FunctionExpression>node, SymbolFlags.Function, bindingName);
1234+
1235+
case SyntaxKind.CallExpression:
1236+
if (isInJavaScriptFile(node)) {
1237+
bindCallExpression(<CallExpression>node);
1238+
}
1239+
break;
1240+
1241+
// Members of classes, interfaces, and modules
12161242
case SyntaxKind.ClassExpression:
12171243
case SyntaxKind.ClassDeclaration:
12181244
return bindClassLikeDeclaration(<ClassLikeDeclaration>node);
@@ -1224,6 +1250,8 @@ namespace ts {
12241250
return bindEnumDeclaration(<EnumDeclaration>node);
12251251
case SyntaxKind.ModuleDeclaration:
12261252
return bindModuleDeclaration(<ModuleDeclaration>node);
1253+
1254+
// Imports and exports
12271255
case SyntaxKind.ImportEqualsDeclaration:
12281256
case SyntaxKind.NamespaceImport:
12291257
case SyntaxKind.ImportSpecifier:
@@ -1243,16 +1271,21 @@ namespace ts {
12431271
function bindSourceFileIfExternalModule() {
12441272
setExportContextFlag(file);
12451273
if (isExternalModule(file)) {
1246-
bindAnonymousDeclaration(file, SymbolFlags.ValueModule, `"${removeFileExtension(file.fileName)}"`);
1274+
bindSourceFileAsExternalModule();
12471275
}
12481276
}
12491277

1250-
function bindExportAssignment(node: ExportAssignment) {
1278+
function bindSourceFileAsExternalModule() {
1279+
bindAnonymousDeclaration(file, SymbolFlags.ValueModule, `"${removeFileExtension(file.fileName) }"`);
1280+
}
1281+
1282+
function bindExportAssignment(node: ExportAssignment | BinaryExpression) {
1283+
const boundExpression = node.kind === SyntaxKind.ExportAssignment ? (<ExportAssignment>node).expression : (<BinaryExpression>node).right;
12511284
if (!container.symbol || !container.symbol.exports) {
12521285
// Export assignment in some sort of block construct
12531286
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node));
12541287
}
1255-
else if (node.expression.kind === SyntaxKind.Identifier) {
1288+
else if (boundExpression.kind === SyntaxKind.Identifier) {
12561289
// An export default clause with an identifier exports all meanings of that identifier
12571290
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Alias, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
12581291
}
@@ -1279,6 +1312,34 @@ namespace ts {
12791312
}
12801313
}
12811314

1315+
function setCommonJsModuleIndicator(node: Node) {
1316+
if (!file.commonJsModuleIndicator) {
1317+
file.commonJsModuleIndicator = node;
1318+
bindSourceFileAsExternalModule();
1319+
}
1320+
}
1321+
1322+
function bindExportsPropertyAssignment(node: BinaryExpression) {
1323+
// When we create a property via 'exports.foo = bar', the 'exports.foo' property access
1324+
// expression is the declaration
1325+
setCommonJsModuleIndicator(node);
1326+
declareSymbol(file.symbol.exports, file.symbol, <PropertyAccessExpression>node.left, SymbolFlags.Property | SymbolFlags.Export, SymbolFlags.None);
1327+
}
1328+
1329+
function bindModuleExportsAssignment(node: BinaryExpression) {
1330+
// 'module.exports = expr' assignment
1331+
setCommonJsModuleIndicator(node);
1332+
bindExportAssignment(node);
1333+
}
1334+
1335+
function bindCallExpression(node: CallExpression) {
1336+
// We're only inspecting call expressions to detect CommonJS modules, so we can skip
1337+
// this check if we've already seen the module indicator
1338+
if (!file.commonJsModuleIndicator && isRequireCall(node)) {
1339+
setCommonJsModuleIndicator(node);
1340+
}
1341+
}
1342+
12821343
function bindClassLikeDeclaration(node: ClassLikeDeclaration) {
12831344
if (node.kind === SyntaxKind.ClassDeclaration) {
12841345
bindBlockScopedDeclaration(node, SymbolFlags.Class, SymbolFlags.ClassExcludes);

0 commit comments

Comments
 (0)