Merge pull request #18427 from amcasey/GH17869

Forbid extraction of empty spans

(cherry picked from commit be5c00f4c9626848fe8e812aad32b86020c296bc)
This commit is contained in:
Andrew Casey 2017-09-13 16:43:14 -07:00 committed by Andrew Casey
parent fbb6cd57c0
commit 063e8a72ec
2 changed files with 11 additions and 0 deletions

View File

@ -404,6 +404,12 @@ function test(x: number) {
]);
testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single token."]);
testExtractRangeFailed("extractRangeFailed9",
`var x = ([#||]1 + 2);`,
[
"Statement or expression expected."
]);
testExtractMethod("extractMethod1",
`namespace A {
let x = 1;

View File

@ -149,6 +149,11 @@ namespace ts.refactor.extractMethod {
// exported only for tests
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan): RangeToExtract {
const length = span.length || 0;
if (length === 0) {
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.StatementOrExpressionExpected)] };
}
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
// This may fail (e.g. you select two statements in the root of a source file)
let start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);