Skip to content

Commit 27bede8

Browse files
committed
Stop requiring that the full range of a declaration fall within the
selection Fixes #18546 (cherry picked from commit af49c60)
1 parent f0b7843 commit 27bede8

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/harness/unittests/extractMethods.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,11 @@ function parsePrimaryExpression(): any {
700700
}
701701
}|]
702702
}
703+
}`);
704+
// Selection excludes leading trivia of declaration
705+
testExtractMethod("extractMethod33",
706+
`function F() {
707+
[#|function G() { }|]
703708
}`);
704709
});
705710

src/services/refactors/extractMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ namespace ts.refactor.extractMethod {
10701070
if (!declInFile) {
10711071
return undefined;
10721072
}
1073-
if (rangeContainsRange(enclosingTextRange, declInFile)) {
1073+
if (rangeContainsStartEnd(enclosingTextRange, declInFile.getStart(), declInFile.end)) {
10741074
// declaration is located in range to be extracted - do nothing
10751075
return undefined;
10761076
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ==ORIGINAL==
2+
function F() {
3+
function G() { }
4+
}
5+
// ==SCOPE::function 'F'==
6+
function F() {
7+
/*RENAME*/newFunction();
8+
9+
function newFunction() {
10+
function G() { }
11+
}
12+
}
13+
// ==SCOPE::global scope==
14+
function F() {
15+
/*RENAME*/newFunction();
16+
}
17+
function newFunction() {
18+
function G() { }
19+
}

tests/cases/fourslash/extract-method-empty-namespace.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/// <reference path='fourslash.ts' />
22

3-
// TODO: GH#18546
4-
// For now this tests that at least we don't crash.
5-
63
////function f() {
74
//// /*start*/namespace N {}/*end*/
85
////}
@@ -13,9 +10,9 @@ edit.applyRefactor({
1310
actionName: "scope_1",
1411
actionDescription: "Extract function into global scope",
1512
newContent: `function f() {
16-
/*RENAME*/newFunction(N);
13+
/*RENAME*/newFunction();
1714
}
18-
function newFunction(N: any) {
15+
function newFunction() {
1916
namespace N { }
2017
}
2118
`

0 commit comments

Comments
 (0)