Skip to content

fix(47076):Fix error term of declaration in modules #47087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -39655,7 +39655,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_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;
@@ -39993,7 +39993,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;
}
@@ -40027,7 +40027,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;
}
@@ -40066,7 +40066,7 @@ namespace ts {
}

function checkExportDeclaration(node: ExportDeclaration) {
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_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;
}
16 changes: 12 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
@@ -727,19 +727,19 @@
"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 namespace or module.": {
"category": "Error",
"code": 1233
},
"An ambient module declaration is only allowed at the top level in a file.": {
"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 namespace or module.": {
"category": "Error",
"code": 1235
},
@@ -1413,7 +1413,15 @@
"category": "Error",
"code": 1472
},

"An import declaration can only be used at the top level of a module.": {
"category": "Error",
"code": 1473
},
"An export declaration can only be used at the top level of a module.": {
"category": "Error",
"code": 1474
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
"code": 2200
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
@@ -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,
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/compiler/check.js(2,5): error TS1473: 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 TS1473: An import declaration can only be used at the top level of a module.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/check.js ===
function container() {
>container : Symbol(container, Decl(check.js, 0, 0))

import "fs";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/check.js ===
function container() {
>container : () => void

import "fs";
}
Original file line number Diff line number Diff line change
@@ -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 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 in a namespace or 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 in a namespace or module.
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
label: type T = {}

Original file line number Diff line number Diff line change
@@ -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 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 in a namespace or 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 in a namespace or module.
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
label: type T = {}

Original file line number Diff line number Diff line change
@@ -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 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 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 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 in a namespace or 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 in a namespace or 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.
48 changes: 24 additions & 24 deletions tests/baselines/reference/moduleElementsInWrongContext.errors.txt
Original file line number Diff line number Diff line change
@@ -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 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 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 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.
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 namespace or 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 namespace or 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 namespace or 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 namespace or 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 namespace or 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 namespace or 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.
}

Loading