Skip to content

Commit 39a51d3

Browse files
author
zhengbli
committed
Unify the use of "filter", "map" and "Object.keys" functions
1 parent 70ca4bd commit 39a51d3

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/compiler/core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ namespace ts {
278278
return hasOwnProperty.call(map, key);
279279
}
280280

281+
export function getKeys<T>(map: Map<T>): string[] {
282+
const keys: string[] = [];
283+
for (const key in map) {
284+
keys.push(key);
285+
}
286+
return keys;
287+
}
288+
281289
export function getProperty<T>(map: Map<T>, key: string): T {
282290
return hasOwnProperty.call(map, key) ? map[key] : undefined;
283291
}

src/services/jsTyping.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ namespace ts.JsTyping {
6666

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

7371
const safeListFilePath = ts.combinePaths(globalCachePath, "safeList.json");
7472
if (!safeList && host.fileExists(safeListFilePath)) {
@@ -84,7 +82,7 @@ namespace ts.JsTyping {
8482
exclude = typingOptions.exclude || [];
8583

8684
if (typingOptions.enableAutoDiscovery) {
87-
const possibleSearchDirs = fileNames.map(ts.getDirectoryPath);
85+
const possibleSearchDirs = map(fileNames, ts.getDirectoryPath);
8886
if (projectRootPath !== undefined) {
8987
possibleSearchDirs.push(projectRootPath);
9088
}
@@ -109,7 +107,7 @@ namespace ts.JsTyping {
109107
const tsdJsonDict = tryParseJson(tsdJsonPath, host);
110108
if (tsdJsonDict) {
111109
for (const notFoundTypingName of notFoundTypingNames) {
112-
if (inferredTypings.hasOwnProperty(notFoundTypingName) && !inferredTypings[notFoundTypingName]) {
110+
if (hasProperty(inferredTypings, notFoundTypingName) && !inferredTypings[notFoundTypingName]) {
113111
delete inferredTypings[notFoundTypingName];
114112
}
115113
}
@@ -156,7 +154,7 @@ namespace ts.JsTyping {
156154
}
157155

158156
for (const typing of typingNames) {
159-
if (!inferredTypings.hasOwnProperty(typing)) {
157+
if (!hasProperty(inferredTypings, typing)) {
160158
inferredTypings[typing] = undefined;
161159
}
162160
}
@@ -169,11 +167,11 @@ namespace ts.JsTyping {
169167
const jsonDict = tryParseJson(jsonPath, host);
170168
if (jsonDict) {
171169
filesToWatch.push(jsonPath);
172-
if (jsonDict.hasOwnProperty("dependencies")) {
173-
mergeTypings(Object.keys(jsonDict.dependencies));
170+
if (hasProperty(jsonDict, "dependencies")) {
171+
mergeTypings(getKeys(jsonDict.dependencies));
174172
}
175-
if (jsonDict.hasOwnProperty("devDependencies")) {
176-
mergeTypings(Object.keys(jsonDict.devDependencies));
173+
if (hasProperty(jsonDict, "devDependencies")) {
174+
mergeTypings(getKeys(jsonDict.devDependencies));
177175
}
178176
}
179177
}
@@ -185,12 +183,17 @@ namespace ts.JsTyping {
185183
* @param fileNames are the names for source files in the project
186184
*/
187185
function getTypingNamesFromSourceFileNames(fileNames: string[]) {
188-
const jsFileNames = fileNames.filter(hasJavaScriptFileExtension);
189-
const inferredTypingNames = jsFileNames.map(f => ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())));
190-
const cleanedTypingNames = inferredTypingNames.map(f => f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""));
191-
safeList === undefined ? mergeTypings(cleanedTypingNames) : mergeTypings(cleanedTypingNames.filter(f => safeList.hasOwnProperty(f)));
186+
const jsFileNames = filter(fileNames, hasJavaScriptFileExtension);
187+
const inferredTypingNames = map(jsFileNames, f => ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())));
188+
const cleanedTypingNames = map(inferredTypingNames, f => f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""));
189+
if (safeList === undefined) {
190+
mergeTypings(cleanedTypingNames);
191+
}
192+
else {
193+
mergeTypings(filter(cleanedTypingNames, f => hasProperty(safeList, f)));
194+
}
192195

193-
const jsxFileNames = fileNames.filter(f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JSX));
196+
const jsxFileNames = filter(fileNames, f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JSX));
194197
if (jsxFileNames.length > 0) {
195198
mergeTypings(["react"]);
196199
}
@@ -208,7 +211,9 @@ namespace ts.JsTyping {
208211

209212
const typingNames: string[] = [];
210213
const packageJsonFiles =
211-
host.readDirectory(nodeModulesPath, /*extension*/ undefined, /*exclude*/ undefined, /*depth*/ 2).filter(f => ts.getBaseFileName(f) === "package.json");
214+
filter(
215+
host.readDirectory(nodeModulesPath, /*extension*/ undefined, /*exclude*/ undefined, /*depth*/ 2),
216+
f => ts.getBaseFileName(f) === "package.json");
212217
for (const packageJsonFile of packageJsonFiles) {
213218
const packageJsonDict = tryParseJson(packageJsonFile, host);
214219
if (!packageJsonDict) { continue; }
@@ -219,14 +224,14 @@ namespace ts.JsTyping {
219224
// we should include all the top level module names for npm 2, and only module names whose
220225
// "_requiredBy" field starts with "#" or equals "/" for npm 3.
221226
if (packageJsonDict._requiredBy &&
222-
packageJsonDict._requiredBy.filter((r: string) => r[0] === "#" || r === "/").length === 0) {
227+
filter(packageJsonDict._requiredBy, (r: string) => r[0] === "#" || r === "/").length === 0) {
223228
continue;
224229
}
225230

226231
// If the package has its own d.ts typings, those will take precedence. Otherwise the package name will be used
227232
// to download d.ts files from DefinitelyTyped
228233
const packageName = packageJsonDict["name"];
229-
if (packageJsonDict.hasOwnProperty("typings")) {
234+
if (hasProperty(packageJsonDict, "typings")) {
230235
const absPath = ts.getNormalizedAbsolutePath(packageJsonDict.typings, ts.getDirectoryPath(packageJsonFile));
231236
inferredTypings[packageName] = absPath;
232237
}
@@ -266,10 +271,10 @@ namespace ts.JsTyping {
266271
const cacheTsdJsonDict = tryParseJson(tsdJsonPath, host);
267272
if (cacheTsdJsonDict) {
268273
const installedTypingFiles = hasProperty(cacheTsdJsonDict, "installed")
269-
? Object.keys(cacheTsdJsonDict.installed)
274+
? getKeys(cacheTsdJsonDict.installed)
270275
: [];
271276
const newMissingTypingNames =
272-
ts.filter(newTypingNames, name => notFoundTypingNames.indexOf(name) < 0 && !isInstalled(name, installedTypingFiles));
277+
filter(newTypingNames, name => notFoundTypingNames.indexOf(name) < 0 && !isInstalled(name, installedTypingFiles));
273278
for (const newMissingTypingName of newMissingTypingNames) {
274279
notFoundTypingNames.push(newMissingTypingName);
275280
}

0 commit comments

Comments
 (0)