Skip to content

Commit a0406c7

Browse files
authored
Port Fix 9894 to master (microsoft#9896)
* 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 8b2ea39 commit a0406c7

10 files changed

+96
-7
lines changed

src/compiler/program.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,7 @@ namespace ts {
22522252
const languageVersion = options.target || ScriptTarget.ES3;
22532253
const outFile = options.outFile || options.out;
22542254

2255-
const firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined);
2255+
const firstNonAmbientExternalModuleSourceFile = forEach(files, f => isExternalModule(f) && !isDeclarationFile(f) ? f : undefined);
22562256
if (options.isolatedModules) {
22572257
if (options.module === ModuleKind.None && languageVersion < ScriptTarget.ES6) {
22582258
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher));
@@ -2264,20 +2264,20 @@ namespace ts {
22642264
programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided));
22652265
}
22662266
}
2267-
else if (firstExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && options.module === ModuleKind.None) {
2267+
else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && options.module === ModuleKind.None) {
22682268
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
2269-
const span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
2270-
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
2269+
const span = getErrorSpanForNode(firstNonAmbientExternalModuleSourceFile, firstNonAmbientExternalModuleSourceFile.externalModuleIndicator);
2270+
programDiagnostics.add(createFileDiagnostic(firstNonAmbientExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
22712271
}
22722272

22732273
// Cannot specify module gen that isn't amd or system with --out
22742274
if (outFile) {
22752275
if (options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {
22762276
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile"));
22772277
}
2278-
else if (options.module === undefined && firstExternalModuleSourceFile) {
2279-
const span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
2280-
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"));
2278+
else if (options.module === undefined && firstNonAmbientExternalModuleSourceFile) {
2279+
const span = getErrorSpanForNode(firstNonAmbientExternalModuleSourceFile, firstNonAmbientExternalModuleSourceFile.externalModuleIndicator);
2280+
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"));
22812281
}
22822282
}
22832283

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+
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+
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;
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
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+
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
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;
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;
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)