From d12110d3e5fe2682fa9f89718c033d975924d306 Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Thu, 13 Sep 2018 09:32:38 -0700 Subject: [PATCH] Respond to CR --- .../codefixes/convertToAsyncFunction.ts | 5 +++-- src/services/utilities.ts | 11 +++++----- .../unittests/convertToAsyncFunction.ts | 6 +++--- ...convertToAsyncFunction_MultipleReturns2.ts | 4 ++-- .../convertToAsyncFunction_bindingPattern.js | 18 +++++++++++++++++ .../convertToAsyncFunction_bindingPattern.ts | 4 ++-- ...yncFunction_bindingPatternNameCollision.js | 20 +++++++++++++++++++ ...yncFunction_bindingPatternNameCollision.ts | 4 ++-- 8 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.js create mode 100644 tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.js diff --git a/src/services/codefixes/convertToAsyncFunction.ts b/src/services/codefixes/convertToAsyncFunction.ts index 51511c2207c..e698218baf9 100644 --- a/src/services/codefixes/convertToAsyncFunction.ts +++ b/src/services/codefixes/convertToAsyncFunction.ts @@ -186,20 +186,21 @@ namespace ts.codefix { } // we only care about identifiers that are parameters and declarations (don't care about other uses) else if (node.parent && (isParameter(node.parent) || isVariableDeclaration(node.parent))) { + const originalName = node.text; // if the identifier name conflicts with a different identifier that we've already seen if (allVarNames.some(ident => ident.originalName === node.text && ident.symbol !== symbol)) { const newName = getNewNameIfConflict(node, allVarNames); identsToRenameMap.set(symbolIdString, newName.identifier); synthNamesMap.set(symbolIdString, newName); - allVarNames.push({ identifier: newName.identifier, symbol, originalName: node.text }); + allVarNames.push({ identifier: newName.identifier, symbol, originalName }); } else { const identifier = getSynthesizedDeepClone(node); identsToRenameMap.set(symbolIdString, identifier); synthNamesMap.set(symbolIdString, { identifier, types: [], numberOfAssignmentsOriginal: allVarNames.filter(elem => elem.identifier.text === node.text).length/*, numberOfAssignmentsSynthesized: 0*/ }); if ((isParameter(node.parent) && isExpressionOrCallOnTypePromise(node.parent.parent)) || isVariableDeclaration(node.parent)) { - allVarNames.push({ identifier, symbol, originalName: node.text }); + allVarNames.push({ identifier, symbol, originalName }); } } } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 4559a88881e..01613a23b0d 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1654,11 +1654,10 @@ namespace ts { return clone; } - export function getSynthesizedDeepCloneWithRenames(node: T, includeTrivia = true, renameMap?: Map, checker?: TypeChecker, callback?: (originalNode: Node, clone: Node) => any): T { - + export function getSynthesizedDeepCloneWithRenames(node: T, includeTrivia = true, renameMap?: Map, checker?: TypeChecker, callback?: (originalNode: Node, clone: Node) => any): T { let clone; - if (node && isIdentifier(node!) && renameMap && checker) { - const symbol = checker.getSymbolAtLocation(node!); + if (isIdentifier(node) && renameMap && checker) { + const symbol = checker.getSymbolAtLocation(node); const renameInfo = symbol && renameMap.get(String(getSymbolId(symbol))); if (renameInfo) { @@ -1667,11 +1666,11 @@ namespace ts { } if (!clone) { - clone = node && getSynthesizedDeepCloneWorker(node as NonNullable, renameMap, checker, callback); + clone = getSynthesizedDeepCloneWorker(node as NonNullable, renameMap, checker, callback); } if (clone && !includeTrivia) suppressLeadingAndTrailingTrivia(clone); - if (callback && node && clone) callback(node!, clone); + if (callback && clone) callback(node, clone); return clone as T; } diff --git a/src/testRunner/unittests/convertToAsyncFunction.ts b/src/testRunner/unittests/convertToAsyncFunction.ts index 05df4f297e8..047df5ffadc 100644 --- a/src/testRunner/unittests/convertToAsyncFunction.ts +++ b/src/testRunner/unittests/convertToAsyncFunction.ts @@ -823,7 +823,7 @@ function [#|f|](): Promise { } return x.then(resp => { var blob = resp.blob().then(blob => blob.byteOffset).catch(err => 'Error'); - return fetch("https://micorosft.com").then(res => console.log("Another one!")); + return fetch("https://microsoft.com").then(res => console.log("Another one!")); }); } ` @@ -1201,7 +1201,7 @@ function [#|f|]() { `); _testConvertToAsyncFunction("convertToAsyncFunction_bindingPattern", ` -function [#|f|]():Promise { +function [#|f|]() { return fetch('https://typescriptlang.org').then(res); } function res({ status, trailer }){ @@ -1210,7 +1210,7 @@ function res({ status, trailer }){ `); _testConvertToAsyncFunction("convertToAsyncFunction_bindingPatternNameCollision", ` -function [#|f|]():Promise { +function [#|f|]() { const result = 'https://typescriptlang.org'; return fetch(result).then(res); } diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_MultipleReturns2.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_MultipleReturns2.ts index 389faf61891..59a02875d84 100644 --- a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_MultipleReturns2.ts +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_MultipleReturns2.ts @@ -7,7 +7,7 @@ function /*[#|*/f/*|]*/(): Promise { } return x.then(resp => { var blob = resp.blob().then(blob => blob.byteOffset).catch(err => 'Error'); - return fetch("https://micorosft.com").then(res => console.log("Another one!")); + return fetch("https://microsoft.com").then(res => console.log("Another one!")); }); } @@ -21,6 +21,6 @@ async function f(): Promise { } const resp = await x; var blob = resp.blob().then(blob_1 => blob_1.byteOffset).catch(err => 'Error'); - const res_2 = await fetch("https://micorosft.com"); + const res_2 = await fetch("https://microsoft.com"); return console.log("Another one!"); } diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.js new file mode 100644 index 00000000000..f06ce44e78b --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.js @@ -0,0 +1,18 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + return fetch('https://typescriptlang.org').then(res); +} +function res({ status, trailer }){ + console.log(status); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = await fetch('https://typescriptlang.org'); + return res(result); +} +function res({ status, trailer }){ + console.log(status); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.ts index 97c68d57260..f06ce44e78b 100644 --- a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.ts +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPattern.ts @@ -1,6 +1,6 @@ // ==ORIGINAL== -function /*[#|*/f/*|]*/():Promise { +function /*[#|*/f/*|]*/() { return fetch('https://typescriptlang.org').then(res); } function res({ status, trailer }){ @@ -9,7 +9,7 @@ function res({ status, trailer }){ // ==ASYNC FUNCTION::Convert to async function== -async function f():Promise { +async function f() { const result = await fetch('https://typescriptlang.org'); return res(result); } diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.js new file mode 100644 index 00000000000..6813472966a --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.js @@ -0,0 +1,20 @@ +// ==ORIGINAL== + +function /*[#|*/f/*|]*/() { + const result = 'https://typescriptlang.org'; + return fetch(result).then(res); +} +function res({ status, trailer }){ + console.log(status); +} + +// ==ASYNC FUNCTION::Convert to async function== + +async function f() { + const result = 'https://typescriptlang.org'; + const result_1 = await fetch(result); + return res(result_1); +} +function res({ status, trailer }){ + console.log(status); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.ts index db0c63535c7..6813472966a 100644 --- a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.ts +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_bindingPatternNameCollision.ts @@ -1,6 +1,6 @@ // ==ORIGINAL== -function /*[#|*/f/*|]*/():Promise { +function /*[#|*/f/*|]*/() { const result = 'https://typescriptlang.org'; return fetch(result).then(res); } @@ -10,7 +10,7 @@ function res({ status, trailer }){ // ==ASYNC FUNCTION::Convert to async function== -async function f():Promise { +async function f() { const result = 'https://typescriptlang.org'; const result_1 = await fetch(result); return res(result_1);