diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index bb7166beeec..efc1e53efb8 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1460,7 +1460,7 @@ namespace ts { const typeLiteralSymbol = createSymbol(SymbolFlags.TypeLiteral, "__type"); addDeclarationToSymbol(typeLiteralSymbol, node, SymbolFlags.TypeLiteral); - typeLiteralSymbol.members = createMapWithEntry(symbol.name, symbol); + typeLiteralSymbol.members = new StringMap([[symbol.name, symbol]]); } function bindObjectLiteralExpression(node: ObjectLiteralExpression) { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 514d225139e..a01c3d9f975 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -352,7 +352,7 @@ namespace ts { ResolvedReturnType } - const builtinGlobals = createMapWithEntry(undefinedSymbol.name, undefinedSymbol); + const builtinGlobals = new StringMap([[undefinedSymbol.name, undefinedSymbol]]); initializeTypeChecker(); @@ -3789,7 +3789,7 @@ namespace ts { type.typeParameters = concatenate(outerTypeParameters, localTypeParameters); type.outerTypeParameters = outerTypeParameters; type.localTypeParameters = localTypeParameters; - (type).instantiations = createMapWithEntry(getTypeListId(type.typeParameters), type); + (type).instantiations = new StringMap([[getTypeListId(type.typeParameters), type]]); (type).target = type; (type).typeArguments = type.typeParameters; type.thisType = createType(TypeFlags.TypeParameter); @@ -3831,7 +3831,7 @@ namespace ts { if (typeParameters) { // Initialize the instantiation cache for generic type aliases. The declared type corresponds to // an instantiation of the type alias with the type parameters supplied as type arguments. - links.instantiations = createMapWithEntry(getTypeListId(links.typeParameters), type); + links.instantiations = new StringMap([[getTypeListId(links.typeParameters), type]]); } } else { @@ -5343,7 +5343,7 @@ namespace ts { type.typeParameters = typeParameters; type.outerTypeParameters = undefined; type.localTypeParameters = typeParameters; - type.instantiations = createMapWithEntry(getTypeListId(type.typeParameters), type); + type.instantiations = new StringMap([[getTypeListId(type.typeParameters), type]]); type.target = type; type.typeArguments = type.typeParameters; type.thisType = createType(TypeFlags.TypeParameter); @@ -6831,7 +6831,7 @@ namespace ts { } sourceStack[depth] = source; targetStack[depth] = target; - maybeStack[depth] = createMapWithEntry(id, RelationComparisonResult.Succeeded); + maybeStack[depth] = new StringMap([[id, RelationComparisonResult.Succeeded]]); depth++; const saveExpandingFlags = expandingFlags; if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) expandingFlags |= 1; diff --git a/src/compiler/collections.ts b/src/compiler/collections.ts index e553363e380..40ae40bad05 100644 --- a/src/compiler/collections.ts +++ b/src/compiler/collections.ts @@ -71,13 +71,19 @@ namespace ts { }; export interface StringMapConstructor { - new(): Map; + new(pairs?: [string, T][]): Map; } /** In runtimes without Maps, this is implemented using an object. */ export const StringMap: StringMapConstructor = usingNativeMaps ? Map : class ShimStringMap implements Map { private data = createDictionaryModeObject(); - constructor() {} + constructor(pairs?: [string, T][]) { + if (pairs) { + for (const [key, value] of pairs) { + this.data[key] = value; + } + } + } clear() { this.data = createDictionaryModeObject(); @@ -276,16 +282,8 @@ namespace ts { export const setIsEmpty: (set: Set) => boolean = usingNativeSets ? set => (set as any).size === 0 : (set: ShimStringSet) => set.isEmpty(); -} -// Map utilities -namespace ts { - /** Create a map containing a single entry key -> value. */ - export function createMapWithEntry(key: string, value: T): Map { - const map = new StringMap(); - map.set(key, value); - return map; - } + // Map utilities /** Set a value in a map, then return that value. */ export function setAndReturn(map: Map, key: K, value: V): V { @@ -477,11 +475,9 @@ namespace ts { naturalNumberKeys.sort((a, b) => toInt(a) - toInt(b)); return naturalNumberKeys.concat(allOtherKeys); } -} -// Set utilities -/* @internal */ -namespace ts { + // Set utilities + /** Union of the `getSet` of each element in the array. */ export function setAggregate(array: T[], getSet: (t: T) => Set): Set { const result = new StringSet(); @@ -506,11 +502,9 @@ namespace ts { }); return result; } -} -// MapLike utilities -/* @internal */ -namespace ts { + // MapLike utilities + const hasOwnProperty = Object.prototype.hasOwnProperty; export function clone(object: T): T { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index cdadbe2a259..ff58f2ca4bb 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -146,7 +146,7 @@ namespace ts { const fingerprint = outputFingerprints.get(fileName); // If output has not been changed, and the file has no external modification - if (fingerprint !== undefined && fingerprint.byteOrderMark === writeByteOrderMark && + if (fingerprint && fingerprint.byteOrderMark === writeByteOrderMark && fingerprint.hash === hash && fingerprint.mtime.getTime() === mtimeBefore.getTime()) { return; diff --git a/src/harness/harness.ts b/src/harness/harness.ts index ab7133b94a4..889dd6738b3 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -925,8 +925,10 @@ namespace Harness { export const defaultLibFileName = "lib.d.ts"; export const es2015DefaultLibFileName = "lib.es2015.d.ts"; - const libFileNameSourceFileMap = ts.createMapWithEntry(defaultLibFileName, - createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + "lib.es5.d.ts"), /*languageVersion*/ ts.ScriptTarget.Latest)); + const libFileNameSourceFileMap = new ts.StringMap([[ + defaultLibFileName, + createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + "lib.es5.d.ts"), /*languageVersion*/ ts.ScriptTarget.Latest) + ]]); export function getDefaultLibrarySourceFile(fileName = defaultLibFileName): ts.SourceFile { if (!isDefaultLibraryFile(fileName)) {