Skip to content

Commit 9f26803

Browse files
committed
Merge branch 'master' of https://github.com/Microsoft/TypeScript into deeplyNestedTypeArgumentInference
2 parents f2e04f2 + 29fcd4a commit 9f26803

File tree

53 files changed

+777
-202
lines changed

Some content is hidden

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

53 files changed

+777
-202
lines changed

src/compiler/binder.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,11 @@ module ts {
661661
}
662662

663663
function bindExportAssignment(node: ExportAssignment) {
664-
if (node.expression.kind === SyntaxKind.Identifier) {
664+
if (!container.symbol || !container.symbol.exports) {
665+
// Export assignment in some sort of block construct
666+
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node));
667+
}
668+
else if (node.expression.kind === SyntaxKind.Identifier) {
665669
// An export default clause with an identifier exports all meanings of that identifier
666670
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Alias, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
667671
}
@@ -672,7 +676,11 @@ module ts {
672676
}
673677

674678
function bindExportDeclaration(node: ExportDeclaration) {
675-
if (!node.exportClause) {
679+
if (!container.symbol || !container.symbol.exports) {
680+
// Export * in some sort of block construct
681+
bindAnonymousDeclaration(node, SymbolFlags.ExportStar, getDeclarationName(node));
682+
}
683+
else if (!node.exportClause) {
676684
// All export * declarations are collected in an __export symbol
677685
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.ExportStar, SymbolFlags.None);
678686
}

src/compiler/checker.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ module ts {
13581358
return result;
13591359
}
13601360

1361-
function signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
1361+
function signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
13621362
let writer = getSingleLineStringWriter();
13631363
getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags);
13641364
let result = writer.string();
@@ -3603,7 +3603,8 @@ module ts {
36033603
function getTypeFromTypeReference(node: TypeReferenceNode | ExpressionWithTypeArguments): Type {
36043604
let links = getNodeLinks(node);
36053605
if (!links.resolvedType) {
3606-
// We only support expressions that are simple qualified names. For other expressions this produces undefined. let typeNameOrExpression = node.kind === SyntaxKind.TypeReference ? (<TypeReferenceNode>node).typeName :
3606+
// We only support expressions that are simple qualified names. For other expressions this produces undefined.
3607+
let typeNameOrExpression = node.kind === SyntaxKind.TypeReference ? (<TypeReferenceNode>node).typeName :
36073608
isSupportedExpressionWithTypeArguments(<ExpressionWithTypeArguments>node) ? (<ExpressionWithTypeArguments>node).expression :
36083609
undefined;
36093610
let symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, SymbolFlags.Type) || unknownSymbol;
@@ -5245,14 +5246,14 @@ module ts {
52455246
if (source.typePredicate && target.typePredicate) {
52465247
if (target.typePredicate.parameterIndex === source.typePredicate.parameterIndex) {
52475248
// Return types from type predicates are treated as booleans. In order to infer types
5248-
// from type predicates we would need to infer from the type of type predicates. Since
5249-
// we can't infer any type information from the return types — we can just add a return
5250-
// statement after the below infer type statement.
5249+
// from type predicates we would need to infer using the type within the type predicate
5250+
// (i.e. 'Foo' from 'x is Foo').
52515251
inferFromTypes(source.typePredicate.type, target.typePredicate.type);
52525252
}
5253-
return;
52545253
}
5255-
inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
5254+
else {
5255+
inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target));
5256+
}
52565257
}
52575258

