Don't try to extract `import` to a method (#18025) · WebReflection/TypeScript@fe1242c · GitHub
Skip to content

Commit fe1242c

Browse files
author
Andy
authored
Don't try to extract import to a method (microsoft#18025)
1 parent 3a0ab74 commit fe1242c

3 files changed

Lines changed: 87 additions & 67 deletions

File tree

src/compiler/utilities.ts

Lines changed: 62 additions & 55 deletions

src/services/refactors/extractMethod.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,7 @@ namespace ts.refactor.extractMethod {
231231
if (errors) {
232232
return { errors };
233233
}
234-
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 } };
234+
return { targetRange: { range: getStatementOrExpressionRange(start), facts: rangeFacts, declarations } };
246235
}
247236

248237
function createErrorResult(sourceFile: SourceFile, start: number, length: number, message: DiagnosticMessage): RangeToExtract {
@@ -459,6 +448,20 @@ namespace ts.refactor.extractMethod {
459448
}
460449
}
461450

451+
function getStatementOrExpressionRange(node: Node): Statement[] | Expression {
452+
if (isStatement(node)) {
453+
return [node];
454+
}
455+
else if (isExpression(node)) {
456+
// If our selection is the expression in an ExpressionStatement, expand
457+
// the selection to include the enclosing Statement (this stops us
458+
// from trying to care about the return value of the extracted function
459+
// and eliminates double semicolon insertion in certain scenarios)
460+
return isExpressionStatement(node.parent) ? [node.parent] : node;
461+
}
462+
return undefined;
463+
}
464+
462465
function isValidExtractionTarget(node: Node): node is Scope {
463466
// Note that we don't use isFunctionLike because we don't want to put the extracted closure *inside* a method
464467
return (node.kind === SyntaxKind.FunctionDeclaration) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);
Lines changed: 10 additions & 0 deletions

0 commit comments

Comments
 (0)