From efc861c76dd6bcb1f2026f30d06ad2d0152d6a92 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 13 Jul 2017 07:10:35 -0700 Subject: [PATCH] Add logging to discoverTypings (#16652) --- src/harness/unittests/typingsInstaller.ts | 40 +++++++++++++++++-- .../typingsInstaller/typingsInstaller.ts | 1 + src/services/jsTyping.ts | 24 +++++++---- src/services/shims.ts | 1 + 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/harness/unittests/typingsInstaller.ts b/src/harness/unittests/typingsInstaller.ts index c23e1529984..5f6b1350578 100644 --- a/src/harness/unittests/typingsInstaller.ts +++ b/src/harness/unittests/typingsInstaller.ts @@ -44,6 +44,18 @@ namespace ts.projectSystem { }); } + function trackingLogger(): { log(message: string): void, finish(): string[] } { + const logs: string[] = []; + return { + log(message) { + logs.push(message); + }, + finish() { + return logs; + } + }; + } + import typingsName = TI.typingsName; describe("local module", () => { @@ -1031,7 +1043,12 @@ namespace ts.projectSystem { const cache = createMap(); const host = createServerHost([app, jquery, chroma]); - const result = JsTyping.discoverTypings(host, [app.path, jquery.path, chroma.path], getDirectoryPath(app.path), /*safeListPath*/ undefined, cache, { enable: true }, []); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(app.path), /*safeListPath*/ undefined, cache, { enable: true }, []); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from file names: ["jquery","chroma-js"]', + 'Result: {"cachedTypingPaths":[],"newTypingNames":["jquery","chroma-js"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}', + ]); assert.deepEqual(result.newTypingNames, ["jquery", "chroma-js"]); }); @@ -1044,7 +1061,12 @@ namespace ts.projectSystem { const cache = createMap(); for (const name of JsTyping.nodeCoreModuleList) { - const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from unresolved imports: ["node","somename"]', + 'Result: {"cachedTypingPaths":[],"newTypingNames":["node","somename"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}', + ]); assert.deepEqual(result.newTypingNames.sort(), ["node", "somename"]); } }); @@ -1060,7 +1082,12 @@ namespace ts.projectSystem { }; const host = createServerHost([f, node]); const cache = createMapFromTemplate({ "node": node.path }); - const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]); + assert.deepEqual(logger.finish(), [ + 'Inferred typings from unresolved imports: ["node","bar"]', + 'Result: {"cachedTypingPaths":["/a/b/node.d.ts"],"newTypingNames":["bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}', + ]); assert.deepEqual(result.cachedTypingPaths, [node.path]); assert.deepEqual(result.newTypingNames, ["bar"]); }); @@ -1080,7 +1107,12 @@ namespace ts.projectSystem { }; const host = createServerHost([app, a, b]); const cache = createMap(); - const result = JsTyping.discoverTypings(host, [app.path], getDirectoryPath(app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []); + const logger = trackingLogger(); + const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []); + assert.deepEqual(logger.finish(), [ + 'Searching for typing names in /node_modules; all files: ["/node_modules/a/package.json"]', + 'Result: {"cachedTypingPaths":[],"newTypingNames":["a"],"filesToWatch":["/bower_components","/node_modules"]}', + ]); assert.deepEqual(result, { cachedTypingPaths: [], newTypingNames: ["a"], // But not "b" diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index 71ad0171bf3..730257d2ccc 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -145,6 +145,7 @@ namespace ts.server.typingsInstaller { const discoverTypingsResult = JsTyping.discoverTypings( this.installTypingHost, + this.log.isEnabled() ? this.log.writeLine : undefined, req.fileNames, req.projectRootPath, this.safeListPath, diff --git a/src/services/jsTyping.ts b/src/services/jsTyping.ts index 60b29b81883..f31276b6498 100644 --- a/src/services/jsTyping.ts +++ b/src/services/jsTyping.ts @@ -51,6 +51,7 @@ namespace ts.JsTyping { */ export function discoverTypings( host: TypingResolutionHost, + log: ((message: string) => void) | undefined, fileNames: string[], projectRootPath: Path, safeListPath: Path, @@ -107,8 +108,9 @@ namespace ts.JsTyping { // add typings for unresolved imports if (unresolvedImports) { - for (const moduleId of unresolvedImports) { - const typingName = nodeCoreModules.has(moduleId) ? "node" : moduleId; + const x = unresolvedImports.map(moduleId => nodeCoreModules.has(moduleId) ? "node" : moduleId); + if (x.length && log) log(`Inferred typings from unresolved imports: ${JSON.stringify(x)}`); + for (const typingName of x) { if (!inferredTypings.has(typingName)) { inferredTypings.set(typingName, undefined); } @@ -136,7 +138,9 @@ namespace ts.JsTyping { newTypingNames.push(typing); } }); - return { cachedTypingPaths, newTypingNames, filesToWatch }; + const result = { cachedTypingPaths, newTypingNames, filesToWatch }; + if (log) log(`Result: ${JSON.stringify(result)}`); + return result; function addInferredTyping(typingName: string) { if (!inferredTypings.has(typingName)) { @@ -153,6 +157,7 @@ namespace ts.JsTyping { } filesToWatch.push(jsonPath); + if (log) log(`Searching for typing names in '${jsonPath}' dependencies`); const jsonConfig: PackageJson = readConfigFile(jsonPath, (path: string) => host.readFile(path)).config; addInferredTypingsFromKeys(jsonConfig.dependencies); addInferredTypingsFromKeys(jsonConfig.devDependencies); @@ -175,19 +180,23 @@ namespace ts.JsTyping { * @param fileNames are the names for source files in the project */ function getTypingNamesFromSourceFileNames(fileNames: string[]) { - for (const j of fileNames) { - if (!hasJavaScriptFileExtension(j)) continue; + const fromFileNames = mapDefined(fileNames, j => { + if (!hasJavaScriptFileExtension(j)) return undefined; const inferredTypingName = removeFileExtension(getBaseFileName(j.toLowerCase())); const cleanedTypingName = inferredTypingName.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); - const safe = safeList.get(cleanedTypingName); - if (safe !== undefined) { + return safeList.get(cleanedTypingName); + }); + if (fromFileNames.length) { + if (log) log(`Inferred typings from file names: ${JSON.stringify(fromFileNames)}`); + for (const safe of fromFileNames) { addInferredTyping(safe); } } const hasJsxFile = some(fileNames, f => fileExtensionIs(f, Extension.Jsx)); if (hasJsxFile) { + if (log) log(`Inferred 'react' typings due to presence of '.jsx' extension`); addInferredTyping("react"); } } @@ -206,6 +215,7 @@ namespace ts.JsTyping { // depth of 2, so we access `node_modules/foo` but not `node_modules/foo/bar` const fileNames = host.readDirectory(packagesFolderPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); + if (log) log(`Searching for typing names in ${packagesFolderPath}; all files: ${JSON.stringify(fileNames)}`); for (const fileName of fileNames) { const normalizedFileName = normalizePath(fileName); const baseFileName = getBaseFileName(normalizedFileName); diff --git a/src/services/shims.ts b/src/services/shims.ts index 4ecd2e4e9b8..2ee1853f18f 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -1116,6 +1116,7 @@ namespace ts { const info = JSON.parse(discoverTypingsJson); return ts.JsTyping.discoverTypings( this.host, + msg => this.logger.log(msg), info.fileNames, toPath(info.projectRootPath, info.projectRootPath, getCanonicalFileName), toPath(info.safeListPath, info.safeListPath, getCanonicalFileName),