Skip to content

Commit a02aaf2

Browse files
committed
Forbid extraction of empty spans
1 parent ece4e4f commit a02aaf2

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
"Cannot extract range containing conditional break or continue statements."
405405
]);
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
@@ -162,6 +162,11 @@ namespace ts.refactor.extractMethod {
162162
*/
163163
export function getRangeToExtract(sourceFile: SourceFile, span: TextSpan): RangeToExtract {
164164
const length = span.length || 0;
165+
166+
if (length === 0) {
167+
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.StatementOrExpressionExpected)] };
168+
}
169+
165170
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
166171
// This may fail (e.g. you select two statements in the root of a source file)
167172
let start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);

0 commit comments

Comments
 (0)