Skip to content

Commit 0346a98

Browse files
committed
- Removing ts. from jsTyping.js
- Adding ".json" file extension filter when retrieving json files from host and removoing filter - simplify isTypingEnabled check
1 parent f76ef47 commit 0346a98

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

src/services/jsTyping.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ namespace ts.JsTyping {
2929
return undefined;
3030
}
3131

32-
function isTypingEnabled(options: TypingOptions): boolean {
33-
if (options) {
34-
if (options.enableAutoDiscovery ||
35-
(options.include && options.include.length > 0) ||
36-
(options.exclude && options.exclude.length > 0)) {
37-
return true;
38-
}
39-
}
40-
return false;
41-
}
42-
4332
/**
4433
* @param host is the object providing I/O related operations.
4534
* @param fileNames are the file names that belong to the same project.
@@ -60,15 +49,15 @@ namespace ts.JsTyping {
6049
// A typing name to typing file path mapping
6150
const inferredTypings: Map<string> = {};
6251

63-
if (!isTypingEnabled(typingOptions)) {
52+
if (!typingOptions || !typingOptions.enableAutoDiscovery) {
6453
return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] };
6554
}
6655

6756
const cachePath = projectRootPath || globalCachePath;
6857
// Only infer typings for .js and .jsx files
69-
fileNames = filter(map(fileNames, ts.normalizePath), f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JS, ScriptKind.JSX));
58+
fileNames = filter(map(fileNames, normalizePath), f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JS, ScriptKind.JSX));
7059

71-
const safeListFilePath = ts.combinePaths(globalCachePath, "safeList.json");
60+
const safeListFilePath = combinePaths(globalCachePath, "safeList.json");
7261
if (!safeList && host.fileExists(safeListFilePath)) {
7362
safeList = tryParseJson(safeListFilePath, host);
7463
}
@@ -82,28 +71,28 @@ namespace ts.JsTyping {
8271
exclude = typingOptions.exclude || [];
8372

8473
if (typingOptions.enableAutoDiscovery) {
85-
const possibleSearchDirs = map(fileNames, ts.getDirectoryPath);
74+
const possibleSearchDirs = map(fileNames, getDirectoryPath);
8675
if (projectRootPath !== undefined) {
8776
possibleSearchDirs.push(projectRootPath);
8877
}
89-
searchDirs = ts.deduplicate(possibleSearchDirs);
78+
searchDirs = deduplicate(possibleSearchDirs);
9079
for (const searchDir of searchDirs) {
91-
const packageJsonPath = ts.combinePaths(searchDir, "package.json");
80+
const packageJsonPath = combinePaths(searchDir, "package.json");
9281
getTypingNamesFromJson(packageJsonPath, filesToWatch);
9382

94-
const bowerJsonPath = ts.combinePaths(searchDir, "bower.json");
83+
const bowerJsonPath = combinePaths(searchDir, "bower.json");
9584
getTypingNamesFromJson(bowerJsonPath, filesToWatch);
9685

97-
const nodeModulesPath = ts.combinePaths(searchDir, "node_modules");
86+
const nodeModulesPath = combinePaths(searchDir, "node_modules");
9887
getTypingNamesFromNodeModuleFolder(nodeModulesPath, filesToWatch);
9988
}
10089

10190
getTypingNamesFromSourceFileNames(fileNames);
10291
getTypingNamesFromCompilerOptions(compilerOptions);
10392
}
10493

105-
const typingsPath = ts.combinePaths(cachePath, "typings");
106-
const tsdJsonPath = ts.combinePaths(cachePath, "tsd.json");
94+
const typingsPath = combinePaths(cachePath, "typings");
95+
const tsdJsonPath = combinePaths(cachePath, "tsd.json");
10796
const tsdJsonDict = tryParseJson(tsdJsonPath, host);
10897
if (tsdJsonDict) {
10998
for (const notFoundTypingName of notFoundTypingNames) {
@@ -122,7 +111,7 @@ namespace ts.JsTyping {
122111
// If the inferred[cachedTypingName] is already not null, which means we found a corresponding
123112
// d.ts file that coming with the package. That one should take higher priority.
124113
if (hasProperty(inferredTypings, cachedTypingName) && !inferredTypings[cachedTypingName]) {
125-
inferredTypings[cachedTypingName] = ts.combinePaths(typingsPath, cachedTypingPath);
114+
inferredTypings[cachedTypingName] = combinePaths(typingsPath, cachedTypingPath);
126115
}
127116
}
128117
}
@@ -190,7 +179,7 @@ namespace ts.JsTyping {
190179
*/
191180
function getTypingNamesFromSourceFileNames(fileNames: string[]) {
192181
const jsFileNames = filter(fileNames, hasJavaScriptFileExtension);
193-
const inferredTypingNames = map(jsFileNames, f => ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())));
182+
const inferredTypingNames = map(jsFileNames, f => removeFileExtension(getBaseFileName(f.toLowerCase())));
194183
const cleanedTypingNames = map(inferredTypingNames, f => f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""));
195184
if (safeList === undefined) {
196185
mergeTypings(cleanedTypingNames);
@@ -216,16 +205,13 @@ namespace ts.JsTyping {
216205
}
217206

218207
const typingNames: string[] = [];
219-
const packageJsonFiles =
220-
filter(
221-
host.readDirectory(nodeModulesPath, /*extension*/ undefined, /*exclude*/ undefined, /*depth*/ 2),
222-
f => ts.getBaseFileName(f) === "package.json");
223-
224-
for (const packageJsonFile of packageJsonFiles) {
225-
const packageJsonDict = tryParseJson(packageJsonFile, host);
208+
const jsonFiles = host.readDirectory(nodeModulesPath, "*.json", /*exclude*/ undefined, /*depth*/ 2);
209+
for (const jsonFile of jsonFiles) {
210+
if (getBaseFileName(jsonFile) !== "package.json") { continue; }
211+
const packageJsonDict = tryParseJson(jsonFile, host);
226212
if (!packageJsonDict) { continue; }
227213

228-
filesToWatch.push(packageJsonFile);
214+
filesToWatch.push(jsonFile);
229215

230216
// npm 3 has the package.json contains a "_requiredBy" field
231217
// we should include all the top level module names for npm 2, and only module names whose
@@ -239,8 +225,8 @@ namespace ts.JsTyping {
239225
// to download d.ts files from DefinitelyTyped
240226
const packageName = packageJsonDict["name"];
241227
if (hasProperty(packageJsonDict, "typings")) {
242-
const absPath = ts.getNormalizedAbsolutePath(packageJsonDict.typings, ts.getDirectoryPath(packageJsonFile));
243-
inferredTypings[packageName] = absPath;
228+
const absolutePath = getNormalizedAbsolutePath(packageJsonDict.typings, getDirectoryPath(jsonFile));
229+
inferredTypings[packageName] = absolutePath;
244230
}
245231
else {
246232
typingNames.push(packageName);
@@ -267,7 +253,7 @@ namespace ts.JsTyping {
267253
* @param host The object providing I/O related operations.
268254
*/
269255
export function updateNotFoundTypingNames(newTypingNames: string[], cachePath: string, host: TypingResolutionHost): void {
270-
const tsdJsonPath = ts.combinePaths(cachePath, "tsd.json");
256+
const tsdJsonPath = combinePaths(cachePath, "tsd.json");
271257
const cacheTsdJsonDict = tryParseJson(tsdJsonPath, host);
272258
if (cacheTsdJsonDict) {
273259
const installedTypingFiles = hasProperty(cacheTsdJsonDict, "installed")

0 commit comments

Comments
 (0)