From 1fbe20fe75fca9d4787e4281f0a717c0fec1e72f Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Tue, 21 Jan 2020 20:49:38 +0100 Subject: [PATCH] resolveJsonModule affectsModuleResolution (#36290) * resolveJsonModule affectsModuleResolutionFixes: #36251 * fix lint --- src/compiler/commandLineParser.ts | 1 + .../unittests/tscWatch/programUpdates.ts | 28 ++++ ...solution-when-resolveJsonModule-changes.js | 123 ++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 tests/baselines/reference/tscWatch/programUpdates/updates-moduleResolution-when-resolveJsonModule-changes.js diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 7440358709e..be72b205131 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -766,6 +766,7 @@ namespace ts { { name: "resolveJsonModule", type: "boolean", + affectsModuleResolution: true, category: Diagnostics.Advanced_Options, description: Diagnostics.Include_modules_imported_with_json_extension }, diff --git a/src/testRunner/unittests/tscWatch/programUpdates.ts b/src/testRunner/unittests/tscWatch/programUpdates.ts index b1074c7b9a2..163ba1085ea 100644 --- a/src/testRunner/unittests/tscWatch/programUpdates.ts +++ b/src/testRunner/unittests/tscWatch/programUpdates.ts @@ -1228,6 +1228,34 @@ export function f(p: C) { return p; }` ] }); + verifyTscWatch({ + scenario, + subScenario: "updates moduleResolution when resolveJsonModule changes", + commandLineArgs: ["-w"], + sys: () => { + const aFile: File = { + path: `${projectRoot}/a.ts`, + content: `import * as data from './data.json'` + }; + const jsonFile: File = { + path: `${projectRoot}/data.json`, + content: `{ "foo": 1 }` + }; + const config: File = { + path: `${projectRoot}/tsconfig.json`, + content: JSON.stringify({ compilerOptions: { moduleResolution: "node" } }) + }; + return createWatchedSystem([aFile, jsonFile, config, libFile], { currentDirectory: projectRoot }); + }, + changes: [ + sys => { + sys.writeFile(`${projectRoot}/tsconfig.json`, JSON.stringify({ compilerOptions: { moduleResolution: "node", resolveJsonModule: true } })); + sys.runQueuedTimeoutCallbacks(); + return "Enable resolveJsonModule"; + }, + ] + }); + verifyTscWatch({ scenario, subScenario: "updates errors when ambient modules of program changes", diff --git a/tests/baselines/reference/tscWatch/programUpdates/updates-moduleResolution-when-resolveJsonModule-changes.js b/tests/baselines/reference/tscWatch/programUpdates/updates-moduleResolution-when-resolveJsonModule-changes.js new file mode 100644 index 00000000000..5db992857c3 --- /dev/null +++ b/tests/baselines/reference/tscWatch/programUpdates/updates-moduleResolution-when-resolveJsonModule-changes.js @@ -0,0 +1,123 @@ +/a/lib/tsc.js -w +//// [/user/username/projects/myproject/a.ts] +import * as data from './data.json' + +//// [/user/username/projects/myproject/data.json] +{ "foo": 1 } + +//// [/user/username/projects/myproject/tsconfig.json] +{"compilerOptions":{"moduleResolution":"node"}} + +//// [/a/lib/lib.d.ts] +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } + +//// [/user/username/projects/myproject/a.js] +"use strict"; +exports.__esModule = true; + + + +Output:: +>> Screen clear +12:00:23 AM - Starting compilation in watch mode... + + +a.ts(1,23): error TS2732: Cannot find module './data.json'. Consider using '--resolveJsonModule' to import module with '.json' extension + + +12:00:26 AM - Found 1 error. Watching for file changes. + + +Program root files: ["/user/username/projects/myproject/a.ts"] +Program options: {"moduleResolution":2,"watch":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +Program files:: +/a/lib/lib.d.ts +/user/username/projects/myproject/a.ts + +Semantic diagnostics in builder refreshed for:: +/a/lib/lib.d.ts +/user/username/projects/myproject/a.ts + +WatchedFiles:: +/user/username/projects/myproject/tsconfig.json: + {"pollingInterval":250} +/user/username/projects/myproject/a.ts: + {"pollingInterval":250} +/a/lib/lib.d.ts: + {"pollingInterval":250} + +FsWatches:: +/user/username/projects/myproject: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +FsWatchesRecursive:: +/user/username/projects/myproject/data.json: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject/node_modules/@types: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +exitCode:: ExitStatus.undefined + +Change:: Enable resolveJsonModule + +//// [/user/username/projects/myproject/tsconfig.json] +{"compilerOptions":{"moduleResolution":"node","resolveJsonModule":true}} + +//// [/user/username/projects/myproject/a.js] file written with same contents + +Output:: +>> Screen clear +12:00:30 AM - File change detected. Starting incremental compilation... + + + +12:00:34 AM - Found 0 errors. Watching for file changes. + + +Program root files: ["/user/username/projects/myproject/a.ts"] +Program options: {"moduleResolution":2,"resolveJsonModule":true,"watch":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"} +Program files:: +/a/lib/lib.d.ts +/user/username/projects/myproject/data.json +/user/username/projects/myproject/a.ts + +Semantic diagnostics in builder refreshed for:: +/a/lib/lib.d.ts +/user/username/projects/myproject/data.json +/user/username/projects/myproject/a.ts + +WatchedFiles:: +/user/username/projects/myproject/tsconfig.json: + {"pollingInterval":250} +/user/username/projects/myproject/a.ts: + {"pollingInterval":250} +/a/lib/lib.d.ts: + {"pollingInterval":250} +/user/username/projects/myproject/data.json: + {"pollingInterval":250} + +FsWatches:: +/user/username/projects/myproject: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +FsWatchesRecursive:: +/user/username/projects/myproject: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject/data.json: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} +/user/username/projects/myproject/node_modules/@types: + {"fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}} + +exitCode:: ExitStatus.undefined