Skip to content

Commit 1c1316e

Browse files
committed
Always error on an attempt to use await as an identifier in modules
1 parent 769f2da commit 1c1316e

26 files changed

+60
-68
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ import {
167167
isIdentifier,
168168
isIdentifierName,
169169
isInJSFile,
170-
isInTopLevelContext,
171170
isJSDocConstructSignature,
172171
isJSDocEnumTag,
173172
isJSDocTemplateTag,
@@ -2457,13 +2456,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
24572456
) {
24582457
file.bindDiagnostics.push(createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), declarationNameToString(node)));
24592458
}
2460-
else if (originalKeywordKind === SyntaxKind.AwaitKeyword) {
2461-
if (isExternalModule(file) && isInTopLevelContext(node)) {
2462-
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module, declarationNameToString(node)));
2463-
}
2464-
else if (node.flags & NodeFlags.AwaitContext) {
2465-
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here, declarationNameToString(node)));
2466-
}
2459+
else if (originalKeywordKind === SyntaxKind.AwaitKeyword && (node.flags & NodeFlags.AwaitContext || isExternalModule(file))) {
2460+
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here, declarationNameToString(node)));
24672461
}
24682462
else if (originalKeywordKind === SyntaxKind.YieldKeyword && node.flags & NodeFlags.YieldContext) {
24692463
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here, declarationNameToString(node)));

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,10 +843,6 @@
843843
"category": "Error",
844844
"code": 1261
845845
},
846-
"Identifier expected. '{0}' is a reserved word at the top-level of a module.": {
847-
"category": "Error",
848-
"code": 1262
849-
},
850846
"Declarations with initializers cannot also have definite assignment assertions.": {
851847
"category": "Error",
852848
"code": 1263

src/compiler/program.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,6 @@ export const plainJSErrors = new Set<number>([
13461346
Diagnostics.A_module_cannot_have_multiple_default_exports.code,
13471347
Diagnostics.Another_export_default_is_here.code,
13481348
Diagnostics.The_first_export_default_is_here.code,
1349-
Diagnostics.Identifier_expected_0_is_a_reserved_word_at_the_top_level_of_a_module.code,
13501349
Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode.code,
13511350
Diagnostics.Identifier_expected_0_is_a_reserved_word_that_cannot_be_used_here.code,
13521351
Diagnostics.constructor_is_a_reserved_word.code,

tests/baselines/reference/exportDefaultAsyncFunction2.errors.txt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
a.ts(1,17): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
2-
asyncawait.ts(2,17): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
3-
c.ts(1,17): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
4-
d.ts(1,17): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
5-
e.ts(1,17): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
a.ts(1,17): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
2+
a.ts(2,28): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
3+
asyncawait.ts(2,17): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
4+
c.ts(1,17): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
5+
d.ts(1,17): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
6+
e.ts(1,17): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
67

78

89
==== asyncawait.ts (1 errors) ====
910
export function async<T>(...args: any[]): any { }
1011
export function await(...args: any[]): any { }
1112
~~~~~
12-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
13+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1314

14-
==== a.ts (1 errors) ====
15+
==== a.ts (2 errors) ====
1516
import { async, await } from 'asyncawait';
1617
~~~~~
17-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
18+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1819
export default async(() => await(Promise.resolve(1)));
20+
~~~~~
21+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1922

2023
==== b.ts (0 errors) ====
2124
export default async () => { return 0; };
2225

2326
==== c.ts (1 errors) ====
2427
import { async, await } from 'asyncawait';
2528
~~~~~
26-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
29+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
2730
export default async<number>();
2831

2932
==== d.ts (1 errors) ====
3033
import { async, await } from 'asyncawait';
3134
~~~~~
32-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
35+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
3336

3437
export default async;
3538

3639
==== e.ts (1 errors) ====
3740
import { async, await } from 'asyncawait';
3841
~~~~~
39-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
42+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
4043

4144
export default async
4245

tests/baselines/reference/plainJSBinderErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plainJSBinderErrors.js(1,1): error TS2528: A module cannot have multiple default exports.
22
plainJSBinderErrors.js(2,1): error TS2528: A module cannot have multiple default exports.
3-
plainJSBinderErrors.js(3,7): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
3+
plainJSBinderErrors.js(3,7): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
44
plainJSBinderErrors.js(4,7): error TS1214: Identifier expected. 'yield' is a reserved word in strict mode. Modules are automatically in strict mode.
55
plainJSBinderErrors.js(6,11): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
66
plainJSBinderErrors.js(9,11): error TS1214: Identifier expected. 'yield' is a reserved word in strict mode. Modules are automatically in strict mode.
@@ -28,7 +28,7 @@ plainJSBinderErrors.js(40,7): error TS1215: Invalid use of 'arguments'. Modules
2828
!!! related TS2752 plainJSBinderErrors.js:1:1: The first export default is here.
2929
const await = 1
3030
~~~~~
31-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
31+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
3232
const yield = 2
3333
~~~~~
3434
!!! error TS1214: Identifier expected. 'yield' is a reserved word in strict mode. Modules are automatically in strict mode.

tests/baselines/reference/topLevelAwaitErrors.10(module=es2022).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
index.ts(2,19): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
index.ts(2,19): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
22

33

44
==== index.ts (1 errors) ====
55
// await disallowed in alias of named import
66
import { await as await } from "./other";
77
~~~~~
8-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
8+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
99

1010
==== other.ts (0 errors) ====
1111
declare const _await: any;

tests/baselines/reference/topLevelAwaitErrors.10(module=esnext).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
index.ts(2,19): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
index.ts(2,19): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
22

33

44
==== index.ts (1 errors) ====
55
// await disallowed in alias of named import
66
import { await as await } from "./other";
77
~~~~~
8-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
8+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
99

1010
==== other.ts (0 errors) ====
1111
declare const _await: any;

tests/baselines/reference/topLevelAwaitErrors.11.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
index.ts(3,8): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
index.ts(3,8): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
22

33

44
==== index.ts (1 errors) ====
55
// await disallowed in import=
66
declare var require: any;
77
import await = require("./other");
88
~~~~~
9-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
9+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1010

1111
==== other.ts (0 errors) ====
1212
declare const _await: any;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
topLevelAwaitErrors.12.ts(5,8): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
topLevelAwaitErrors.12.ts(5,8): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
22

33

44
==== topLevelAwaitErrors.12.ts (1 errors) ====
@@ -8,5 +8,5 @@ topLevelAwaitErrors.12.ts(5,8): error TS1262: Identifier expected. 'await' is a
88
// await disallowed in import=namespace when in a module
99
import await = foo.await;
1010
~~~~~
11-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
11+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1212

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
topLevelAwaitErrors.12.ts(5,8): error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
1+
topLevelAwaitErrors.12.ts(5,8): error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
22

33

44
==== topLevelAwaitErrors.12.ts (1 errors) ====
@@ -8,5 +8,5 @@ topLevelAwaitErrors.12.ts(5,8): error TS1262: Identifier expected. 'await' is a
88
// await disallowed in import=namespace when in a module
99
import await = foo.await;
1010
~~~~~
11-
!!! error TS1262: Identifier expected. 'await' is a reserved word at the top-level of a module.
11+
!!! error TS1359: Identifier expected. 'await' is a reserved word that cannot be used here.
1212

0 commit comments

Comments
 (0)