Skip to content

Commit e02998f

Browse files
authored
fix(48541): forbid function extraction to arrow function with expression body (#48548)
1 parent d5f5c6d commit e02998f

File tree

8 files changed

+20
-9
lines changed

8 files changed

+20
-9
lines changed

src/harness/fourslashImpl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,9 +3529,10 @@ namespace FourSlash {
35293529
};
35303530
}
35313531

3532-
public verifyRefactorAvailable(negative: boolean, triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string) {
3532+
public verifyRefactorAvailable(negative: boolean, triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string, actionDescription?: string) {
35333533
let refactors = this.getApplicableRefactorsAtSelection(triggerReason);
3534-
refactors = refactors.filter(r => r.name === name && (actionName === undefined || r.actions.some(a => a.name === actionName)));
3534+
refactors = refactors.filter(r =>
3535+
r.name === name && (actionName === undefined || r.actions.some(a => a.name === actionName)) && (actionDescription === undefined || r.actions.some(a => a.description === actionDescription)));
35353536
const isAvailable = refactors.length > 0;
35363537

35373538
if (negative) {

src/harness/fourslashInterfaceImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ namespace FourSlashInterface {
215215
this.state.verifyRefactorsAvailable(names);
216216
}
217217

218-
public refactorAvailable(name: string, actionName?: string) {
219-
this.state.verifyRefactorAvailable(this.negative, "implicit", name, actionName);
218+
public refactorAvailable(name: string, actionName?: string, actionDescription?: string) {
219+
this.state.verifyRefactorAvailable(this.negative, "implicit", name, actionName, actionDescription);
220220
}
221221

222222
public refactorAvailableForTriggerReason(triggerReason: ts.RefactorTriggerReason, name: string, actionName?: string) {

src/services/refactors/extractSymbol.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,8 @@ namespace ts.refactor.extractSymbol {
639639
}
640640

641641
function isScope(node: Node): node is Scope {
642-
return isFunctionLikeDeclaration(node) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
642+
return isArrowFunction(node) ? isFunctionBody(node.body) :
643+
isFunctionLikeDeclaration(node) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
643644
}
644645

645646
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ==ORIGINAL==
22
const f = () => /*[#|*/2 + 1/*|]*/;
3-
// ==SCOPE::Extract to constant in global scope==
3+
// ==SCOPE::Extract to constant in enclosing scope==
44
const newLocal = 2 + 1;
55
const f = () => /*RENAME*/newLocal;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ==ORIGINAL==
22
const f = () => /*[#|*/2 + 1/*|]*/;
3-
// ==SCOPE::Extract to constant in global scope==
3+
// ==SCOPE::Extract to constant in enclosing scope==
44
const newLocal = 2 + 1;
55
const f = () => /*RENAME*/newLocal;

tests/cases/fourslash/extract-method-two-type-literals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
goTo.select("1", "2");
66
edit.applyRefactor({
77
refactorName: "Extract Symbol",
8-
actionName: "function_scope_1",
8+
actionName: "function_scope_0",
99
actionDescription: "Extract to function in global scope",
1010
newContent:
1111
`(x: {}, y: {}) => (/*RENAME*/newFunction(x, y));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo() {
4+
//// let x = [1, 2, 3];
5+
//// let y = x.map(e => /*a*/e + e/*b*/);
6+
////}
7+
8+
goTo.select("a", "b");
9+
verify.not.refactorAvailable("Extract Symbol", "function_scope_0", "Extract to inner function in arrow function");

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ declare namespace FourSlashInterface {
271271
codeFixDiagnosticsAvailableAtMarkers(markerNames: string[], diagnosticCode?: number): void;
272272
applicableRefactorAvailableForRange(): void;
273273

274-
refactorAvailable(name: string, actionName?: string): void;
274+
refactorAvailable(name: string, actionName?: string, actionDescription?: string): void;
275275
refactorAvailableForTriggerReason(triggerReason: RefactorTriggerReason, name: string, action?: string): void;
276276
refactorKindAvailable(refactorKind: string, expected: string[], preferences?: {}): void;
277277
}

0 commit comments

Comments
 (0)