From 3084ffbf3f8862140fe2e2a14ab7d6028c1f89d2 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 30 Sep 2016 06:09:37 -0700 Subject: [PATCH] Ensure that `checkGrammarModuleElementContext` reliably returns `true` when there is bad grammar --- src/compiler/checker.ts | 8 +++++--- tests/baselines/reference/exportInFunction.errors.txt | 9 +++++++++ tests/baselines/reference/exportInFunction.js | 8 ++++++++ tests/cases/compiler/exportInFunction.ts | 2 ++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/exportInFunction.errors.txt create mode 100644 tests/baselines/reference/exportInFunction.js create mode 100644 tests/cases/compiler/exportInFunction.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6ebbd83e375..9da683a0ffd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17433,9 +17433,11 @@ namespace ts { } function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean { - if (node.parent.kind !== SyntaxKind.SourceFile && node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.ModuleDeclaration) { - return grammarErrorOnFirstToken(node, errorMessage); + const isInAppropriateContext = node.parent.kind === SyntaxKind.SourceFile || node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.ModuleDeclaration; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } + return !isInAppropriateContext; } function checkExportSpecifier(node: ExportSpecifier) { @@ -17476,7 +17478,7 @@ namespace ts { checkExpressionCached(node.expression); } - checkExternalModuleExports(container); + checkExternalModuleExports(container); if (node.isExportEquals && !isInAmbientContext(node)) { if (modulekind === ModuleKind.ES6) { diff --git a/tests/baselines/reference/exportInFunction.errors.txt b/tests/baselines/reference/exportInFunction.errors.txt new file mode 100644 index 00000000000..cfd005952f9 --- /dev/null +++ b/tests/baselines/reference/exportInFunction.errors.txt @@ -0,0 +1,9 @@ +tests/cases/compiler/exportInFunction.ts(3,1): error TS1005: '}' expected. + + +==== tests/cases/compiler/exportInFunction.ts (1 errors) ==== + function f() { + export = 0; + + +!!! error TS1005: '}' expected. \ No newline at end of file diff --git a/tests/baselines/reference/exportInFunction.js b/tests/baselines/reference/exportInFunction.js new file mode 100644 index 00000000000..3bf68397ea7 --- /dev/null +++ b/tests/baselines/reference/exportInFunction.js @@ -0,0 +1,8 @@ +//// [exportInFunction.ts] +function f() { + export = 0; + + +//// [exportInFunction.js] +function f() { +} diff --git a/tests/cases/compiler/exportInFunction.ts b/tests/cases/compiler/exportInFunction.ts new file mode 100644 index 00000000000..9bb0c5dcd0f --- /dev/null +++ b/tests/cases/compiler/exportInFunction.ts @@ -0,0 +1,2 @@ +function f() { + export = 0;