Skip to content

Commit 95c22d1

Browse files
islandryusandersn
andauthored
fix(47076):Fix error term of declaration in modules (microsoft#47087)
* Fix error term of declaration in modules * fix test * change error code of "An import declaration can only be used at the top level of a module." * Separate js and ts files for export errors in module. * Change non-top-level error in namespace * format Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent 2d0a00d commit 95c22d1

14 files changed

+135
-98
lines changed

Diff for: src/compiler/checker.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -39683,7 +39683,7 @@ namespace ts {
3968339683
const isAmbientExternalModule: boolean = isAmbientModule(node);
3968439684
const contextErrorMessage = isAmbientExternalModule
3968539685
? Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file
39686-
: Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module;
39686+
: Diagnostics.A_namespace_declaration_is_only_allowed_at_the_top_level_of_a_namespace_or_module;
3968739687
if (checkGrammarModuleElementContext(node, contextErrorMessage)) {
3968839688
// If we hit a module declaration in an illegal context, just bail out to avoid cascading errors.
3968939689
return;
@@ -40021,7 +40021,7 @@ namespace ts {
4002140021
}
4002240022

4002340023
function checkImportDeclaration(node: ImportDeclaration) {
40024-
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
40024+
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)) {
4002540025
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
4002640026
return;
4002740027
}
@@ -40055,7 +40055,7 @@ namespace ts {
4005540055
}
4005640056

4005740057
function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
40058-
if (checkGrammarModuleElementContext(node, Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) {
40058+
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)) {
4005940059
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
4006040060
return;
4006140061
}
@@ -40094,7 +40094,7 @@ namespace ts {
4009440094
}
4009540095

4009640096
function checkExportDeclaration(node: ExportDeclaration) {
40097-
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) {
40097+
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)) {
4009840098
// If we hit an export in an illegal context, just bail out to avoid cascading errors.
4009940099
return;
4010040100
}

Diff for: src/compiler/diagnosticMessages.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -727,19 +727,19 @@
727727
"category": "Error",
728728
"code": 1231
729729
},
730-
"An import declaration can only be used in a namespace or module.": {
730+
"An import declaration can only be used at the top level of a namespace or module.": {
731731
"category": "Error",
732732
"code": 1232
733733
},
734-
"An export declaration can only be used in a module.": {
734+
"An export declaration can only be used at the top level of a namespace or module.": {
735735
"category": "Error",
736736
"code": 1233
737737
},
738738
"An ambient module declaration is only allowed at the top level in a file.": {
739739
"category": "Error",
740740
"code": 1234
741741
},
742-
"A namespace declaration is only allowed in a namespace or module.": {
742+
"A namespace declaration is only allowed at the top level of a namespace or module.": {
743743
"category": "Error",
744744
"code": 1235
745745
},
@@ -1413,7 +1413,15 @@
14131413
"category": "Error",
14141414
"code": 1472
14151415
},
1416-
1416+
"An import declaration can only be used at the top level of a module.": {
1417+
"category": "Error",
1418+
"code": 1473
1419+
},
1420+
"An export declaration can only be used at the top level of a module.": {
1421+
"category": "Error",
1422+
"code": 1474
1423+
},
1424+
14171425
"The types of '{0}' are incompatible between these types.": {
14181426
"category": "Error",
14191427
"code": 2200

Diff for: src/compiler/program.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,9 @@ namespace ts {
861861
Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block.code,
862862
Diagnostics.A_set_accessor_cannot_have_rest_parameter.code,
863863
Diagnostics.A_set_accessor_must_have_exactly_one_parameter.code,
864-
Diagnostics.An_export_declaration_can_only_be_used_in_a_module.code,
864+
Diagnostics.An_export_declaration_can_only_be_used_at_the_top_level_of_a_module.code,
865865
Diagnostics.An_export_declaration_cannot_have_modifiers.code,
866-
Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module.code,
866+
Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module.code,
867867
Diagnostics.An_import_declaration_cannot_have_modifiers.code,
868868
Diagnostics.An_object_member_cannot_be_declared_optional.code,
869869
Diagnostics.Argument_of_dynamic_import_cannot_be_spread_element.code,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/check.js(2,5): error TS1473: An import declaration can only be used at the top level of a module.
2+
3+
4+
==== tests/cases/compiler/check.js (1 errors) ====
5+
function container() {
6+
import "fs";
7+
~~~~~~
8+
!!! error TS1473: An import declaration can only be used at the top level of a module.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/check.js ===
2+
function container() {
3+
>container : Symbol(container, Decl(check.js, 0, 0))
4+
5+
import "fs";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/check.js ===
2+
function container() {
3+
>container : () => void
4+
5+
import "fs";
6+
}

Diff for: tests/baselines/reference/labeledStatementWithLabel.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
2-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
1+
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.
2+
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.
33

44

55
==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts (2 errors) ====
@@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.t
1515

1616
label: module M { }
1717
~~~~~~
18-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
18+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
1919
label: namespace N {}
2020
~~~~~~~~~
21-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
21+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
2222
label: type T = {}
2323

Diff for: tests/baselines/reference/labeledStatementWithLabel_es2015.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
2-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
1+
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.
2+
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.
33

44

55
==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts (2 errors) ====
@@ -15,9 +15,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_e
1515

1616
label: module M { }
1717
~~~~~~
18-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
18+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
1919
label: namespace N {}
2020
~~~~~~~~~
21-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
21+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
2222
label: type T = {}
2323

Diff for: tests/baselines/reference/labeledStatementWithLabel_strict.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s
88
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(9,1): error TS1344: 'A label is not allowed here.
99
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(10,1): error TS1344: 'A label is not allowed here.
1010
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,1): error TS1344: 'A label is not allowed here.
11-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
11+
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.
1212
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,1): error TS1344: 'A label is not allowed here.
13-
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
13+
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.
1414
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(14,1): error TS1344: 'A label is not allowed here.
1515

1616

@@ -48,12 +48,12 @@ tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_s
4848
~~~~~
4949
!!! error TS1344: 'A label is not allowed here.
5050
~~~~~~
51-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
51+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
5252
label: namespace N {}
5353
~~~~~
5454
!!! error TS1344: 'A label is not allowed here.
5555
~~~~~~~~~
56-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
56+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
5757
label: type T = {}
5858
~~~~~
5959
!!! error TS1344: 'A label is not allowed here.
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed in a namespace or module.
2-
tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed in a namespace or module.
3-
tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed in a namespace or module.
1+
tests/cases/compiler/moduleElementsInWrongContext.ts(2,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
2+
tests/cases/compiler/moduleElementsInWrongContext.ts(3,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
3+
tests/cases/compiler/moduleElementsInWrongContext.ts(7,5): error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
44
tests/cases/compiler/moduleElementsInWrongContext.ts(9,5): error TS1234: An ambient module declaration is only allowed at the top level in a file.
55
tests/cases/compiler/moduleElementsInWrongContext.ts(13,5): error TS1231: An export assignment must be at the top level of a file or module declaration.
6-
tests/cases/compiler/moduleElementsInWrongContext.ts(17,5): error TS1233: An export declaration can only be used in a module.
7-
tests/cases/compiler/moduleElementsInWrongContext.ts(18,5): error TS1233: An export declaration can only be used in a module.
8-
tests/cases/compiler/moduleElementsInWrongContext.ts(19,5): error TS1233: An export declaration can only be used in a module.
6+
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.
7+
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.
8+
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.
99
tests/cases/compiler/moduleElementsInWrongContext.ts(20,5): error TS1258: A default export must be at the top level of a file or module declaration.
1010
tests/cases/compiler/moduleElementsInWrongContext.ts(21,5): error TS1184: Modifiers cannot appear here.
1111
tests/cases/compiler/moduleElementsInWrongContext.ts(22,5): error TS1184: Modifiers cannot appear here.
12-
tests/cases/compiler/moduleElementsInWrongContext.ts(23,5): error TS1232: An import declaration can only be used in a namespace or module.
13-
tests/cases/compiler/moduleElementsInWrongContext.ts(24,5): error TS1232: An import declaration can only be used in a namespace or module.
14-
tests/cases/compiler/moduleElementsInWrongContext.ts(25,5): error TS1232: An import declaration can only be used in a namespace or module.
15-
tests/cases/compiler/moduleElementsInWrongContext.ts(26,5): error TS1232: An import declaration can only be used in a namespace or module.
16-
tests/cases/compiler/moduleElementsInWrongContext.ts(27,5): error TS1232: An import declaration can only be used in a namespace or module.
17-
tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An import declaration can only be used in a namespace or module.
12+
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.
13+
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.
14+
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.
15+
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.
16+
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.
17+
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.
1818

1919

2020
==== tests/cases/compiler/moduleElementsInWrongContext.ts (17 errors) ====
2121
{
2222
module M { }
2323
~~~~~~
24-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
24+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
2525
export namespace N {
2626
~~~~~~
27-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
27+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
2828
export interface I { }
2929
}
3030

3131
namespace Q.K { }
3232
~~~~~~~~~
33-
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
33+
!!! error TS1235: A namespace declaration is only allowed at the top level of a namespace or module.
3434

3535
declare module "ambient" {
3636
~~~~~~~
@@ -46,13 +46,13 @@ tests/cases/compiler/moduleElementsInWrongContext.ts(28,5): error TS1232: An imp
4646
function foo() { }
4747
export * from "ambient";
4848
~~~~~~
49-
!!! error TS1233: An export declaration can only be used in a module.
49+
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
5050
export { foo };
5151
~~~~~~
52-
!!! error TS1233: An export declaration can only be used in a module.
52+
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
5353
export { baz as b } from "ambient";
5454
~~~~~~
55-
!!! error TS1233: An export declaration can only be used in a module.
55+
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
5656
export default v;
5757
~~~~~~
5858
!!! 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
6464
!!! error TS1184: Modifiers cannot appear here.
6565
import I = M;
6666
~~~~~~
67-
!!! error TS1232: An import declaration can only be used in a namespace or module.
67+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
6868
import I2 = require("foo");
6969
~~~~~~
70-
!!! error TS1232: An import declaration can only be used in a namespace or module.
70+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
7171
import * as Foo from "ambient";
7272
~~~~~~
73-
!!! error TS1232: An import declaration can only be used in a namespace or module.
73+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
7474
import bar from "ambient";
7575
~~~~~~
76-
!!! error TS1232: An import declaration can only be used in a namespace or module.
76+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
7777
import { baz } from "ambient";
7878
~~~~~~
79-
!!! error TS1232: An import declaration can only be used in a namespace or module.
79+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
8080
import "ambient";
8181
~~~~~~
82-
!!! error TS1232: An import declaration can only be used in a namespace or module.
82+
!!! error TS1232: An import declaration can only be used at the top level of a namespace or module.
8383
}
8484

0 commit comments

Comments
 (0)