52585259
function inferFromIndexTypes(source: Type, target: Type, sourceKind: IndexKind, targetKind: IndexKind) {
@@ -11103,6 +11104,15 @@ module ts {
1110311104
function checkModuleDeclaration(node: ModuleDeclaration) {
1110411105
if (produceDiagnostics) {
1110511106
// Grammar checking
11107+
let isAmbientExternalModule = node.name.kind === SyntaxKind.StringLiteral;
11108+
let contextErrorMessage = isAmbientExternalModule
11109+
? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file
11110+
: Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module;
11111+
if (checkGrammarModuleElementContext(node, contextErrorMessage)) {
11112+
// If we hit a module declaration in an illegal context, just bail out to avoid cascading errors.
11113+
return;
11114+
}
11115+
1110611116
if (!checkGrammarDeclarationNameInStrictMode(node) && !checkGrammarDecorators(node) && !checkGrammarModifiers(node)) {
1110711117
if (!isInAmbientContext(node) && node.name.kind === SyntaxKind.StringLiteral) {
1110811118
grammarErrorOnNode(node.name, Diagnostics.Only_ambient_modules_can_use_quoted_names);
@@ -11139,7 +11149,7 @@ module ts {
1113911149
}
1114011150

1114111151
// Checks for ambient external modules.
11142-
if (node.name.kind === SyntaxKind.StringLiteral) {
11152+
if (isAmbientExternalModule) {
1114311153
if (!isGlobalSourceFile(node.parent)) {
1114411154
error(node.name, Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules);
1114511155
}
@@ -11215,6 +11225,10 @@ module ts {
1121511225
}
1121611226

1121711227
function checkImportDeclaration(node: ImportDeclaration) {
11228+
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
11229+
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
11230+
return;
11231+
}
1121811232
if (!checkGrammarImportDeclarationNameInStrictMode(node) && !checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
1121911233
grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers);
1122011234
}
@@ -11237,6 +11251,11 @@ module ts {
1123711251
}
1123811252

1123911253
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
11254+
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
11255+
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
11256+
return;
11257+
}
11258+
1124011259
checkGrammarDeclarationNameInStrictMode(node) || checkGrammarDecorators(node) || checkGrammarModifiers(node);
1124111260
if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
1124211261
checkImportBinding(node);
@@ -11268,9 +11287,15 @@ module ts {
1126811287
}
1126911288

1127011289
function checkExportDeclaration(node: ExportDeclaration) {
11290+
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) {
11291+
// If we hit an export in an illegal context, just bail out to avoid cascading errors.
11292+
return;
11293+
}
11294+
1127111295
if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
1127211296
grammarErrorOnFirstToken(node, Diagnostics.An_export_declaration_cannot_have_modifiers);
1127311297
}
11298+
1127411299
if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) {
1127511300
if (node.exportClause) {
1127611301
// export { x, y }
@@ -11292,6 +11317,12 @@ module ts {
1129211317
}
1129311318
}
1129411319

11320+
function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean {
11321+
if (node.parent.kind !== SyntaxKind.SourceFile && node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.ModuleDeclaration) {
11322+
return grammarErrorOnFirstToken(node, errorMessage);
11323+
}
11324+
}
11325+
1129511326
function checkExportSpecifier(node: ExportSpecifier) {
1129611327
checkAliasSymbol(node);
1129711328
if (!(<ExportDeclaration>node.parent.parent).moduleSpecifier) {
@@ -11300,6 +11331,11 @@ module ts {
1130011331
}
1130111332

1130211333
function checkExportAssignment(node: ExportAssignment) {
11334+
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) {
11335+
// If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
11336+
return;
11337+
}
11338+
1130311339
let container = node.parent.kind === SyntaxKind.SourceFile ? <SourceFile>node.parent : <ModuleDeclaration>node.parent.parent;
1130411340
if (container.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>container).name.kind === SyntaxKind.Identifier) {
1130511341
error(node, Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace);
@@ -11315,7 +11351,8 @@ module ts {
1131511351
else {
1131611352
checkExpressionCached(node.expression);
1131711353
}
11318-
checkExternalModuleExports(container);
11354+
11355+
checkExternalModuleExports(<SourceFile | ModuleDeclaration>container);
1131911356

1132011357
if (node.isExportEquals && !isInAmbientContext(node)) {
1132111358
if (languageVersion >= ScriptTarget.ES6) {
@@ -11329,7 +11366,7 @@ module ts {
1132911366
}
1133011367
}
1133111368

11332-
function getModuleStatements(node: Declaration): ModuleElement[] {
11369+
function getModuleStatements(node: Declaration): Statement[] {
1133311370
if (node.kind === SyntaxKind.SourceFile) {
1133411371
return (<SourceFile>node).statements;
1133511372
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ module ts {
186186
A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods: { code: 1228, category: DiagnosticCategory.Error, key: "A type predicate is only allowed in return type position for functions and methods." },
187187
A_type_predicate_cannot_reference_a_rest_parameter: { code: 1229, category: DiagnosticCategory.Error, key: "A type predicate cannot reference a rest parameter." },
188188
A_type_predicate_cannot_reference_element_0_in_a_binding_pattern: { code: 1230, category: DiagnosticCategory.Error, key: "A type predicate cannot reference element '{0}' in a binding pattern." },
189+
An_export_assignment_can_only_be_used_in_a_module: { code: 1231, category: DiagnosticCategory.Error, key: "An export assignment can only be used in a module." },
190+
An_import_declaration_can_only_be_used_in_a_namespace_or_module: { code: 1232, category: DiagnosticCategory.Error, key: "An import declaration can only be used in a namespace or module." },
191+
An_export_declaration_can_only_be_used_in_a_module: { code: 1233, category: DiagnosticCategory.Error, key: "An export declaration can only be used in a module." },
192+
An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file: { code: 1234, category: DiagnosticCategory.Error, key: "An ambient module declaration is only allowed at the top level in a file." },
193+
A_namespace_declaration_is_only_allowed_in_a_namespace_or_module: { code: 1235, category: DiagnosticCategory.Error, key: "A namespace declaration is only allowed in a namespace or module." },
189194
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
190195
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
191196
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,26 @@
731731
"category": "Error",
732732
"code": 1230
733733
},
734+
"An export assignment can only be used in a module.": {
735+
"category": "Error",
736+
"code": 1231
737+
},
738+
"An import declaration can only be used in a namespace or module.": {
739+
"category": "Error",
740+
"code": 1232
741+
},
742+
"An export declaration can only be used in a module.": {
743+
"category": "Error",
744+
"code": 1233
745+
},
746+
"An ambient module declaration is only allowed at the top level in a file.": {
747+
"category": "Error",
748+
"code": 1234
749+
},
750+
"A namespace declaration is only allowed in a namespace or module.": {
751+
"category": "Error",
752+
"code": 1235
753+
},
734754

735755

736756
"Duplicate identifier '{0}'.": {

src/compiler/emitter.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,23 +1903,21 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
19031903
function emitNewExpression(node: NewExpression) {
19041904
write("new ");
19051905

1906-
// Spread operator logic can be supported in new expressions in ES5 using a combination
1906+
// Spread operator logic is supported in new expressions in ES5 using a combination
19071907
// of Function.prototype.bind() and Function.prototype.apply().
19081908
//
19091909
// Example:
19101910
//
1911-
// var arguments = [1, 2, 3, 4, 5];
1912-
// new Array(...arguments);
1911+
// var args = [1, 2, 3, 4, 5];
1912+
// new Array(...args);
19131913
//
1914-
// Could be transpiled into ES5:
1914+
// is compiled into the following ES5:
19151915
//
1916-
// var arguments = [1, 2, 3, 4, 5];
1917-
// new (Array.bind.apply(Array, [void 0].concat(arguments)));
1916+
// var args = [1, 2, 3, 4, 5];
1917+
// new (Array.bind.apply(Array, [void 0].concat(args)));
19181918
//
1919-
// `[void 0]` is the first argument which represents `thisArg` to the bind method above.
1920-
// And `thisArg` will be set to the return value of the constructor when instantiated
1921-
// with the new operator — regardless of any value we set `thisArg` to. Thus, we set it
1922-
// to an undefined, `void 0`.
1919+
// The 'thisArg' to 'bind' is ignored when invoking the result of 'bind' with 'new',
1920+
// Thus, we set it to undefined ('void 0').
19231921
if (languageVersion === ScriptTarget.ES5 &&
19241922
node.arguments &&
19251923
hasSpreadElement(node.arguments)) {

0 commit comments

Comments
 (0)