Allow toplevel return only in non-ESM JS files

This commit is contained in:
Nathan Shively-Sanders
2022-05-02 14:45:02 -07:00
parent 714ea8e524
commit 6291ae3ba2
12 changed files with 84 additions and 11 deletions

View File

@@ -38913,7 +38913,7 @@ namespace ts {
}
if (!container) {
if (!!getSourceFileOfNode(node).externalModuleIndicator || node.flags & NodeFlags.Ambient) {
if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) {
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
}
return;

View File

@@ -0,0 +1,8 @@
tests/cases/compiler/asiReturn.ts(2,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/compiler/asiReturn.ts (1 errors) ====
// This should be an error for using a return outside a function, but ASI should work properly
return
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.

View File

@@ -0,0 +1,9 @@
tests/cases/compiler/fileWithNextLine3.ts(3,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/compiler/fileWithNextLine3.ts (1 errors) ====
// Note: there is a nextline (0x85) between the return and the
// 0. It should be counted as a space and should not trigger ASI
return…0;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.

View File

@@ -0,0 +1,11 @@
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (1 errors) ====
return this.edit(role)
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
.then((role: Role) =>
this.roleService.add(role)
.then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data));

View File

@@ -6,12 +6,12 @@ return this.edit(role)
>this.edit : any
>this : typeof globalThis
>edit : any
>role : error
>role : any
.then((role: Role) =>
>then : any
>(role: Role) => this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data) : (role: Role) => any
>role : error
>role : Role
this.roleService.add(role)
>this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data) : any
@@ -22,14 +22,14 @@ return this.edit(role)
>this : typeof globalThis
>roleService : any
>add : any
>role : error
>role : Role
.then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data));
>then : any
>(data: ng.IHttpPromiseCallbackArg<Role>) => data.data : (data: ng.IHttpPromiseCallbackArg<Role>) => any
>data : error
>data : ng.IHttpPromiseCallbackArg<Role>
>ng : any
>data.data : error
>data.data : any
>data : ng.IHttpPromiseCallbackArg<Role>
>data : any

View File

@@ -1,8 +1,11 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts(1,6): error TS1009: Trailing comma not allowed.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts(2,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (2 errors) ====
var a,
~
!!! error TS1009: Trailing comma not allowed.
return;
return;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.

View File

@@ -1,10 +1,13 @@
tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts(1,7): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts(3,5): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts (2 errors) ====
if (a.indexOf(-(4/3))) // We should not get a regex here because of the / in the comment.
~
!!! error TS2304: Cannot find name 'a'.
{
return true;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
}

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression1.ts (1 errors) ====
return /(#?-?\d*\.\d\w*%?)|(@?#?[\w-?]+%?)/g;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.

View File

@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement1.ts (1 errors) ====
return;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.

View File

@@ -0,0 +1,9 @@
tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement2.ts(2,4): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement2.ts (1 errors) ====
{
return;
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
}

View File

@@ -0,0 +1,16 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserStatementIsNotAMemberVariableDeclaration1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserStatementIsNotAMemberVariableDeclaration1.ts (1 errors) ====
return {
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
"set": function (key, value) {
// 'private' should not be considered a member variable here.
private[key] = value;
}
};

View File

@@ -11,8 +11,8 @@ return {
// 'private' should not be considered a member variable here.
private[key] = value;
>private[key] = value : any
>private[key] : error
>private : error
>private[key] : any
>private : any
>key : any
>value : any