Skip to content

Commit

Permalink
Don't try to extract import to a method: simpler fix (#18054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy authored and mhegazy committed Aug 25, 2017
1 parent 3644771 commit 62678cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/services/refactors/extractMethod.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/extract-method-not-for-import.ts
@@ -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');

0 comments on commit 62678cd

Please sign in to comment.