Skip to content

Commit 62678cd

Browse files
Andymhegazy
Andy
authored andcommitted
Don't try to extract import to a method: simpler fix (#18054)
1 parent 3644771 commit 62678cd

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/services/refactors/extractMethod.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,7 @@ namespace ts.refactor.extractMethod {
232232
return { errors };
233233
}
234234

235-
// If our selection is the expression in an ExpressionStatement, expand
236-
// the selection to include the enclosing Statement (this stops us
237-
// from trying to care about the return value of the extracted function
238-
// and eliminates double semicolon insertion in certain scenarios)
239-
const range = isStatement(start)
240-
? [start]
241-
: start.parent && start.parent.kind === SyntaxKind.ExpressionStatement
242-
? [start.parent as Statement]
243-
: start as Expression;
244-
245-
return { targetRange: { range, facts: rangeFacts, declarations } };
235+
return { targetRange: { range: getStatementOrExpressionRange(start), facts: rangeFacts, declarations } };
246236
}
247237

248238
function createErrorResult(sourceFile: SourceFile, start: number, length: number, message: DiagnosticMessage): RangeToExtract {
@@ -459,6 +449,20 @@ namespace ts.refactor.extractMethod {
459449
}
460450
}
461451

452+
function getStatementOrExpressionRange(node: Node): Statement[] | Expression {
453+
if (isStatement(node)) {
454+
return [node];
455+
}
456+
else if (isPartOfExpression(node)) {
457+
// If our selection is the expression in an ExpressionStatement, expand
458+
// the selection to include the enclosing Statement (this stops us
459+
// from trying to care about the return value of the extracted function
460+
// and eliminates double semicolon insertion in certain scenarios)
461+
return isExpressionStatement(node.parent) ? [node.parent] : node as Expression;
462+
}
463+
return undefined;
464+
}
465+
462466
function isValidExtractionTarget(node: Node): node is Scope {
463467
// Note that we don't use isFunctionLike because we don't want to put the extracted closure *inside* a method
464468
return (node.kind === SyntaxKind.FunctionDeclaration) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////i/**/mport _ from "./b";
5+
6+
// @Filename: /b.ts
7+
////export default function f() {}
8+
9+
goTo.marker("");
10+
verify.not.refactorAvailable('Extract Method');

0 commit comments

Comments
 (0)