fix(48541): forbid function extraction to arrow function with expression body (#48548)

This commit is contained in:
Oleksandr T
2022-04-04 22:27:49 +03:00
committed by GitHub
parent d5f5c6d61e
commit e02998f70d
8 changed files with 20 additions and 9 deletions

View File

@@ -3529,9 +3529,10 @@ namespace FourSlash {
};
}
public verifyRefactorAvailable(negative: boolean, triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string) {
public verifyRefactorAvailable(negative: boolean, triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string, actionDescription?: string) {
let refactors = this.getApplicableRefactorsAtSelection(triggerReason);
refactors = refactors.filter(r => r.name === name && (actionName === undefined || r.actions.some(a => a.name === actionName)));
refactors = refactors.filter(r =>
r.name === name && (actionName === undefined || r.actions.some(a => a.name === actionName)) && (actionDescription === undefined || r.actions.some(a => a.description === actionDescription)));
const isAvailable = refactors.length > 0;
if (negative) {

View File

@@ -215,8 +215,8 @@ namespace FourSlashInterface {
this.state.verifyRefactorsAvailable(names);
}
public refactorAvailable(name: string, actionName?: string) {
this.state.verifyRefactorAvailable(this.negative, "implicit", name, actionName);
public refactorAvailable(name: string, actionName?: string, actionDescription?: string) {
this.state.verifyRefactorAvailable(this.negative, "implicit", name, actionName, actionDescription);
}
public refactorAvailableForTriggerReason(triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string) {

View File

@@ -639,7 +639,8 @@ namespace ts.refactor.extractSymbol {
}
function isScope(node: Node): node is Scope {
return isFunctionLikeDeclaration(node) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
return isArrowFunction(node) ? isFunctionBody(node.body) :
isFunctionLikeDeclaration(node) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
}
/**

View File

@@ -1,5 +1,5 @@
// ==ORIGINAL==
const f = () => /*[#|*/2 + 1/*|]*/;
// ==SCOPE::Extract to constant in global scope==
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 2 + 1;
const f = () => /*RENAME*/newLocal;

View File

@@ -1,5 +1,5 @@
// ==ORIGINAL==
const f = () => /*[#|*/2 + 1/*|]*/;
// ==SCOPE::Extract to constant in global scope==
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 2 + 1;
const f = () => /*RENAME*/newLocal;

View File

@@ -5,7 +5,7 @@
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_1",
actionName: "function_scope_0",
actionDescription: "Extract to function in global scope",
newContent:
`(x: {}, y: {}) => (/*RENAME*/newFunction(x, y));

View File

@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
////function foo() {
//// let x = [1, 2, 3];
//// let y = x.map(e => /*a*/e + e/*b*/);
////}
goTo.select("a", "b");
verify.not.refactorAvailable("Extract Symbol", "function_scope_0", "Extract to inner function in arrow function");

View File

@@ -271,7 +271,7 @@ declare namespace FourSlashInterface {
codeFixDiagnosticsAvailableAtMarkers(markerNames: string[], diagnosticCode?: number): void;
applicableRefactorAvailableForRange(): void;
refactorAvailable(name: string, actionName?: string): void;
refactorAvailable(name: string, actionName?: string, actionDescription?: string): void;
refactorAvailableForTriggerReason(triggerReason: RefactorTriggerReason, name: string, action?: string): void;
refactorKindAvailable(refactorKind: string, expected: string[], preferences?: {}): void;
}