fix(38073): hide 'Extract to function in global scope' action for arrow functions which use 'this' (#38107)
This commit is contained in:
parent
4109bba9e1
commit
738b6b5b68
|
@ -405,24 +405,24 @@ namespace ts.refactor.extractSymbol {
|
|||
rangeFacts |= RangeFacts.UsesThis;
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
|
||||
// You cannot extract global declarations
|
||||
(errors || (errors = [] as Diagnostic[])).push(createDiagnosticForNode(node, Messages.functionWillNotBeVisibleInTheNewScope));
|
||||
}
|
||||
// falls through
|
||||
case SyntaxKind.ClassExpression:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.MethodDeclaration:
|
||||
case SyntaxKind.Constructor:
|
||||
case SyntaxKind.GetAccessor:
|
||||
case SyntaxKind.SetAccessor:
|
||||
// do not dive into functions (except arrow functions) or classes
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isFunctionLikeDeclaration(node) || isClassLike(node)) {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
case SyntaxKind.ClassDeclaration:
|
||||
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
|
||||
// You cannot extract global declarations
|
||||
(errors || (errors = [] as Diagnostic[])).push(createDiagnosticForNode(node, Messages.functionWillNotBeVisibleInTheNewScope));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// do not dive into functions or classes
|
||||
return false;
|
||||
}
|
||||
const savedPermittedJumps = permittedJumps;
|
||||
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.IfStatement:
|
||||
permittedJumps = PermittedJumps.None;
|
||||
|
|
14
tests/cases/fourslash/extract-method38.ts
Normal file
14
tests/cases/fourslash/extract-method38.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////function bar(fn: () => void) {}
|
||||
////
|
||||
////class Foo {
|
||||
//// x: number;
|
||||
//// foo() {
|
||||
//// /*start*/bar(() => { this.x });/*end*/
|
||||
//// }
|
||||
////}
|
||||
|
||||
goTo.select("start", "end");
|
||||
verify.refactorAvailable("Extract Symbol", "function_scope_1");
|
||||
verify.not.refactorAvailable("Extract Symbol", "function_scope_2");
|
14
tests/cases/fourslash/extract-method39.ts
Normal file
14
tests/cases/fourslash/extract-method39.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////function bar(fn: () => void) {}
|
||||
////
|
||||
////class Foo {
|
||||
//// x: number;
|
||||
//// foo() {
|
||||
//// /*start*/bar(() => {});/*end*/
|
||||
//// }
|
||||
////}
|
||||
|
||||
goTo.select("start", "end");
|
||||
verify.refactorAvailable("Extract Symbol", "function_scope_1");
|
||||
verify.refactorAvailable("Extract Symbol", "function_scope_2");
|
Loading…
Reference in a new issue