Skip to content

Commit 1fde409

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

3 files changed

Lines changed: 48 additions & 65 deletions

File tree

src/compiler/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2453,7 +2453,17 @@ namespace ts {
24532453
enableAutoDiscovery?: boolean;
24542454
include?: string[];
24552455
exclude?: string[];
2456-
[option: string]: any;
2456+
[option: string]: string[] | boolean;
2457+
}
2458+
2459+
export interface DiscoverTypingsSettings {
2460+
fileNames: string[]; // The file names that belong to the same project.
2461+
cachePath: string; // The path to the typings cache
2462+
projectRootPath: string; // The path to the project root directory
2463+
safeListPath: string; // The path used to retrieve the safe list
2464+
packageNameToTypingLocation: Map<string>; // The map of package names to their cached typing locations
2465+
typingOptions: TypingOptions; // Used to customize the typing inference process
2466+
compilerOptions: CompilerOptions; // Used as a source for typing inference
24572467
}
24582468

24592469
export const 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
@@ -230,7 +230,7 @@ namespace ts {
230230
getPreProcessedFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
231231
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
232232
getDefaultCompilationSettings(): string;
233-
discoverTypings(fileNamesJson: string, cachePath: string, projectRootPath: string, safeListPath: string, typingOptionsJson: string, compilerOptionsJson: string): string;
233+
discoverTypings(discoverTypingsJson: string): string;
234234
}
235235

236236
function logInternalError(logger: Logger, err: Error) {
@@ -985,20 +985,19 @@ namespace ts {
985985
);
986986
}
987987

988-
public discoverTypings(fileNamesJson: string, cachePath: string, projectRootPath: string, safeListPath: string, typingOptionsJson: string, compilerOptionsJson: string): string {
988+
public discoverTypings(discoverTypingsJson: string): string {
989989
const getCanonicalFileName = createGetCanonicalFileName(/*useCaseSensitivefileNames:*/ false);
990990
return this.forwardJSONCall("discoverTypings()", () => {
991-
const typingOptions = <TypingOptions>JSON.parse(typingOptionsJson);
992-
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
993-
const fileNames: string[] = JSON.parse(fileNamesJson);
991+
const settings = <DiscoverTypingsSettings>JSON.parse(discoverTypingsJson);
994992
return ts.JsTyping.discoverTypings(
995993
this.host,
996-
fileNames,
997-
toPath(cachePath, cachePath, getCanonicalFileName),
998-
toPath(projectRootPath, projectRootPath, getCanonicalFileName),
999-
toPath(safeListPath, safeListPath, getCanonicalFileName),
1000-
typingOptions,
1001-
compilerOptions);
994+
settings.fileNames,
995+
toPath(settings.cachePath, settings.cachePath, getCanonicalFileName),
996+
toPath(settings.projectRootPath, settings.projectRootPath, getCanonicalFileName),
997+
toPath(settings.safeListPath, settings.safeListPath, getCanonicalFileName),
998+
settings.packageNameToTypingLocation,
999+
settings.typingOptions,
1000+
settings.compilerOptions);
10021001
});
10031002
}
10041003
}

0 commit comments

Comments
 (0)