Skip to content

Commit 3330f2a

Browse files
author
Andy
authored
JsTyping: Remove "safeList" global variable (#17304)
1 parent 977d907 commit 3330f2a

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

src/compiler/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* @internal */
44
namespace ts {
55
export const emptyArray: never[] = [] as never[];
6+
export const emptyMap: ReadonlyMap<never> = createMap<never>();
67

78
export const externalHelpersModuleNameText = "tslib";
89

src/harness/unittests/typingsInstaller.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,8 @@ namespace ts.projectSystem {
10271027
});
10281028

10291029
describe("discover typings", () => {
1030+
const emptySafeList = emptyMap;
1031+
10301032
it("should use mappings from safe list", () => {
10311033
const app = {
10321034
path: "/a/b/app.js",
@@ -1040,11 +1042,12 @@ namespace ts.projectSystem {
10401042
path: "/a/b/chroma.min.js",
10411043
content: ""
10421044
};
1043-
const cache = createMap<string>();
1045+
1046+
const safeList = createMapFromTemplate({ jquery: "jquery", chroma: "chroma-js" });
10441047

10451048
const host = createServerHost([app, jquery, chroma]);
10461049
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 }, []);
1050+
const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), safeList, emptyMap, { enable: true }, emptyArray);
10481051
assert.deepEqual(logger.finish(), [
10491052
'Inferred typings from file names: ["jquery","chroma-js"]',
10501053
'Result: {"cachedTypingPaths":[],"newTypingNames":["jquery","chroma-js"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
@@ -1062,7 +1065,7 @@ namespace ts.projectSystem {
10621065

10631066
for (const name of JsTyping.nodeCoreModuleList) {
10641067
const logger = trackingLogger();
1065-
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
1068+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), emptySafeList, cache, { enable: true }, [name, "somename"]);
10661069
assert.deepEqual(logger.finish(), [
10671070
'Inferred typings from unresolved imports: ["node","somename"]',
10681071
'Result: {"cachedTypingPaths":[],"newTypingNames":["node","somename"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
@@ -1083,7 +1086,7 @@ namespace ts.projectSystem {
10831086
const host = createServerHost([f, node]);
10841087
const cache = createMapFromTemplate<string>({ "node": node.path });
10851088
const logger = trackingLogger();
1086-
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
1089+
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), emptySafeList, cache, { enable: true }, ["fs", "bar"]);
10871090
assert.deepEqual(logger.finish(), [
10881091
'Inferred typings from unresolved imports: ["node","bar"]',
10891092
'Result: {"cachedTypingPaths":["/a/b/node.d.ts"],"newTypingNames":["bar"],"filesToWatch":["/a/b/bower_components","/a/b/node_modules"]}',
@@ -1108,7 +1111,7 @@ namespace ts.projectSystem {
11081111
const host = createServerHost([app, a, b]);
11091112
const cache = createMap<string>();
11101113
const logger = trackingLogger();
1111-
const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []);
1114+
const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(<Path>app.path), emptySafeList, cache, { enable: true }, /*unresolvedImports*/ []);
11121115
assert.deepEqual(logger.finish(), [
11131116
'Searching for typing names in /node_modules; all files: ["/node_modules/a/package.json"]',
11141117
'Result: {"cachedTypingPaths":[],"newTypingNames":["a"],"filesToWatch":["/bower_components","/node_modules"]}',

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace ts.server.typingsInstaller {
8585
private readonly missingTypingsSet: Map<true> = createMap<true>();
8686
private readonly knownCachesSet: Map<true> = createMap<true>();
8787
private readonly projectWatchers: Map<FileWatcher[]> = createMap<FileWatcher[]>();
88+
private safeList: JsTyping.SafeList | undefined;
8889
readonly pendingRunRequests: PendingRequest[] = [];
8990

9091
private installRunCount = 1;
@@ -143,12 +144,15 @@ namespace ts.server.typingsInstaller {
143144
this.processCacheLocation(req.cachePath);
144145
}
145146

147+
if (this.safeList === undefined) {
148+
this.safeList = JsTyping.loadSafeList(this.installTypingHost, this.safeListPath);
149+
}
146150
const discoverTypingsResult = JsTyping.discoverTypings(
147151
this.installTypingHost,
148152
this.log.isEnabled() ? this.log.writeLine : undefined,
149153
req.fileNames,
150154
req.projectRootPath,
151-
this.safeListPath,
155+
this.safeList,
152156
this.packageNameToTypingLocation,
153157
req.typeAcquisition,
154158
req.unresolvedImports);

src/services/jsTyping.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ namespace ts.JsTyping {
2525
typings?: string;
2626
}
2727

28-
// A map of loose file names to library names
29-
// that we are confident require typings
30-
let safeList: Map<string>;
31-
3228
/* @internal */
3329
export const nodeCoreModuleList: ReadonlyArray<string> = [
3430
"buffer", "querystring", "events", "http", "cluster",
@@ -40,6 +36,16 @@ namespace ts.JsTyping {
4036

4137
const nodeCoreModules = arrayToMap(<string[]>nodeCoreModuleList, x => x);
4238

39+
/**
40+
* A map of loose file names to library names that we are confident require typings
41+
*/
42+
export type SafeList = ReadonlyMap<string>;
43+
44+
export function loadSafeList(host: TypingResolutionHost, safeListPath: Path): SafeList {
45+
const result = readConfigFile(safeListPath, path => host.readFile(path));
46+
return createMapFromTemplate<string>(result.config);
47+
}
48+
4349
/**
4450
* @param host is the object providing I/O related operations.
4551
* @param fileNames are the file names that belong to the same project
@@ -54,8 +60,8 @@ namespace ts.JsTyping {
5460
log: ((message: string) => void) | undefined,
5561
fileNames: string[],
5662
projectRootPath: Path,
57-
safeListPath: Path,
58-
packageNameToTypingLocation: Map<string>,
63+
safeList: SafeList,
64+
packageNameToTypingLocation: ReadonlyMap<string>,
5965
typeAcquisition: TypeAcquisition,
6066
unresolvedImports: ReadonlyArray<string>):
6167
{ cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } {
@@ -75,11 +81,6 @@ namespace ts.JsTyping {
7581
}
7682
});
7783

78-
if (!safeList) {
79-
const result = readConfigFile(safeListPath, (path: string) => host.readFile(path));
80-
safeList = createMapFromTemplate<string>(result.config);
81-
}
82-
8384
const filesToWatch: string[] = [];
8485

8586
forEach(typeAcquisition.include, addInferredTyping);

src/services/shims.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ namespace ts {
10051005

10061006
class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
10071007
private logPerformance = false;
1008+
private safeList: JsTyping.SafeList | undefined;
10081009

10091010
constructor(factory: ShimFactory, public readonly logger: Logger, private readonly host: CoreServicesShimHostAdapter) {
10101011
super(factory);
@@ -1114,12 +1115,15 @@ namespace ts {
11141115
const getCanonicalFileName = createGetCanonicalFileName(/*useCaseSensitivefileNames:*/ false);
11151116
return this.forwardJSONCall("discoverTypings()", () => {
11161117
const info = <DiscoverTypingsInfo>JSON.parse(discoverTypingsJson);
1117-
return ts.JsTyping.discoverTypings(
1118+
if (this.safeList === undefined) {
1119+
this.safeList = JsTyping.loadSafeList(this.host, toPath(info.safeListPath, info.safeListPath, getCanonicalFileName));
1120+
}
1121+
return JsTyping.discoverTypings(
11181122
this.host,
11191123
msg => this.logger.log(msg),
11201124
info.fileNames,
11211125
toPath(info.projectRootPath, info.projectRootPath, getCanonicalFileName),
1122-
toPath(info.safeListPath, info.safeListPath, getCanonicalFileName),
1126+
this.safeList,
11231127
info.packageNameToTypingLocation,
11241128
info.typeAcquisition,
11251129
info.unresolvedImports);

0 commit comments

Comments
 (0)