diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index defdde238fb..298eebc1a5a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8126,6 +8126,20 @@ module ts { } function checkReturnStatement(node: ReturnStatement) { + // Grammar checking + var parent = node.parent; + var inFunctionBlock = false; + while (parent && parent.kind !== SyntaxKind.SourceFile) { + inFunctionBlock = isFunctionBlock(parent); + if (inFunctionBlock) { + break; + } + parent = parent.parent; + } + if (!inFunctionBlock) { + grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); + } + if (node.expression) { var func = getContainingFunction(node); if (func) { @@ -8149,6 +8163,20 @@ module ts { } function checkWithStatement(node: WithStatement) { + // Grammar checking + if (node.statement.kind === SyntaxKind.ReturnStatement) { + // Grammar check for invalid use of return statement + grammarErrorOnFirstToken(node.statement, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); + } + else if (node.statement.kind === SyntaxKind.Block) { + forEach((node.statement).statements, statement => { + if (statement.kind === SyntaxKind.ReturnStatement) { + // Grammar check for invalid use of return statement + grammarErrorOnFirstToken(node.statement, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); + } + }); + } + checkExpression(node.expression); error(node.expression, Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); } diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index f24bb111db9..ed0aec3c501 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -4665,7 +4665,7 @@ module ts { case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: return checkProperty(node); - case SyntaxKind.ReturnStatement: return checkReturnStatement(node); + //case SyntaxKind.ReturnStatement: return checkReturnStatement(node); case SyntaxKind.SetAccessor: return checkSetAccessor(node); case SyntaxKind.SourceFile: return checkSourceFile(node); case SyntaxKind.ShorthandPropertyAssignment: return checkShorthandPropertyAssignment(node); diff --git a/tests/baselines/reference/parserNotRegex1.errors.txt b/tests/baselines/reference/parserNotRegex1.errors.txt index 9e592541fd0..7eb06157dfc 100644 --- a/tests/baselines/reference/parserNotRegex1.errors.txt +++ b/tests/baselines/reference/parserNotRegex1.errors.txt @@ -1,5 +1,5 @@ -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,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) ==== diff --git a/tests/baselines/reference/parserWithStatement2.errors.txt b/tests/baselines/reference/parserWithStatement2.errors.txt index 47ad0c8a920..c2d4b2efa96 100644 --- a/tests/baselines/reference/parserWithStatement2.errors.txt +++ b/tests/baselines/reference/parserWithStatement2.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts(2,3): error TS1108: A 'return' statement can only be used within a function body. tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts(1,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'. +tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts(2,3): error TS1108: A 'return' statement can only be used within a function body. ==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts (2 errors) ====