Remove the "set" function and use map.set with multiple lines of code if necessary.

This commit is contained in:
Andy Hanson 2016-12-12 08:42:12 -08:00
parent 5c304d0d0f
commit b53b5cf4ab
14 changed files with 91 additions and 45 deletions

View file

@ -349,7 +349,10 @@ namespace ts {
// Otherwise, we'll be merging into a compatible existing symbol (for example when
// you have multiple 'vars' with the same name in the same container). In this case
// just add this node into the declarations list of the symbol.
symbol = symbolTable.get(name) || set(symbolTable, name, createSymbol(SymbolFlags.None, name));
symbol = symbolTable.get(name);
if (!symbol) {
symbolTable.set(name, symbol = createSymbol(SymbolFlags.None, name));
}
if (name && (includes & SymbolFlags.Classifiable)) {
classifiableNames.set(name, name);
@ -359,7 +362,7 @@ namespace ts {
if (symbol.isReplaceableByMethod) {
// Javascript constructor-declared symbols can be discarded in favor of
// prototype symbols like methods.
symbol = set(symbolTable, name, createSymbol(SymbolFlags.None, name));
symbolTable.set(name, symbol = createSymbol(SymbolFlags.None, name));
}
else {
if (node.name) {

View file

@ -4062,7 +4062,8 @@ namespace ts {
const memberSymbol = getSymbolOfNode(member);
const value = getEnumMemberValue(member);
if (!memberTypes.has(value)) {
const memberType = set(memberTypes, value, createEnumLiteralType(memberSymbol, enumType, "" + value));
const memberType = createEnumLiteralType(memberSymbol, enumType, "" + value);
memberTypes.set(value, memberType);
memberTypeList.push(memberType);
}
}
@ -5192,7 +5193,11 @@ namespace ts {
function getSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
const instantiations = signature.instantiations || (signature.instantiations = createMap<Signature>());
const id = getTypeListId(typeArguments);
return instantiations.get(id) || set(instantiations, id, createSignatureInstantiation(signature, typeArguments));
let instantiation = instantiations.get(id);
if (!instantiation) {
instantiations.set(id, instantiation = createSignatureInstantiation(signature, typeArguments));
}
return instantiation;
}
function createSignatureInstantiation(signature: Signature, typeArguments: Type[]): Signature {
@ -5348,7 +5353,8 @@ namespace ts {
const id = getTypeListId(typeArguments);
let type = target.instantiations.get(id);
if (!type) {
type = set(target.instantiations, id, <TypeReference>createObjectType(ObjectFlags.Reference, target.symbol));
type = <TypeReference>createObjectType(ObjectFlags.Reference, target.symbol);
target.instantiations.set(id, type);
type.flags |= typeArguments ? getPropagatingFlagsOfTypes(typeArguments, /*excludeKinds*/ 0) : 0;
type.target = target;
type.typeArguments = typeArguments;
@ -5395,7 +5401,11 @@ namespace ts {
const links = getSymbolLinks(symbol);
const typeParameters = links.typeParameters;
const id = getTypeListId(typeArguments);
return links.instantiations.get(id) || set(links.instantiations, id, instantiateTypeNoAlias(type, createTypeMapper(typeParameters, typeArguments)));
let instantiation = links.instantiations.get(id);
if (!instantiation) {
links.instantiations.set(id, instantiation = instantiateTypeNoAlias(type, createTypeMapper(typeParameters, typeArguments)));
}
return instantiation;
}
// Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include
@ -5844,7 +5854,8 @@ namespace ts {
let type = unionTypes.get(id);
if (!type) {
const propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
type = set(unionTypes, id, <UnionType>createType(TypeFlags.Union | propagatedFlags));
type = <UnionType>createType(TypeFlags.Union | propagatedFlags);
unionTypes.set(id, type);
type.types = types;
type.aliasSymbol = aliasSymbol;
type.aliasTypeArguments = aliasTypeArguments;
@ -5918,7 +5929,8 @@ namespace ts {
let type = intersectionTypes.get(id);
if (!type) {
const propagatedFlags = getPropagatingFlagsOfTypes(typeSet, /*excludeKinds*/ TypeFlags.Nullable);
type = set(intersectionTypes, id, <IntersectionType>createType(TypeFlags.Intersection | propagatedFlags));
type = <IntersectionType>createType(TypeFlags.Intersection | propagatedFlags);
intersectionTypes.set(id, type);
type.types = typeSet;
type.aliasSymbol = aliasSymbol;
type.aliasTypeArguments = aliasTypeArguments;
@ -6100,7 +6112,11 @@ namespace ts {
}
// Otherwise we defer the operation by creating an indexed access type.
const id = objectType.id + "," + indexType.id;
return indexedAccessTypes.get(id) || set(indexedAccessTypes, id, createIndexedAccessType(objectType, indexType));
let type = indexedAccessTypes.get(id);
if (!type) {
indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType));
}
return type;
}
// In the following we resolve T[K] to the type of the property in T selected by K.
const apparentObjectType = getApparentType(objectType);
@ -6268,7 +6284,11 @@ namespace ts {
function getLiteralTypeForText(flags: TypeFlags, text: string) {
const map = flags & TypeFlags.StringLiteral ? stringLiteralTypes : numericLiteralTypes;
return map.get(text) || set(map, text, createLiteralType(flags, text));
let type = map.get(text);
if (!type) {
map.set(text, type = createLiteralType(flags, text));
}
return type;
}
function getTypeFromLiteralTypeNode(node: LiteralTypeNode): Type {
@ -7060,7 +7080,8 @@ namespace ts {
if (source.symbol.name !== target.symbol.name ||
!(source.symbol.flags & SymbolFlags.RegularEnum) || !(target.symbol.flags & SymbolFlags.RegularEnum) ||
(source.flags & TypeFlags.Union) !== (target.flags & TypeFlags.Union)) {
return set(enumRelation, id, false);
enumRelation.set(id, false);
return false;
}
const targetEnumType = getTypeOfSymbol(target.symbol);
for (const property of getPropertiesOfType(getTypeOfSymbol(source.symbol))) {
@ -7071,11 +7092,13 @@ namespace ts {
errorReporter(Diagnostics.Property_0_is_missing_in_type_1, property.name,
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
}
return set(enumRelation, id, false);
enumRelation.set(id, false);
return false;
}
}
}
return set(enumRelation, id, true);
enumRelation.set(id, true);
return true;
}
function isSimpleTypeRelatedTo(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorReporter?: ErrorReporter) {
@ -9815,7 +9838,8 @@ namespace ts {
if (isIncomplete(firstAntecedentType)) {
return createFlowType(result, /*incomplete*/ true);
}
return set(cache, key, result);
cache.set(key, result);
return result;
}
function isMatchingReferenceDiscriminant(expr: Expression) {
@ -11771,9 +11795,9 @@ namespace ts {
}
function getJsxType(name: string) {
const jsxType = jsxTypes.get(name);
let jsxType = jsxTypes.get(name);
if (jsxType === undefined) {
return set(jsxTypes, name, getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType);
jsxTypes.set(name, jsxType = getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType);
}
return jsxType;
}

View file

@ -156,15 +156,6 @@ namespace ts {
}
}
/**
* Unlike `map.set(key, value)`, this returns the value, making it useful as an expression.
* Prefer `map.set(key, value)` for statements.
*/
export function set<T>(map: Map<T>, key: MapKey, value: T): T {
map.set(key, value);
return value;
}
export function createFileMap<T>(keyMapper?: (key: string) => string): FileMap<T> {
const files = createMap<T>();
return {
@ -1050,14 +1041,14 @@ namespace ts {
* Creates the array if it does not already exist.
*/
export function multiMapAdd<V>(map: Map<V[]>, key: string | number, value: V): V[] {
const values = map.get(key);
let values = map.get(key);
if (values) {
values.push(value);
return values;
}
else {
return set(map, key, [value]);
map.set(key, values = [value]);
}
return values;
}
/**

View file

@ -2545,7 +2545,8 @@ namespace ts {
while (true) {
const generatedName = baseName + i;
if (isUniqueName(generatedName)) {
return set(generatedNameSet, generatedName, generatedName);
generatedNameSet.set(generatedName, generatedName);
return generatedName;
}
i++;
}

View file

@ -1131,7 +1131,11 @@ namespace ts {
function internIdentifier(text: string): string {
text = escapeIdentifier(text);
return identifiers.get(text) || set(identifiers, text, text);
let identifier = identifiers.get(text);
if (identifier === undefined) {
identifiers.set(text, identifier = text);
}
return identifier;
}
// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues

View file

@ -277,9 +277,13 @@ namespace ts {
const resolutions: T[] = [];
const cache = createMap<T>();
for (const name of names) {
const result = cache.has(name)
? cache.get(name)
: set(cache, name, loader(name, containingFile));
let result: T;
if (cache.has(name)) {
result = cache.get(name);
}
else {
cache.set(name, result = loader(name, containingFile));
}
resolutions.push(result);
}
return resolutions;

View file

@ -60,7 +60,8 @@ namespace ts {
}
currentSourceFile = node;
currentModuleInfo = set(moduleInfoMap, getOriginalNodeId(node), collectExternalModuleInfo(node, resolver, compilerOptions));
currentModuleInfo = collectExternalModuleInfo(node, resolver, compilerOptions);
moduleInfoMap.set(getOriginalNodeId(node), currentModuleInfo);
// Perform the transformation.
const transformModule = transformModuleDelegates.get(moduleKind) || transformModuleDelegates.get(ModuleKind.None);

View file

@ -73,11 +73,13 @@ namespace ts {
// see comment to 'substitutePostfixUnaryExpression' for more details
// Collect information about the external module and dependency groups.
moduleInfo = set(moduleInfoMap, id, collectExternalModuleInfo(node, resolver, compilerOptions));
moduleInfo = collectExternalModuleInfo(node, resolver, compilerOptions);
moduleInfoMap.set(id, moduleInfo);
// Make sure that the name of the 'exports' function does not conflict with
// existing identifiers.
exportFunction = set(exportFunctionsMap, id, createUniqueName("exports"));
exportFunction = createUniqueName("exports");
exportFunctionsMap.set(id, exportFunction);
contextObject = createUniqueName("context");
// Add the body of the module.

View file

@ -378,8 +378,11 @@ namespace ts {
}
function cachedFileExists(fileName: string): boolean {
const fileExists = cachedExistingFiles.get(fileName);
return fileExists !== undefined ? fileExists : set(cachedExistingFiles, fileName, hostFileExists(fileName));
let fileExists = cachedExistingFiles.get(fileName);
if (fileExists === undefined) {
cachedExistingFiles.set(fileName, fileExists = hostFileExists(fileName));
}
return fileExists;
}
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void) {

View file

@ -3323,7 +3323,9 @@ namespace ts {
for (const name in syntaxKindEnum) {
if (syntaxKindEnum[name] === kind) {
return set(syntaxKindCache, kind, kind.toString() + " (" + name + ")");
const result = `${kind} (${name})`;
syntaxKindCache.set(kind, result);
return result;
}
}
}

View file

@ -918,9 +918,11 @@ namespace Harness {
return undefined;
}
const sourceFile = libFileNameSourceFileMap.get(fileName);
return sourceFile || ts.set(libFileNameSourceFileMap, fileName,
createSourceFileAndAssertInvariants(fileName, IO.readFile(libFolder + fileName), ts.ScriptTarget.Latest));
let sourceFile = libFileNameSourceFileMap.get(fileName);
if (!sourceFile) {
libFileNameSourceFileMap.set(fileName, sourceFile = createSourceFileAndAssertInvariants(fileName, IO.readFile(libFolder + fileName), ts.ScriptTarget.Latest));
}
return sourceFile;
}
export function getDefaultLibFileName(options: ts.CompilerOptions): string {

View file

@ -34,7 +34,8 @@ namespace ts.server {
let lineMap = this.lineMaps.get(fileName);
if (!lineMap) {
const scriptSnapshot = this.host.getScriptSnapshot(fileName);
lineMap = set(this.lineMaps, fileName, ts.computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength())));
lineMap = ts.computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength()));
this.lineMaps.set(fileName, lineMap);
}
return lineMap;
}

View file

@ -188,7 +188,11 @@ namespace ts {
}
function getWordSpans(word: string): TextSpan[] {
return stringToWordSpans.get(word) || set(stringToWordSpans, word, breakIntoWordSpans(word));
let spans = stringToWordSpans.get(word);
if (!spans) {
stringToWordSpans.set(word, spans = breakIntoWordSpans(word));
}
return spans;
}
function matchTextChunk(candidate: string, chunk: TextChunk, punctuationStripped: boolean): PatternMatch {

View file

@ -532,7 +532,11 @@ namespace ts {
}
function getDeclarations(name: string) {
return result.get(name) || set(result, name, []);
let declarations = result.get(name);
if (!declarations) {
result.set(name, declarations = []);
}
return declarations;
}
function getDeclarationName(declaration: Declaration) {