Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Add reason for a disabled code action#37871
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
bd9f3cfecb957ddd177418684d3693723fba57100e569c6f86b6d9708ae8304e86312c7734694cf992adFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,33 +15,61 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { | ||
| addBraces: boolean; | ||
| } | ||
| type InfoOrError = { | ||
Member 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. Maybe this type should be generic and reused? | ||
| info: Info, | ||
| error?: never | ||
| } | { | ||
| info?: never, | ||
| error: string | ||
| }; | ||
| function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] { | ||
| const { file, startPosition, triggerReason } = context; | ||
| const info = getConvertibleArrowFunctionAtPosition(file, startPosition, triggerReason === "invoked"); | ||
Member 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. Personally, I'd take the hit and rename the outer | ||
| if (!info) return emptyArray; | ||
| return [{ | ||
| name: refactorName, | ||
| description: refactorDescription, | ||
| actions: [ | ||
| info.addBraces ? | ||
| { | ||
| name: addBracesActionName, | ||
| description: addBracesActionDescription | ||
| } : { | ||
| name: removeBracesActionName, | ||
| description: removeBracesActionDescription | ||
| } | ||
| ] | ||
| }]; | ||
| if (info.error === undefined) { | ||
Member 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. In other places, this is a non-null check on | ||
| return [{ | ||
| name: refactorName, | ||
| description: refactorDescription, | ||
| actions: [ | ||
| info.info.addBraces ? | ||
| { | ||
| name: addBracesActionName, | ||
| description: addBracesActionDescription | ||
| } : { | ||
| name: removeBracesActionName, | ||
| description: removeBracesActionDescription | ||
| } | ||
| ] | ||
| }]; | ||
| } | ||
| if (context.preferences.provideRefactorNotApplicableReason) { | ||
| return [{ | ||
| name: refactorName, | ||
| description: refactorDescription, | ||
| actions: [{ | ||
| name: addBracesActionName, | ||
| description: addBracesActionDescription, | ||
| notApplicableReason: info.error | ||
Member 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. Without having read the code in detail, it seems intuitively unlikely to me that add-braces and remove-braces would fail for the same reason. | ||
| }, { | ||
| name: removeBracesActionName, | ||
| description: removeBracesActionDescription, | ||
| notApplicableReason: info.error | ||
| }] | ||
| }]; | ||
| } | ||
| return emptyArray; | ||
| } | ||
| function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { | ||
| const { file, startPosition } = context; | ||
| const info = getConvertibleArrowFunctionAtPosition(file, startPosition); | ||
| if (!info) return undefined; | ||
| if (!info || !info.info) return undefined; | ||
| const { expression, returnStatement, func } = info; | ||
| const { expression, returnStatement, func } = info.info; | ||
| let body: ConciseBody; | ||
| @@ -70,28 +98,45 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction { | ||
| return { renameFilename: undefined, renameLocation: undefined, edits }; | ||
| } | ||
| function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, considerFunctionBodies = true): Info | undefined { | ||
| function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number, considerFunctionBodies = true): InfoOrError | undefined { | ||
Member 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. Can this still return | ||
| const node = getTokenAtPosition(file, startPosition); | ||
| const func = getContainingFunction(node); | ||
| // Only offer a refactor in the function body on explicit refactor requests. | ||
| if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) | ||
| || (rangeContainsRange(func.body, node) && !considerFunctionBodies))) return undefined; | ||
| if (!func) { | ||
| return { | ||
| error: getLocaleSpecificMessage(Diagnostics.Could_not_find_a_containing_arrow_function) | ||
Member 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. Or any other sort of containing function? | ||
| }; | ||
| } | ||
| if (!isArrowFunction(func)) { | ||
| return { | ||
| error: getLocaleSpecificMessage(Diagnostics.Containing_function_is_not_an_arrow_function) | ||
| }; | ||
| } | ||
| if ((!rangeContainsRange(func, node) || rangeContainsRange(func.body, node) && !considerFunctionBodies)) { | ||
| return undefined; | ||
| } | ||
| if (isExpression(func.body)) { | ||
| return { | ||
| func, | ||
| addBraces: true, | ||
| expression: func.body | ||
| info: { | ||
| 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 | ||
| info: { | ||
| func, | ||
| addBraces: false, | ||
| expression: firstStatement.expression, | ||
| returnStatement: firstStatement | ||
| } | ||
| }; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Would
DiagnosticMessagebe narrower and lazier thanstring?