diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index 6d8eed3b8b041..190cd1d5be0bc 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -768,6 +768,11 @@ function parsePrimaryExpression(): any { } }|] } +}`); + // Selection excludes leading trivia of declaration + testExtractMethod("extractMethod33", + `function F() { + [#|function G() { }|] }`); }); diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 3b8ea19a9f00e..8551ea2eddccf 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -1209,7 +1209,7 @@ namespace ts.refactor.extractMethod { if (!declInFile) { return undefined; } - if (rangeContainsRange(enclosingTextRange, declInFile)) { + if (rangeContainsStartEnd(enclosingTextRange, declInFile.getStart(), declInFile.end)) { // declaration is located in range to be extracted - do nothing return undefined; } diff --git a/tests/baselines/reference/extractMethod/extractMethod33.ts b/tests/baselines/reference/extractMethod/extractMethod33.ts new file mode 100644 index 0000000000000..79a6626a535c2 --- /dev/null +++ b/tests/baselines/reference/extractMethod/extractMethod33.ts @@ -0,0 +1,19 @@ +// ==ORIGINAL== +function F() { + function G() { } +} +// ==SCOPE::inner function in function 'F'== +function F() { + /*RENAME*/newFunction(); + + function newFunction() { + function G() { } + } +} +// ==SCOPE::function in global scope== +function F() { + /*RENAME*/newFunction(); +} +function newFunction() { + function G() { } +} diff --git a/tests/cases/fourslash/extract-method-empty-namespace.ts b/tests/cases/fourslash/extract-method-empty-namespace.ts index 9da3faaf92704..3a29992350df5 100644 --- a/tests/cases/fourslash/extract-method-empty-namespace.ts +++ b/tests/cases/fourslash/extract-method-empty-namespace.ts @@ -1,8 +1,5 @@ /// -// TODO: GH#18546 -// For now this tests that at least we don't crash. - ////function f() { //// /*start*/namespace N {}/*end*/ ////} @@ -13,9 +10,9 @@ edit.applyRefactor({ actionName: "scope_1", actionDescription: "Extract to function in global scope", newContent: `function f() { - /*RENAME*/newFunction(N); + /*RENAME*/newFunction(); } -function newFunction(N: any) { +function newFunction() { namespace N { } } `