Skip to content

Commit 2056e13

Browse files
committed
No error in toplevel script files
Only issue "no top-level return" error for modules, not scripts, regardless of whether it's TS or JS.
1 parent 0aab0d1 commit 2056e13

15 files changed

+19
-89
lines changed

src/compiler/checker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -38913,7 +38913,9 @@ namespace ts {
3891338913
}
3891438914

3891538915
if (!container) {
38916-
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
38916+
if (!!getSourceFileOfNode(node).externalModuleIndicator) {
38917+
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
38918+
}
3891738919
return;
3891838920
}
3891938921

src/compiler/program.ts

+1
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ namespace ts {
913913
Diagnostics.A_rest_parameter_cannot_have_an_initializer.code,
914914
Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list.code,
915915
Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma.code,
916+
Diagnostics.A_return_statement_can_only_be_used_within_a_function_body.code,
916917
Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.code,
917918
Diagnostics.A_set_accessor_cannot_have_rest_parameter.code,
918919
Diagnostics.A_set_accessor_must_have_exactly_one_parameter.code,

tests/baselines/reference/ambientWithStatements.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
tests/cases/compiler/ambientWithStatements.ts(2,5): error TS1036: Statements are not allowed in ambient contexts.
22
tests/cases/compiler/ambientWithStatements.ts(3,5): error TS1104: A 'continue' statement can only be used within an enclosing iteration statement.
3-
tests/cases/compiler/ambientWithStatements.ts(11,5): error TS1108: A 'return' statement can only be used within a function body.
43
tests/cases/compiler/ambientWithStatements.ts(25,5): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
54

65

7-
==== tests/cases/compiler/ambientWithStatements.ts (4 errors) ====
6+
==== tests/cases/compiler/ambientWithStatements.ts (3 errors) ====
87
declare module M {
98
break;
109
~~~~~
@@ -20,8 +19,6 @@ tests/cases/compiler/ambientWithStatements.ts(25,5): error TS2410: The 'with' st
2019
1;
2120
L: var y;
2221
return;
23-
~~~~~~
24-
!!! error TS1108: A 'return' statement can only be used within a function body.
2522
switch (x) {
2623
case 1:
2724
break;

tests/baselines/reference/asiReturn.errors.txt

-8
This file was deleted.

tests/baselines/reference/fileWithNextLine3.errors.txt

-9
This file was deleted.

tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.errors.txt

-11
This file was deleted.

tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types

+5-5
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 : any
9+
>role : error
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 : Role
14+
>role : error
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 : Role
25+
>role : error
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 : ng.IHttpPromiseCallbackArg<Role>
30+
>data : error
3131
>ng : any
32-
>data.data : any
32+
>data.data : error
3333
>data : ng.IHttpPromiseCallbackArg<Role>
3434
>data : any
3535

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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.
32

43

5-
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (2 errors) ====
4+
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (1 errors) ====
65
var a,
76
~
87
!!! error TS1009: Trailing comma not allowed.
9-
return;
10-
~~~~~~
11-
!!! error TS1108: A 'return' statement can only be used within a function body.
8+
return;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
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.
32

43

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

tests/baselines/reference/parserRegularExpression1.errors.txt

-7
This file was deleted.

tests/baselines/reference/parserReturnStatement1.errors.txt

-7
This file was deleted.

tests/baselines/reference/parserReturnStatement2.errors.txt

-9
This file was deleted.

tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.errors.txt

-16
This file was deleted.

tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types

+2-2
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] : any
15-
>private : any
14+
>private[key] : error
15+
>private : error
1616
>key : any
1717
>value : any
1818

tests/baselines/reference/plainJSGrammarErrors.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(202,22): error TS17012: 't
100100
tests/cases/conformance/salsa/plainJSGrammarErrors.js(203,30): message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments
101101
tests/cases/conformance/salsa/plainJSGrammarErrors.js(204,30): message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments
102102
tests/cases/conformance/salsa/plainJSGrammarErrors.js(205,36): error TS1325: Argument of dynamic import cannot be spread element.
103+
tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'return' statement can only be used within a function body.
103104

104105

105-
==== tests/cases/conformance/salsa/plainJSGrammarErrors.js (102 errors) ====
106+
==== tests/cases/conformance/salsa/plainJSGrammarErrors.js (103 errors) ====
106107
class C {
107108
// #private mistakes
108109
q = #unbound
@@ -514,4 +515,6 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(205,36): error TS1325: Arg
514515
!!! error TS1325: Argument of dynamic import cannot be spread element.
515516

516517
return
518+
~~~~~~
519+
!!! error TS1108: A 'return' statement can only be used within a function body.
517520

0 commit comments

Comments
 (0)