Skip to content

Commit

Permalink
Merge pull request #18427 from amcasey/GH17869
Browse files Browse the repository at this point in the history
Forbid extraction of empty spans
  • Loading branch information
amcasey committed Sep 13, 2017
2 parents 288a57c + a02aaf2 commit be5c00f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/harness/unittests/extractMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ function test(x: number) {
"Cannot extract range containing conditional break or continue statements."
]);

testExtractRangeFailed("extractRangeFailed9",
`var x = ([#||]1 + 2);`,
[
"Statement or expression expected."
]);

testExtractMethod("extractMethod1",
`namespace A {
let x = 1;
Expand Down
5 changes: 5 additions & 0 deletions src/services/refactors/extractMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit be5c00f

Please sign in to comment.