Skip to content

[Release-2.0] Fix 9829 : do not report error using import, export, module augmentation in d.t.s #9894

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 6 commits into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ namespace ts {
const languageVersion = options.target || ScriptTarget.ES3;
const outFile = options.outFile || options.out;

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/0.d.ts ===

export = a;
>a : Symbol(a, Decl(0.d.ts, 2, 11))

declare var a: number;
>a : Symbol(a, Decl(0.d.ts, 2, 11))

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/0.d.ts ===

export = a;
>a : number

declare var a: number;
>a : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tests/cases/compiler/1.ts(2,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.


==== tests/cases/compiler/1.ts (1 errors) ====

export var j = "hello"; // error
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

==== tests/cases/compiler/0.d.ts (0 errors) ====
export = a;
declare var a: number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/noErrorUsingImportExportModuleAugmentationInDeclarationFile2.ts] ////

//// [1.ts]

export var j = "hello"; // error

//// [0.d.ts]
export = a;
declare var a: number;

//// [1.js]
"use strict";
exports.j = "hello"; // error
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/1.ts(1,1): error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.


==== tests/cases/compiler/0.d.ts (0 errors) ====

export = a;
declare var a: number;

==== tests/cases/compiler/1.ts (1 errors) ====
export var j = "hello"; // error
~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/noErrorUsingImportExportModuleAugmentationInDeclarationFile3.ts] ////

//// [0.d.ts]

export = a;
declare var a: number;

//// [1.ts]
export var j = "hello"; // error


//// [1.js]
"use strict";
exports.j = "hello"; // error
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @module: none
// @filename: 0.d.ts

export = a;
declare var a: number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @module: none

// @filename: 1.ts
export var j = "hello"; // error

// @filename: 0.d.ts
export = a;
declare var a: number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @module: none

// @filename: 0.d.ts
export = a;
declare var a: number;

// @filename: 1.ts
export var j = "hello"; // error