Skip to content

Commit efc861c

Browse files
author
Andy
authored
Add logging to discoverTypings (#16652)
1 parent 33836f8 commit efc861c

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/harness/unittests/typingsInstaller.ts

Lines changed: 36 additions & 4 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", () => {
@@ -1031,7 +1043,12 @@ namespace ts.projectSystem {
10311043
const cache = createMap<string>();
10321044

10331045
const host = createServerHost([app, jquery, chroma]);
1034-
const result = JsTyping.discoverTypings(host, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, []);
1046+
const logger = trackingLogger();
1047+
const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, []);
1048+
assert.deepEqual(logger.finish(), [
1049+
'Inferred typings from file names: ["jquery","chroma-js"]',
1050+
'Result: {"cachedTypingPaths":[],"newTypingNames":["jquery","chroma-js"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1051+
]);
10351052
assert.deepEqual(result.newTypingNames, ["jquery", "chroma-js"]);
10361053
});
10371054

@@ -1044,7 +1061,12 @@ namespace ts.projectSystem {
10441061
const cache = createMap<string>();
10451062

10461063
for (const name of JsTyping.nodeCoreModuleList) {
1047-
const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
1064+
const logger = trackingLogger();
1065+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
1066+
assert.deepEqual(logger.finish(), [
1067+
'Inferred typings from unresolved imports: ["node","somename"]',
1068+
'Result: {"cachedTypingPaths":[],"newTypingNames":["node","somename"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1069+
]);
10481070
assert.deepEqual(result.newTypingNames.sort(), ["node", "somename"]);
10491071
}
10501072
});
@@ -1060,7 +1082,12 @@ namespace ts.projectSystem {
10601082
};
10611083
const host = createServerHost([f, node]);
10621084
const cache = createMapFromTemplate<string>({ "node": node.path });
1063-
const result = JsTyping.discoverTypings(host, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
1085+
const logger = trackingLogger();
1086+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
1087+
assert.deepEqual(logger.finish(), [
1088+
'Inferred typings from unresolved imports: ["node","bar"]',
1089+
'Result: {"cachedTypingPaths":["/a/b/node.d.ts"],"newTypingNames":["bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
1090+
]);
10641091
assert.deepEqual(result.cachedTypingPaths, [node.path]);
10651092
assert.deepEqual(result.newTypingNames, ["bar"]);
10661093
});
@@ -1080,7 +1107,12 @@ namespace ts.projectSystem {
10801107
};
10811108
const host = createServerHost([app, a, b]);
10821109
const cache = createMap<string>();
1083-
const result = JsTyping.discoverTypings(host, [app.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []);
1110+
const logger = trackingLogger();
1111+
const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []);
1112+
assert.deepEqual(logger.finish(), [
1113+
'Searching for typing names in /node_modules; all files: ["/node_modules/a/package.json"]',
1114+
'Result: {"cachedTypingPaths":[],"newTypingNames":["a"],"filesToWatch":["/bower_components","/node_modules"]}',
1115+
]);
10841116
assert.deepEqual(result, {
10851117
cachedTypingPaths: [],
10861118
newTypingNames: ["a"], // But not "b"

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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace ts.JsTyping {
5151
*/
5252
export function discoverTypings(
5353
host: TypingResolutionHost,
54+
log: ((message: string) => void) | undefined,
5455
fileNames: string[],
5556
projectRootPath: Path,
5657
safeListPath: Path,
@@ -107,8 +108,9 @@ namespace ts.JsTyping {
107108

108109
// add typings for unresolved imports
109110
if (unresolvedImports) {
110-
for (const moduleId of unresolvedImports) {
111-
const typingName = nodeCoreModules.has(moduleId) ? "node" : moduleId;
111+
const x = unresolvedImports.map(moduleId => nodeCoreModules.has(moduleId) ? "node" : moduleId);
112+
if (x.length && log) log(`Inferred typings from unresolved imports: ${JSON.stringify(x)}`);
113+
for (const typingName of x) {
112114
if (!inferredTypings.has(typingName)) {
113115
inferredTypings.set(typingName, undefined);
114116
}
@@ -136,7 +138,9 @@ namespace ts.JsTyping {
136138
newTypingNames.push(typing);
137139
}
138140
});
139-
return { cachedTypingPaths, newTypingNames, filesToWatch };
141+
const result = { cachedTypingPaths, newTypingNames, filesToWatch };
142+
if (log) log(`Result: ${JSON.stringify(result)}`);
143+
return result;
140144

141145
function addInferredTyping(typingName: string) {
142146
if (!inferredTypings.has(typingName)) {
@@ -153,6 +157,7 @@ namespace ts.JsTyping {
153157
}
154158

155159
filesToWatch.push(jsonPath);
160+
if (log) log(`Searching for typing names in '${jsonPath}' dependencies`);
156161
const jsonConfig: PackageJson = readConfigFile(jsonPath, (path: string) => host.readFile(path)).config;
157162
addInferredTypingsFromKeys(jsonConfig.dependencies);
158163
addInferredTypingsFromKeys(jsonConfig.devDependencies);
@@ -175,19 +180,23 @@ namespace ts.JsTyping {
175180
* @param fileNames are the names for source files in the project
176181
*/
177182
function getTypingNamesFromSourceFileNames(fileNames: string[]) {
178-
for (const j of fileNames) {
179-
if (!hasJavaScriptFileExtension(j)) continue;
183+
const fromFileNames = mapDefined(fileNames, j => {
184+
if (!hasJavaScriptFileExtension(j)) return undefined;
180185

181186
const inferredTypingName = removeFileExtension(getBaseFileName(j.toLowerCase()));
182187
const cleanedTypingName = inferredTypingName.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, "");
183-
const safe = safeList.get(cleanedTypingName);
184-
if (safe !== undefined) {
188+
return safeList.get(cleanedTypingName);
189+
});
190+
if (fromFileNames.length) {
191+
if (log) log(`Inferred typings from file names: ${JSON.stringify(fromFileNames)}`);
192+
for (const safe of fromFileNames) {
185193
addInferredTyping(safe);
186194
}
187195
}
188196

189197
const hasJsxFile = some(fileNames, f => fileExtensionIs(f, Extension.Jsx));
190198
if (hasJsxFile) {
199+
if (log) log(`Inferred 'react' typings due to presence of '.jsx' extension`);
191200
addInferredTyping("react");
192201
}
193202
}
@@ -206,6 +215,7 @@ namespace ts.JsTyping {
206215

207216
// depth of 2, so we access `node_modules/foo` but not `node_modules/foo/bar`
208217
const fileNames = host.readDirectory(packagesFolderPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
218+
if (log) log(`Searching for typing names in ${packagesFolderPath}; all files: ${JSON.stringify(fileNames)}`);
209219
for (const fileName of fileNames) {
210220
const normalizedFileName = normalizePath(fileName);
211221
const baseFileName = getBaseFileName(normalizedFileName);

src/services/shims.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ namespace ts {
11161116
const info = <DiscoverTypingsInfo>JSON.parse(discoverTypingsJson);
11171117
return ts.JsTyping.discoverTypings(
11181118
this.host,
1119+
msg => this.logger.log(msg),
11191120
info.fileNames,
11201121
toPath(info.projectRootPath, info.projectRootPath, getCanonicalFileName),
11211122
toPath(info.safeListPath, info.safeListPath, getCanonicalFileName),

0 commit comments

Comments
 (0)