Skip to content

Commit 950046a

Browse files
author
Andy Hanson
committed
Add logging to discoverTypings
1 parent 47c1563 commit 950046a

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/harness/unittests/typingsInstaller.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ namespace ts.projectSystem {
4444
});
4545
}
4646

47+
function trackingLogger(): { log(message: string): void, finish(): string[] } {
48+
const logs: string[] = [];
49+
return {
50+
log(message) {
51+
logs.push(message);
52+
},
53+
finish() {
54+
return logs;
55+
}
56+
};
57+
}
58+
4759
import typingsName = TI.typingsName;
4860

4961
describe("local module", () => {
@@ -1025,7 +1037,12 @@ namespace ts.projectSystem {
10251037
const cache = createMap<string>();
10261038

10271039
const host = createServerHost([app, jquery, chroma]);
1028-
const result = JsTyping.discoverTypings(host, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, []);
1040+
const logger = trackingLogger();
1041+
const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, []);
1042+
assert.deepEqual(logger.finish(), [
1043+
'Inferred typings from file names: ["jquery","chroma-js"]',
1044+
'Result: {"cachedTypingPaths":[],"newTypingNames":["jquery","chroma-js"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1045+
]);
10291046
assert.deepEqual(result.newTypingNames, ["jquery", "chroma-js"]);
10301047
});
10311048

@@ -1038,7 +1055,12 @@ namespace ts.projectSystem {
10381055
const cache = createMap<string>();
10391056

10401057
for (const name of JsTyping.nodeCoreModuleList) {
1041-
const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
1058+
const logger = trackingLogger();
1059+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
1060+
assert.deepEqual(logger.finish(), [
1061+
'Inferred typings from unresolved imports: ["node","somename"]',
1062+
'Result: {"cachedTypingPaths":[],"newTypingNames":["node","somename"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1063+
]);
10421064
assert.deepEqual(result.newTypingNames.sort(), ["node", "somename"]);
10431065
}
10441066
});
@@ -1054,7 +1076,12 @@ namespace ts.projectSystem {
10541076
};
10551077
const host = createServerHost([f, node]);
10561078
const cache = createMapFromTemplate<string>({ "node": node.path });
1057-
const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
1079+
const logger = trackingLogger();
1080+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
1081+
assert.deepEqual(logger.finish(), [
1082+
'Inferred typings from unresolved imports: ["node","bar"]',
1083+
'Result: {"cachedTypingPaths":["/a/b/node.d.ts"],"newTypingNames":["bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1084+
]);
10581085
assert.deepEqual(result.cachedTypingPaths, [node.path]);
10591086
assert.deepEqual(result.newTypingNames, ["bar"]);
10601087
});

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace ts.server.typingsInstaller {
145145

146146
const discoverTypingsResult = JsTyping.discoverTypings(
147147
this.installTypingHost,
148+
this.log.isEnabled() ? this.log.writeLine : undefined,
148149
req.fileNames,
149150
req.projectRootPath,
150151
this.safeListPath,

src/services/jsTyping.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace ts.JsTyping {
5353
*/
5454
export function discoverTypings(
5555
host: TypingResolutionHost,
56+
log: ((message: string) => void) | undefined,
5657
fileNames: string[],
5758
projectRootPath: Path,
5859
safeListPath: Path,
@@ -109,8 +110,9 @@ namespace ts.JsTyping {
109110

110111
// add typings for unresolved imports
111112
if (unresolvedImports) {
112-
for (const moduleId of unresolvedImports) {
113-
const typingName = nodeCoreModules.has(moduleId) ? "node" : moduleId;
113+
const x = unresolvedImports.map(moduleId => nodeCoreModules.has(moduleId) ? "node" : moduleId);
114+
if (x.length && log) log(`Inferred typings from unresolved imports: ${JSON.stringify(x)}`);
115+
for (const typingName of x) {
114116
if (!inferredTypings.has(typingName)) {
115117
inferredTypings.set(typingName, undefined);
116118
}
@@ -138,7 +140,9 @@ namespace ts.JsTyping {
138140
newTypingNames.push(typing);
139141
}
140142
});
141-
return { cachedTypingPaths, newTypingNames, filesToWatch };
143+
const result = { cachedTypingPaths, newTypingNames, filesToWatch };
144+
if (log) log(`Result: ${JSON.stringify(result)}`);
145+
return result;
142146

143147
/**
144148
* Merge a given list of typingNames to the inferredTypings map
@@ -161,6 +165,7 @@ namespace ts.JsTyping {
161165
function getTypingNamesFromJson(jsonPath: string, filesToWatch: string[]) {
162166
if (host.fileExists(jsonPath)) {
163167
filesToWatch.push(jsonPath);
168+
if (log) log(`Searching for typing names in '${jsonPath}' dependencies`);
164169
}
165170
const result = readConfigFile(jsonPath, (path: string) => host.readFile(path));
166171
const jsonConfig: PackageJson = result.config;
@@ -190,11 +195,14 @@ namespace ts.JsTyping {
190195
const cleanedTypingNames = map(inferredTypingNames, f => f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""));
191196

192197
if (safeList !== EmptySafeList) {
193-
mergeTypings(ts.mapDefined(cleanedTypingNames, f => safeList.get(f)));
198+
const fromFileNames = ts.mapDefined(cleanedTypingNames, f => safeList.get(f));
199+
if (fromFileNames.length && log) log(`Inferred typings from file names: ${JSON.stringify(fromFileNames)}`);
200+
mergeTypings(fromFileNames);
194201
}
195202

196203
const hasJsxFile = forEach(fileNames, f => ensureScriptKind(f, getScriptKindFromFileName(f)) === ScriptKind.JSX);
197204
if (hasJsxFile) {
205+
if (log) log(`Inferred 'react' typings due to presence of '.jsx' extension`);
198206
mergeTypings(["react"]);
199207
}
200208
}
@@ -213,6 +221,7 @@ namespace ts.JsTyping {
213221

214222
const typingNames: string[] = [];
215223
const fileNames = host.readDirectory(packagesFolderPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
224+
if (log) log(`Searching for typing names in ${packagesFolderPath}; all files: ${JSON.stringify(fileNames)}`);
216225
for (const fileName of fileNames) {
217226
const normalizedFileName = normalizePath(fileName);
218227
const baseFileName = getBaseFileName(normalizedFileName);

src/services/shims.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ namespace ts {
11361136
const info = <DiscoverTypingsInfo>JSON.parse(discoverTypingsJson);
11371137
return ts.JsTyping.discoverTypings(
11381138
this.host,
1139+
msg => this.logger.log(msg),
11391140
info.fileNames,
11401141
toPath(info.projectRootPath, info.projectRootPath, getCanonicalFileName),
11411142
toPath(info.safeListPath, info.safeListPath, getCanonicalFileName),

0 commit comments

Comments
 (0)