Skip to content

Reparse top level 'await' in modules #39084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33533,7 +33533,7 @@ namespace ts {
function checkThrowStatement(node: ThrowStatement) {
// Grammar checking
if (!checkGrammarStatementInAmbientContext(node)) {
if (node.expression === undefined) {
if (isIdentifier(node.expression) && !node.expression.escapedText) {
grammarErrorAfterFirstToken(node, Diagnostics.Line_break_not_permitted_here);
}
}
Expand Down
26 changes: 22 additions & 4 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,13 @@ namespace ts {
modifiers
);
node.name = asName(name);
node.transformFlags |= propagateChildFlags(node.name);
node.transformFlags |=
kind === SyntaxKind.MethodDeclaration ||
kind === SyntaxKind.GetAccessor ||
kind === SyntaxKind.SetAccessor ||
kind === SyntaxKind.PropertyDeclaration ?
propagatePropertyNameFlags(node.name) :
propagateChildFlags(node.name) ;
return node;
}

Expand Down Expand Up @@ -824,6 +830,9 @@ namespace ts {
// NOTE: we do not use `setChildren` here because typeArguments in an identifier do not contribute to transformations
node.typeArguments = createNodeArray(typeArguments);
}
if (node.originalKeywordKind === SyntaxKind.AwaitKeyword) {
node.transformFlags |= TransformFlags.ContainsPossibleTopLevelAwait;
}
return node;
}

