From 6eefb65a264e47fead40c2f31e48e01db060861a Mon Sep 17 00:00:00 2001 From: islandryu Date: Fri, 10 Dec 2021 01:17:55 +0900 Subject: [PATCH 1/6] Fix error term of declaration in modules --- src/compiler/checker.ts | 8 ++-- src/compiler/diagnosticMessages.json | 12 +++-- src/compiler/program.ts | 4 +- ...DeclarationInModuleDeclaration2.errors.txt | 12 +++++ ...ortDeclarationInModuleDeclaration2.symbols | 7 +++ ...mportDeclarationInModuleDeclaration2.types | 5 ++ .../labeledStatementWithLabel.errors.txt | 8 ++-- ...abeledStatementWithLabel_es2015.errors.txt | 8 ++-- ...abeledStatementWithLabel_strict.errors.txt | 8 ++-- .../moduleElementsInWrongContext.errors.txt | 48 +++++++++---------- .../moduleElementsInWrongContext2.errors.txt | 48 +++++++++---------- .../moduleElementsInWrongContext3.errors.txt | 48 +++++++++---------- .../reference/plainJSGrammarErrors.errors.txt | 8 ++-- .../importDeclarationInModuleDeclaration2.ts | 8 ++++ 14 files changed, 134 insertions(+), 98 deletions(-) create mode 100644 tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt create mode 100644 tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols create mode 100644 tests/baselines/reference/importDeclarationInModuleDeclaration2.types create mode 100644 tests/cases/compiler/importDeclarationInModuleDeclaration2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ada97551aacc..1eb3fce3fb249 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39502,7 +39502,7 @@ namespace ts { const isAmbientExternalModule: boolean = isAmbientModule(node); const contextErrorMessage = isAmbientExternalModule ? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file - : Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module; + : Diagnostics.A_namespace_declaration_is_only_allowed_at_the_top_level_of_a_module; if (checkGrammarModuleElementContext(node, contextErrorMessage)) { // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. return; @@ -39795,7 +39795,7 @@ namespace ts { } function checkImportDeclaration(node: ImportDeclaration) { - if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + if (checkGrammarModuleElementContext(node,isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. return; } @@ -39829,7 +39829,7 @@ namespace ts { } function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) { - if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. return; } @@ -39868,7 +39868,7 @@ namespace ts { } function checkExportDeclaration(node: ExportDeclaration) { - if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) { + if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module)) { // If we hit an export in an illegal context, just bail out to avoid cascading errors. return; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 778a9304aa75e..1a36cc0240480 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -727,11 +727,11 @@ "category": "Error", "code": 1231 }, - "An import declaration can only be used in a namespace or module.": { + "An import declaration can only be used at the top level of a namespace or module.": { "category": "Error", "code": 1232 }, - "An export declaration can only be used in a module.": { + "An export declaration can only be used at the top level of a module.": { "category": "Error", "code": 1233 }, @@ -739,7 +739,7 @@ "category": "Error", "code": 1234 }, - "A namespace declaration is only allowed in a namespace or module.": { + "A namespace declaration is only allowed at the top level of a module.": { "category": "Error", "code": 1235 }, @@ -4161,7 +4161,7 @@ "category": "Message", "code": 6041 }, - + "Generates corresponding '.map' file.": { "category": "Message", "code": 6043 @@ -7236,5 +7236,9 @@ "A 'return' statement cannot be used inside a class static block.": { "category": "Error", "code": 18041 + }, + "An import declaration can only be used at the top level of a module": { + "category": "Error", + "code": 18042 } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 80115dbd35dab..750fffdbc80ad 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -861,9 +861,9 @@ namespace ts { Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.code, Diagnostics.A_set_accessor_cannot_have_rest_parameter.code, Diagnostics.A_set_accessor_must_have_exactly_one_parameter.code, - Diagnostics.An_export_declaration_can_only_be_used_in_a_module.code, + Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module.code, Diagnostics.An_export_declaration_cannot_have_modifiers.code, - Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module.code, + Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module.code, Diagnostics.An_import_declaration_cannot_have_modifiers.code, Diagnostics.An_object_member_cannot_be_declared_optional.code, Diagnostics.Argument_of_dynamic_import_cannot_be_spread_element.code, diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt new file mode 100644 index 0000000000000..1f8ebf8e42f47 --- /dev/null +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/check.js(1,8): error TS8006: 'module' declarations can only be used in TypeScript files. +tests/cases/compiler/check.js(2,25): error TS1147: Import declarations in a namespace cannot reference a module. + + +==== tests/cases/compiler/check.js (2 errors) ==== + module m2 { + ~~ +!!! error TS8006: 'module' declarations can only be used in TypeScript files. + import m3 = require("use_glo_M1_public"); + ~~~~~~~~~~~~~~~~~~~ +!!! error TS1147: Import declarations in a namespace cannot reference a module. + } \ No newline at end of file diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols b/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols new file mode 100644 index 0000000000000..0f8967900a8aa --- /dev/null +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/check.js === +module m2 { +>m2 : Symbol(m2, Decl(check.js, 0, 0)) + + import m3 = require("use_glo_M1_public"); +>m3 : Symbol(m3, Decl(check.js, 0, 11)) +} diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.types b/tests/baselines/reference/importDeclarationInModuleDeclaration2.types new file mode 100644 index 0000000000000..482cae6d0d173 --- /dev/null +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.types @@ -0,0 +1,5 @@ +=== tests/cases/compiler/check.js === +module m2 { + import m3 = require("use_glo_M1_public"); +>m3 : any +} diff --git a/tests/baselines/reference/labeledStatementWithLabel.errors.txt b/tests/baselines/reference/labeledStatementWithLabel.errors.txt index c7259e484eaaa..d2487beee4bd8 100644 --- a/tests/baselines/reference/labeledStatementWithLabel.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. ==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts (2 errors) ==== @@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.t label: module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: namespace N {} ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: type T = {} \ No newline at end of file diff --git a/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt b/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt index 95c136b953de6..a1abad24d7723 100644 --- a/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. ==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts (2 errors) ==== @@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_e label: module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: namespace N {} ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: type T = {} \ No newline at end of file diff --git a/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt b/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt index 863604a083680..da8cf271d878c 100644 --- a/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt @@ -8,9 +8,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(9,1): error TS1344: 'A label is not allowed here. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(10,1): error TS1344: 'A label is not allowed here. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,1): error TS1344: 'A label is not allowed here. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,1): error TS1344: 'A label is not allowed here. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(14,1): error TS1344: 'A label is not allowed here. @@ -48,12 +48,12 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s ~~~~~ !!! error TS1344: 'A label is not allowed here. ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: namespace N {} ~~~~~ !!! error TS1344: 'A label is not allowed here. ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. label: type T = {} ~~~~~ !!! error TS1344: 'A label is not allowed here. diff --git a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt index 146a6dcd951df..22872e2566e8f 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt @@ -1,36 +1,36 @@ -tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(18,5): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(19,5): error TS1233: An export declaration can only be used in a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(18,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(19,5): error TS1233: An export declaration can only be used at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext.ts(20,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext.ts(21,5): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext.ts(22,5): error TS1184: Modifiers cannot appear here. -tests/cases/compiler/moduleElementsInWrongContext.ts(23,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(24,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(25,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(26,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(27,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can only be used in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(23,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(24,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(25,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(26,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(27,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. ==== tests/cases/compiler/moduleElementsInWrongContext.ts (17 errors) ==== { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. declare module "ambient" { ~~~~~~~ @@ -46,13 +46,13 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An imp function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. @@ -64,21 +64,21 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An imp !!! error TS1184: Modifiers cannot appear here. import I = M; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import I2 = require("foo"); ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import * as Foo from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import bar from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import { baz } from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. } \ No newline at end of file diff --git a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt index f858bf9923e2b..3e8c9d863f8b9 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt @@ -1,36 +1,36 @@ -tests/cases/compiler/moduleElementsInWrongContext2.ts(2,5): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(3,5): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(7,5): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext2.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext2.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext2.ts(17,5): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(18,5): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(19,5): error TS1233: An export declaration can only be used in a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(17,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(18,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(19,5): error TS1233: An export declaration can only be used at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext2.ts(20,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext2.ts(21,5): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext2.ts(22,5): error TS1184: Modifiers cannot appear here. -tests/cases/compiler/moduleElementsInWrongContext2.ts(23,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(24,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(25,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(26,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(27,5): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An import declaration can only be used in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(23,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(24,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(25,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(26,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(27,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An import declaration can only be used at the top level of a namespace or module. ==== tests/cases/compiler/moduleElementsInWrongContext2.ts (17 errors) ==== function blah () { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. declare module "ambient" { ~~~~~~~ @@ -46,13 +46,13 @@ tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An im function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. @@ -64,21 +64,21 @@ tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An im !!! error TS1184: Modifiers cannot appear here. import I = M; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import I2 = require("foo"); ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import * as Foo from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import bar from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import { baz } from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. } \ No newline at end of file diff --git a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt index df78fa08741f1..da829ef35decb 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt @@ -1,20 +1,20 @@ -tests/cases/compiler/moduleElementsInWrongContext3.ts(3,9): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(4,9): error TS1235: A namespace declaration is only allowed in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(8,9): error TS1235: A namespace declaration is only allowed in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(3,9): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(4,9): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(8,9): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext3.ts(10,9): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext3.ts(14,9): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext3.ts(18,9): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(19,9): error TS1233: An export declaration can only be used in a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(20,9): error TS1233: An export declaration can only be used in a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(18,9): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(19,9): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(20,9): error TS1233: An export declaration can only be used at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext3.ts(21,9): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext3.ts(22,9): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext3.ts(23,9): error TS1184: Modifiers cannot appear here. -tests/cases/compiler/moduleElementsInWrongContext3.ts(24,9): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(25,9): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(26,9): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(27,9): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(28,9): error TS1232: An import declaration can only be used in a namespace or module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An import declaration can only be used in a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(24,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(25,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(26,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(27,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(28,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An import declaration can only be used at the top level of a namespace or module. ==== tests/cases/compiler/moduleElementsInWrongContext3.ts (17 errors) ==== @@ -22,16 +22,16 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An im { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed in a namespace or module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a module. declare module "ambient" { ~~~~~~~ @@ -47,13 +47,13 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An im function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. @@ -65,21 +65,21 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An im !!! error TS1184: Modifiers cannot appear here. import I = M; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import I2 = require("foo"); ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import * as Foo from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import bar from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import { baz } from "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. import "ambient"; ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS1232: An import declaration can only be used at the top level of a namespace or module. } } \ No newline at end of file diff --git a/tests/baselines/reference/plainJSGrammarErrors.errors.txt b/tests/baselines/reference/plainJSGrammarErrors.errors.txt index fa3affdb840ed..d5453e3023a2c 100644 --- a/tests/baselines/reference/plainJSGrammarErrors.errors.txt +++ b/tests/baselines/reference/plainJSGrammarErrors.errors.txt @@ -44,8 +44,8 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(65,1): error TS1042: 'asyn tests/cases/conformance/salsa/plainJSGrammarErrors.js(66,1): error TS1042: 'async' modifier cannot be used here. tests/cases/conformance/salsa/plainJSGrammarErrors.js(67,1): error TS1191: An import declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(68,1): error TS1193: An export declaration cannot have modifiers. -tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1233: An export declaration can only be used in a module. -tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS1232: An import declaration can only be used in a namespace or module. +tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS18042: An import declaration can only be used at the top level of a module tests/cases/conformance/salsa/plainJSGrammarErrors.js(72,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/conformance/salsa/plainJSGrammarErrors.js(75,5): error TS1184: Modifiers cannot appear here. tests/cases/conformance/salsa/plainJSGrammarErrors.js(78,5): error TS1042: 'static' modifier cannot be used here. @@ -267,10 +267,10 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'r function nestedExports() { export { staticParam } ~~~~~~ -!!! error TS1233: An export declaration can only be used in a module. +!!! error TS1233: An export declaration can only be used at the top level of a module. import 'fs' ~~~~~~ -!!! error TS1232: An import declaration can only be used in a namespace or module. +!!! error TS18042: An import declaration can only be used at the top level of a module export default 12 ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. diff --git a/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts b/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts new file mode 100644 index 0000000000000..68c373ffdc5ad --- /dev/null +++ b/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts @@ -0,0 +1,8 @@ +// @allowJs: true +// @noEmit: true +// @checkJs: true + +// @filename: check.js +module m2 { + import m3 = require("use_glo_M1_public"); +} \ No newline at end of file From 0f223d7d49632127ea61fde9784a34c5e1782324 Mon Sep 17 00:00:00 2001 From: islandryu Date: Fri, 10 Dec 2021 02:43:18 +0900 Subject: [PATCH 2/6] fix test --- src/compiler/diagnosticMessages.json | 2 +- ...portDeclarationInModuleDeclaration2.errors.txt | 15 ++++++--------- .../importDeclarationInModuleDeclaration2.symbols | 7 +++---- .../importDeclarationInModuleDeclaration2.types | 7 ++++--- .../reference/plainJSGrammarErrors.errors.txt | 4 ++-- .../importDeclarationInModuleDeclaration2.ts | 4 ++-- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1a36cc0240480..fb0be8ebb989b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -7237,7 +7237,7 @@ "category": "Error", "code": 18041 }, - "An import declaration can only be used at the top level of a module": { + "An import declaration can only be used at the top level of a module.": { "category": "Error", "code": 18042 } diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt index 1f8ebf8e42f47..91b2b621139dd 100644 --- a/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt @@ -1,12 +1,9 @@ -tests/cases/compiler/check.js(1,8): error TS8006: 'module' declarations can only be used in TypeScript files. -tests/cases/compiler/check.js(2,25): error TS1147: Import declarations in a namespace cannot reference a module. +tests/cases/compiler/check.js(2,5): error TS18042: An import declaration can only be used at the top level of a module. -==== tests/cases/compiler/check.js (2 errors) ==== - module m2 { - ~~ -!!! error TS8006: 'module' declarations can only be used in TypeScript files. - import m3 = require("use_glo_M1_public"); - ~~~~~~~~~~~~~~~~~~~ -!!! error TS1147: Import declarations in a namespace cannot reference a module. +==== tests/cases/compiler/check.js (1 errors) ==== + function container() { + import "fs"; + ~~~~~~ +!!! error TS18042: An import declaration can only be used at the top level of a module. } \ No newline at end of file diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols b/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols index 0f8967900a8aa..ae8fbc1291a0c 100644 --- a/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.symbols @@ -1,7 +1,6 @@ === tests/cases/compiler/check.js === -module m2 { ->m2 : Symbol(m2, Decl(check.js, 0, 0)) +function container() { +>container : Symbol(container, Decl(check.js, 0, 0)) - import m3 = require("use_glo_M1_public"); ->m3 : Symbol(m3, Decl(check.js, 0, 11)) + import "fs"; } diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.types b/tests/baselines/reference/importDeclarationInModuleDeclaration2.types index 482cae6d0d173..a100c277dca5e 100644 --- a/tests/baselines/reference/importDeclarationInModuleDeclaration2.types +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.types @@ -1,5 +1,6 @@ === tests/cases/compiler/check.js === -module m2 { - import m3 = require("use_glo_M1_public"); ->m3 : any +function container() { +>container : () => void + + import "fs"; } diff --git a/tests/baselines/reference/plainJSGrammarErrors.errors.txt b/tests/baselines/reference/plainJSGrammarErrors.errors.txt index d5453e3023a2c..2cb1f37178c51 100644 --- a/tests/baselines/reference/plainJSGrammarErrors.errors.txt +++ b/tests/baselines/reference/plainJSGrammarErrors.errors.txt @@ -45,7 +45,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(66,1): error TS1042: 'asyn tests/cases/conformance/salsa/plainJSGrammarErrors.js(67,1): error TS1191: An import declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(68,1): error TS1193: An export declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS18042: An import declaration can only be used at the top level of a module +tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS18042: An import declaration can only be used at the top level of a module. tests/cases/conformance/salsa/plainJSGrammarErrors.js(72,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/conformance/salsa/plainJSGrammarErrors.js(75,5): error TS1184: Modifiers cannot appear here. tests/cases/conformance/salsa/plainJSGrammarErrors.js(78,5): error TS1042: 'static' modifier cannot be used here. @@ -270,7 +270,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'r !!! error TS1233: An export declaration can only be used at the top level of a module. import 'fs' ~~~~~~ -!!! error TS18042: An import declaration can only be used at the top level of a module +!!! error TS18042: An import declaration can only be used at the top level of a module. export default 12 ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. diff --git a/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts b/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts index 68c373ffdc5ad..2df0aadb8ae67 100644 --- a/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts +++ b/tests/cases/compiler/importDeclarationInModuleDeclaration2.ts @@ -3,6 +3,6 @@ // @checkJs: true // @filename: check.js -module m2 { - import m3 = require("use_glo_M1_public"); +function container() { + import "fs"; } \ No newline at end of file From 3e1c3c0db4a209d215094b62af6d2f36550f6947 Mon Sep 17 00:00:00 2001 From: islandryu Date: Thu, 13 Jan 2022 02:41:52 +0900 Subject: [PATCH 3/6] change error code of "An import declaration can only be used at the top level of a module." --- src/compiler/diagnosticMessages.json | 9 ++++----- .../importDeclarationInModuleDeclaration2.errors.txt | 4 ++-- .../baselines/reference/plainJSGrammarErrors.errors.txt | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index fb0be8ebb989b..700db7f6a7e74 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1409,7 +1409,10 @@ "category": "Error", "code": 1471 }, - + "An import declaration can only be used at the top level of a module.": { + "category": "Error", + "code": 1472 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", "code": 2200 @@ -7236,9 +7239,5 @@ "A 'return' statement cannot be used inside a class static block.": { "category": "Error", "code": 18041 - }, - "An import declaration can only be used at the top level of a module.": { - "category": "Error", - "code": 18042 } } diff --git a/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt index 91b2b621139dd..872f28a4383b7 100644 --- a/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt +++ b/tests/baselines/reference/importDeclarationInModuleDeclaration2.errors.txt @@ -1,9 +1,9 @@ -tests/cases/compiler/check.js(2,5): error TS18042: An import declaration can only be used at the top level of a module. +tests/cases/compiler/check.js(2,5): error TS1472: An import declaration can only be used at the top level of a module. ==== tests/cases/compiler/check.js (1 errors) ==== function container() { import "fs"; ~~~~~~ -!!! error TS18042: An import declaration can only be used at the top level of a module. +!!! error TS1472: An import declaration can only be used at the top level of a module. } \ No newline at end of file diff --git a/tests/baselines/reference/plainJSGrammarErrors.errors.txt b/tests/baselines/reference/plainJSGrammarErrors.errors.txt index 2cb1f37178c51..1e2fd974e174e 100644 --- a/tests/baselines/reference/plainJSGrammarErrors.errors.txt +++ b/tests/baselines/reference/plainJSGrammarErrors.errors.txt @@ -45,7 +45,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(66,1): error TS1042: 'asyn tests/cases/conformance/salsa/plainJSGrammarErrors.js(67,1): error TS1191: An import declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(68,1): error TS1193: An export declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS18042: An import declaration can only be used at the top level of a module. +tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS1472: An import declaration can only be used at the top level of a module. tests/cases/conformance/salsa/plainJSGrammarErrors.js(72,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/conformance/salsa/plainJSGrammarErrors.js(75,5): error TS1184: Modifiers cannot appear here. tests/cases/conformance/salsa/plainJSGrammarErrors.js(78,5): error TS1042: 'static' modifier cannot be used here. @@ -270,7 +270,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'r !!! error TS1233: An export declaration can only be used at the top level of a module. import 'fs' ~~~~~~ -!!! error TS18042: An import declaration can only be used at the top level of a module. +!!! error TS1472: An import declaration can only be used at the top level of a module. export default 12 ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. From ea3344e754bf1088606abb611f16b66e771f8db9 Mon Sep 17 00:00:00 2001 From: islandryu Date: Wed, 2 Feb 2022 13:38:45 +0900 Subject: [PATCH 4/6] Separate js and ts files for export errors in module. --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 7 +++++-- .../moduleElementsInWrongContext.errors.txt | 12 ++++++------ .../moduleElementsInWrongContext2.errors.txt | 12 ++++++------ .../moduleElementsInWrongContext3.errors.txt | 12 ++++++------ .../reference/plainJSGrammarErrors.errors.txt | 4 ++-- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1eb3fce3fb249..def2d60b73a36 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39868,7 +39868,7 @@ namespace ts { } function checkExportDeclaration(node: ExportDeclaration) { - if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module)) { + if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { // If we hit an export in an illegal context, just bail out to avoid cascading errors. return; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 700db7f6a7e74..160358ad1d2c3 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -731,7 +731,7 @@ "category": "Error", "code": 1232 }, - "An export declaration can only be used at the top level of a module.": { + "An export declaration can only be used at the top level of a namespace or module.": { "category": "Error", "code": 1233 }, @@ -1413,6 +1413,10 @@ "category": "Error", "code": 1472 }, + "An export declaration can only be used at the top level of a module.": { + "category": "Error", + "code": 1473 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", "code": 2200 @@ -4164,7 +4168,6 @@ "category": "Message", "code": 6041 }, - "Generates corresponding '.map' file.": { "category": "Message", "code": 6043 diff --git a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt index 22872e2566e8f..a28305843eb29 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt @@ -3,9 +3,9 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A names tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(18,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(19,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(18,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(19,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext.ts(20,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext.ts(21,5): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext.ts(22,5): error TS1184: Modifiers cannot appear here. @@ -46,13 +46,13 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An imp function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. diff --git a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt index 3e8c9d863f8b9..7c2c2ce485a8c 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt @@ -3,9 +3,9 @@ tests/cases/compiler/moduleElementsInWrongContext2.ts(3,5): error TS1235: A name tests/cases/compiler/moduleElementsInWrongContext2.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext2.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext2.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext2.ts(17,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(18,5): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(19,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(17,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(18,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(19,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext2.ts(20,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext2.ts(21,5): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext2.ts(22,5): error TS1184: Modifiers cannot appear here. @@ -46,13 +46,13 @@ tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An im function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. diff --git a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt index da829ef35decb..5ec20c70851e8 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt @@ -3,9 +3,9 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(4,9): error TS1235: A name tests/cases/compiler/moduleElementsInWrongContext3.ts(8,9): error TS1235: A namespace declaration is only allowed at the top level of a module. tests/cases/compiler/moduleElementsInWrongContext3.ts(10,9): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext3.ts(14,9): error TS1231: An export assignment must be at the top level of a file or module declaration. -tests/cases/compiler/moduleElementsInWrongContext3.ts(18,9): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(19,9): error TS1233: An export declaration can only be used at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(20,9): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(18,9): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(19,9): error TS1233: An export declaration can only be used at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(20,9): error TS1233: An export declaration can only be used at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext3.ts(21,9): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext3.ts(22,9): error TS1184: Modifiers cannot appear here. tests/cases/compiler/moduleElementsInWrongContext3.ts(23,9): error TS1184: Modifiers cannot appear here. @@ -47,13 +47,13 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An im function foo() { } export * from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { foo }; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export { baz as b } from "ambient"; ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1233: An export declaration can only be used at the top level of a namespace or module. export default v; ~~~~~~ !!! error TS1258: A default export must be at the top level of a file or module declaration. diff --git a/tests/baselines/reference/plainJSGrammarErrors.errors.txt b/tests/baselines/reference/plainJSGrammarErrors.errors.txt index 1e2fd974e174e..0592544e0fd16 100644 --- a/tests/baselines/reference/plainJSGrammarErrors.errors.txt +++ b/tests/baselines/reference/plainJSGrammarErrors.errors.txt @@ -44,7 +44,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(65,1): error TS1042: 'asyn tests/cases/conformance/salsa/plainJSGrammarErrors.js(66,1): error TS1042: 'async' modifier cannot be used here. tests/cases/conformance/salsa/plainJSGrammarErrors.js(67,1): error TS1191: An import declaration cannot have modifiers. tests/cases/conformance/salsa/plainJSGrammarErrors.js(68,1): error TS1193: An export declaration cannot have modifiers. -tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1233: An export declaration can only be used at the top level of a module. +tests/cases/conformance/salsa/plainJSGrammarErrors.js(70,5): error TS1473: An export declaration can only be used at the top level of a module. tests/cases/conformance/salsa/plainJSGrammarErrors.js(71,5): error TS1472: An import declaration can only be used at the top level of a module. tests/cases/conformance/salsa/plainJSGrammarErrors.js(72,5): error TS1258: A default export must be at the top level of a file or module declaration. tests/cases/conformance/salsa/plainJSGrammarErrors.js(75,5): error TS1184: Modifiers cannot appear here. @@ -267,7 +267,7 @@ tests/cases/conformance/salsa/plainJSGrammarErrors.js(207,1): error TS1108: A 'r function nestedExports() { export { staticParam } ~~~~~~ -!!! error TS1233: An export declaration can only be used at the top level of a module. +!!! error TS1473: An export declaration can only be used at the top level of a module. import 'fs' ~~~~~~ !!! error TS1472: An import declaration can only be used at the top level of a module. From 151604a21628629b864c2cfb6e786da05896a040 Mon Sep 17 00:00:00 2001 From: islandryu Date: Fri, 4 Feb 2022 00:48:53 +0900 Subject: [PATCH 5/6] Change non-top-level error in namespace --- src/compiler/checker.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- .../reference/labeledStatementWithLabel.errors.txt | 8 ++++---- .../labeledStatementWithLabel_es2015.errors.txt | 8 ++++---- .../labeledStatementWithLabel_strict.errors.txt | 8 ++++---- .../moduleElementsInWrongContext.errors.txt | 12 ++++++------ .../moduleElementsInWrongContext2.errors.txt | 12 ++++++------ .../moduleElementsInWrongContext3.errors.txt | 12 ++++++------ 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index def2d60b73a36..71aa39c25c485 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39502,7 +39502,7 @@ namespace ts { const isAmbientExternalModule: boolean = isAmbientModule(node); const contextErrorMessage = isAmbientExternalModule ? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file - : Diagnostics.A_namespace_declaration_is_only_allowed_at_the_top_level_of_a_module; + : Diagnostics.A_namespace_declaration_is_only_allowed_at_the_top_level_of_a_namespace_or_module; if (checkGrammarModuleElementContext(node, contextErrorMessage)) { // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. return; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 160358ad1d2c3..3d56e725504cd 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -739,7 +739,7 @@ "category": "Error", "code": 1234 }, - "A namespace declaration is only allowed at the top level of a module.": { + "A namespace declaration is only allowed at the top level of a namespace or module.": { "category": "Error", "code": 1235 }, diff --git a/tests/baselines/reference/labeledStatementWithLabel.errors.txt b/tests/baselines/reference/labeledStatementWithLabel.errors.txt index d2487beee4bd8..64dd004249f81 100644 --- a/tests/baselines/reference/labeledStatementWithLabel.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. ==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts (2 errors) ==== @@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.t label: module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: namespace N {} ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: type T = {} \ No newline at end of file diff --git a/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt b/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt index a1abad24d7723..c2945254121ed 100644 --- a/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. ==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts (2 errors) ==== @@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_e label: module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: namespace N {} ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: type T = {} \ No newline at end of file diff --git a/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt b/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt index da8cf271d878c..01f09aeab4df1 100644 --- a/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt +++ b/tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt @@ -8,9 +8,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(9,1): error TS1344: 'A label is not allowed here. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(10,1): error TS1344: 'A label is not allowed here. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,1): error TS1344: 'A label is not allowed here. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,1): error TS1344: 'A label is not allowed here. -tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(14,1): error TS1344: 'A label is not allowed here. @@ -48,12 +48,12 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s ~~~~~ !!! error TS1344: 'A label is not allowed here. ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: namespace N {} ~~~~~ !!! error TS1344: 'A label is not allowed here. ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. label: type T = {} ~~~~~ !!! error TS1344: 'A label is not allowed here. diff --git a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt index a28305843eb29..4226266e82992 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext.errors.txt @@ -1,6 +1,6 @@ -tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. @@ -21,16 +21,16 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An imp { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. declare module "ambient" { ~~~~~~~ diff --git a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt index 7c2c2ce485a8c..22fa234747db1 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext2.errors.txt @@ -1,6 +1,6 @@ -tests/cases/compiler/moduleElementsInWrongContext2.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext2.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext2.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext2.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext2.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext2.ts(17,5): error TS1233: An export declaration can only be used at the top level of a namespace or module. @@ -21,16 +21,16 @@ tests/cases/compiler/moduleElementsInWrongContext2.ts(28,5): error TS1232: An im function blah () { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. declare module "ambient" { ~~~~~~~ diff --git a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt index 5ec20c70851e8..55d9ec47c03bf 100644 --- a/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt +++ b/tests/baselines/reference/moduleElementsInWrongContext3.errors.txt @@ -1,6 +1,6 @@ -tests/cases/compiler/moduleElementsInWrongContext3.ts(3,9): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(4,9): error TS1235: A namespace declaration is only allowed at the top level of a module. -tests/cases/compiler/moduleElementsInWrongContext3.ts(8,9): error TS1235: A namespace declaration is only allowed at the top level of a module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(3,9): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(4,9): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. +tests/cases/compiler/moduleElementsInWrongContext3.ts(8,9): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. tests/cases/compiler/moduleElementsInWrongContext3.ts(10,9): error TS1234: An ambient module declaration is only allowed at the top level in a file. tests/cases/compiler/moduleElementsInWrongContext3.ts(14,9): error TS1231: An export assignment must be at the top level of a file or module declaration. tests/cases/compiler/moduleElementsInWrongContext3.ts(18,9): error TS1233: An export declaration can only be used at the top level of a namespace or module. @@ -22,16 +22,16 @@ tests/cases/compiler/moduleElementsInWrongContext3.ts(29,9): error TS1232: An im { module M { } ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export namespace N { ~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. export interface I { } } namespace Q.K { } ~~~~~~~~~ -!!! error TS1235: A namespace declaration is only allowed at the top level of a module. +!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module. declare module "ambient" { ~~~~~~~ From de5461f2d7e329525a7e32d4c4559f293ebaef2f Mon Sep 17 00:00:00 2001 From: islandryu Date: Fri, 4 Feb 2022 12:32:12 +0900 Subject: [PATCH 6/6] format --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 71aa39c25c485..477c2f5495eae 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -39795,7 +39795,7 @@ namespace ts { } function checkImportDeclaration(node: ImportDeclaration) { - if (checkGrammarModuleElementContext(node,isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { + if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. return; }