Skip to content

fix(38815): Extract to function crash #38865

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 1 commit into from
Jun 1, 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
16 changes: 15 additions & 1 deletion src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,20 @@ namespace ts.refactor.extractSymbol {
rangeFacts |= RangeFacts.UsesThis;
}
break;
case SyntaxKind.ArrowFunction:
// check if arrow function uses this
forEachChild(node, function check(n) {
if (isThis(n)) {
rangeFacts |= RangeFacts.UsesThis;
}
else if (isClassLike(n) || (isFunctionLike(n) && !isArrowFunction(n))) {
return false;
}
else {
forEachChild(n, check);
}
});
// falls through
case SyntaxKind.ClassDeclaration:
case SyntaxKind.FunctionDeclaration:
if (isSourceFile(node.parent) && node.parent.externalModuleIndicator === undefined) {
Expand All @@ -418,7 +432,7 @@ namespace ts.refactor.extractSymbol {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
// do not dive into functions (except arrow functions) or classes
// do not dive into functions or classes
return false;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/cases/fourslash/extract-method40.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

////const foo = /*start*/{
//// a: 1,
//// b: () => { return 1; }
////}/*end*/

goTo.select("start", "end");
edit.applyRefactor({
refactorName: "Extract Symbol",
actionName: "function_scope_0",
actionDescription: "Extract to function in global scope",
newContent:
`const foo = /*RENAME*/newFunction()

function newFunction() {
return {
a: 1,
b: () => { return 1; }
};
}
`
});
20 changes: 20 additions & 0 deletions tests/cases/fourslash/extract-method41.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <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");