Skip to content

Commit 6291ae3

Browse files
committed
Allow toplevel return only in non-ESM JS files
1 parent 714ea8e commit 6291ae3

12 files changed

+84
-11
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38913,7 +38913,7 @@ namespace ts {
3891338913
}
3891438914

3891538915
if (!container) {
38916-
if (!!getSourceFileOfNode(node).externalModuleIndicator || node.flags & NodeFlags.Ambient) {
38916+
if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) {
3891738917
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
3891838918
}
3891938919
return;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/compiler/asiReturn.ts(2,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/compiler/asiReturn.ts (1 errors) ====
5+
// This should be an error for using a return outside a function, but ASI should work properly
6+
return
7+
~~~~~~
8+
!!! error TS1108: A 'return' statement can only be used within a function body.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/fileWithNextLine3.ts(3,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/compiler/fileWithNextLine3.ts (1 errors) ====
5+
// Note: there is a nextline (0x85) between the return and the
6+
// 0. It should be counted as a space and should not trigger ASI
7+
return…0;
8+
~~~~~~
9+
!!! error TS1108: A 'return' statement can only be used within a function body.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (1 errors) ====
5+
return this.edit(role)
6+
~~~~~~
7+
!!! error TS1108: A 'return' statement can only be used within a function body.
8+
.then((role: Role) =>
9+
this.roleService.add(role)
10+
.then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data));
11+

tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ return this.edit(role)
66
>this.edit : any
77
>this : typeof globalThis
88
>edit : any
9-
>role : error
9+
>role : any
1010

1111
.then((role: Role) =>
1212
>then : any
1313
>(role: Role) => this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data) : (role: Role) => any
14-
>role : error
14+
>role : Role
1515

1616
this.roleService.add(role)
1717
>this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data) : any
@@ -22,14 +22,14 @@ return this.edit(role)
2222
>this : typeof globalThis
2323
>roleService : any
2424
>add : any
25-
>role : error
25+
>role : Role
2626

2727
.then((data: ng.IHttpPromiseCallbackArg<Role>) => data.data));
2828
>then : any
2929
>(data: ng.IHttpPromiseCallbackArg<Role>) => data.data : (data: ng.IHttpPromiseCallbackArg<Role>) => any
30-
>data : error
30+
>data : ng.IHttpPromiseCallbackArg<Role>
3131
>ng : any
32-
>data.data : error
32+
>data.data : any
3333
>data : ng.IHttpPromiseCallbackArg<Role>
3434
>data : any
3535

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts(1,6): error TS1009: Trailing comma not allowed.
2+
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.
23

34

4-
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (1 errors) ====
5+
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (2 errors) ====
56
var a,
67
~
78
!!! error TS1009: Trailing comma not allowed.
8-
return;
9+
return;
10+
~~~~~~
11+
!!! error TS1108: A 'return' statement can only be used within a function body.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts(1,7): error TS2304: Cannot find name 'a'.
2+
tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts(3,5): error TS1108: A 'return' statement can only be used within a function body.
23

34

4-
==== tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts (1 errors) ====
5+
==== tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts (2 errors) ====
56
if (a.indexOf(-(4/3))) // We should not get a regex here because of the / in the comment.
67
~
78
!!! error TS2304: Cannot find name 'a'.
89
{
910
return true;
11+
~~~~~~
12+
!!! error TS1108: A 'return' statement can only be used within a function body.
1013
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpression1.ts (1 errors) ====
5+
return /(#?-?\d*\.\d\w*%?)|(@?#?[\w-?]+%?)/g;
6+
~~~~~~
7+
!!! error TS1108: A 'return' statement can only be used within a function body.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement1.ts (1 errors) ====
5+
return;
6+
~~~~~~
7+
!!! error TS1108: A 'return' statement can only be used within a function body.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement2.ts(2,4): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement2.ts (1 errors) ====
5+
{
6+
return;
7+
~~~~~~
8+
!!! error TS1108: A 'return' statement can only be used within a function body.
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserStatementIsNotAMemberVariableDeclaration1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserStatementIsNotAMemberVariableDeclaration1.ts (1 errors) ====
5+
return {
6+
~~~~~~
7+
!!! error TS1108: A 'return' statement can only be used within a function body.
8+
9+
"set": function (key, value) {
10+
11+
// 'private' should not be considered a member variable here.
12+
private[key] = value;
13+
14+
}
15+
16+
};

tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ return {
1111
// 'private' should not be considered a member variable here.
1212
private[key] = value;
1313
>private[key] = value : any
14-
>private[key] : error
15-
>private : error
14+
>private[key] : any
15+
>private : any
1616
>key : any
1717
>value : any
1818

0 commit comments

Comments
 (0)