Skip to content

Commit 063e8a7

Browse files
committed
Merge pull request #18427 from amcasey/GH17869
Forbid extraction of empty spans (cherry picked from commit be5c00f)
1 parent fbb6cd5 commit 063e8a7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/harness/unittests/extractMethods.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ function test(x: number) {
404404
]);
405405
testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single token."]);
406406

407+
testExtractRangeFailed("extractRangeFailed9",
408+
`var x = ([#||]1 + 2);`,
409+
[
410+
"Statement or expression expected."
411+
]);
412+
407413
testExtractMethod("extractMethod1",
408414
`namespace A {
409415
let x = 1;

src/services/refactors/extractMethod.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ namespace ts.refactor.extractMethod {
149149
// exported only for tests
150150
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan): RangeToExtract {
151151
const length = span.length || 0;
152+
153+
if (length === 0) {
154+
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.StatementOrExpressionExpected)] };
155+
}
156+
152157
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
153158
// This may fail (e.g. you select two statements in the root of a source file)
154159
let start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);

0 commit comments

Comments
 (0)