JsTyping: Remove "safeList" global variable (#17304)

This commit is contained in:
Andy
2017-07-27 10:54:47 -07:00
committed by GitHub
parent 977d907417
commit 3330f2a33b
5 changed files with 32 additions and 19 deletions

View File

@@ -3,6 +3,7 @@
/* @internal */
namespace ts {
export const emptyArray: never[] = [] as never[];
export const emptyMap: ReadonlyMap<never> = createMap<never>();
export const externalHelpersModuleNameText = "tslib";

View File

@@ -1027,6 +1027,8 @@ namespace ts.projectSystem {
});
describe("discover typings", () => {
const emptySafeList = emptyMap;
it("should use mappings from safe list", () => {
const app = {
path: "/a/b/app.js",
@@ -1040,11 +1042,12 @@ namespace ts.projectSystem {
path: "/a/b/chroma.min.js",
content: ""
};
const cache = createMap<string>();
const safeList = createMapFromTemplate({ jquery: "jquery", chroma: "chroma-js" });
const host = createServerHost([app, jquery, chroma]);
const logger = trackingLogger();
const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, []);
const result = JsTyping.discoverTypings(host, logger.log, [app.path, jquery.path, chroma.path], getDirectoryPath(<Path>app.path), safeList, emptyMap, { enable: true }, emptyArray);
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"]}',
@@ -1062,7 +1065,7 @@ namespace ts.projectSystem {
for (const name of JsTyping.nodeCoreModuleList) {
const logger = trackingLogger();
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, [name, "somename"]);
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), emptySafeList, 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"]}',
@@ -1083,7 +1086,7 @@ namespace ts.projectSystem {
const host = createServerHost([f, node]);
const cache = createMapFromTemplate<string>({ "node": node.path });
const logger = trackingLogger();
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), /*safeListPath*/ undefined, cache, { enable: true }, ["fs", "bar"]);
const result = JsTyping.discoverTypings(host, logger.log, [f.path], getDirectoryPath(<Path>f.path), emptySafeList, 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"]}',
@@ -1108,7 +1111,7 @@ namespace ts.projectSystem {
const host = createServerHost([app, a, b]);
const cache = createMap<string>();
const logger = trackingLogger();
const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(<Path>app.path), /*safeListPath*/ undefined, cache, { enable: true }, /*unresolvedImports*/ []);
const result = JsTyping.discoverTypings(host, logger.log, [app.path], getDirectoryPath(<Path>app.path), emptySafeList, 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"]}',

View File

@@ -85,6 +85,7 @@ namespace ts.server.typingsInstaller {
private readonly missingTypingsSet: Map<true> = createMap<true>();
private readonly knownCachesSet: Map<true> = createMap<true>();
private readonly projectWatchers: Map<FileWatcher[]> = createMap<FileWatcher[]>();
private safeList: JsTyping.SafeList | undefined;
readonly pendingRunRequests: PendingRequest[] = [];
private installRunCount = 1;
@@ -143,12 +144,15 @@ namespace ts.server.typingsInstaller {
this.processCacheLocation(req.cachePath);
}
if (this.safeList === undefined) {
this.safeList = JsTyping.loadSafeList(this.installTypingHost, this.safeListPath);
}
const discoverTypingsResult = JsTyping.discoverTypings(
this.installTypingHost,
this.log.isEnabled() ? this.log.writeLine : undefined,
req.fileNames,
req.projectRootPath,
this.safeListPath,
this.safeList,
this.packageNameToTypingLocation,
req.typeAcquisition,
req.unresolvedImports);

View File

@@ -25,10 +25,6 @@ namespace ts.JsTyping {
typings?: string;
}
// A map of loose file names to library names
// that we are confident require typings
let safeList: Map<string>;
/* @internal */
export const nodeCoreModuleList: ReadonlyArray<string> = [
"buffer", "querystring", "events", "http", "cluster",
@@ -40,6 +36,16 @@ namespace ts.JsTyping {
const nodeCoreModules = arrayToMap(<string[]>nodeCoreModuleList, x => x);
/**
* A map of loose file names to library names that we are confident require typings
*/
export type SafeList = ReadonlyMap<string>;
export function loadSafeList(host: TypingResolutionHost, safeListPath: Path): SafeList {
const result = readConfigFile(safeListPath, path => host.readFile(path));
return createMapFromTemplate<string>(result.config);
}
/**
* @param host is the object providing I/O related operations.
* @param fileNames are the file names that belong to the same project
@@ -54,8 +60,8 @@ namespace ts.JsTyping {
log: ((message: string) => void) | undefined,
fileNames: string[],
projectRootPath: Path,
safeListPath: Path,
packageNameToTypingLocation: Map<string>,
safeList: SafeList,
packageNameToTypingLocation: ReadonlyMap<string>,
typeAcquisition: TypeAcquisition,
unresolvedImports: ReadonlyArray<string>):
{ cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } {
@@ -75,11 +81,6 @@ namespace ts.JsTyping {
}
});
if (!safeList) {
const result = readConfigFile(safeListPath, (path: string) => host.readFile(path));
safeList = createMapFromTemplate<string>(result.config);
}
const filesToWatch: string[] = [];
forEach(typeAcquisition.include, addInferredTyping);

View File

@@ -1005,6 +1005,7 @@ namespace ts {
class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
private logPerformance = false;
private safeList: JsTyping.SafeList | undefined;
constructor(factory: ShimFactory, public readonly logger: Logger, private readonly host: CoreServicesShimHostAdapter) {
super(factory);
@@ -1114,12 +1115,15 @@ namespace ts {
const getCanonicalFileName = createGetCanonicalFileName(/*useCaseSensitivefileNames:*/ false);
return this.forwardJSONCall("discoverTypings()", () => {
const info = <DiscoverTypingsInfo>JSON.parse(discoverTypingsJson);
return ts.JsTyping.discoverTypings(
if (this.safeList === undefined) {
this.safeList = JsTyping.loadSafeList(this.host, toPath(info.safeListPath, info.safeListPath, getCanonicalFileName));
}
return JsTyping.discoverTypings(
this.host,
msg => this.logger.log(msg),
info.fileNames,
toPath(info.projectRootPath, info.projectRootPath, getCanonicalFileName),
toPath(info.safeListPath, info.safeListPath, getCanonicalFileName),
this.safeList,
info.packageNameToTypingLocation,
info.typeAcquisition,
info.unresolvedImports);