Skip to content

Commit 68d2ee0

Browse files
authored
fix(38815): dive in arrow functions to check only this usage instead of checking all statements (#38865)
1 parent b944ce5 commit 68d2ee0

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/services/refactors/extractSymbol.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,20 @@ namespace ts.refactor.extractSymbol {
405405
rangeFacts |= RangeFacts.UsesThis;
406406
}
407407
break;
408+
case SyntaxKind.ArrowFunction:
409+
// check if arrow function uses this
410+
forEachChild(node, function check(n) {
411+
if (isThis(n)) {
412+
rangeFacts |= RangeFacts.UsesThis;
413+
}
414+
else if (isClassLike(n) || (isFunctionLike(n) && !isArrowFunction(n))) {
415+
return false;
416+
}
417+
else {
418+
forEachChild(n, check);
419+
}
420+
});
421+
// falls through
408422
case SyntaxKind.ClassDeclaration:
409423
case SyntaxKind.FunctionDeclaration:
410424
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
@@ -418,7 +432,7 @@ namespace ts.refactor.extractSymbol {
418432
case SyntaxKind.Constructor:
419433
case SyntaxKind.GetAccessor:
420434
case SyntaxKind.SetAccessor:
421-
// do not dive into functions (except arrow functions) or classes
435+
// do not dive into functions or classes
422436
return false;
423437
}
424438

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const foo = /*start*/{
4+
//// a: 1,
5+
//// b: () => { return 1; }
6+
////}/*end*/
7+
8+
goTo.select("start", "end");
9+
edit.applyRefactor({
10+
refactorName: "Extract Symbol",
11+
actionName: "function_scope_0",
12+
actionDescription: "Extract to function in global scope",
13+
newContent:
14+
`const foo = /*RENAME*/newFunction()
15+
16+
function newFunction() {
17+
return {
18+
a: 1,
19+
b: () => { return 1; }
20+
};
21+
}
22+
`
23+
});
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function bar(fn: () => void) {}
4+
////
5+
////class Foo {
6+
//// x: number;
7+
//// foo() {
8+
//// /*start*/bar(() => {
9+
//// () => {
10+
//// () => {
11+
//// this.x;
12+
//// }
13+
//// }
14+
//// });/*end*/
15+
//// }
16+
////}
17+
18+
goTo.select("start", "end");
19+
verify.refactorAvailable("Extract Symbol", "function_scope_1");
20+
verify.not.refactorAvailable("Extract Symbol", "function_scope_2");

0 commit comments

Comments
 (0)