Skip to content

Commit 738b6b5

Browse files
authored
fix(38073): hide 'Extract to function in global scope' action for arrow functions which use 'this' (microsoft#38107)
1 parent 4109bba commit 738b6b5

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/services/refactors/extractSymbol.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -405,24 +405,24 @@ namespace ts.refactor.extractSymbol {
405405
rangeFacts |= RangeFacts.UsesThis;
406406
}
407407
break;
408+
case SyntaxKind.ClassDeclaration:
409+
case SyntaxKind.FunctionDeclaration:
410+
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
411+
// You cannot extract global declarations
412+
(errors || (errors = [] as Diagnostic[])).push(createDiagnosticForNode(node, Messages.functionWillNotBeVisibleInTheNewScope));
413+
}
414+
// falls through
415+
case SyntaxKind.ClassExpression:
416+
case SyntaxKind.FunctionExpression:
417+
case SyntaxKind.MethodDeclaration:
418+
case SyntaxKind.Constructor:
419+
case SyntaxKind.GetAccessor:
420+
case SyntaxKind.SetAccessor:
421+
// do not dive into functions (except arrow functions) or classes
422+
return false;
408423
}
409424

410-
if (isFunctionLikeDeclaration(node) || isClassLike(node)) {
411-
switch (node.kind) {
412-
case SyntaxKind.FunctionDeclaration:
413-
case SyntaxKind.ClassDeclaration:
414-
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
415-
// You cannot extract global declarations
416-
(errors || (errors = [] as Diagnostic[])).push(createDiagnosticForNode(node, Messages.functionWillNotBeVisibleInTheNewScope));
417-
}
418-
break;
419-
}
420-
421-
// do not dive into functions or classes
422-
return false;
423-
}
424425
const savedPermittedJumps = permittedJumps;
425-
426426
switch (node.kind) {
427427
case SyntaxKind.IfStatement:
428428
permittedJumps = PermittedJumps.None;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function bar(fn: () => void) {}
4+
////
5+
////class Foo {
6+
//// x: number;
7+
//// foo() {
8+
//// /*start*/bar(() => { this.x });/*end*/
9+
//// }
10+
////}
11+
12+
goTo.select("start", "end");
13+
verify.refactorAvailable("Extract Symbol", "function_scope_1");
14+
verify.not.refactorAvailable("Extract Symbol", "function_scope_2");
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function bar(fn: () => void) {}
4+
////
5+
////class Foo {
6+
//// x: number;
7+
//// foo() {
8+
//// /*start*/bar(() => {});/*end*/
9+
//// }
10+
////}
11+
12+
goTo.select("start", "end");
13+
verify.refactorAvailable("Extract Symbol", "function_scope_1");
14+
verify.refactorAvailable("Extract Symbol", "function_scope_2");

0 commit comments

Comments
 (0)