From b533b24686d5f2c799e2c5edf6283243b6a8d2df Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 7 Sep 2017 07:28:12 -0700 Subject: [PATCH] extractMethod: Don't try to extract a single token (#18090) * extractMethod: Don't try to extract a single token * Update tests --- src/services/refactors/extractMethod.ts | 4 ++-- tests/cases/fourslash/extract-method-not-for-token.ts | 6 ++++++ tests/cases/fourslash/extract-method13.ts | 8 ++++---- tests/cases/fourslash/extract-method5.ts | 5 +++-- tests/cases/fourslash/extract-method7.ts | 4 ++-- 5 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 tests/cases/fourslash/extract-method-not-for-token.ts diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 6fe664e6c81..b76ab9376dc 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -95,7 +95,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 identifier."); + export const InsufficientSelection = createMessage("Select more than a single token."); 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"); @@ -239,7 +239,7 @@ namespace ts.refactor.extractMethod { } function checkRootNode(node: Node): Diagnostic[] | undefined { - if (isIdentifier(node)) { + if (isToken(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-token.ts new file mode 100644 index 00000000000..756716441cd --- /dev/null +++ b/tests/cases/fourslash/extract-method-not-for-token.ts @@ -0,0 +1,6 @@ +/// + +////"/**/foo"; + +goTo.marker(""); +verify.not.refactorAvailable('Extract Method'); diff --git a/tests/cases/fourslash/extract-method13.ts b/tests/cases/fourslash/extract-method13.ts index 14a146a80c5..94ad86e4399 100644 --- a/tests/cases/fourslash/extract-method13.ts +++ b/tests/cases/fourslash/extract-method13.ts @@ -4,8 +4,8 @@ // Also checks that we correctly find non-conflicting names in static contexts. //// class C { -//// static j = /*c*/100/*d*/; -//// constructor(q: string = /*a*/"hello"/*b*/) { +//// static j = /*c*/1 + 1/*d*/; +//// constructor(q: string = /*a*/"a" + "b"/*b*/) { //// } //// } @@ -29,10 +29,10 @@ verify.currentFileContentIs(`class C { } private static newFunction(): string { - return "hello"; + return "a" + "b"; } private static newFunction_1() { - return 100; + return 1 + 1; } }`); \ No newline at end of file diff --git a/tests/cases/fourslash/extract-method5.ts b/tests/cases/fourslash/extract-method5.ts index 10294298b08..d1e70d10716 100644 --- a/tests/cases/fourslash/extract-method5.ts +++ b/tests/cases/fourslash/extract-method5.ts @@ -5,7 +5,7 @@ // annotation in the extracted function //// function f() { -//// var x: 1 | 2 | 3 = /*start*/2/*end*/; +//// var x: 1 | 2 | 3 = /*start*/1 + 1 === 2 ? 1 : 2/*end*/; //// } goTo.select('start', 'end'); @@ -14,11 +14,12 @@ edit.applyRefactor({ actionName: "scope_0", actionDescription: "Extract function into function 'f'", }); +// TODO: GH#18091 (fix formatting to use `2 ? 1 :` and not `2?1:`) verify.currentFileContentIs( `function f() { var x: 1 | 2 | 3 = newFunction(); function newFunction(): 1 | 2 | 3 { - return 2; + return 1 + 1 === 2?1: 2; } }`); \ No newline at end of file diff --git a/tests/cases/fourslash/extract-method7.ts b/tests/cases/fourslash/extract-method7.ts index 95c9cbe9897..d10c7c3136e 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*/3/*b*/) { +//// function fn(x = /*a*/1 + 1/*b*/) { //// } goTo.select('a', 'b'); @@ -15,6 +15,6 @@ edit.applyRefactor({ verify.currentFileContentIs(`function fn(x = newFunction()) { } function newFunction() { - return 3; + return 1 + 1; } `);