From 6900c8ff81dbc6c14e400c326f6b4473bf016207 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 18 Nov 2014 22:58:11 -0800 Subject: [PATCH] Move module grammar checks to the grammar checker. --- src/compiler/parser.ts | 24 +++++---- ...DeclarationInModuleDeclaration1.errors.txt | 9 ++++ .../privacyGloImportParseErrors.errors.txt | 26 +--------- .../privacyImportParseErrors.errors.txt | 50 +------------------ .../importDeclarationInModuleDeclaration1.ts | 3 ++ 5 files changed, 29 insertions(+), 83 deletions(-) create mode 100644 tests/baselines/reference/importDeclarationInModuleDeclaration1.errors.txt create mode 100644 tests/cases/compiler/importDeclarationInModuleDeclaration1.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 775ed08ca47..586c9f7e107 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3782,15 +3782,6 @@ module ts { } else { node.body = parseModuleBody(); - forEach((node.body).statements, s => { - if (s.kind === SyntaxKind.ExportAssignment) { - // Export assignments are not allowed in an internal module - grammarErrorOnNode(s, Diagnostics.An_export_assignment_cannot_be_used_in_an_internal_module); - } - else if (s.kind === SyntaxKind.ImportDeclaration && (s).externalModuleName) { - grammarErrorOnNode(s, Diagnostics.Import_declarations_in_an_internal_module_cannot_reference_an_external_module); - } - }); } return finishNode(node); } @@ -4122,6 +4113,7 @@ module ts { case SyntaxKind.GetAccessor: return visitGetAccessor(node); case SyntaxKind.IndexSignature: return visitIndexSignature(node); case SyntaxKind.Method: return visitMethod(node); + case SyntaxKind.ModuleDeclaration: return visitModuleDeclaration(node); case SyntaxKind.ObjectLiteral: return visitObjectLiteral(node); case SyntaxKind.Parameter: return visitParameter(node); case SyntaxKind.PostfixOperator: return visitPostfixOperator(node); @@ -4291,6 +4283,20 @@ module ts { checkParameterList(node.parameters); } + function visitModuleDeclaration(node: ModuleDeclaration): void { + if (node.name.kind === SyntaxKind.Identifier && node.body.kind === SyntaxKind.ModuleBlock) { + forEach((node.body).statements, s => { + if (s.kind === SyntaxKind.ExportAssignment) { + // Export assignments are not allowed in an internal module + grammarErrorOnNode(s, Diagnostics.An_export_assignment_cannot_be_used_in_an_internal_module); + } + else if (s.kind === SyntaxKind.ImportDeclaration && (s).externalModuleName) { + grammarErrorOnNode(s, Diagnostics.Import_declarations_in_an_internal_module_cannot_reference_an_external_module); + } + }); + } + } + function visitObjectLiteral(node: ObjectLiteral): void { var seen: Map = {}; var Property = 1; diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration1.errors.txt b/tests/baselines/reference/importDeclarationInModuleDeclaration1.errors.txt new file mode 100644 index 00000000000..82132947ee5 --- /dev/null +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration1.errors.txt @@ -0,0 +1,9 @@ +tests/cases/compiler/importDeclarationInModuleDeclaration1.ts(2,5): error TS1147: Import declarations in an internal module cannot reference an external module. + + +==== tests/cases/compiler/importDeclarationInModuleDeclaration1.ts (1 errors) ==== + module m2 { + import m3 = require("use_glo_M1_public"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1147: Import declarations in an internal module cannot reference an external module. + } \ No newline at end of file diff --git a/tests/baselines/reference/privacyGloImportParseErrors.errors.txt b/tests/baselines/reference/privacyGloImportParseErrors.errors.txt index b87e6867a8f..0ac0ed35105 100644 --- a/tests/baselines/reference/privacyGloImportParseErrors.errors.txt +++ b/tests/baselines/reference/privacyGloImportParseErrors.errors.txt @@ -1,12 +1,4 @@ -tests/cases/compiler/privacyGloImportParseErrors.ts(59,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(69,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(81,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(82,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(121,9): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(125,13): error TS1147: Import declarations in an internal module cannot reference an external module. tests/cases/compiler/privacyGloImportParseErrors.ts(133,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context. -tests/cases/compiler/privacyGloImportParseErrors.ts(146,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyGloImportParseErrors.ts(149,9): error TS1147: Import declarations in an internal module cannot reference an external module. tests/cases/compiler/privacyGloImportParseErrors.ts(22,27): error TS2435: Ambient external modules cannot be nested in other modules. tests/cases/compiler/privacyGloImportParseErrors.ts(30,20): error TS2435: Ambient external modules cannot be nested in other modules. tests/cases/compiler/privacyGloImportParseErrors.ts(59,37): error TS2307: Cannot find external module 'm1_M3_public'. @@ -16,7 +8,7 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(138,16): error TS2435: Ambie tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambient external modules cannot be nested in other modules. -==== tests/cases/compiler/privacyGloImportParseErrors.ts (16 errors) ==== +==== tests/cases/compiler/privacyGloImportParseErrors.ts (8 errors) ==== module m1 { export module m1_M1_public { export class c1 { @@ -80,8 +72,6 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambie var m1_im2_private_v4_private = m1_im2_private.f1(); import m1_im3_private = require("m1_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm1_M3_public'. export var m1_im3_private_v1_public = m1_im3_private.c1; @@ -94,8 +84,6 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambie var m1_im3_private_v4_private = m1_im3_private.f1(); import m1_im4_private = require("m1_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm1_M4_private'. export var m1_im4_private_v1_public = m1_im4_private.c1; @@ -110,11 +98,7 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambie export import m1_im1_public = m1_M1_public; export import m1_im2_public = m1_M2_private; export import m1_im3_public = require("m1_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. export import m1_im4_public = require("m1_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } module glo_M1_public { @@ -154,14 +138,10 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambie module m2 { import errorImport = require("glo_M2_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import nonerrorImport = glo_M1_public; module m5 { import m5_errorImport = require("glo_M2_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import m5_nonerrorImport = glo_M1_public; } } @@ -191,13 +171,9 @@ tests/cases/compiler/privacyGloImportParseErrors.ts(141,12): error TS2435: Ambie module m2 { import m3 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. module m4 { var a = 10; import m2 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } } \ No newline at end of file diff --git a/tests/baselines/reference/privacyImportParseErrors.errors.txt b/tests/baselines/reference/privacyImportParseErrors.errors.txt index 3388cdc3b9c..6e901ab2b6d 100644 --- a/tests/baselines/reference/privacyImportParseErrors.errors.txt +++ b/tests/baselines/reference/privacyImportParseErrors.errors.txt @@ -1,22 +1,6 @@ -tests/cases/compiler/privacyImportParseErrors.ts(59,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(69,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(81,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(82,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(143,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(153,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(166,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(167,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(273,9): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(277,13): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(302,9): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(306,13): error TS1147: Import declarations in an internal module cannot reference an external module. tests/cases/compiler/privacyImportParseErrors.ts(314,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context. tests/cases/compiler/privacyImportParseErrors.ts(326,9): error TS1029: 'export' modifier must precede 'declare' modifier. tests/cases/compiler/privacyImportParseErrors.ts(328,9): error TS1038: A 'declare' modifier cannot be used in an already ambient context. -tests/cases/compiler/privacyImportParseErrors.ts(341,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(344,9): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(350,5): error TS1147: Import declarations in an internal module cannot reference an external module. -tests/cases/compiler/privacyImportParseErrors.ts(353,9): error TS1147: Import declarations in an internal module cannot reference an external module. tests/cases/compiler/privacyImportParseErrors.ts(22,27): error TS2435: Ambient external modules cannot be nested in other modules. tests/cases/compiler/privacyImportParseErrors.ts(30,20): error TS2435: Ambient external modules cannot be nested in other modules. tests/cases/compiler/privacyImportParseErrors.ts(59,37): error TS2307: Cannot find external module 'm1_M3_public'. @@ -49,7 +33,7 @@ tests/cases/compiler/privacyImportParseErrors.ts(333,16): error TS2435: Ambient tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient external modules cannot be nested in other modules. -==== tests/cases/compiler/privacyImportParseErrors.ts (49 errors) ==== +==== tests/cases/compiler/privacyImportParseErrors.ts (33 errors) ==== export module m1 { export module m1_M1_public { export class c1 { @@ -113,8 +97,6 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient var m1_im2_private_v4_private = m1_im2_private.f1(); import m1_im3_private = require("m1_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm1_M3_public'. export var m1_im3_private_v1_public = m1_im3_private.c1; @@ -127,8 +109,6 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient var m1_im3_private_v4_private = m1_im3_private.f1(); import m1_im4_private = require("m1_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm1_M4_private'. export var m1_im4_private_v1_public = m1_im4_private.c1; @@ -143,11 +123,7 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient export import m1_im1_public = m1_M1_public; export import m1_im2_public = m1_M2_private; export import m1_im3_public = require("m1_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. export import m1_im4_public = require("m1_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } module m2 { @@ -213,8 +189,6 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient var m1_im2_private_v4_private = m1_im2_private.f1(); import m1_im3_private = require("m2_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm2_M3_public'. export var m1_im3_private_v1_public = m1_im3_private.c1; @@ -227,8 +201,6 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient var m1_im3_private_v4_private = m1_im3_private.f1(); import m1_im4_private = require("m2_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. ~~~~~~~~~~~~~~~ !!! error TS2307: Cannot find external module 'm2_M4_private'. export var m1_im4_private_v1_public = m1_im4_private.c1; @@ -244,11 +216,7 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient export import m1_im1_public = m2_M1_public; export import m1_im2_public = m2_M2_private; export import m1_im3_public = require("m2_M3_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. export import m1_im4_public = require("m2_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } export module glo_M1_public { @@ -375,14 +343,10 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient module m2 { import errorImport = require("glo_M2_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import nonerrorImport = glo_M1_public; module m5 { import m5_errorImport = require("glo_M2_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import m5_nonerrorImport = glo_M1_public; } } @@ -416,14 +380,10 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient module m2 { import errorImport = require("glo_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import nonerrorImport = glo_M3_private; module m5 { import m5_errorImport = require("glo_M4_private"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. import m5_nonerrorImport = glo_M3_private; } } @@ -481,26 +441,18 @@ tests/cases/compiler/privacyImportParseErrors.ts(336,12): error TS2435: Ambient module m2 { import m3 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. module m4 { var a = 10; import m2 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } } export module m3 { import m3 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. module m4 { var a = 10; import m2 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in an internal module cannot reference an external module. } } \ No newline at end of file diff --git a/tests/cases/compiler/importDeclarationInModuleDeclaration1.ts b/tests/cases/compiler/importDeclarationInModuleDeclaration1.ts new file mode 100644 index 00000000000..9f7f9ed7ccd --- /dev/null +++ b/tests/cases/compiler/importDeclarationInModuleDeclaration1.ts @@ -0,0 +1,3 @@ +module m2 { + import m3 = require("use_glo_M1_public"); +} \ No newline at end of file