From f0b78437f88b25077e4816dff83ba375d4481329 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 15 Sep 2017 13:15:31 -0700 Subject: [PATCH] Merge pull request #18508 from amcasey/ExtractSingleToken Re-enable extraction of a single token (cherry picked from commit 47b61ac18c304028036914bba1cc65220b4732f9) --- src/harness/unittests/extractMethods.ts | 2 +- src/services/refactors/extractMethod.ts | 4 ++-- ...hod-not-for-token.ts => extract-method-not-for-empty.ts} | 0 tests/cases/fourslash/extract-method13.ts | 6 +++--- tests/cases/fourslash/extract-method7.ts | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) rename tests/cases/fourslash/{extract-method-not-for-token.ts => extract-method-not-for-empty.ts} (100%) diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index b0ba6143042..3a3dbf13bed 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -402,7 +402,7 @@ function test(x: number) { [ "Cannot extract range containing conditional break or continue statements." ]); - testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single token."]); + testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single identifier."]); testExtractRangeFailed("extractRangeFailed9", `var x = ([#||]1 + 2);`, diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 93f320cb261..c66f6d361bc 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -92,7 +92,7 @@ namespace ts.refactor.extractMethod { export const CannotExtractRangeThatContainsWritesToReferencesLocatedOutsideOfTheTargetRangeInGenerators: DiagnosticMessage = createMessage("Cannot extract range containing writes to references located outside of the target range in generators."); export const TypeWillNotBeVisibleInTheNewScope = createMessage("Type will not visible in the new scope."); export const FunctionWillNotBeVisibleInTheNewScope = createMessage("Function will not visible in the new scope."); - export const InsufficientSelection = createMessage("Select more than a single token."); + export const InsufficientSelection = createMessage("Select more than a single identifier."); export const CannotExtractExportedEntity = createMessage("Cannot extract exported declaration"); export const CannotCombineWritesAndReturns = createMessage("Cannot combine writes and returns"); export const CannotExtractReadonlyPropertyInitializerOutsideConstructor = createMessage("Cannot move initialization of read-only class property outside of the constructor"); @@ -232,7 +232,7 @@ namespace ts.refactor.extractMethod { } function checkRootNode(node: Node): Diagnostic[] | undefined { - if (isToken(isExpressionStatement(node) ? node.expression : node)) { + if (isIdentifier(isExpressionStatement(node) ? node.expression : node)) { return [createDiagnosticForNode(node, Messages.InsufficientSelection)]; } return undefined; diff --git a/tests/cases/fourslash/extract-method-not-for-token.ts b/tests/cases/fourslash/extract-method-not-for-empty.ts similarity index 100% rename from tests/cases/fourslash/extract-method-not-for-token.ts rename to tests/cases/fourslash/extract-method-not-for-empty.ts diff --git a/tests/cases/fourslash/extract-method13.ts b/tests/cases/fourslash/extract-method13.ts index 7c4586d0eec..9bc8970b989 100644 --- a/tests/cases/fourslash/extract-method13.ts +++ b/tests/cases/fourslash/extract-method13.ts @@ -5,7 +5,7 @@ //// class C { //// static j = /*c*/1 + 1/*d*/; -//// constructor(q: string = /*a*/"a" + "b"/*b*/) { +//// constructor(q: string = /*a*/"hello"/*b*/) { //// } //// } @@ -21,7 +21,7 @@ edit.applyRefactor({ } private static newFunction(): string { - return "a" + "b"; + return "hello"; } }` }); @@ -42,7 +42,7 @@ edit.applyRefactor({ } private static newFunction(): string { - return "a" + "b"; + return "hello"; } }` }); diff --git a/tests/cases/fourslash/extract-method7.ts b/tests/cases/fourslash/extract-method7.ts index 5e1fd13ed2f..8ef8eb38d3f 100644 --- a/tests/cases/fourslash/extract-method7.ts +++ b/tests/cases/fourslash/extract-method7.ts @@ -3,7 +3,7 @@ // You cannot extract a function initializer into the function's body. // The innermost scope (scope_0) is the sibling of the function, not the function itself. -//// function fn(x = /*a*/1 + 1/*b*/) { +//// function fn(x = /*a*/3/*b*/) { //// } goTo.select('a', 'b'); @@ -15,7 +15,7 @@ edit.applyRefactor({ `function fn(x = /*RENAME*/newFunction()) { } function newFunction() { - return 1 + 1; + return 3; } ` });