Skip to content

Add getOrUpdate helper #10436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ 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[name] || (symbolTable[name] = createSymbol(SymbolFlags.None, name));
symbol = getOrUpdate(symbolTable, name, name => createSymbol(SymbolFlags.None, name));

if (name && (includes & SymbolFlags.Classifiable)) {
classifiableNames[name] = name;
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ namespace ts {
function getSymbolLinks(symbol: Symbol): SymbolLinks {
if (symbol.flags & SymbolFlags.Transient) return <TransientSymbol>symbol;
const id = getSymbolId(symbol);
return symbolLinks[id] || (symbolLinks[id] = {});
return getOrUpdateArray(symbolLinks, id, () => ({}));
}

function getNodeLinks(node: Node): NodeLinks {
const nodeId = getNodeId(node);
return nodeLinks[nodeId] || (nodeLinks[nodeId] = { flags: 0 });
return getOrUpdateArray<NodeLinks>(nodeLinks, nodeId, () => ({ flags: 0 }));
}

function isGlobalSourceFile(node: Node) {
Expand Down Expand Up @@ -8351,7 +8351,7 @@ namespace ts {
// If we have previously computed the control flow type for the reference at
// this flow loop junction, return the cached type.
const id = getFlowNodeId(flow);
const cache = flowLoopCaches[id] || (flowLoopCaches[id] = createMap<Type>());
const cache = getOrUpdateArray<Map<Type>>(flowLoopCaches, id, () => createMap<Type>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that createMap takes an argument. If the change recommended below for getOrUpdateArray is applied, then it would no longer be safe to pass createMap directly.

if (!key) {
key = getFlowCacheKey(reference);
}
Expand Down
17 changes: 17 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,23 @@ namespace ts {
return hasOwnProperty.call(map, key) ? map[key] : undefined;
}

/**
* Gets a property in a Map, or if it doesn't exist, creates and returns it.
* `makeValue` is also passed the key to avoid creating unnecessary closures.
*/
export function getOrUpdate<T>(map: Map<T>, key: string, makeValue: (key: string) => T) {
return map[key] || (map[key] = makeValue(key));
}

/**
* Gets a value in an array, or if it doesn't exist, creates and returns it.
* It is the caller's responsibility to ensure that `key` is within the bounds of the array.
* `makeValue` is also passed the key to avoid creating unnecessary closures.
*/
export function getOrUpdateArray<T>(arr: T[], key: number, makeValue: (key: number) => T) {
return arr[key] || (arr[key] = makeValue(key));
}

/**
* Gets the owned, enumerable property keys of a map-like.
*
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ namespace ts {

function internIdentifier(text: string): string {
text = escapeIdentifier(text);
return identifiers[text] || (identifiers[text] = text);
return getOrUpdate(identifiers, text, text => text);
}

// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ namespace ts {
}

function getDeclarations(name: string) {
return result[name] || (result[name] = []);
return getOrUpdate(result, name, () => []);
}

function getDeclarationName(declaration: Declaration) {
Expand Down