Skip to content

Commit 631ab0a

Browse files
author
Andy
authored
Merge pull request microsoft#11246 from Microsoft/export_in_function
Ensure that `checkGrammarModuleElementContext` reliably returns `true` when there is bad grammar
2 parents bcdf1dc + 228ddde commit 631ab0a

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/compiler/checker.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -17714,9 +17714,11 @@ namespace ts {
1771417714
}
1771517715

1771617716
function checkGrammarModuleElementContext(node: Statement, errorMessage: DiagnosticMessage): boolean {
17717-
if (node.parent.kind !== SyntaxKind.SourceFile && node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.ModuleDeclaration) {
17718-
return grammarErrorOnFirstToken(node, errorMessage);
17717+
const isInAppropriateContext = node.parent.kind === SyntaxKind.SourceFile || node.parent.kind === SyntaxKind.ModuleBlock || node.parent.kind === SyntaxKind.ModuleDeclaration;
17718+
if (!isInAppropriateContext) {
17719+
grammarErrorOnFirstToken(node, errorMessage);
1771917720
}
17721+
return !isInAppropriateContext;
1772017722
}
1772117723

1772217724
function checkExportSpecifier(node: ExportSpecifier) {
@@ -17757,7 +17759,7 @@ namespace ts {
1775717759
checkExpressionCached(node.expression);
1775817760
}
1775917761

17760-
checkExternalModuleExports(<SourceFile | ModuleDeclaration>container);
17762+
checkExternalModuleExports(container);
1776117763

1776217764
if (node.isExportEquals && !isInAmbientContext(node)) {
1776317765
if (modulekind === ModuleKind.ES6) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/exportInFunction.ts(3,1): error TS1005: '}' expected.
2+
3+
4+
==== tests/cases/compiler/exportInFunction.ts (1 errors) ====
5+
function f() {
6+
export = 0;
7+
8+
9+
!!! error TS1005: '}' expected.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [exportInFunction.ts]
2+
function f() {
3+
export = 0;
4+
5+
6+
//// [exportInFunction.js]
7+
function f() {
8+
export = 0;
9+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function f() {
2+
export = 0;

0 commit comments

Comments
 (0)