Skip to content
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

[WIP] Promises to Async Await Code Fix #24939

Closed
Show file tree
Hide file tree
Changes from 202 commits
Commits
Show all changes
208 commits
Select commit Hold shift + click to select a range
f3971df
updated build files
elizabethdinella May 31, 2018
6f803f6
Initial commit of changing all functions that return a promise to async
elizabethdinella May 31, 2018
b8d8183
adding await to .then() works
elizabethdinella Jun 4, 2018
69795c8
Try block is added
elizabethdinella Jun 5, 2018
6c0a712
major refactor - still buggy
elizabethdinella Jun 5, 2018
546ee1b
Fixed to allow for non inlined functions in a then()
elizabethdinella Jun 6, 2018
8019335
Got catch blocks and onRej functions working
elizabethdinella Jun 7, 2018
6e31ff9
Fixed try/catch formatting
elizabethdinella Jun 7, 2018
469da81
Initial commit of recursive function to create cascading catch blocks
elizabethdinella Jun 8, 2018
8c8ebe9
Fixed catch param names
elizabethdinella Jun 8, 2018
635cc04
First commit of nested trys and cascading catches
elizabethdinella Jun 8, 2018
5099bd1
Fixed the outermost catch to take the correct variable
elizabethdinella Jun 8, 2018
b8353dc
Refactored to use objects instead of tuple arrays
elizabethdinella Jun 8, 2018
25efbed
Initial commit of recursive refactoring
elizabethdinella Jun 11, 2018
0b760bf
Got inlined arrow functions working with recursive method
elizabethdinella Jun 12, 2018
d3a92d9
Fixed if not rej function
elizabethdinella Jun 12, 2018
460c6dd
Added a couple tests
elizabethdinella Jun 12, 2018
73ed97e
Added a series of test cases
elizabethdinella Jun 13, 2018
effb559
Fixed copy paste mistakes in tests
elizabethdinella Jun 14, 2018
3b1aff1
Fixed linting errors
elizabethdinella Jun 14, 2018
71b422d
Fixed suggestion diagnostics to only suggest when funciton is returni…
elizabethdinella Jun 14, 2018
9b2aa30
Refactored to avoid using getChildren()
elizabethdinella Jun 14, 2018
2384440
Created isPromiseLike function in the checker to replace my isPromise…
elizabethdinella Jun 15, 2018
b806922
Fixed formatting and spacing on tests and added es6 target
elizabethdinella Jun 15, 2018
602a8a9
Fixed spacing on tests and added a couple more tests
elizabethdinella Jun 15, 2018
c1e5de6
More variable name fixes
elizabethdinella Jun 15, 2018
bece3e1
Fixed more variable name issues
elizabethdinella Jun 15, 2018
4820eff
Added awaits to all callbacks that are function references (not defin…
elizabethdinella Jun 15, 2018
84521cb
adding tests for multiple thens
elizabethdinella Jun 18, 2018
f9e3840
adding tests for multiple thens
elizabethdinella Jun 18, 2018
8a80eef
Fixed merge conflicts
elizabethdinella Jun 18, 2018
dd69f0d
cherry pick
elizabethdinella Jun 15, 2018
6e4810d
More cherry pick
elizabethdinella Jun 15, 2018
024ad02
cherry pick
elizabethdinella Jun 14, 2018
015f972
Initial commit of multiple thens
elizabethdinella Jun 18, 2018
41e007e
Fixed error in fourslash case
elizabethdinella Jun 18, 2018
5b958c5
Fixed merge conflicts
elizabethdinella Jun 18, 2018
68e0195
oops
elizabethdinella Jun 18, 2018
4f972ff
fixed more merge conflicts
elizabethdinella Jun 18, 2018
bc9d554
Refactored to use let declarations
elizabethdinella Jun 18, 2018
25ea1b1
merge conflicts
elizabethdinella Jun 18, 2018
515b5ff
initial commit of var -> let refactor
elizabethdinella Jun 19, 2018
8104e36
Only create a let declaration when there is a rej or catch
elizabethdinella Jun 19, 2018
4075943
Fixed rej after code to have labels to correct semantics. Also added …
elizabethdinella Jun 19, 2018
92ecaf1
Fixed tests to not return awaits (redundant as async functions do thi…
elizabethdinella Jun 19, 2018
e29e0e8
merged tests
elizabethdinella Jun 19, 2018
28dca79
Removed await keyword from returns
elizabethdinella Jun 19, 2018
4ac1742
fixed minor typos
elizabethdinella Jun 19, 2018
cfc3418
merged tests
elizabethdinella Jun 19, 2018
0b75df3
fixed more minors issues
elizabethdinella Jun 19, 2018
4483758
Merge branch 'promisesToAsyncAwaitTest' of https://github.com/elizabe…
elizabethdinella Jun 19, 2018
f992010
Can't return statements in a block
elizabethdinella Jun 20, 2018
2b56131
Merge branch 'promisesToAsyncAwaitTest' of https://github.com/elizabe…
elizabethdinella Jun 20, 2018
f113a41
fixed let declarations to be inside try blocks
elizabethdinella Jun 20, 2018
b1ff6d1
Merge branch 'promisesToAsyncAwaitTest' of https://github.com/elizabe…
elizabethdinella Jun 20, 2018
abeace9
fixed spacing
elizabethdinella Jun 20, 2018
caf2431
Merge branch 'promisesToAsyncAwaitTest' of https://github.com/elizabe…
elizabethdinella Jun 20, 2018
806d14a
removed unnessecary variable declarations
elizabethdinella Jun 20, 2018
4ed331e
switched to the checker function: isPromiseLike
elizabethdinella Jun 22, 2018
32d07ba
Refactored the getReturnStmts function
elizabethdinella Jun 22, 2018
bd54e37
Added tests for arrow functions and methods
elizabethdinella Jun 25, 2018
6e5b414
Merge branch 'promisesToAsyncAwaitTest' of https://github.com/elizabe…
elizabethdinella Jun 25, 2018
3b69059
Fixed minor issues on tests - added async keyword to result and use l…
elizabethdinella Jun 25, 2018
b1f5837
Forget await keyword
elizabethdinella Jun 25, 2018
f324936
Refactored functionality to get variable names
elizabethdinella Jun 25, 2018
2d3e5a9
Fixed small issues in tests - don't return await
elizabethdinella Jun 25, 2018
3a23df8
Created clones where necessary and added support for function express…
elizabethdinella Jun 25, 2018
1b72493
Added curser markers and fixed typos
elizabethdinella Jun 25, 2018
a4fcd03
Fixed curser marker issue
elizabethdinella Jun 25, 2018
cb1cfab
Removed unnessecary clones
elizabethdinella Jun 25, 2018
4c5af5a
Changed wording of the diagnostic
elizabethdinella Jun 25, 2018
40f136b
Refactoring based on PR comments
elizabethdinella Jun 25, 2018
6856457
Changed name of file and changed diagnostic messages
elizabethdinella Jun 25, 2018
8dfeac3
file name change
elizabethdinella Jun 25, 2018
8b7ed24
Switched to baseline tests
elizabethdinella Jun 25, 2018
9895781
Initial commit of creating new names when necessary
elizabethdinella Jun 26, 2018
d64ddb5
generating variable names with _1, _2, ..., _n when scopes intersect
elizabethdinella Jun 26, 2018
97156d1
Fixed generating new variables to not occur when _ is used as an argu…
elizabethdinella Jun 26, 2018
cd0d492
Initial commit of baseline tests
elizabethdinella Jun 27, 2018
588cb56
Switched to baseline tests
elizabethdinella Jun 27, 2018
a57c6d0
Fixed to not pass argName to different scopes (catch -> try scope)
elizabethdinella Jun 27, 2018
fc2c9be
Added tests for finally
elizabethdinella Jun 27, 2018
7ba13d8
Fixed linting errors
elizabethdinella Jun 27, 2018
28aef07
Fixed globalPromiseType bug
elizabethdinella Jun 28, 2018
81cd525
Added test cases for promises in a .then() and conflicting variable n…
elizabethdinella Jun 29, 2018
182648a
Cherry-pick fixes
elizabethdinella Jun 29, 2018
6eef8ad
merge from master
elizabethdinella Jun 29, 2018
aed1c73
merge - good state
elizabethdinella Jun 29, 2018
b23946e
Fixed casting to use isType() functions
elizabethdinella Jun 29, 2018
99bc200
initial commit of nested inner callbacks
elizabethdinella Jul 2, 2018
ae5efa4
initial commit of renaming before refactor
elizabethdinella Jul 3, 2018
a030b1e
initial commit of renaming using getSynthesizedDeepClone
elizabethdinella Jul 5, 2018
209a14f
Fixed finding inner promises bug
elizabethdinella Jul 5, 2018
2e470cd
First commit of conflicting var names working
elizabethdinella Jul 5, 2018
bc618d5
added synthesized variable decl names to the varNamesMap
elizabethdinella Jul 6, 2018
13fefe6
Fixed synthesizing new variable declaration names after renames
elizabethdinella Jul 6, 2018
f3dafad
Fixed synthMap to have the correct values
elizabethdinella Jul 6, 2018
fe1c20c
Fixed inner promise returning bug
elizabethdinella Jul 6, 2018
7e0c435
Added more tests
elizabethdinella Jul 6, 2018
0eb995e
More tests
elizabethdinella Jul 6, 2018
7d89e1e
Initial commit of checking for more to refactor in arrow function body
elizabethdinella Jul 9, 2018
78e6dd7
Buggy commit -> removed returns from arrow function body
elizabethdinella Jul 10, 2018
563d7b7
Added new baselines
elizabethdinella Jul 10, 2018
84139b6
Fixed nextDotThen bug
elizabethdinella Jul 10, 2018
9acf608
Fixed to no longer rename function names -> instead rename local and …
elizabethdinella Jul 10, 2018
e6bcadd
Merge branch 'promisesToAsyncAwaitCodeFix' of https://github.com/eliz…
elizabethdinella Jul 10, 2018
2a98d64
Fixed getNextDotThen logic
elizabethdinella Jul 10, 2018
2b31faa
Added tests and baselines
elizabethdinella Jul 10, 2018
70d4173
Added tests and baselines
elizabethdinella Jul 10, 2018
d0d4aff
Initial buggy commit of returning vars that have callbacks elsewhere
elizabethdinella Jul 10, 2018
b88022b
More progress on returning variables that reference promises
elizabethdinella Jul 12, 2018
71f9419
Returning variables that hold promises with callbacks working in simp…
elizabethdinella Jul 12, 2018
08dd6a2
Added tests and baselines
elizabethdinella Jul 12, 2018
4e7a1c0
Added functionality to glue multiple callbacks together and refactor
elizabethdinella Jul 13, 2018
67275d8
Added tests and baseliens
elizabethdinella Jul 13, 2018
f00af95
Fixed linting errors
elizabethdinella Jul 13, 2018
ac1046b
Initial commit of buggy passing node flags
elizabethdinella Jul 13, 2018
607aded
new strategy for getting next dot then and fixed bugs in gluing toget…
elizabethdinella Jul 16, 2018
404705b
Fixed more bugs in gluing expressions together
elizabethdinella Jul 16, 2018
26f3787
Initial commit of multiple var decls to refactor
elizabethdinella Jul 17, 2018
50c6b4a
Fixed expression statement bug
elizabethdinella Jul 17, 2018
eae41dd
Added a flag for a last line return stmt
elizabethdinella Jul 17, 2018
627ac68
Fixed replacing nodes and bug in collecting exprs of the same var name
elizabethdinella Jul 18, 2018
82bc741
Added tests and baselines
elizabethdinella Jul 18, 2018
6b25e01
Fixed small bug with seperating vars
elizabethdinella Jul 18, 2018
18e18d8
Initial commit of preserving node flags on declarations (var, let, co…
elizabethdinella Jul 18, 2018
c90ce2c
Added tests and baselines
elizabethdinella Jul 18, 2018
dbf7928
More work on returning variables that hold promises
elizabethdinella Jul 19, 2018
3088e43
Fixed assignment exprsesions
elizabethdinella Jul 19, 2018
963a759
edited tests and added baselines
elizabethdinella Jul 19, 2018
c2879de
Fixed node flags bug
elizabethdinella Jul 19, 2018
b446923
Fixed var decl lists with multiple var decls
elizabethdinella Jul 20, 2018
3cf0dfd
Added baselines
elizabethdinella Jul 20, 2018
9e3302e
Keep track of last ret statement
elizabethdinella Jul 23, 2018
2b04c4a
Reuse variable declarations if possible. Fixed some linting errors
elizabethdinella Jul 23, 2018
d3d43d3
Fixed catch returns
elizabethdinella Jul 23, 2018
99320bc
Added baselines
elizabethdinella Jul 23, 2018
5989e3a
Fixed checks for no prevArg
elizabethdinella Jul 23, 2018
136e2f3
Added baselines
elizabethdinella Jul 23, 2018
4d9584d
Fixed check for arg name bug
elizabethdinella Jul 23, 2018
1ea6daf
Removed all refactoring for variable returns without direct callbacks
elizabethdinella Jul 24, 2018
c71c5a5
Removed uneccesary argument to getCallbackBody
elizabethdinella Jul 24, 2018
c374480
More code cleanup
elizabethdinella Jul 24, 2018
5065ce9
Fixed lint errors
elizabethdinella Jul 24, 2018
d9597f0
Adjusted unit tests to match new spec that does not provide refactor …
elizabethdinella Jul 24, 2018
366b812
Fixed dummy identifier bug
elizabethdinella Jul 25, 2018
15249e1
Fixed the way we replace nodes
elizabethdinella Jul 25, 2018
cb98743
Added tests and baselines
elizabethdinella Jul 25, 2018
02c37d9
Fixed PR comments
elizabethdinella Jul 25, 2018
4f26e8c
Removed duplicate message
elizabethdinella Jul 25, 2018
8ba2097
Fixed tests to include promise libraries
elizabethdinella Jul 25, 2018
906b2ff
Refactored to use exposed getPromisedTypeOfPromise
elizabethdinella Jul 25, 2018
ccca0c0
Fixed rename bugs
elizabethdinella Jul 25, 2018
f0e1ff4
Fixed parseCallback failure cases
elizabethdinella Jul 25, 2018
64b1276
Added tests and baselines
elizabethdinella Jul 25, 2018
d3f125a
Fixed variable name bugs
elizabethdinella Jul 26, 2018
ebd69f2
Fixed renames
elizabethdinella Jul 31, 2018
b39772d
fixed linting errors and code review nits
elizabethdinella Jul 31, 2018
db243ad
Fixed arrow function implicit return bug
elizabethdinella Jul 31, 2018
9e548a0
Cleaned up baselines and fixed test linting errors
elizabethdinella Jul 31, 2018
fd7977b
Initial commmit of consts
elizabethdinella Aug 1, 2018
994c5e7
Modified tests to allow for consts
elizabethdinella Aug 1, 2018
5b32226
Addressed code review feedback
elizabethdinella Aug 1, 2018
aa41e2f
Fixed bug - now preserves innner callback statements that do not need…
elizabethdinella Aug 1, 2018
41f0bb8
Fixed linting errors
elizabethdinella Aug 1, 2018
35b9638
addressed more code review comments
elizabethdinella Aug 1, 2018
4d8f6d2
Updated tests
elizabethdinella Aug 1, 2018
cf331b4
Removed calling checker.getTypeAtLocation() from suggestion diags
elizabethdinella Aug 3, 2018
60ababa
added internal tags
elizabethdinella Aug 3, 2018
d472124
Switched to use SynthIdentifier type
elizabethdinella Aug 3, 2018
6c7a10c
Added a transformer class
elizabethdinella Aug 3, 2018
774aece
Removed export of custom type
elizabethdinella Aug 3, 2018
b73bb55
Cleaned up tests
elizabethdinella Aug 3, 2018
87dd683
Removed unnecessary file
elizabethdinella Aug 3, 2018
1a4eb08
Added seperate variables for synthesized uses and original uses, fixe…
elizabethdinella Aug 6, 2018
de28298
Cleaned up baselines
elizabethdinella Aug 6, 2018
32d9bae
Addressed code review comments
elizabethdinella Aug 6, 2018
5c01d7b
refactored getSynthesizedDeepClone
elizabethdinella Aug 6, 2018
57b7ad2
Fixed linting errors
elizabethdinella Aug 6, 2018
9d6a0e6
Switched transformer to an interface instead of a class
elizabethdinella Aug 6, 2018
8f9e306
refactored lastDotThen function to make more sense
elizabethdinella Aug 7, 2018
16fc028
Don't reuse variable declarations
elizabethdinella Aug 7, 2018
d1d5e76
Replace nodes immediately as you refactor
elizabethdinella Aug 7, 2018
794c52b
Added comments and addressed code review comments
elizabethdinella Aug 8, 2018
9bc4aa5
Fixed deep clone function to take a calback
elizabethdinella Aug 8, 2018
e8a6fb9
Addressed more code review comments - removed tuples
elizabethdinella Aug 8, 2018
55d4cad
Addressed more code review comments - removed unecessary helper function
elizabethdinella Aug 8, 2018
1cfee77
Refactored renameCollidingVarNames to be mroe readable
elizabethdinella Aug 8, 2018
a17738d
More refactoring on renameCollidingVariables to make it more readable
elizabethdinella Aug 8, 2018
0f4d97c
Initial commit of synthesizing a type for code paths
elizabethdinella Aug 8, 2018
52b7c96
Addressed code review comments - removed all instances of the incorre…
elizabethdinella Aug 8, 2018
bb6b869
Second commit of synthesized type
elizabethdinella Aug 8, 2018
f41da80
Added tests and baselines
elizabethdinella Aug 8, 2018
2d21376
More tests and baselines
elizabethdinella Aug 8, 2018
af20f70
fixed const bug and updated tests accordingly
elizabethdinella Aug 8, 2018
e36b55a
Changed the way we get the union type
elizabethdinella Aug 8, 2018
31e5433
Added more tests and baselines
elizabethdinella Aug 8, 2018
39ba3ea
Fixed linting errors
elizabethdinella Aug 8, 2018
8777657
Addressed code review comments and removed extraneous comments
elizabethdinella Aug 9, 2018
e6c676e
Added tests and baselines
elizabethdinella Aug 9, 2018
25edf4c
Merged with master
elizabethdinella Aug 9, 2018
54b6f2a
Addressed code review comments
elizabethdinella Aug 9, 2018
e2c02d6
Created a rename synthesized deep clone function and addressed other …
elizabethdinella Aug 9, 2018
1e72a46
addressed more code review comments
elizabethdinella Aug 10, 2018
ee994f1
code review changes
elizabethdinella Aug 10, 2018
8bcac69
added tests and baselines
elizabethdinella Aug 10, 2018
05cb7bf
Got rid of unnecessary function and fixed linting errors
elizabethdinella Aug 10, 2018
12bd6fc
Fixed linting error
elizabethdinella Aug 10, 2018
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,154 changes: 1,080 additions & 1,074 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace ts {
return node ? getTypeFromTypeNode(node) : errorType;
},
getParameterType: getTypeAtPosition,
getPromisedTypeOfPromise,
getReturnTypeOfSignature,
getNullableType,
getNonNullableType,
Expand Down
16 changes: 14 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4183,7 +4183,10 @@
"category": "Suggestion",
"code": 80005
},

"This may be converted to an async function.": {
"category": "Suggestion",
"code": 80006
},
"Add missing 'super()' call": {
"category": "Message",
"code": 90001
Expand Down Expand Up @@ -4556,12 +4559,21 @@
"category": "Message",
"code": 95062
},

"Add missing enum member '{0}'": {
"category": "Message",
"code": 95063
},
"Add all missing imports": {
"category": "Message",
"code": 95064
},
"Convert to async function":{
"category": "Message",
"code": 95065
},
"Convert all to async functions": {
"category": "Message",
"code": 95066
}
}
}
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,8 @@ namespace ts {
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;
/* @internal */
getPromisedTypeOfPromise(promise: Type, errorNode?: Node): Type | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

mark this as /** @internal */

getReturnTypeOfSignature(signature: Signature): Type;
/**
* Gets the type of a parameter at a given position in a signature.
Expand Down
550 changes: 550 additions & 0 deletions src/services/codefixes/convertToAsyncFunction.ts

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ts {
export function computeSuggestionDiagnostics(sourceFile: SourceFile, program: Program, cancellationToken: CancellationToken): DiagnosticWithLocation[] {
program.getSemanticDiagnostics(sourceFile, cancellationToken);
const diags: DiagnosticWithLocation[] = [];
const checker = program.getDiagnosticsProducingTypeChecker();

if (sourceFile.commonJsModuleIndicator &&
(programContainsEs6Modules(program) || compilerOptionsIndicateEs6Modules(program.getCompilerOptions())) &&
Expand Down Expand Up @@ -68,6 +69,9 @@ namespace ts {
}
}

if (isFunctionLikeDeclaration(node)) {
addConvertToAsyncFunctionDiagnostics(node, checker, diags);
}
node.forEachChild(check);
}
}
Expand Down Expand Up @@ -109,7 +113,64 @@ namespace ts {
}
}

function addConvertToAsyncFunctionDiagnostics(node: FunctionLikeDeclaration, checker: TypeChecker, diags: DiagnosticWithLocation[]): void {

const functionType = node.type ? checker.getTypeFromTypeNode(node.type) : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

This should get the type of node and not consume node.type directly.

Copy link
Member

Choose a reason for hiding this comment

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

        if (isAsyncFunction(node) || !node.body) {
            return;
        }

        const functionType = checker.getTypeAtLocation(node);
        if (!functionType)
        {
            return;
        }

if (isAsyncFunction(node) || !node.body || !functionType) {
return;
}

const callSignatures = checker.getSignaturesOfType(functionType, SignatureKind.Call);
const returnType = callSignatures.length ? checker.getReturnTypeOfSignature(callSignatures[0]) : undefined;

if (!returnType || !checker.getPromisedTypeOfPromise(returnType)) {
return;
}

// collect all the return statements
// check that a property access expression exists in there and that it is a handler
const returnStatements = getReturnStatementsWithPromiseHandlers(node);
if (returnStatements.length > 0) {
diags.push(createDiagnosticForNode(isVariableDeclaration(node.parent) ? node.parent.name : node, Diagnostics.This_may_be_converted_to_an_async_function));
Copy link
Member

Choose a reason for hiding this comment

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

node seems like a pretty big span. You might want to check where we report other function suggestions - I think it's just on the name or signature.

}
}

function getErrorNodeFromCommonJsIndicator(commonJsModuleIndicator: Node): Node {
return isBinaryExpression(commonJsModuleIndicator) ? commonJsModuleIndicator.left : commonJsModuleIndicator;
}

/** @internal */
export function getReturnStatementsWithPromiseHandlers(node: Node): Node[] {
const returnStatements: Node[] = [];
if (isFunctionLike(node)) {
forEachChild(node, visit);
}
else {
visit(node);
}

function visit(child: Node) {
if (isFunctionLike(child)) {
return;
}

if (isReturnStatement(child)) {
forEachChild(child, addHandlers);
}

function addHandlers(returnChild: Node) {
if (isPromiseHandler(returnChild)) {
returnStatements.push(child as ReturnStatement);
}
}

forEachChild(child, visit);
}
return returnStatements;
}

function isPromiseHandler(node: Node): boolean {
return (isCallExpression(node) && isPropertyAccessExpression(node.expression) &&
(node.expression.name.text === "then" || node.expression.name.text === "catch"));
Copy link
Contributor

Choose a reason for hiding this comment

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

what about "finally"?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see you handling finally later on in the quick fix but not here.

}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"codefixes/addMissingInvocationForDecorator.ts",
"codefixes/annotateWithTypeFromJSDoc.ts",
"codefixes/convertFunctionToEs6Class.ts",
"codefixes/convertToAsyncFunction.ts",
"codefixes/convertToEs6Module.ts",
"codefixes/correctQualifiedNameToIndexedAccessType.ts",
"codefixes/fixClassIncorrectlyImplementsInterface.ts",
Expand Down
34 changes: 27 additions & 7 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace ts {

export function isJumpStatementTarget(node: Node): node is Identifier & { parent: BreakOrContinueStatement } {
return node.kind === SyntaxKind.Identifier && isBreakOrContinueStatement(node.parent) && node.parent.label === node;
}
}

export function isLabelOfLabeledStatement(node: Node): node is Identifier {
return node.kind === SyntaxKind.Identifier && isLabeledStatement(node.parent) && node.parent.label === node;
Expand Down Expand Up @@ -396,7 +396,7 @@ namespace ts {
export function isThis(node: Node): boolean {
switch (node.kind) {
case SyntaxKind.ThisKeyword:
// case SyntaxKind.ThisType: TODO: GH#9267
// case SyntaxKind.ThisType: TODO: GH#9267
return true;
case SyntaxKind.Identifier:
// 'this' as a parameter
Expand Down Expand Up @@ -1650,14 +1650,34 @@ namespace ts {
* WARNING: This is an expensive operation and is only intended to be used in refactorings
* and code fixes (because those are triggered by explicit user actions).
*/
export function getSynthesizedDeepClone<T extends Node | undefined>(node: T, includeTrivia = true): T {
const clone = node && getSynthesizedDeepCloneWorker(node as NonNullable<T>);
export function getSynthesizedDeepClone<T extends Node | undefined>(node: T, includeTrivia = true, renameMap?: Map<Identifier>, checker?: TypeChecker, callback?: (originalNode: Node, clone: Node) => any): T {
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather this be left as-is and have the extra logic in the refactor code file. I don't want getSynthesizedDeepClone to turn into a Swiss Army Knife of Arbitrary Transforms

Copy link
Member

Choose a reason for hiding this comment

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

I suggested putting it here because it requires a deep clone. Are you okay with the code-fix-specific helper being largely copied from the shared helper?


let clone;
if (node && isIdentifier(node!) && renameMap && checker) {
const symbol = checker.getSymbolAtLocation(node!);
const renameInfo = symbol && renameMap.get(String(getSymbolId(symbol)));

if (renameInfo) {
clone = createIdentifier(renameInfo.text);
}
}

if (!clone) {
clone = node && getSynthesizedDeepCloneWorker(node as NonNullable<T>, renameMap, checker, callback);
}

if (clone && !includeTrivia) suppressLeadingAndTrailingTrivia(clone);
return clone;
if (callback && node) callback(node!, clone);

return clone as T;
}

function getSynthesizedDeepCloneWorker<T extends Node>(node: T): T {
const visited = visitEachChild(node, getSynthesizedDeepClone, nullTransformationContext);

function getSynthesizedDeepCloneWorker<T extends Node>(node: T, renameMap?: Map<Identifier>, checker?: TypeChecker, callback?: (originalNode: Node, clone: Node) => any): T {
const visited = visitEachChild(node, function wrapper(node) {
return getSynthesizedDeepClone(node, /*includeTrivia*/ true, renameMap, checker, callback);
}, nullTransformationContext);

if (visited === node) {
// This only happens for leaf nodes - internal nodes always see their children change.
const clone = getSynthesizedClone(node);
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"unittests/compileOnSave.ts",
"unittests/configurationExtension.ts",
"unittests/convertCompilerOptionsFromJson.ts",
"unittests/convertToAsyncFunction.ts",
"unittests/convertToBase64.ts",
"unittests/convertTypeAcquisitionFromJson.ts",
"unittests/customTransforms.ts",
Expand Down
Loading