From 114dad7f56b7a476deaba3e673c9926d47afc6ce Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Sun, 22 Dec 2019 13:24:31 -0800 Subject: [PATCH] Add top-level await for esnext and system modules (#35813) --- src/compiler/binder.ts | 7 ++- src/compiler/checker.ts | 40 ++++++++++---- src/compiler/diagnosticMessages.json | 4 ++ src/compiler/transformers/es2017.ts | 53 ++++++++++++++----- src/compiler/transformers/es2018.ts | 28 +++++----- src/compiler/transformers/module/system.ts | 5 +- src/compiler/types.ts | 15 +++--- .../awaitInNonAsyncFunction.errors.txt | 4 +- ...it(module=esnext,target=es2015).errors.txt | 9 ++++ ...LevelAwait(module=esnext,target=es2015).js | 8 +++ ...Await(module=esnext,target=es2015).symbols | 7 +++ ...elAwait(module=esnext,target=es2015).types | 9 ++++ ...LevelAwait(module=esnext,target=es2017).js | 8 +++ ...Await(module=esnext,target=es2017).symbols | 7 +++ ...elAwait(module=esnext,target=es2017).types | 9 ++++ ...it(module=system,target=es2015).errors.txt | 9 ++++ ...LevelAwait(module=system,target=es2015).js | 18 +++++++ ...Await(module=system,target=es2015).symbols | 7 +++ ...elAwait(module=system,target=es2015).types | 9 ++++ ...LevelAwait(module=system,target=es2017).js | 18 +++++++ ...Await(module=system,target=es2017).symbols | 7 +++ ...elAwait(module=system,target=es2017).types | 9 ++++ .../topLevelAwaitNonModule.errors.txt | 11 ++++ .../reference/topLevelAwaitNonModule.js | 6 +++ .../reference/topLevelAwaitNonModule.symbols | 4 ++ .../reference/topLevelAwaitNonModule.types | 5 ++ .../externalModules/topLevelAwait.ts | 4 ++ .../externalModules/topLevelAwaitNonModule.ts | 3 ++ 28 files changed, 274 insertions(+), 49 deletions(-) create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).errors.txt create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).js create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).symbols create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).types create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).js create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).symbols create mode 100644 tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).types create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2015).errors.txt create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2015).js create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2015).symbols create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2015).types create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2017).js create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2017).symbols create mode 100644 tests/baselines/reference/topLevelAwait(module=system,target=es2017).types create mode 100644 tests/baselines/reference/topLevelAwaitNonModule.errors.txt create mode 100644 tests/baselines/reference/topLevelAwaitNonModule.js create mode 100644 tests/baselines/reference/topLevelAwaitNonModule.symbols create mode 100644 tests/baselines/reference/topLevelAwaitNonModule.types create mode 100644 tests/cases/conformance/externalModules/topLevelAwait.ts create mode 100644 tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 0b19d7806f..8fb80a8003 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -3902,10 +3902,13 @@ namespace ts { switch (kind) { case SyntaxKind.AsyncKeyword: - case SyntaxKind.AwaitExpression: - // async/await is ES2017 syntax, but may be ES2018 syntax (for async generators) + // async is ES2017 syntax, but may be ES2018 syntax (for async generators) transformFlags |= TransformFlags.AssertES2018 | TransformFlags.AssertES2017; break; + case SyntaxKind.AwaitExpression: + // await is ES2017 syntax, but may be ES2018 syntax (for async generators) + transformFlags |= TransformFlags.AssertES2018 | TransformFlags.AssertES2017 | TransformFlags.ContainsAwait; + break; case SyntaxKind.TypeAssertionExpression: case SyntaxKind.AsExpression: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d33fed84e5..0a3658bde1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -26437,21 +26437,41 @@ namespace ts { return undefinedWideningType; } + function isTopLevelAwait(node: AwaitExpression) { + const container = getThisContainer(node, /*includeArrowFunctions*/ true); + return isSourceFile(container); + } + function checkAwaitExpression(node: AwaitExpression): Type { // Grammar checking if (produceDiagnostics) { if (!(node.flags & NodeFlags.AwaitContext)) { - // use of 'await' in non-async function - const sourceFile = getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - const span = getSpanOfTokenAtPosition(sourceFile, node.pos); - const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.await_expression_is_only_allowed_within_an_async_function); - const func = getContainingFunction(node); - if (func && func.kind !== SyntaxKind.Constructor && (getFunctionFlags(func) & FunctionFlags.Async) === 0) { - const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async); - addRelatedInfo(diagnostic, relatedInfo); + if (isTopLevelAwait(node)) { + const sourceFile = getSourceFileOfNode(node); + if ((moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) || + languageVersion < ScriptTarget.ES2017 || + !isEffectiveExternalModule(sourceFile, compilerOptions)) { + if (!hasParseDiagnostics(sourceFile)) { + const span = getSpanOfTokenAtPosition(sourceFile, node.pos); + const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, + Diagnostics.await_outside_of_an_async_function_is_only_allowed_at_the_top_level_of_a_module_when_module_is_esnext_or_system_and_target_is_es2017_or_higher); + diagnostics.add(diagnostic); + } + } + } + else { + // use of 'await' in non-async function + const sourceFile = getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + const span = getSpanOfTokenAtPosition(sourceFile, node.pos); + const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.await_expression_is_only_allowed_within_an_async_function); + const func = getContainingFunction(node); + if (func && func.kind !== SyntaxKind.Constructor && (getFunctionFlags(func) & FunctionFlags.Async) === 0) { + const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async); + addRelatedInfo(diagnostic, relatedInfo); + } + diagnostics.add(diagnostic); } - diagnostics.add(diagnostic); } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index ee1f593f98..18b5435514 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1059,6 +1059,10 @@ "category": "Error", "code": 1360 }, + "'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher.": { + "category": "Error", + "code": 1361 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", diff --git a/src/compiler/transformers/es2017.ts b/src/compiler/transformers/es2017.ts index 005d5a8fdd..594dc0d3e5 100644 --- a/src/compiler/transformers/es2017.ts +++ b/src/compiler/transformers/es2017.ts @@ -7,6 +7,11 @@ namespace ts { AsyncMethodsWithSuper = 1 << 0 } + const enum ContextFlags { + NonTopLevel = 1 << 0, + HasLexicalThis = 1 << 1 + } + export function transformES2017(context: TransformationContext) { const { resumeLexicalEnvironment, @@ -41,7 +46,7 @@ namespace ts { /** A set of node IDs for generated super accessors (variable statements). */ const substitutedSuperAccessors: boolean[] = []; - let topLevel: boolean; + let contextFlags: ContextFlags = 0; // Save the previous transformation hooks. const previousOnEmitNode = context.onEmitNode; @@ -58,17 +63,35 @@ namespace ts { return node; } - topLevel = isEffectiveStrictModeSourceFile(node, compilerOptions); + setContextFlag(ContextFlags.NonTopLevel, false); + setContextFlag(ContextFlags.HasLexicalThis, !isEffectiveStrictModeSourceFile(node, compilerOptions)); const visited = visitEachChild(node, visitor, context); addEmitHelpers(visited, context.readEmitHelpers()); return visited; } - function doOutsideOfTopLevel(cb: (value: T) => U, value: T) { - if (topLevel) { - topLevel = false; + function setContextFlag(flag: ContextFlags, val: boolean) { + contextFlags = val ? contextFlags | flag : contextFlags & ~flag; + } + + function inContext(flags: ContextFlags) { + return (contextFlags & flags) !== 0; + } + + function inTopLevelContext() { + return !inContext(ContextFlags.NonTopLevel); + } + + function inHasLexicalThisContext() { + return inContext(ContextFlags.HasLexicalThis); + } + + function doWithContext(flags: ContextFlags, cb: (value: T) => U, value: T) { + const contextFlagsToSet = flags & ~contextFlags; + if (contextFlagsToSet) { + setContextFlag(contextFlagsToSet, /*val*/ true); const result = cb(value); - topLevel = true; + setContextFlag(contextFlagsToSet, /*val*/ false); return result; } return cb(value); @@ -91,16 +114,16 @@ namespace ts { return visitAwaitExpression(node); case SyntaxKind.MethodDeclaration: - return doOutsideOfTopLevel(visitMethodDeclaration, node); + return doWithContext(ContextFlags.NonTopLevel | ContextFlags.HasLexicalThis, visitMethodDeclaration, node); case SyntaxKind.FunctionDeclaration: - return doOutsideOfTopLevel(visitFunctionDeclaration, node); + return doWithContext(ContextFlags.NonTopLevel | ContextFlags.HasLexicalThis, visitFunctionDeclaration, node); case SyntaxKind.FunctionExpression: - return doOutsideOfTopLevel(visitFunctionExpression, node); + return doWithContext(ContextFlags.NonTopLevel | ContextFlags.HasLexicalThis, visitFunctionExpression, node); case SyntaxKind.ArrowFunction: - return visitArrowFunction(node); + return doWithContext(ContextFlags.NonTopLevel, visitArrowFunction, node); case SyntaxKind.PropertyAccessExpression: if (capturedSuperProperties && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.SuperKeyword) { @@ -119,7 +142,7 @@ namespace ts { case SyntaxKind.Constructor: case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: - return doOutsideOfTopLevel(visitDefault, node); + return doWithContext(ContextFlags.NonTopLevel | ContextFlags.HasLexicalThis, visitDefault, node); default: return visitEachChild(node, visitor, context); @@ -237,6 +260,10 @@ namespace ts { * @param node The node to visit. */ function visitAwaitExpression(node: AwaitExpression): Expression { + // do not downlevel a top-level await as it is module syntax... + if (inTopLevelContext()) { + return visitEachChild(node, visitor, context); + } return setOriginalNode( setTextRange( createYield( @@ -457,7 +484,7 @@ namespace ts { createReturn( createAwaiterHelper( context, - !topLevel, + inHasLexicalThisContext(), hasLexicalArguments, promiseConstructor, transformAsyncFunctionBodyWorker(node.body, statementOffset) @@ -498,7 +525,7 @@ namespace ts { else { const expression = createAwaiterHelper( context, - !topLevel, + inHasLexicalThisContext(), hasLexicalArguments, promiseConstructor, transformAsyncFunctionBodyWorker(node.body!) diff --git a/src/compiler/transformers/es2018.ts b/src/compiler/transformers/es2018.ts index 01cabe76e5..f03b24fc45 100644 --- a/src/compiler/transformers/es2018.ts +++ b/src/compiler/transformers/es2018.ts @@ -26,7 +26,7 @@ namespace ts { let enabledSubstitutions: ESNextSubstitutionFlags; let enclosingFunctionFlags: FunctionFlags; let enclosingSuperContainerFlags: NodeCheckFlags = 0; - let topLevel: boolean; + let hasLexicalThis: boolean; /** Keeps track of property names accessed on super (`super.x`) within async functions. */ let capturedSuperProperties: UnderscoreEscapedMap; @@ -43,7 +43,7 @@ namespace ts { } exportedVariableStatement = false; - topLevel = isEffectiveStrictModeSourceFile(node, compilerOptions); + hasLexicalThis = !isEffectiveStrictModeSourceFile(node, compilerOptions); const visited = visitEachChild(node, visitor, context); addEmitHelpers(visited, context.readEmitHelpers()); return visited; @@ -64,11 +64,11 @@ namespace ts { return node; } - function doOutsideOfTopLevel(cb: (value: T) => U, value: T) { - if (topLevel) { - topLevel = false; + function doWithLexicalThis(cb: (value: T) => U, value: T) { + if (!hasLexicalThis) { + hasLexicalThis = true; const result = cb(value); - topLevel = true; + hasLexicalThis = false; return result; } return cb(value); @@ -108,17 +108,17 @@ namespace ts { case SyntaxKind.VoidExpression: return visitVoidExpression(node as VoidExpression); case SyntaxKind.Constructor: - return doOutsideOfTopLevel(visitConstructorDeclaration, node as ConstructorDeclaration); + return doWithLexicalThis(visitConstructorDeclaration, node as ConstructorDeclaration); case SyntaxKind.MethodDeclaration: - return doOutsideOfTopLevel(visitMethodDeclaration, node as MethodDeclaration); + return doWithLexicalThis(visitMethodDeclaration, node as MethodDeclaration); case SyntaxKind.GetAccessor: - return doOutsideOfTopLevel(visitGetAccessorDeclaration, node as GetAccessorDeclaration); + return doWithLexicalThis(visitGetAccessorDeclaration, node as GetAccessorDeclaration); case SyntaxKind.SetAccessor: - return doOutsideOfTopLevel(visitSetAccessorDeclaration, node as SetAccessorDeclaration); + return doWithLexicalThis(visitSetAccessorDeclaration, node as SetAccessorDeclaration); case SyntaxKind.FunctionDeclaration: - return doOutsideOfTopLevel(visitFunctionDeclaration, node as FunctionDeclaration); + return doWithLexicalThis(visitFunctionDeclaration, node as FunctionDeclaration); case SyntaxKind.FunctionExpression: - return doOutsideOfTopLevel(visitFunctionExpression, node as FunctionExpression); + return doWithLexicalThis(visitFunctionExpression, node as FunctionExpression); case SyntaxKind.ArrowFunction: return visitArrowFunction(node as ArrowFunction); case SyntaxKind.Parameter: @@ -139,7 +139,7 @@ namespace ts { return visitEachChild(node, visitor, context); case SyntaxKind.ClassDeclaration: case SyntaxKind.ClassExpression: - return doOutsideOfTopLevel(visitDefault, node); + return doWithLexicalThis(visitDefault, node); default: return visitEachChild(node, visitor, context); } @@ -774,7 +774,7 @@ namespace ts { visitLexicalEnvironment(node.body!.statements, visitor, context, statementOffset) ) ), - !topLevel + hasLexicalThis ) ); diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 7c47559245..bc1ba49f21 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -262,13 +262,16 @@ namespace ts { insertStatementsAfterStandardPrologue(statements, endLexicalEnvironment()); const exportStarFunction = addExportStarIfNeeded(statements)!; // TODO: GH#18217 + const modifiers = node.transformFlags & TransformFlags.ContainsAwait ? + createModifiersFromModifierFlags(ModifierFlags.Async) : + undefined; const moduleObject = createObjectLiteral([ createPropertyAssignment("setters", createSettersArray(exportStarFunction, dependencyGroups) ), createPropertyAssignment("execute", createFunctionExpression( - /*modifiers*/ undefined, + modifiers, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5ef9e86b25..746eb4e546 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5608,9 +5608,10 @@ namespace ts { ContainsBlockScopedBinding = 1 << 15, ContainsBindingPattern = 1 << 16, ContainsYield = 1 << 17, - ContainsHoistedDeclarationOrCompletion = 1 << 18, - ContainsDynamicImport = 1 << 19, - ContainsClassFields = 1 << 20, + ContainsAwait = 1 << 18, + ContainsHoistedDeclarationOrCompletion = 1 << 19, + ContainsDynamicImport = 1 << 20, + ContainsClassFields = 1 << 21, // Please leave this as 1 << 29. // It is the maximum bit we can set before we outgrow the size of a v8 small integer (SMI) on an x86 system. @@ -5636,10 +5637,10 @@ namespace ts { OuterExpressionExcludes = HasComputedFlags, PropertyAccessExcludes = OuterExpressionExcludes, NodeExcludes = PropertyAccessExcludes, - ArrowFunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, - FunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, - ConstructorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, - MethodOrAccessorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, + ArrowFunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, + FunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, + ConstructorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, + MethodOrAccessorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread, PropertyExcludes = NodeExcludes | ContainsLexicalThis, ClassExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsComputedPropertyName, ModuleExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsHoistedDeclarationOrCompletion, diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt index 5cd8468ac3..d7706ed107 100644 --- a/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt +++ b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt @@ -13,7 +13,7 @@ tests/cases/compiler/awaitInNonAsyncFunction.ts(31,5): error TS1308: 'await' exp tests/cases/compiler/awaitInNonAsyncFunction.ts(34,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. tests/cases/compiler/awaitInNonAsyncFunction.ts(35,5): error TS1308: 'await' expression is only allowed within an async function. tests/cases/compiler/awaitInNonAsyncFunction.ts(39,5): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. -tests/cases/compiler/awaitInNonAsyncFunction.ts(40,1): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(40,1): error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. ==== tests/cases/compiler/awaitInNonAsyncFunction.ts (16 errors) ==== @@ -100,4 +100,4 @@ tests/cases/compiler/awaitInNonAsyncFunction.ts(40,1): error TS1308: 'await' exp !!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. await null; ~~~~~ -!!! error TS1308: 'await' expression is only allowed within an async function. \ No newline at end of file +!!! error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. \ No newline at end of file diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).errors.txt b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).errors.txt new file mode 100644 index 0000000000..9efe0c2aa6 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. + + +==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ==== + export const x = 1; + await x; + ~~~~~ +!!! error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. + \ No newline at end of file diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).js b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).js new file mode 100644 index 0000000000..6a2b5fe680 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).js @@ -0,0 +1,8 @@ +//// [topLevelAwait.ts] +export const x = 1; +await x; + + +//// [topLevelAwait.js] +export const x = 1; +await x; diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).symbols b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).symbols new file mode 100644 index 0000000000..7eccf23a23 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + +await x; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).types b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).types new file mode 100644 index 0000000000..5f579192ee --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : 1 +>1 : 1 + +await x; +>await x : 1 +>x : 1 + diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).js b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).js new file mode 100644 index 0000000000..6a2b5fe680 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).js @@ -0,0 +1,8 @@ +//// [topLevelAwait.ts] +export const x = 1; +await x; + + +//// [topLevelAwait.js] +export const x = 1; +await x; diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).symbols b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).symbols new file mode 100644 index 0000000000..7eccf23a23 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + +await x; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + diff --git a/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).types b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).types new file mode 100644 index 0000000000..5f579192ee --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : 1 +>1 : 1 + +await x; +>await x : 1 +>x : 1 + diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2015).errors.txt b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).errors.txt new file mode 100644 index 0000000000..9efe0c2aa6 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. + + +==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ==== + export const x = 1; + await x; + ~~~~~ +!!! error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. + \ No newline at end of file diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2015).js b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).js new file mode 100644 index 0000000000..c6d4e03efb --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).js @@ -0,0 +1,18 @@ +//// [topLevelAwait.ts] +export const x = 1; +await x; + + +//// [topLevelAwait.js] +System.register([], function (exports_1, context_1) { + "use strict"; + var x; + var __moduleName = context_1 && context_1.id; + return { + setters: [], + execute: async function () { + exports_1("x", x = 1); + await x; + } + }; +}); diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2015).symbols b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).symbols new file mode 100644 index 0000000000..7eccf23a23 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + +await x; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2015).types b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).types new file mode 100644 index 0000000000..5f579192ee --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2015).types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : 1 +>1 : 1 + +await x; +>await x : 1 +>x : 1 + diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2017).js b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).js new file mode 100644 index 0000000000..c6d4e03efb --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).js @@ -0,0 +1,18 @@ +//// [topLevelAwait.ts] +export const x = 1; +await x; + + +//// [topLevelAwait.js] +System.register([], function (exports_1, context_1) { + "use strict"; + var x; + var __moduleName = context_1 && context_1.id; + return { + setters: [], + execute: async function () { + exports_1("x", x = 1); + await x; + } + }; +}); diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2017).symbols b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).symbols new file mode 100644 index 0000000000..7eccf23a23 --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + +await x; +>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12)) + diff --git a/tests/baselines/reference/topLevelAwait(module=system,target=es2017).types b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).types new file mode 100644 index 0000000000..5f579192ee --- /dev/null +++ b/tests/baselines/reference/topLevelAwait(module=system,target=es2017).types @@ -0,0 +1,9 @@ +=== tests/cases/conformance/externalModules/topLevelAwait.ts === +export const x = 1; +>x : 1 +>1 : 1 + +await x; +>await x : 1 +>x : 1 + diff --git a/tests/baselines/reference/topLevelAwaitNonModule.errors.txt b/tests/baselines/reference/topLevelAwaitNonModule.errors.txt new file mode 100644 index 0000000000..8e2d3a6eab --- /dev/null +++ b/tests/baselines/reference/topLevelAwaitNonModule.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts(1,1): error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. +tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts(1,7): error TS2304: Cannot find name 'x'. + + +==== tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts (2 errors) ==== + await x; + ~~~~~ +!!! error TS1361: 'await' outside of an async function is only allowed at the top level of a module when '--module' is 'esnext' or 'system' and '--target' is 'es2017' or higher. + ~ +!!! error TS2304: Cannot find name 'x'. + \ No newline at end of file diff --git a/tests/baselines/reference/topLevelAwaitNonModule.js b/tests/baselines/reference/topLevelAwaitNonModule.js new file mode 100644 index 0000000000..98fa13cb62 --- /dev/null +++ b/tests/baselines/reference/topLevelAwaitNonModule.js @@ -0,0 +1,6 @@ +//// [topLevelAwaitNonModule.ts] +await x; + + +//// [topLevelAwaitNonModule.js] +await x; diff --git a/tests/baselines/reference/topLevelAwaitNonModule.symbols b/tests/baselines/reference/topLevelAwaitNonModule.symbols new file mode 100644 index 0000000000..03c11745c1 --- /dev/null +++ b/tests/baselines/reference/topLevelAwaitNonModule.symbols @@ -0,0 +1,4 @@ +=== tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts === +await x; +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/topLevelAwaitNonModule.types b/tests/baselines/reference/topLevelAwaitNonModule.types new file mode 100644 index 0000000000..8b61253fc5 --- /dev/null +++ b/tests/baselines/reference/topLevelAwaitNonModule.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts === +await x; +>await x : any +>x : any + diff --git a/tests/cases/conformance/externalModules/topLevelAwait.ts b/tests/cases/conformance/externalModules/topLevelAwait.ts new file mode 100644 index 0000000000..7c92e507e5 --- /dev/null +++ b/tests/cases/conformance/externalModules/topLevelAwait.ts @@ -0,0 +1,4 @@ +// @target: es2015,es2017 +// @module: esnext,system +export const x = 1; +await x; diff --git a/tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts b/tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts new file mode 100644 index 0000000000..5a98c9423a --- /dev/null +++ b/tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts @@ -0,0 +1,3 @@ +// @target: esnext +// @module: esnext +await x;