Don't try to extract import to a method: simpler fix (#18054)

This commit is contained in:
Andy
2017-08-25 15:12:21 -07:00
committed by Mohamed Hegazy
parent 3644771ab6
commit 62678cd736
2 changed files with 25 additions and 11 deletions

View File

@@ -232,17 +232,7 @@ namespace ts.refactor.extractMethod {
return { errors };
}
// If our selection is the expression in an ExpressionStatement, expand
// the selection to include the enclosing Statement (this stops us
// from trying to care about the return value of the extracted function
// and eliminates double semicolon insertion in certain scenarios)
const range = isStatement(start)
? [start]
: start.parent && start.parent.kind === SyntaxKind.ExpressionStatement
? [start.parent as Statement]
: start as Expression;
return { targetRange: { range, facts: rangeFacts, declarations } };
return { targetRange: { range: getStatementOrExpressionRange(start), facts: rangeFacts, declarations } };
}
function createErrorResult(sourceFile: SourceFile, start: number, length: number, message: DiagnosticMessage): RangeToExtract {
@@ -459,6 +449,20 @@ namespace ts.refactor.extractMethod {
}
}
function getStatementOrExpressionRange(node: Node): Statement[] | Expression {
if (isStatement(node)) {
return [node];
}
else if (isPartOfExpression(node)) {
// If our selection is the expression in an ExpressionStatement, expand
// the selection to include the enclosing Statement (this stops us
// from trying to care about the return value of the extracted function
// and eliminates double semicolon insertion in certain scenarios)
return isExpressionStatement(node.parent) ? [node.parent] : node as Expression;
}
return undefined;
}
function isValidExtractionTarget(node: Node): node is Scope {
// Note that we don't use isFunctionLike because we don't want to put the extracted closure *inside* a method
return (node.kind === SyntaxKind.FunctionDeclaration) || isSourceFile(node) || isModuleBlock(node) || isClassLike(node);

View File

@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////i/**/mport _ from "./b";
// @Filename: /b.ts
////export default function f() {}
goTo.marker("");
verify.not.refactorAvailable('Extract Method');