Skip to content

Commit 6aad783

Browse files
committed
- Adding DiscoverTypingsSettings
- Remove all references to Tsd. Instead pass a map of package names to cached typing locations
1 parent b3ceea3 commit 6aad783

File tree

3 files changed

+48
-65
lines changed

3 files changed

+48
-65
lines changed

src/compiler/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,17 @@ namespace ts {
24362436
enableAutoDiscovery?: boolean;
24372437
include?: string[];
24382438
exclude?: string[];
2439-
[option: string]: any;
2439+
[option: string]: string[] | boolean;
2440+
}
2441+
2442+
export interface DiscoverTypingsSettings {
2443+
fileNames: string[]; // The file names that belong to the same project.
2444+
cachePath: string; // The path to the typings cache
2445+
projectRootPath: string; // The path to the project root directory
2446+
safeListPath: string; // The path used to retrieve the safe list
2447+
packageNameToTypingLocation: Map<string>; // The map of package names to their cached typing locations
2448+
typingOptions: TypingOptions; // Used to customize the typing inference process
2449+
compilerOptions: CompilerOptions; // Used as a source for typing inference
24402450
}
24412451

24422452
export enum ModuleKind {

src/services/jsTyping.ts

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,11 @@ namespace ts.JsTyping {
1313
readDirectory: (path: string, extension?: string, exclude?: string[], depth?: number) => string[];
1414
};
1515

16-
interface TsdJson {
17-
version: string;
18-
repo: string;
19-
ref: string;
20-
path: string;
21-
installed?: Map<TsdInstalledItem>;
22-
};
23-
24-
interface TsdInstalledItem {
25-
commit: string;
26-
};
27-
2816
interface PackageJson {
2917
_requiredBy?: string[];
3018
dependencies?: Map<string>;
3119
devDependencies?: Map<string>;
32-
name: string;
20+
name?: string;
3321
optionalDependencies?: Map<string>;
3422
peerDependencies?: Map<string>;
3523
typings?: string;
@@ -41,19 +29,21 @@ namespace ts.JsTyping {
4129

4230
/**
4331
* @param host is the object providing I/O related operations.
44-
* @param fileNames are the file names that belong to the same project.
32+
* @param fileNames are the file names that belong to the same project
4533
* @param cachePath is the path to the typings cache
4634
* @param projectRootPath is the path to the project root directory
4735
* @param safeListPath is the path used to retrieve the safe list
48-
* @param typingOptions are used for customizing the typing inference process.
49-
* @param compilerOptions are used as a source of typing inference.
36+
* @param packageNameToTypingLocation is the map of package names to their cached typing locations
37+
* @param typingOptions are used to customize the typing inference process
38+
* @param compilerOptions are used as a source for typing inference
5039
*/
5140
export function discoverTypings(
5241
host: TypingResolutionHost,
5342
fileNames: string[],
5443
cachePath: Path,
5544
projectRootPath: Path,
5645
safeListPath: Path,
46+
packageNameToTypingLocation: Map<string>,
5747
typingOptions: TypingOptions,
5848
compilerOptions: CompilerOptions):
5949
{ cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } {
@@ -69,8 +59,9 @@ namespace ts.JsTyping {
6959
fileNames = filter(map(fileNames, normalizePath), f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JS, ScriptKind.JSX));
7060

7161
if (!safeList) {
72-
const result = readConfigFile(safeListPath, host.readFile);
62+
const result = readConfigFile(safeListPath, (path: string) => host.readFile(path));
7363
if (result.config) { safeList = result.config; }
64+
else { safeList = {}; };
7465
}
7566

7667
const filesToWatch: string[] = [];
@@ -81,44 +72,27 @@ namespace ts.JsTyping {
8172
mergeTypings(typingOptions.include);
8273
exclude = typingOptions.exclude || [];
8374

84-
if (typingOptions.enableAutoDiscovery) {
85-
const possibleSearchDirs = map(fileNames, getDirectoryPath);
86-
if (projectRootPath !== undefined) {
87-
possibleSearchDirs.push(projectRootPath);
88-
}
89-
searchDirs = deduplicate(possibleSearchDirs);
90-
for (const searchDir of searchDirs) {
91-
const packageJsonPath = combinePaths(searchDir, "package.json");
92-
getTypingNamesFromJson(packageJsonPath, filesToWatch);
75+
const possibleSearchDirs = map(fileNames, getDirectoryPath);
76+
if (projectRootPath !== undefined) {
77+
possibleSearchDirs.push(projectRootPath);
78+
}
79+
searchDirs = deduplicate(possibleSearchDirs);
80+
for (const searchDir of searchDirs) {
81+
const packageJsonPath = combinePaths(searchDir, "package.json");
82+
getTypingNamesFromJson(packageJsonPath, filesToWatch);
9383

94-
const bowerJsonPath = combinePaths(searchDir, "bower.json");
95-
getTypingNamesFromJson(bowerJsonPath, filesToWatch);
84+
const bowerJsonPath = combinePaths(searchDir, "bower.json");
85+
getTypingNamesFromJson(bowerJsonPath, filesToWatch);
9686

97-
const nodeModulesPath = combinePaths(searchDir, "node_modules");
98-
getTypingNamesFromNodeModuleFolder(nodeModulesPath, filesToWatch);
99-
}
100-
getTypingNamesFromSourceFileNames(fileNames);
87+
const nodeModulesPath = combinePaths(searchDir, "node_modules");
88+
getTypingNamesFromNodeModuleFolder(nodeModulesPath, filesToWatch);
10189
}
90+
getTypingNamesFromSourceFileNames(fileNames);
10291

103-
const typingsPath = combinePaths(cachePath, "typings");
104-
const tsdJsonPath = combinePaths(cachePath, "tsd.json");
105-
const result = readConfigFile(tsdJsonPath, host.readFile);
106-
if (result.config) {
107-
const tsdJson: TsdJson = result.config;
108-
109-
// The "installed" property in the tsd.json serves as a registry of installed typings. Each item
110-
// of this object has a key of the relative file path, and a value that contains the corresponding
111-
// commit hash.
112-
if (tsdJson.installed) {
113-
for (const cachedTypingPath in tsdJson.installed) {
114-
// Assuming the cachedTypingPath has the format of "[package name]/[file name]"
115-
const cachedTypingName = cachedTypingPath.substr(0, cachedTypingPath.indexOf("/"));
116-
// If the inferred[cachedTypingName] is already not null, which means we found a corresponding
117-
// d.ts file that coming with the package. That one should take higher priority.
118-
if (hasProperty(inferredTypings, cachedTypingName) && !inferredTypings[cachedTypingName]) {
119-
inferredTypings[cachedTypingName] = combinePaths(typingsPath, cachedTypingPath);
120-
}
121-
}
92+
// Add the cached typing locations for inferred typings that are already installed
93+
for (const name in packageNameToTypingLocation) {
94+
if (hasProperty(inferredTypings, name) && !inferredTypings[name]) {
95+
inferredTypings[name] = packageNameToTypingLocation[name];
12296
}
12397
}
12498

@@ -158,7 +132,7 @@ namespace ts.JsTyping {
158132
* Get the typing info from common package manager json files like package.json or bower.json
159133
*/
160134
function getTypingNamesFromJson(jsonPath: string, filesToWatch: string[]) {
161-
const result = readConfigFile(jsonPath, host.readFile);
135+
const result = readConfigFile(jsonPath, (path: string) => host.readFile(path));
162136
if (result.config) {
163137
const jsonConfig: PackageJson = result.config;
164138
filesToWatch.push(jsonPath);
@@ -215,7 +189,7 @@ namespace ts.JsTyping {
215189
for (const fileName of fileNames) {
216190
const normalizedFileName = normalizePath(fileName);
217191
if (getBaseFileName(normalizedFileName) !== "package.json") { continue; }
218-
const result = readConfigFile(normalizedFileName, host.readFile);
192+
const result = readConfigFile(normalizedFileName, (path: string) => host.readFile(path));
219193
if (!result.config) { continue; }
220194
const packageJson: PackageJson = result.config;
221195
filesToWatch.push(normalizedFileName);

src/services/shims.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace ts {
232232
getPreProcessedFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
233233
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
234234
getDefaultCompilationSettings(): string;
235-
discoverTypings(fileNamesJson: string, cachePath: string, projectRootPath: string, safeListPath: string, typingOptionsJson: string, compilerOptionsJson: string): string;
235+
discoverTypings(discoverTypingsJson: string): string;
236236
}
237237

238238
function logInternalError(logger: Logger, err: Error) {
@@ -987,20 +987,19 @@ namespace ts {
987987
);
988988
}
989989

990-
public discoverTypings(fileNamesJson: string, cachePath: string, projectRootPath: string, safeListPath: string, typingOptionsJson: string, compilerOptionsJson: string): string {
990+
public discoverTypings(discoverTypingsJson: string): string {
991991
const getCanonicalFileName = createGetCanonicalFileName(/*useCaseSensitivefileNames:*/ false);
992992
return this.forwardJSONCall("discoverTypings()", () => {
993-
const typingOptions = <TypingOptions>JSON.parse(typingOptionsJson);
994-
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
995-
const fileNames: string[] = JSON.parse(fileNamesJson);
993+
const settings = <DiscoverTypingsSettings>JSON.parse(discoverTypingsJson);
996994
return ts.JsTyping.discoverTypings(
997995
this.host,
998-
fileNames,
999-
toPath(cachePath, cachePath, getCanonicalFileName),
1000-
toPath(projectRootPath, projectRootPath, getCanonicalFileName),
1001-
toPath(safeListPath, safeListPath, getCanonicalFileName),
1002-
typingOptions,
1003-
compilerOptions);
996+
settings.fileNames,
997+
toPath(settings.cachePath, settings.cachePath, getCanonicalFileName),
998+
toPath(settings.projectRootPath, settings.projectRootPath, getCanonicalFileName),
999+
toPath(settings.safeListPath, settings.safeListPath, getCanonicalFileName),
1000+
settings.packageNameToTypingLocation,
1001+
settings.typingOptions,
1002+
settings.compilerOptions);
10041003
});
10051004
}
10061005
}

0 commit comments

Comments
 (0)