-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
add support for add or remove braces to arrow function #23423
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a69d9c
add support for add or remove braces to arrow function
Kingwl 32be0c7
add tests and fix
Kingwl bd9a8b5
stash
Kingwl 0c06126
fix converter
Kingwl 1a59eb3
update body only
Kingwl b6669c9
revert wrong inline parameter
Kingwl de75f14
fix void return statement
Kingwl 5497b42
remove some case
Kingwl d470f32
Merge branch 'master' into add-braces
Kingwl 3d9a6ab
remove failed test
Kingwl 590476b
add more test and fix others
Kingwl 0d730c0
Merge branch 'master' into add-braces
Kingwl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/services/refactors/addOrRemoveBracesToArrowFunction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* @internal */ | ||
namespace ts.refactor.addOrRemoveBracesToArrowFunction { | ||
const refactorName = "Add or remove braces in an arrow function"; | ||
const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message; | ||
const addBracesActionName = "Add braces to arrow function"; | ||
const removeBracesActionName = "Remove braces from arrow function"; | ||
const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message; | ||
const removeBracesActionDescription = Diagnostics.Remove_braces_from_arrow_function.message; | ||
registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); | ||
|
||
interface Info { | ||
func: ArrowFunction; | ||
expression: Expression | undefined; | ||
returnStatement?: ReturnStatement; | ||
addBraces: boolean; | ||
} | ||
|
||
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { | ||
const { file, startPosition } = context; | ||
const info = getConvertibleArrowFunctionAtPosition(file, startPosition); | ||
if (!info) return undefined; | ||
|
||
return [{ | ||
name: refactorName, | ||
description: refactorDescription, | ||
actions: [ | ||
info.addBraces ? | ||
{ | ||
name: addBracesActionName, | ||
description: addBracesActionDescription | ||
} : { | ||
name: removeBracesActionName, | ||
description: removeBracesActionDescription | ||
} | ||
] | ||
}]; | ||
} | ||
|
||
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { | ||
const { file, startPosition } = context; | ||
const info = getConvertibleArrowFunctionAtPosition(file, startPosition); | ||
if (!info) return undefined; | ||
|
||
const { expression, returnStatement, func } = info; | ||
|
||
let body: ConciseBody; | ||
if (actionName === addBracesActionName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it matter whether this agrees with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happened with this? |
||
const returnStatement = createReturn(expression); | ||
body = createBlock([returnStatement], /* multiLine */ true); | ||
suppressLeadingAndTrailingTrivia(body); | ||
copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true); | ||
} | ||
else if (actionName === removeBracesActionName && returnStatement) { | ||
const actualExpression = expression || createVoidZero(); | ||
body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression; | ||
suppressLeadingAndTrailingTrivia(body); | ||
copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false); | ||
} | ||
else { | ||
Debug.fail("invalid action"); | ||
} | ||
|
||
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body)); | ||
return { renameFilename: undefined, renameLocation: undefined, edits }; | ||
} | ||
|
||
function needsParentheses(expression: Expression) { | ||
return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression); | ||
} | ||
|
||
function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined { | ||
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); | ||
const func = getContainingFunction(node); | ||
if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined; | ||
|
||
if (isExpression(func.body)) { | ||
return { | ||
func, | ||
addBraces: true, | ||
expression: func.body | ||
}; | ||
} | ||
else if (func.body.statements.length === 1) { | ||
const firstStatement = first(func.body.statements); | ||
if (isReturnStatement(firstStatement)) { | ||
return { | ||
func, | ||
addBraces: false, | ||
expression: firstStatement.expression, | ||
returnStatement: firstStatement | ||
}; | ||
} | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => a + 1; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Add braces to arrow function", | ||
actionDescription: "Add braces to arrow function", | ||
newContent: `const foo = a => { | ||
return a + 1; | ||
};`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction10.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return (1, 2, 3); }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => (1, 2, 3);`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction11.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return 1, 2, 3; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => (1, 2, 3);`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction12.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return "foo"; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => "foo";`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction13.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return null; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => null;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction14.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return undefined; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => undefined;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction15.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return void 0; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => void 0;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction16.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return {}; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => ({});`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction17.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return `abc{a}`; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => \`abc{a}\`;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction18.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return `abc`; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => \`abc\`;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction19.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return a; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => a;`, | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => ({ a: 1 }); | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Add braces to arrow function", | ||
actionDescription: "Add braces to arrow function", | ||
newContent: `const foo = a => { | ||
return ({ a: 1 }); | ||
};`, | ||
}); |
14 changes: 14 additions & 0 deletions
14
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction20.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { | ||
//// // return comment | ||
//// return a; | ||
//// }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => /* return comment*/ a;`, | ||
}); |
11 changes: 11 additions & 0 deletions
11
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction21.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = /*a*/a/*b*/ => { return; }; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Add or remove braces in an arrow function", | ||
actionName: "Remove braces from arrow function", | ||
actionDescription: "Remove braces from arrow function", | ||
newContent: `const foo = a => void 0;`, | ||
}); |
27 changes: 27 additions & 0 deletions
27
tests/cases/fourslash/refactorAddOrRemoveBracesToArrowFunction22.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const /*a*/foo/*b*/ = /*c*/(/*d*//*e*/aa/*f*/aa, /*g*/b/*h*/) /*i*//*j*/ /*k*/=>/*l*/ /*m*/{/*n*/ /*o*/return/*p*/ 1; }; | ||
|
||
goTo.select("a", "b"); | ||
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("c", "d"); | ||
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("e", "f"); | ||
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("g", "h"); | ||
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("i", "j"); | ||
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("k", "l"); | ||
verify.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("m", "n"); | ||
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") | ||
|
||
goTo.select("o", "p"); | ||
verify.not.refactorAvailable("Add or remove braces in an arrow function", "Remove braces from arrow function") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand why
expression
is required, but can be undefined, whereasreturnStatement
is optional. Should they follow the same pattern?