Skip to content

Commit bd6d2c0

Browse files
authored
[Release-2.0] Fix 9829 : do not report error using import, export, module augmentation in d.t.s (#9894)
* Only error in non-declaration file * Add tests and baselines * Addess PR: get the first non-ambient external module file * Rename test file and update baseline * Add tests and baselines * Update baselines
1 parent da21296 commit bd6d2c0

10 files changed

+96
-7
lines changed

src/compiler/program.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ namespace ts {
22632263
const languageVersion = options.target || ScriptTarget.ES3;
22642264
const outFile = options.outFile || options.out;
22652265

2266-
const firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined);
2266+
const firstNonAmbientExternalModuleSourceFile = forEach(files, f => isExternalModule(f) && !isDeclarationFile(f) ? f : undefined);
22672267
if (options.isolatedModules) {
22682268
if (options.module === ModuleKind.None && languageVersion < ScriptTarget.ES6) {
22692269
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher));
@@ -2275,20 +2275,20 @@ namespace ts {
22752275
programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided));
22762276
}
22772277
}
2278-
else if (firstExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && options.module === ModuleKind.None) {
2278+
else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && options.module === ModuleKind.None) {
22792279
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
2280-
const span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
2281-
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
2280+
const span = getErrorSpanForNode(firstNonAmbientExternalModuleSourceFile, firstNonAmbientExternalModuleSourceFile.externalModuleIndicator);
2281+
programDiagnostics.add(createFileDiagnostic(firstNonAmbientExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
22822282
}
22832283

22842284
// Cannot specify module gen that isn't amd or system with --out
22852285
if (outFile) {
22862286
if (options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {
22872287
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile"));
22882288
}
2289-
else if (options.module === undefined && firstExternalModuleSourceFile) {
2290-
const span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
2291-
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_using_option_0_unless_the_module_flag_is_amd_or_system, options.out ? "out" : "outFile"));
2289+
else if (options.module === undefined && firstNonAmbientExternalModuleSourceFile) {
2290+
const span = getErrorSpanForNode(firstNonAmbientExternalModuleSourceFile, firstNonAmbientExternalModuleSourceFile.externalModuleIndicator);
2291+
programDiagnostics.add(createFileDiagnostic(firstNonAmbientExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_using_option_0_unless_the_module_flag_is_amd_or_system, options.out ? "out" : "outFile"));
22922292
}
22932293
}
22942294

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/0.d.ts ===
2+
3+
export = a;
4+
>a : Symbol(a, Decl(0.d.ts, 2, 11))
5+
6+
declare var a: number;
7+
>a : Symbol(a, Decl(0.d.ts, 2, 11))
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/0.d.ts ===
2+
3+
export = a;
4+
>a : number
5+
6+
declare var a: number;
7+
>a : number
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/1.ts(2,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
2+
3+
4+
==== tests/cases/compiler/1.ts (1 errors) ====
5+
6+
export var j = "hello"; // error
7+
~~~~~~~~~~~~~~~~~~~~~~~
8+
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
9+
10+
==== tests/cases/compiler/0.d.ts (0 errors) ====
11+
export = a;
12+
declare var a: number;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/noErrorUsingImportExportModuleAugmentationInDeclarationFile2.ts] ////
2+
3+
//// [1.ts]
4+
5+
export var j = "hello"; // error
6+
7+
//// [0.d.ts]
8+
export = a;
9+
declare var a: number;
10+
11+
//// [1.js]
12+
"use strict";
13+
exports.j = "hello"; // error
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/1.ts(1,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
2+
3+
4+
==== tests/cases/compiler/0.d.ts (0 errors) ====
5+
6+
export = a;
7+
declare var a: number;
8+
9+
==== tests/cases/compiler/1.ts (1 errors) ====
10+
export var j = "hello"; // error
11+
~~~~~~~~~~~~~~~~~~~~~~~
12+
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
13+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/noErrorUsingImportExportModuleAugmentationInDeclarationFile3.ts] ////
2+
3+
//// [0.d.ts]
4+
5+
export = a;
6+
declare var a: number;
7+
8+
//// [1.ts]
9+
export var j = "hello"; // error
10+
11+
12+
//// [1.js]
13+
"use strict";
14+
exports.j = "hello"; // error
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @module: none
2+
// @filename: 0.d.ts
3+
4+
export = a;
5+
declare var a: number;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @module: none
2+
3+
// @filename: 1.ts
4+
export var j = "hello"; // error
5+
6+
// @filename: 0.d.ts
7+
export = a;
8+
declare var a: number;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @module: none
2+
3+
// @filename: 0.d.ts
4+
export = a;
5+
declare var a: number;
6+
7+
// @filename: 1.ts
8+
export var j = "hello"; // error

0 commit comments

Comments
 (0)