Skip to content

fix(38073): Extract to function refactoring generates invalid code for arrow function that references this #38107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/extract-method38.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions tests/cases/fourslash/extract-method39.ts
Original file line number Diff line number Diff line change
@@ -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");