From 24febc2445c39c16ab1b018e18b067b942dcef83 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 29 Oct 2018 11:56:49 -0700 Subject: [PATCH] Allow to combine --resolveJsonModule with --isolatedModules (#28207) --- src/compiler/program.ts | 6 +++--- .../reference/isolatedModules_resolveJsonModule.js | 12 ++++++++++++ .../isolatedModules_resolveJsonModule.symbols | 8 ++++++++ .../isolatedModules_resolveJsonModule.types | 8 ++++++++ .../compiler/isolatedModules_resolveJsonModule.ts | 8 ++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/isolatedModules_resolveJsonModule.js create mode 100644 tests/baselines/reference/isolatedModules_resolveJsonModule.symbols create mode 100644 tests/baselines/reference/isolatedModules_resolveJsonModule.types create mode 100644 tests/cases/compiler/isolatedModules_resolveJsonModule.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 3e927c0b75f71..85c6b27f4ec7d 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2667,13 +2667,13 @@ namespace ts { const languageVersion = options.target || ScriptTarget.ES3; const outFile = options.outFile || options.out; - const firstNonAmbientExternalModuleSourceFile = forEach(files, f => isExternalModule(f) && !f.isDeclarationFile ? f : undefined); + const firstNonAmbientExternalModuleSourceFile = find(files, f => isExternalModule(f) && !f.isDeclarationFile); if (options.isolatedModules) { if (options.module === ModuleKind.None && languageVersion < ScriptTarget.ES2015) { createDiagnosticForOptionName(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher, "isolatedModules", "target"); } - const firstNonExternalModuleSourceFile = forEach(files, f => !isExternalModule(f) && !f.isDeclarationFile ? f : undefined); + const firstNonExternalModuleSourceFile = find(files, f => !isExternalModule(f) && !f.isDeclarationFile && f.scriptKind !== ScriptKind.JSON); 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)); @@ -2716,7 +2716,7 @@ namespace ts { const dir = getCommonSourceDirectory(); // If we failed to find a good common directory, but outDir is specified and at least one of our files is on a windows drive/URL/other resource, add a failure - if (options.outDir && dir === "" && forEach(files, file => getRootLength(file.fileName) > 1)) { + if (options.outDir && dir === "" && files.some(file => getRootLength(file.fileName) > 1)) { createDiagnosticForOptionName(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files, "outDir"); } } diff --git a/tests/baselines/reference/isolatedModules_resolveJsonModule.js b/tests/baselines/reference/isolatedModules_resolveJsonModule.js new file mode 100644 index 0000000000000..cdff02c07f0b7 --- /dev/null +++ b/tests/baselines/reference/isolatedModules_resolveJsonModule.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/isolatedModules_resolveJsonModule.ts] //// + +//// [a.ts] +import j = require("./j.json"); + +//// [j.json] +{} + + +//// [a.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/isolatedModules_resolveJsonModule.symbols b/tests/baselines/reference/isolatedModules_resolveJsonModule.symbols new file mode 100644 index 0000000000000..6282706d3aa92 --- /dev/null +++ b/tests/baselines/reference/isolatedModules_resolveJsonModule.symbols @@ -0,0 +1,8 @@ +=== /a.ts === +import j = require("./j.json"); +>j : Symbol(j, Decl(a.ts, 0, 0)) + +=== /j.json === +{} +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModules_resolveJsonModule.types b/tests/baselines/reference/isolatedModules_resolveJsonModule.types new file mode 100644 index 0000000000000..c63dd52da1a8b --- /dev/null +++ b/tests/baselines/reference/isolatedModules_resolveJsonModule.types @@ -0,0 +1,8 @@ +=== /a.ts === +import j = require("./j.json"); +>j : {} + +=== /j.json === +{} +>{} : {} + diff --git a/tests/cases/compiler/isolatedModules_resolveJsonModule.ts b/tests/cases/compiler/isolatedModules_resolveJsonModule.ts new file mode 100644 index 0000000000000..51eec55d17210 --- /dev/null +++ b/tests/cases/compiler/isolatedModules_resolveJsonModule.ts @@ -0,0 +1,8 @@ +// @isolatedModules: true +// @resolveJsonModule: true + +// @Filename: /a.ts +import j = require("./j.json"); + +// @Filename: /j.json +{}