Skip to content

Commit e82e841

Browse files
committed
Added emit for async functions in ES6
1 parent d19a200 commit e82e841

File tree

280 files changed

+3208
-767
lines changed

Some content is hidden

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

280 files changed

+3208
-767
lines changed

Diff for: src/compiler/checker.ts

+42-97
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module ts {
102102
let globalArraySymbol: Symbol;
103103
let globalESSymbolConstructorSymbol: Symbol;
104104

105-
let getGlobalPromiseSymbol: () => Symbol;
105+
let getGlobalPromiseConstructorSymbol: () => Symbol;
106106

107107
let globalObjectType: ObjectType;
108108
let globalFunctionType: ObjectType;
@@ -139,7 +139,6 @@ module ts {
139139
let symbolLinks: SymbolLinks[] = [];
140140
let nodeLinks: NodeLinks[] = [];
141141
let potentialThisCollisions: Node[] = [];
142-
var potentialArgumentsCollisions: Node[] = [];
143142

144143
let diagnostics = createDiagnosticCollection();
145144

@@ -5420,11 +5419,13 @@ module ts {
54205419
// can explicitly bound arguments objects
54215420
let container = getContainingFunction(node);
54225421
if (symbol === argumentsSymbol) {
5423-
if (container.kind === SyntaxKind.ArrowFunction && languageVersion < ScriptTarget.ES6) {
5424-
error(node, Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression);
5425-
}
5426-
else if (node.parserContextFlags & ParserContextFlags.Await) {
5427-
captureLexicalArguments(node, container);
5422+
if (container.kind === SyntaxKind.ArrowFunction) {
5423+
if (languageVersion < ScriptTarget.ES6) {
5424+
error(node, Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression);
5425+
}
5426+
else if (node.parserContextFlags & ParserContextFlags.Await) {
5427+
error(node, Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression);
5428+
}
54285429
}
54295430
}
54305431

@@ -5434,7 +5435,6 @@ module ts {
54345435

54355436
checkCollisionWithCapturedSuperVariable(node, node);
54365437
checkCollisionWithCapturedThisVariable(node, node);
5437-
checkCollisionWithCapturedArgumentsVariable(node, node);
54385438
checkBlockScopedBindingCapturedInLoop(node, symbol);
54395439

54405440
return getNarrowedTypeOfSymbol(getExportSymbolOfValueSymbolIfExported(symbol), node);
@@ -5516,10 +5516,9 @@ module ts {
55165516
// When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code
55175517
needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES6);
55185518
}
5519-
5520-
if (node.parserContextFlags & ParserContextFlags.Await) {
5519+
else if (node.parserContextFlags & ParserContextFlags.Await) {
55215520
// if 'this' is part of an async function, we will need to capture 'this'
5522-
needToCaptureLexicalThis = true;
5521+
needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES6);
55235522
}
55245523

55255524
switch (container.kind) {
@@ -5561,14 +5560,6 @@ module ts {
55615560
return anyType;
55625561
}
55635562

5564-
function captureLexicalArguments(node: Node, container: Node): void {
5565-
if (node.parent.kind !== SyntaxKind.Parameter) {
5566-
getNodeLinks(node).flags |= NodeCheckFlags.LexicalArguments;
5567-
}
5568-
5569-
getNodeLinks(container).flags |= NodeCheckFlags.CaptureArguments;
5570-
}
5571-
55725563
function isInConstructorArgumentInitializer(node: Node, constructorDecl: Node): boolean {
55735564
for (let n = node; n && n !== constructorDecl; n = n.parent) {
55745565
if (n.kind === SyntaxKind.Parameter) {
@@ -7303,7 +7294,7 @@ module ts {
73037294
let widenedType = getWidenedType(type);
73047295
return isAsync
73057296
? createPromiseType(widenedType, func)
7306-
: type;
7297+
: widenedType;
73077298
}
73087299

73097300
/// Returns a set of types relating to every return expression relating to a function block.
@@ -7421,7 +7412,6 @@ module ts {
74217412
if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) {
74227413
checkCollisionWithCapturedSuperVariable(node, (<FunctionExpression>node).name);
74237414
checkCollisionWithCapturedThisVariable(node, (<FunctionExpression>node).name);
7424-
checkCollisionWithCapturedArgumentsVariable(node, (<FunctionExpression>node).name);
74257415
}
74267416

74277417
return type;
@@ -7574,13 +7564,7 @@ module ts {
75747564
function checkAwaitExpression(node: AwaitExpression): Type {
75757565
// Grammar checking
75767566
if (!(node.parserContextFlags & ParserContextFlags.Await)) {
7577-
var parameter = getContainingParameter(node);
7578-
if (parameter && parameter.parserContextFlags & ParserContextFlags.Await) {
7579-
grammarErrorAfterFirstToken(node, Diagnostics._0_expression_is_not_allowed_in_an_initializer, "await");
7580-
}
7581-
else {
7582-
grammarErrorOnFirstToken(node, Diagnostics.await_expression_must_be_contained_within_an_async_function);
7583-
}
7567+
grammarErrorOnFirstToken(node, Diagnostics.await_expression_must_be_contained_within_an_async_function);
75847568
}
75857569

75867570
var operandType = checkExpression(node.expression);
@@ -7994,13 +7978,7 @@ module ts {
79947978
function checkYieldExpression(node: YieldExpression): void {
79957979
// Grammar checking
79967980
if (!(node.parserContextFlags & ParserContextFlags.Yield)) {
7997-
let parameter = getContainingParameter(node);
7998-
if (parameter && (parameter.parserContextFlags & ParserContextFlags.GeneratorParameter)) {
7999-
grammarErrorAfterFirstToken(node, Diagnostics._0_expression_is_not_allowed_in_an_initializer, "yield");
8000-
}
8001-
else {
8002-
grammarErrorOnFirstToken(node, Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration);
8003-
}
7981+
grammarErrorOnFirstToken(node, Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration);
80047982
}
80057983
else {
80067984
grammarErrorOnFirstToken(node, Diagnostics.yield_expressions_are_not_currently_supported);
@@ -8907,23 +8885,25 @@ module ts {
89078885
// the "promised type" of a type is the type of the "value" argument of the "onfulfilled" callback.
89088886
let globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType();
89098887
if (globalPromiseLikeType !== emptyObjectType && isTypeAssignableTo(type, globalPromiseLikeType)) {
8910-
let awaitedTypes: Type[] = [];
89118888
let thenProp = getPropertyOfType(type, "then");
8912-
let thenType = getTypeOfSymbol(thenProp);
8913-
let thenSignatures = getSignaturesOfType(thenType, SignatureKind.Call);
8914-
for (let thenSignature of thenSignatures) {
8915-
thenSignature = getErasedSignature(thenSignature);
8916-
let onfulfilledParameterType = getTypeAtPosition(thenSignature, 0);
8917-
let onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, SignatureKind.Call);
8918-
for (let onfulfilledParameterSignature of onfulfilledParameterSignatures) {
8919-
let valueParameterType = getTypeAtPosition(onfulfilledParameterSignature, 0);
8920-
if (valueParameterType !== type) {
8921-
awaitedTypes.push(valueParameterType);
8889+
if (thenProp) {
8890+
let awaitedTypes: Type[] = [];
8891+
let thenType = getTypeOfSymbol(thenProp);
8892+
let thenSignatures = getSignaturesOfType(thenType, SignatureKind.Call);
8893+
for (let thenSignature of thenSignatures) {
8894+
thenSignature = getErasedSignature(thenSignature);
8895+
let onfulfilledParameterType = getTypeAtPosition(thenSignature, 0);
8896+
let onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, SignatureKind.Call);
8897+
for (let onfulfilledParameterSignature of onfulfilledParameterSignatures) {
8898+
let valueParameterType = getTypeAtPosition(onfulfilledParameterSignature, 0);
8899+
if (valueParameterType !== type) {
8900+
awaitedTypes.push(valueParameterType);
8901+
}
89228902
}
89238903
}
8924-
}
8925-
8926-
return getUnionType(awaitedTypes);
8904+
8905+
return getUnionType(awaitedTypes);
8906+
}
89278907
}
89288908

89298909
return emptyObjectType;
@@ -9142,7 +9122,6 @@ module ts {
91429122

91439123
checkCollisionWithCapturedSuperVariable(node, node.name);
91449124
checkCollisionWithCapturedThisVariable(node, node.name);
9145-
checkCollisionWithCapturedArgumentsVariable(node, node.name);
91469125
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
91479126
}
91489127
}
@@ -9262,12 +9241,6 @@ module ts {
92629241
}
92639242
}
92649243

9265-
function checkCollisionWithCapturedArgumentsVariable(node: Node, name: Identifier): void {
9266-
if (needCollisionCheckForIdentifier(node, name, "_arguments")) {
9267-
potentialArgumentsCollisions.push(node);
9268-
}
9269-
}
9270-
92719244
// this function will run after checking the source file so 'CaptureThis' is correct for all nodes
92729245
function checkIfThisIsCapturedInEnclosingScope(node: Node): void {
92739246
let current = node;
@@ -9286,25 +9259,6 @@ module ts {
92869259
}
92879260
}
92889261

9289-
function checkIfArgumentsIsCapturedInEnclosingScope(node: Node): void {
9290-
let current = node;
9291-
while (current) {
9292-
if (getNodeCheckFlags(current) & NodeCheckFlags.CaptureArguments) {
9293-
let isDeclaration = node.kind !== SyntaxKind.Identifier;
9294-
if (isDeclaration) {
9295-
error((<Declaration>node).name, Diagnostics.Duplicate_identifier_arguments_Compiler_uses_variable_declaration_arguments_to_capture_arguments_reference);
9296-
}
9297-
else {
9298-
error(node, Diagnostics.Expression_resolves_to_variable_declaration_arguments_that_compiler_uses_to_capture_arguments_reference);
9299-
}
9300-
9301-
return;
9302-
}
9303-
9304-
current = current.parent;
9305-
}
9306-
}
9307-
93089262
function checkCollisionWithCapturedSuperVariable(node: Node, name: Identifier) {
93099263
if (!needCollisionCheckForIdentifier(node, name, "_super")) {
93109264
return;
@@ -9421,15 +9375,17 @@ module ts {
94219375
}
94229376

94239377
function getPromiseConstructor(node: SignatureDeclaration): EntityName {
9424-
if (node.type && node.type.kind === SyntaxKind.TypeReference) {
9425-
return (<TypeReferenceNode>node.type).typeName;
9426-
}
9427-
9428-
let globalPromiseSymbol = getGlobalPromiseSymbol();
9429-
if (globalPromiseSymbol && globalPromiseSymbol === resolveName(node, "Promise", SymbolFlags.Value, undefined, undefined)) {
9430-
return <Identifier>globalPromiseSymbol.valueDeclaration.name;
9378+
if (isAsyncFunctionLike(node)) {
9379+
let links = getNodeLinks(node);
9380+
if (!links.promiseConstructor) {
9381+
if (node.type && node.type.kind === SyntaxKind.TypeReference) {
9382+
links.promiseConstructor = (<TypeReferenceNode>node.type).typeName;
9383+
}
9384+
}
9385+
9386+
return links.promiseConstructor;
94319387
}
9432-
9388+
94339389
return undefined;
94349390
}
94359391

@@ -9550,14 +9506,8 @@ module ts {
95509506
}
95519507
checkCollisionWithCapturedSuperVariable(node, <Identifier>node.name);
95529508
checkCollisionWithCapturedThisVariable(node, <Identifier>node.name);
9553-
checkCollisionWithCapturedArgumentsVariable(node, <Identifier>node.name);
95549509
checkCollisionWithRequireExportsInGeneratedCode(node, <Identifier>node.name);
95559510
}
9556-
9557-
if (symbol === argumentsSymbol && (node.parserContextFlags & ParserContextFlags.Await)) {
9558-
let container = getContainingFunction(node);
9559-
captureLexicalArguments(node.name, container);
9560-
}
95619511
}
95629512

95639513
function checkVariableDeclaration(node: VariableDeclaration) {
@@ -11258,7 +11208,6 @@ module ts {
1125811208
emitDecorate = false;
1125911209
emitParam = false;
1126011210
potentialThisCollisions.length = 0;
11261-
potentialArgumentsCollisions.length = 0;
1126211211

1126311212
forEach(node.statements, checkSourceElement);
1126411213
checkFunctionExpressionBodies(node);
@@ -11272,11 +11221,6 @@ module ts {
1127211221
potentialThisCollisions.length = 0;
1127311222
}
1127411223

11275-
if (potentialArgumentsCollisions.length) {
11276-
forEach(potentialArgumentsCollisions, checkIfArgumentsIsCapturedInEnclosingScope);
11277-
potentialArgumentsCollisions.length = 0;
11278-
}
11279-
1128011224
if (emitExtends) {
1128111225
links.flags |= NodeCheckFlags.EmitExtends;
1128211226
}
@@ -12279,6 +12223,7 @@ module ts {
1227912223
serializeTypeOfNode,
1228012224
serializeParameterTypesOfNode,
1228112225
serializeReturnTypeOfNode,
12226+
getPromiseConstructor,
1228212227
};
1228312228
}
1228412229

@@ -12313,10 +12258,10 @@ module ts {
1231312258
getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator"));
1231412259
getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator"));
1231512260
getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator"));
12316-
getGlobalPromiseSymbol = memoize(() => getGlobalTypeSymbol("Promise"));
12317-
getGlobalPromiseType = memoize(() => getTypeOfGlobalSymbol(getGlobalPromiseSymbol(), /*arity*/ 1));
12261+
getGlobalPromiseType = memoize(() => getGlobalType("Promise", /*arity*/ 1));
1231812262
getGlobalPromiseLikeType = memoize(() => getGlobalType("PromiseLike", /*arity*/ 1));
1231912263
getInstantiatedGlobalPromiseLikeType = memoize(createInstantiatedPromiseLikeType);
12264+
getGlobalPromiseConstructorSymbol = memoize(() => getGlobalValueSymbol("Promise"));
1232012265
getGlobalPromiseConstructorLikeType = memoize(() => getGlobalType("PromiseConstructorLike"));
1232112266
getGlobalThenableType = memoize(createThenableType);
1232212267

Diff for: src/compiler/diagnosticInformationMap.generated.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ module ts {
383383
A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." },
384384
Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions: { code: 2520, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'. Compiler uses declaration '{1}' to support async functions." },
385385
Expression_resolves_to_variable_declaration_0_that_compiler_uses_to_support_async_functions: { code: 2521, category: DiagnosticCategory.Error, key: "Expression resolves to variable declaration '{0}' that compiler uses to support async functions." },
386-
Duplicate_identifier_arguments_Compiler_uses_variable_declaration_arguments_to_capture_arguments_reference: { code: 2522, category: DiagnosticCategory.Error, key: "Duplicate identifier '_arguments'. Compiler uses variable declaration '_arguments' to capture 'arguments' reference." },
387-
Expression_resolves_to_variable_declaration_arguments_that_compiler_uses_to_capture_arguments_reference: { code: 2523, category: DiagnosticCategory.Error, key: "Expression resolves to variable declaration '_arguments' that compiler uses to capture 'arguments' reference." },
386+
The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression: { code: 2522, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an async arrow function Consider using a standard async function expression." },
388387
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
389388
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
390389
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },

Diff for: src/compiler/diagnosticMessages.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -1522,14 +1522,10 @@
15221522
"category": "Error",
15231523
"code": 2521
15241524
},
1525-
"Duplicate identifier '_arguments'. Compiler uses variable declaration '_arguments' to capture 'arguments' reference.": {
1525+
"The 'arguments' object cannot be referenced in an async arrow function Consider using a standard async function expression.": {
15261526
"category": "Error",
15271527
"code": 2522
15281528
},
1529-
"Expression resolves to variable declaration '_arguments' that compiler uses to capture 'arguments' reference.": {
1530-
"category": "Error",
1531-
"code": 2523
1532-
},
15331529

15341530
"Import declaration '{0}' is using private name '{1}'.": {
15351531
"category": "Error",

0 commit comments

Comments
 (0)