MCPcopy Create free account
hub / github.com/nodejs/node / renameCollidingVarNames

Function renameCollidingVarNames

test/fixtures/snapshot/typescript.js:151525–151589  ·  view source on GitHub ↗
(nodeToRename, checker, synthNamesMap)

Source from the content-addressed store, hash-verified

151523 It then checks for any collisions and renames them through getSynthesizedDeepClone
151524 */
151525 function renameCollidingVarNames(nodeToRename, checker, synthNamesMap) {
151526 var identsToRenameMap = new ts.Map(); // key is the symbol id
151527 var collidingSymbolMap = ts.createMultiMap();
151528 ts.forEachChild(nodeToRename, function visit(node) {
151529 if (!ts.isIdentifier(node)) {
151530 ts.forEachChild(node, visit);
151531 return;
151532 }
151533 var symbol = checker.getSymbolAtLocation(node);
151534 if (symbol) {
151535 var type = checker.getTypeAtLocation(node);
151536 // Note - the choice of the last call signature is arbitrary
151537 var lastCallSignature = getLastCallSignature(type, checker);
151538 var symbolIdString = ts.getSymbolId(symbol).toString();
151539 // If the identifier refers to a function, we want to add the new synthesized variable for the declaration. Example:
151540 // fetch('...').then(response => { ... })
151541 // will eventually become
151542 // const response = await fetch('...')
151543 // so we push an entry for 'response'.
151544 if (lastCallSignature && !ts.isParameter(node.parent) && !ts.isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) {
151545 var firstParameter = ts.firstOrUndefined(lastCallSignature.parameters);
151546 var ident = (firstParameter === null || firstParameter === void 0 ? void 0 : firstParameter.valueDeclaration)
151547 && ts.isParameter(firstParameter.valueDeclaration)
151548 && ts.tryCast(firstParameter.valueDeclaration.name, ts.isIdentifier)
151549 || ts.factory.createUniqueName("result", 16 /* GeneratedIdentifierFlags.Optimistic */);
151550 var synthName = getNewNameIfConflict(ident, collidingSymbolMap);
151551 synthNamesMap.set(symbolIdString, synthName);
151552 collidingSymbolMap.add(ident.text, symbol);
151553 }
151554 // We only care about identifiers that are parameters, variable declarations, or binding elements
151555 else if (node.parent && (ts.isParameter(node.parent) || ts.isVariableDeclaration(node.parent) || ts.isBindingElement(node.parent))) {
151556 var originalName = node.text;
151557 var collidingSymbols = collidingSymbolMap.get(originalName);
151558 // if the identifier name conflicts with a different identifier that we've already seen
151559 if (collidingSymbols && collidingSymbols.some(function (prevSymbol) { return prevSymbol !== symbol; })) {
151560 var newName = getNewNameIfConflict(node, collidingSymbolMap);
151561 identsToRenameMap.set(symbolIdString, newName.identifier);
151562 synthNamesMap.set(symbolIdString, newName);
151563 collidingSymbolMap.add(originalName, symbol);
151564 }
151565 else {
151566 var identifier = ts.getSynthesizedDeepClone(node);
151567 synthNamesMap.set(symbolIdString, createSynthIdentifier(identifier));
151568 collidingSymbolMap.add(originalName, symbol);
151569 }
151570 }
151571 }
151572 });
151573 return ts.getSynthesizedDeepCloneWithReplacements(nodeToRename, /*includeTrivia*/ true, function (original) {
151574 if (ts.isBindingElement(original) && ts.isIdentifier(original.name) && ts.isObjectBindingPattern(original.parent)) {
151575 var symbol = checker.getSymbolAtLocation(original.name);
151576 var renameInfo = symbol && identsToRenameMap.get(String(ts.getSymbolId(symbol)));
151577 if (renameInfo && renameInfo.text !== (original.name || original.propertyName).getText()) {
151578 return ts.factory.createBindingElement(original.dotDotDotToken, original.propertyName || original.name, renameInfo, original.initializer);
151579 }
151580 }
151581 else if (ts.isIdentifier(original)) {
151582 var symbol = checker.getSymbolAtLocation(original);

Callers 1

convertToAsyncFunctionFunction · 0.85

Calls 11

getLastCallSignatureFunction · 0.85
getNewNameIfConflictFunction · 0.85
createSynthIdentifierFunction · 0.85
StringClass · 0.85
forEachChildMethod · 0.80
someMethod · 0.80
hasMethod · 0.65
addMethod · 0.65
getMethod · 0.65
toStringMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected