diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 610288da1a7..b20c8959307 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2896,20 +2896,12 @@ module ts { function parseWithStatement(): WithStatement { var node = createNode(SyntaxKind.WithStatement); - var startPos = scanner.getTokenPos(); parseExpected(SyntaxKind.WithKeyword); - var endPos = scanner.getStartPos(); parseExpected(SyntaxKind.OpenParenToken); node.expression = parseExpression(); parseExpected(SyntaxKind.CloseParenToken); node.statement = parseStatement(); - node = finishNode(node); - if (isInStrictMode) { - // Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such - // a context is an - grammarErrorAtPos(startPos, endPos - startPos, Diagnostics.with_statements_are_not_allowed_in_strict_mode); - } - return node; + return finishNode(node); } function parseCaseClause(): CaseOrDefaultClause { @@ -4019,6 +4011,7 @@ module ts { case SyntaxKind.TypeReference: return visitTypeReference(node); case SyntaxKind.VariableDeclaration: return visitVariableDeclaration(node); case SyntaxKind.VariableStatement: return visitVariableStatement(node); + case SyntaxKind.WithStatement: return visitWithStatement(node); } } @@ -4630,8 +4623,8 @@ module ts { } } - function allowLetAndConstDeclarations(node: Node): boolean { - switch (node.kind) { + function allowLetAndConstDeclarations(parent: Node): boolean { + switch (parent.kind) { case SyntaxKind.IfStatement: case SyntaxKind.DoStatement: case SyntaxKind.WhileStatement: @@ -4640,11 +4633,19 @@ module ts { case SyntaxKind.ForInStatement: return false; case SyntaxKind.LabeledStatement: - return allowLetAndConstDeclarations(node.parent); + return allowLetAndConstDeclarations(parent.parent); } return true; - } + } + + function visitWithStatement(node: WithStatement): void { + if (node.flags & NodeFlags.ParsedInStrictMode) { + // Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such + // a context is an + grammarErrorOnFirstToken(node, Diagnostics.with_statements_are_not_allowed_in_strict_mode); + } + } } export function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program { diff --git a/tests/baselines/reference/methodInAmbientClass1.errors.txt b/tests/baselines/reference/methodInAmbientClass1.errors.txt new file mode 100644 index 00000000000..3bf8f8af12a --- /dev/null +++ b/tests/baselines/reference/methodInAmbientClass1.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/methodInAmbientClass1.ts(2,20): error TS1037: A function implementation cannot be declared in an ambient context. +tests/cases/compiler/methodInAmbientClass1.ts(2,12): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement. + + +==== tests/cases/compiler/methodInAmbientClass1.ts (2 errors) ==== + declare class Foo { + fn(): boolean { + ~ +!!! error TS1037: A function implementation cannot be declared in an ambient context. + ~~~~~~~ +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement. + } + } \ No newline at end of file diff --git a/tests/cases/compiler/methodInAmbientClass1.ts b/tests/cases/compiler/methodInAmbientClass1.ts new file mode 100644 index 00000000000..8590977f89b --- /dev/null +++ b/tests/cases/compiler/methodInAmbientClass1.ts @@ -0,0 +1,4 @@ + declare class Foo { + fn(): boolean { + } + } \ No newline at end of file