Expand Down Expand Up @@ -2094,7 +2103,7 @@ namespace ts {
node.name = asName(name);
node.transformFlags =
propagateChildFlags(node.expression) |
propagateChildFlags(node.name);
propagatePropertyNameFlags(node.name);
if (isSuperKeyword(expression)) {
// super method calls require a lexical 'this'
// super method calls require 'super' hoisting in ES2017 and ES2018 async functions and async generators
Expand Down Expand Up @@ -5777,14 +5786,23 @@ namespace ts {
return tokenValue;
}

function propagatePropertyNameFlags(node: PropertyName, transformFlags: TransformFlags) {
function propagatePropertyNameFlags(node: Node | undefined) {
if (!node) return TransformFlags.None;
// `await` in a property name should not be considered a possible top-level await keyword
const transformFlags = propagateChildFlags(node);
return isIdentifier(node) ?
transformFlags & ~TransformFlags.ContainsPossibleTopLevelAwait :
transformFlags;
}

function propagatePropertyNameFlagsOfChild(node: PropertyName, transformFlags: TransformFlags) {
return transformFlags | (node.transformFlags & TransformFlags.PropertyNamePropagatingFlags);
}

function propagateChildFlags(child: Node | undefined): TransformFlags {
if (!child) return TransformFlags.None;
const childFlags = child.transformFlags & ~getTransformFlagsSubtreeExclusions(child.kind);
return isNamedDeclaration(child) && isPropertyName(child.name) ? propagatePropertyNameFlags(child.name, childFlags) : childFlags;
return isNamedDeclaration(child) && isPropertyName(child.name) ? propagatePropertyNameFlagsOfChild(child.name, childFlags) : childFlags;
}

function propagateChildrenFlags(children: NodeArray<Node> | undefined): TransformFlags {
Expand Down
196 changes: 176 additions & 20 deletions src/compiler/parser.ts

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2722,7 +2722,7 @@ namespace ts {

export interface ThrowStatement extends Statement {
readonly kind: SyntaxKind.ThrowStatement;
readonly expression?: Expression;
readonly expression: Expression;
}

export interface TryStatement extends Statement {
Expand Down Expand Up @@ -6242,6 +6242,7 @@ namespace ts {
ContainsHoistedDeclarationOrCompletion = 1 << 20,
ContainsDynamicImport = 1 << 21,
ContainsClassFields = 1 << 22,
ContainsPossibleTopLevelAwait = 1 << 23,

// 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.
Expand All @@ -6268,13 +6269,13 @@ namespace ts {
OuterExpressionExcludes = HasComputedFlags,
PropertyAccessExcludes = OuterExpressionExcludes,
NodeExcludes = PropertyAccessExcludes,
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,
ArrowFunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread | ContainsPossibleTopLevelAwait,
FunctionExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread | ContainsPossibleTopLevelAwait,
ConstructorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread | ContainsPossibleTopLevelAwait,
MethodOrAccessorExcludes = NodeExcludes | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsAwait | ContainsHoistedDeclarationOrCompletion | ContainsBindingPattern | ContainsObjectRestOrSpread | ContainsPossibleTopLevelAwait,
PropertyExcludes = NodeExcludes | ContainsLexicalThis | ContainsPossibleTopLevelAwait,
ClassExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsComputedPropertyName,
ModuleExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsHoistedDeclarationOrCompletion,
ModuleExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsLexicalThis | ContainsBlockScopedBinding | ContainsHoistedDeclarationOrCompletion | ContainsPossibleTopLevelAwait,
TypeExcludes = ~ContainsTypeScript,
ObjectLiteralExcludes = NodeExcludes | ContainsTypeScriptClassSyntax | ContainsComputedPropertyName | ContainsObjectRestOrSpread,
ArrayLiteralOrCallOrNewExcludes = NodeExcludes | ContainsRestOrSpread,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ namespace ts {

case SyntaxKind.ThrowStatement:
return factory.updateThrowStatement(<ThrowStatement>node,
nodeVisitor((<ThrowStatement>node).expression!, visitor, isExpression)); // expression could be `undefined` due to invalid parse.
nodeVisitor((<ThrowStatement>node).expression, visitor, isExpression));

case SyntaxKind.TryStatement:
return factory.updateTryStatement(<TryStatement>node,
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ declare namespace ts {
}
export interface ThrowStatement extends Statement {
readonly kind: SyntaxKind.ThrowStatement;
readonly expression?: Expression;
readonly expression: Expression;
}
export interface TryStatement extends Statement {
readonly kind: SyntaxKind.TryStatement;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ declare namespace ts {
}
export interface ThrowStatement extends Statement {
readonly kind: SyntaxKind.ThrowStatement;
readonly expression?: Expression;
readonly expression: Expression;
}
export interface TryStatement extends Statement {
readonly kind: SyntaxKind.TryStatement;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/throwWithoutNewLine2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ throw
a;

//// [throwWithoutNewLine2.js]
throw;
throw ;
a;
1 change: 1 addition & 0 deletions tests/baselines/reference/throwWithoutNewLine2.types
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
=== tests/cases/compiler/throwWithoutNewLine2.ts ===
throw
a;
> : any
>a : any

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,43 @@ tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1378: Top
await x;
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.


// reparse element access as await
await [x];
await [x, x];

// reparse call as await
declare function f(): number;
await (x);
await (f(), x);
await <number>(x);
await <number>(f(), x);

// reparse tagged template as await
await ``;
await <string> ``;

// member names should be ok
class C1 {
await() {}
}
class C2 {
get await() { return 1; }
set await(value) { }
}
class C3 {
await = 1;
}
({
await() {}
});
({
get await() { return 1 },
set await(value) { }
});
({
await: 1
});

// property access name should be ok
C1.prototype.await;
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
//// [topLevelAwait.ts]
export const x = 1;
await x;


// reparse element access as await
await [x];
await [x, x];

// reparse call as await
declare function f(): number;
await (x);
await (f(), x);
await <number>(x);
await <number>(f(), x);

// reparse tagged template as await
await ``;
await <string> ``;

// member names should be ok
class C1 {
await() {}
}
class C2 {
get await() { return 1; }
set await(value) { }
}
class C3 {
await = 1;
}
({
await() {}
});
({
get await() { return 1 },
set await(value) { }
});
({
await: 1
});

// property access name should be ok
C1.prototype.await;

//// [topLevelAwait.js]
export const x = 1;
await x;
// reparse element access as await
await [x];
await [x, x];
await (x);
await (f(), x);
await (x);
await (f(), x);
// reparse tagged template as await
await ``;
await ``;
// member names should be ok
class C1 {
await() { }
}
class C2 {
get await() { return 1; }
set await(value) { }
}
class C3 {
constructor() {
this.await = 1;
}
}
({
await() { }
});
({
get await() { return 1; },
set await(value) { }
});
({
await: 1
});
// property access name should be ok
C1.prototype.await;
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,84 @@ export const x = 1;
await x;
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

// reparse element access as await
await [x];
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await [x, x];
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

// reparse call as await
declare function f(): number;
>f : Symbol(f, Decl(topLevelAwait.ts, 5, 13))

await (x);
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await (f(), x);
>f : Symbol(f, Decl(topLevelAwait.ts, 5, 13))
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await <number>(x);
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

await <number>(f(), x);
>f : Symbol(f, Decl(topLevelAwait.ts, 5, 13))
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))

// reparse tagged template as await
await ``;
await <string> ``;

// member names should be ok
class C1 {
>C1 : Symbol(C1, Decl(topLevelAwait.ts, 16, 18))

await() {}
>await : Symbol(C1.await, Decl(topLevelAwait.ts, 19, 10))
}
class C2 {
>C2 : Symbol(C2, Decl(topLevelAwait.ts, 21, 1))

get await() { return 1; }
>await : Symbol(C2.await, Decl(topLevelAwait.ts, 22, 10), Decl(topLevelAwait.ts, 23, 29))

set await(value) { }
>await : Symbol(C2.await, Decl(topLevelAwait.ts, 22, 10), Decl(topLevelAwait.ts, 23, 29))
>value : Symbol(value, Decl(topLevelAwait.ts, 24, 14))
}
class C3 {
>C3 : Symbol(C3, Decl(topLevelAwait.ts, 25, 1))

await = 1;
>await : Symbol(C3.await, Decl(topLevelAwait.ts, 26, 10))
}
({
await() {}
>await : Symbol(await, Decl(topLevelAwait.ts, 29, 2))

});
({
get await() { return 1 },
>await : Symbol(await, Decl(topLevelAwait.ts, 32, 2), Decl(topLevelAwait.ts, 33, 29))

set await(value) { }
>await : Symbol(await, Decl(topLevelAwait.ts, 32, 2), Decl(topLevelAwait.ts, 33, 29))
>value : Symbol(value, Decl(topLevelAwait.ts, 34, 14))

});
({
await: 1
>await : Symbol(await, Decl(topLevelAwait.ts, 36, 2))

});

// property access name should be ok
C1.prototype.await;
>C1.prototype.await : Symbol(C1.await, Decl(topLevelAwait.ts, 19, 10))
>C1.prototype : Symbol(C1.prototype)
>C1 : Symbol(C1, Decl(topLevelAwait.ts, 16, 18))
>prototype : Symbol(C1.prototype)
>await : Symbol(C1.await, Decl(topLevelAwait.ts, 19, 10))

Loading