diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5efefc51d24..07faeb90663 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38913,7 +38913,9 @@ namespace ts { } if (!container) { - grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); + if (!!getSourceFileOfNode(node).externalModuleIndicator) { + grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); + } return; } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 8cacf4baa34..c7f886ea311 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -913,6 +913,7 @@ namespace ts { Diagnostics.A_rest_parameter_cannot_have_an_initializer.code, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list.code, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma.code, + Diagnostics.A_return_statement_can_only_be_used_within_a_function_body.code, Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.code, Diagnostics.A_set_accessor_cannot_have_rest_parameter.code, Diagnostics.A_set_accessor_must_have_exactly_one_parameter.code, diff --git a/tests/baselines/reference/ambientWithStatements.errors.txt b/tests/baselines/reference/ambientWithStatements.errors.txt index c7cb28a0f46..e6d3bce40d4 100644 --- a/tests/baselines/reference/ambientWithStatements.errors.txt +++ b/tests/baselines/reference/ambientWithStatements.errors.txt @@ -1,10 +1,9 @@ tests/cases/compiler/ambientWithStatements.ts(2,5): error TS1036: Statements are not allowed in ambient contexts. tests/cases/compiler/ambientWithStatements.ts(3,5): error TS1104: A 'continue' statement can only be used within an enclosing iteration statement. -tests/cases/compiler/ambientWithStatements.ts(11,5): error TS1108: A 'return' statement can only be used within a function body. 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'. -==== tests/cases/compiler/ambientWithStatements.ts (4 errors) ==== +==== tests/cases/compiler/ambientWithStatements.ts (3 errors) ==== declare module M { break; ~~~~~ @@ -20,8 +19,6 @@ tests/cases/compiler/ambientWithStatements.ts(25,5): error TS2410: The 'with' st 1; L: var y; return; - ~~~~~~ -!!! error TS1108: A 'return' statement can only be used within a function body. switch (x) { case 1: break; diff --git a/tests/baselines/reference/asiReturn.errors.txt b/tests/baselines/reference/asiReturn.errors.txt deleted file mode 100644 index 1cc3ec8a3c1..00000000000 --- a/tests/baselines/reference/asiReturn.errors.txt +++ /dev/null @@ -1,8 +0,0 @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/fileWithNextLine3.errors.txt b/tests/baselines/reference/fileWithNextLine3.errors.txt deleted file mode 100644 index 647ec722549..00000000000 --- a/tests/baselines/reference/fileWithNextLine3.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.errors.txt b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.errors.txt deleted file mode 100644 index 48a1da7d22c..00000000000 --- a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.errors.txt +++ /dev/null @@ -1,11 +0,0 @@ -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) => data.data)); - \ No newline at end of file diff --git a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types index f311c6093cf..51877e2f09d 100644 --- a/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types +++ b/tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.types @@ -6,12 +6,12 @@ return this.edit(role) >this.edit : any >this : typeof globalThis >edit : any ->role : any +>role : error .then((role: Role) => >then : any >(role: Role) => this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg) => data.data) : (role: Role) => any ->role : Role +>role : error this.roleService.add(role) >this.roleService.add(role) .then((data: ng.IHttpPromiseCallbackArg) => data.data) : any @@ -22,14 +22,14 @@ return this.edit(role) >this : typeof globalThis >roleService : any >add : any ->role : Role +>role : error .then((data: ng.IHttpPromiseCallbackArg) => data.data)); >then : any >(data: ng.IHttpPromiseCallbackArg) => data.data : (data: ng.IHttpPromiseCallbackArg) => any ->data : ng.IHttpPromiseCallbackArg +>data : error >ng : any ->data.data : any +>data.data : error >data : ng.IHttpPromiseCallbackArg >data : any diff --git a/tests/baselines/reference/parserErrorRecovery_VariableList1.errors.txt b/tests/baselines/reference/parserErrorRecovery_VariableList1.errors.txt index b69cab688f0..2abf07e719b 100644 --- a/tests/baselines/reference/parserErrorRecovery_VariableList1.errors.txt +++ b/tests/baselines/reference/parserErrorRecovery_VariableList1.errors.txt @@ -1,11 +1,8 @@ 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 (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserErrorRecovery_VariableList1.ts (1 errors) ==== var a, ~ !!! error TS1009: Trailing comma not allowed. - return; - ~~~~~~ -!!! error TS1108: A 'return' statement can only be used within a function body. \ No newline at end of file + return; \ No newline at end of file diff --git a/tests/baselines/reference/parserNotRegex1.errors.txt b/tests/baselines/reference/parserNotRegex1.errors.txt index 63b78ce50d2..cba48155c50 100644 --- a/tests/baselines/reference/parserNotRegex1.errors.txt +++ b/tests/baselines/reference/parserNotRegex1.errors.txt @@ -1,13 +1,10 @@ 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 (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/parserNotRegex1.ts (1 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. } \ No newline at end of file diff --git a/tests/baselines/reference/parserRegularExpression1.errors.txt b/tests/baselines/reference/parserRegularExpression1.errors.txt deleted file mode 100644 index 724b8e703ea..00000000000 --- a/tests/baselines/reference/parserRegularExpression1.errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/parserReturnStatement1.errors.txt b/tests/baselines/reference/parserReturnStatement1.errors.txt deleted file mode 100644 index 8c80b45c4c3..00000000000 --- a/tests/baselines/reference/parserReturnStatement1.errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/parserReturnStatement2.errors.txt b/tests/baselines/reference/parserReturnStatement2.errors.txt deleted file mode 100644 index 5f335efbeae..00000000000 --- a/tests/baselines/reference/parserReturnStatement2.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -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. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.errors.txt b/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.errors.txt deleted file mode 100644 index 0532fd28d63..00000000000 --- a/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.errors.txt +++ /dev/null @@ -1,16 +0,0 @@ -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; - - } - - }; \ No newline at end of file diff --git a/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types b/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types index b7d836b0ad5..0c6f01372ad 100644 --- a/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types +++ b/tests/baselines/reference/parserStatementIsNotAMemberVariableDeclaration1.types @@ -11,8 +11,8 @@ return { // 'private' should not be considered a member variable here. private[key] = value; >private[key] = value : any ->private[key] : any ->private : any +>private[key] : error +>private : error >key : any >value : any diff --git a/tests/baselines/reference/plainJSGrammarErrors.errors.txt b/tests/baselines/reference/plainJSGrammarErrors.errors.txt index 0ab5b17e0c2..c49f7618bf4 100644 --- a/tests/baselines/reference/plainJSGrammarErrors.errors.txt +++ b/tests/baselines/reference/plainJSGrammarErrors.errors.txt @@ -100,9 +100,10 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(202,22): error TS17012: 't tests/cases/conformance/salsa/plainJSGrammarErrors.js(203,30): message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments tests/cases/conformance/salsa/plainJSGrammarErrors.js(204,30): message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments tests/cases/conformance/salsa/plainJSGrammarErrors.js(205,36): error TS1325: Argument of dynamic import cannot be spread element. +tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'return' statement can only be used within a function body. -==== tests/cases/conformance/salsa/plainJSGrammarErrors.js (102 errors) ==== +==== tests/cases/conformance/salsa/plainJSGrammarErrors.js (103 errors) ==== class C { // #private mistakes q = #unbound @@ -514,4 +515,6 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(205,36): error TS1325: Arg !!! error TS1325: Argument of dynamic import cannot be spread element. return + ~~~~~~ +!!! error TS1108: A 'return' statement can only be used within a function body. \ No newline at end of file