Skip to content

Commit 889e5ac

Browse files
committed
Clean up/move some Map helper functions.
1 parent 1dc495a commit 889e5ac

File tree

9 files changed

+18
-151
lines changed

9 files changed

+18
-151
lines changed

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,8 @@ namespace ts {
10161016
}
10171017
}
10181018

1019-
const literalFiles = reduceOwnProperties(literalFileMap, addFileToOutput, []);
1020-
const wildcardFiles = reduceOwnProperties(wildcardFileMap, addFileToOutput, []);
1019+
const literalFiles = reduceProperties(literalFileMap, addFileToOutput, []);
1020+
const wildcardFiles = reduceProperties(wildcardFileMap, addFileToOutput, []);
10211021
wildcardFiles.sort(host.useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive);
10221022
return {
10231023
fileNames: literalFiles.concat(wildcardFiles),

src/compiler/core.ts

Lines changed: 9 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ namespace ts {
3030
map["__"] = undefined;
3131
delete map["__"];
3232

33-
if (template) {
34-
copyOwnProperties(template, map);
33+
// Copies keys/values from template. Note that for..in will not throw if
34+
// template is undefined, and instead will just exit the loop.
35+
for (const key in template) if (hasOwnProperty.call(template, key)) {
36+
map[key] = template[key];
3537
}
3638

3739
return map;
@@ -412,9 +414,6 @@ namespace ts {
412414
/**
413415
* Enumerates the properties of a Map<T>, invoking a callback and returning the first truthy result.
414416
*
415-
* NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
416-
* forEachOwnProperties instead as it offers better runtime safety.
417-
*
418417
* @param map A map for which properties should be enumerated.
419418
* @param callback A callback to invoke for each property.
420419
*/
@@ -426,53 +425,6 @@ namespace ts {
426425
return result;
427426
}
428427

429-
/**
430-
* Enumerates the owned properties of a MapLike<T>, invoking a callback and returning the first truthy result.
431-
*
432-
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
433-
* forEachProperty instead as it offers better performance.
434-
*
435-
* @param map A map for which properties should be enumerated.
436-
* @param callback A callback to invoke for each property.
437-
*/
438-
export function forEachOwnProperty<T, U>(map: MapLike<T>, callback: (value: T, key: string) => U): U {
439-
let result: U;
440-
for (const key in map) if (hasOwnProperty.call(map, key)) {
441-
if (result = callback(map[key], key)) break;
442-
}
443-
return result;
444-
}
445-
446-
/**
447-
* Maps key-value pairs of a map into a new map.
448-
*
449-
* NOTE: The key-value pair passed to the callback is *not* safe to cache between invocations
450-
* of the callback.
451-
*
452-
* @param map A map.
453-
* @param callback A callback that maps a key-value pair into a new key-value pair.
454-
*/
455-
export function mapPairs<T, U>(map: Map<T>, callback: (entry: [string, T]) => [string, U]): Map<U> {
456-
let result: Map<U>;
457-
if (map) {
458-
result = createMap<U>();
459-
let inPair: [string, T];
460-
for (const key in map) {
461-
if (inPair) {
462-
inPair[0] = key;
463-
inPair[1] = map[key];
464-
}
465-
else {
466-
inPair = [key, map[key]];
467-
}
468-
469-
const outPair = callback(inPair);
470-
result[outPair[0]] = outPair[1];
471-
}
472-
}
473-
return result;
474-
}
475-
476428
/**
477429
* Returns true if a Map<T> has some matching property.
478430
*
@@ -489,9 +441,6 @@ namespace ts {
489441
/**
490442
* Performs a shallow copy of the properties from a source Map<T> to a target MapLike<T>
491443
*
492-
* NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
493-
* copyOwnProperties instead as it offers better runtime safety.
494-
*
495444
* @param source A map from which properties should be copied.
496445
* @param target A map to which properties should be copied.
497446
*/
@@ -501,21 +450,6 @@ namespace ts {
501450
}
502451
}
503452

504-
/**
505-
* Performs a shallow copy of the owned properties from a source map to a target map-like.
506-
*
507-
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
508-
* copyProperties instead as it offers better performance.
509-
*
510-
* @param source A map-like from which properties should be copied.
511-
* @param target A map-like to which properties should be copied.
512-
*/
513-
export function copyOwnProperties<T>(source: MapLike<T>, target: MapLike<T>): void {
514-
for (const key in source) if (hasOwnProperty.call(source, key)) {
515-
target[key] = source[key];
516-
}
517-
}
518-
519453
/**
520454
* Reduce the properties of a map.
521455
*
@@ -552,72 +486,9 @@ namespace ts {
552486
return result;
553487
}
554488

555-
/**
556-
* Counts the properties of a map.
557-
*
558-
* NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
559-
* countOwnProperties instead as it offers better runtime safety.
560-
*
561-
* @param map A map whose properties should be counted.
562-
* @param predicate An optional callback used to limit which properties should be counted.
563-
*/
564-
export function countProperties<T>(map: Map<T>, predicate?: (value: T, key: string) => boolean) {
565-
let count = 0;
566-
for (const key in map) {
567-
if (!predicate || predicate(map[key], key)) {
568-
count++;
569-
}
570-
}
571-
return count;
572-
}
573-
574-
/**
575-
* Counts the owned properties of a map-like.
576-
*
577-
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
578-
* countProperties instead as it offers better performance.
579-
*
580-
* @param map A map-like whose properties should be counted.
581-
* @param predicate An optional callback used to limit which properties should be counted.
582-
*/
583-
export function countOwnProperties<T>(map: MapLike<T>, predicate?: (value: T, key: string) => boolean) {
584-
let count = 0;
585-
for (const key in map) if (hasOwnProperty.call(map, key)) {
586-
if (!predicate || predicate(map[key], key)) {
587-
count++;
588-
}
589-
}
590-
return count;
591-
}
592-
593-
/**
594-
* Performs a shallow equality comparison of the contents of two maps.
595-
*
596-
* NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
597-
* equalOwnProperties instead as it offers better runtime safety.
598-
*
599-
* @param left A map whose properties should be compared.
600-
* @param right A map whose properties should be compared.
601-
*/
602-
export function equalProperties<T>(left: Map<T>, right: Map<T>, equalityComparer?: (left: T, right: T) => boolean) {
603-
if (left === right) return true;
604-
if (!left || !right) return false;
605-
for (const key in left) {
606-
if (!(key in right)) return false;
607-
if (equalityComparer ? !equalityComparer(left[key], right[key]) : left[key] !== right[key]) return false;
608-
}
609-
for (const key in right) {
610-
if (!(key in left)) return false;
611-
}
612-
return true;
613-
}
614-
615489
/**
616490
* Performs a shallow equality comparison of the contents of two map-likes.
617491
*
618-
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
619-
* equalProperties instead as it offers better performance.
620-
*
621492
* @param left A map-like whose properties should be compared.
622493
* @param right A map-like whose properties should be compared.
623494
*/
@@ -670,17 +541,13 @@ namespace ts {
670541
return result;
671542
}
672543

673-
export function extend<T1 extends MapLike<{}>, T2 extends MapLike<{}>>(first: T1 , second: T2): T1 & T2 {
544+
export function extend<T1, T2>(first: T1 , second: T2): T1 & T2 {
674545
const result: T1 & T2 = <any>{};
675-
for (const id in first) {
676-
if (hasOwnProperty.call(first, id)) {
677-
(result as any)[id] = first[id];
678-
}
546+
for (const id in second) if (hasOwnProperty.call(second, id)) {
547+
(result as any)[id] = (second as any)[id];
679548
}
680-
for (const id in second) {
681-
if (hasOwnProperty.call(second, id) && !hasOwnProperty.call(result, id)) {
682-
(result as any)[id] = second[id];
683-
}
549+
for (const id in first) if (hasOwnProperty.call(first, id)) {
550+
(result as any)[id] = (first as any)[id];
684551
}
685552
return result;
686553
}

src/harness/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ namespace FourSlash {
773773
}
774774

775775
public verifyRangesWithSameTextReferenceEachOther() {
776-
ts.forEachOwnProperty(this.rangesByText(), ranges => this.verifyRangesReferenceEachOther(ranges));
776+
ts.forEachProperty(this.rangesByText(), ranges => this.verifyRangesReferenceEachOther(ranges));
777777
}
778778

779779
private verifyReferencesWorker(references: ts.ReferenceEntry[], fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) {

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace Harness.LanguageService {
135135

136136
public getFilenames(): string[] {
137137
const fileNames: string[] = [];
138-
ts.forEachOwnProperty(this.fileNameToScript, (scriptInfo) => {
138+
ts.forEachProperty(this.fileNameToScript, (scriptInfo) => {
139139
if (scriptInfo.isRootFile) {
140140
// only include root files here
141141
// usually it means that we won't include lib.d.ts in the list of root files so it won't mess the computation of compilation root dir.

src/harness/unittests/moduleResolution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ export = C;
358358
function test(files: Map<string>, options: CompilerOptions, currentDirectory: string, useCaseSensitiveFileNames: boolean, rootFiles: string[], diagnosticCodes: number[]): void {
359359
const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
360360
if (!useCaseSensitiveFileNames) {
361-
files = mapPairs(files, ([fileName, file]) => [getCanonicalFileName(fileName), file]);
361+
files = reduceProperties(files, (files, file, fileName) => (files[getCanonicalFileName(fileName)] = file, files), createMap<string>());
362362
}
363363

364364
const host: CompilerHost = {

src/harness/unittests/reuseProgramStructure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ namespace ts {
183183
}
184184
else {
185185
assert.isTrue(cache !== undefined, `expected ${caption} to be set`);
186-
assert.isTrue(equalProperties(expectedContent, cache, entryChecker), `contents of ${caption} did not match the expected contents.`);
186+
assert.isTrue(equalOwnProperties(expectedContent, cache, entryChecker), `contents of ${caption} did not match the expected contents.`);
187187
}
188188
}
189189

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace ts {
6969
}
7070

7171
function checkMapKeys(caption: string, map: Map<any>, expectedKeys: string[]) {
72-
assert.equal(countProperties(map), expectedKeys.length, `${caption}: incorrect size of map`);
72+
assert.equal(reduceProperties(map, count => count + 1, 0), expectedKeys.length, `${caption}: incorrect size of map`);
7373
for (const name of expectedKeys) {
7474
assert.isTrue(name in map, `${caption} is expected to contain ${name}, actual keys: ${Object.keys(map)}`);
7575
}

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ namespace ts.server {
14191419
/*recursive*/ true
14201420
);
14211421

1422-
project.directoriesWatchedForWildcards = reduceOwnProperties(projectOptions.wildcardDirectories, (watchers, flag, directory) => {
1422+
project.directoriesWatchedForWildcards = reduceProperties(createMap(projectOptions.wildcardDirectories), (watchers, flag, directory) => {
14231423
if (comparePaths(configDirectoryPath, directory, ".", !this.host.useCaseSensitiveFileNames) !== Comparison.EqualTo) {
14241424
const recursive = (flag & WatchDirectoryFlags.Recursive) !== 0;
14251425
this.log(`Add ${ recursive ? "recursive " : ""}watcher for: ${directory}`);

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ namespace ts {
20582058
options[opt.name] = parseCustomTypeOption(opt, value, diagnostics);
20592059
}
20602060
else {
2061-
if (!forEachOwnProperty(opt.type, v => v === value)) {
2061+
if (!forEachProperty(opt.type, v => v === value)) {
20622062
// Supplied value isn't a valid enum value.
20632063
diagnostics.push(createCompilerDiagnosticForInvalidCustomType(opt));
20642064
}

0 commit comments

Comments
 (0)