mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
fix(48541): forbid function extraction to arrow function with expression body (#48548)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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));
|
||||
|
||||
9
tests/cases/fourslash/extract-method44.ts
Normal file
9
tests/cases/fourslash/extract-method44.ts
Normal 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");
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user