diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 986546772e..6001760c4a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -49,7 +49,7 @@ namespace ts { const compilerOptions = host.getCompilerOptions(); const languageVersion = compilerOptions.target || ScriptTarget.ES3; - const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS; + const modulekind = typeof compilerOptions.module === "number" ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS; const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System; const emitResolver = createResolver(); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 447fdd9799..1a8a53d517 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -439,6 +439,10 @@ "category": "Error", "code": 1147 }, + "Cannot compile modules unless the '--module' flag is provided. Consider setting the 'module' compiler option in a 'tsconfig.json' file.": { + "category": "Error", + "code": 1148 + }, "File name '{0}' differs from already included file name '{1}' only in casing": { "category": "Error", "code": 1149 @@ -2127,6 +2131,10 @@ "category": "Error", "code": 5042 }, + "Option 'isolatedModules' can only be used when either option '--module' is provided or option 'target' is 'ES2015' or higher.": { + "category": "Error", + "code": 5047 + }, "Option 'inlineSources' can only be used when either option '--inlineSourceMap' or option '--sourceMap' is provided.": { "category": "Error", "code": 5051 @@ -2163,7 +2171,7 @@ "category": "Error", "code": 5059 }, - + "Concatenate and emit output to single file.": { "category": "Message", diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 47b6d44ccb..3c75f31add 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1288,13 +1288,23 @@ namespace ts { const languageVersion = options.target || ScriptTarget.ES3; const outFile = options.outFile || options.out; + const firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined); if (options.isolatedModules) { + if (!options.module && 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)); + } + const firstNonExternalModuleSourceFile = forEach(files, f => !isExternalModule(f) && !isDeclarationFile(f) ? f : undefined); if (firstNonExternalModuleSourceFile) { const span = getErrorSpanForNode(firstNonExternalModuleSourceFile, firstNonExternalModuleSourceFile); 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) { + // 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_compile_modules_unless_the_module_flag_is_provided_Consider_setting_the_module_compiler_option_in_a_tsconfig_json_file)); + } // Cannot specify module gen target of es6 when below es6 if (options.module === ModuleKind.ES6 && languageVersion < ScriptTarget.ES6) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3406f40bf3..193807af1a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2011,7 +2011,7 @@ namespace ts { } export function getEmitModuleKind(compilerOptions: CompilerOptions) { - return compilerOptions.module ? + return typeof compilerOptions.module === "number" ? compilerOptions.module : getEmitScriptTarget(compilerOptions) === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS; }