From 6e512a495f235fed8d1a0a10b619e181c2ef2a5c Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 14 Sep 2017 11:16:21 -0700 Subject: [PATCH] extractMethod: Don't try to extract an ExpressionStatement consisting of a single token (#18450) * extractMethod: Don't try to extract an ExpressionStatement consisting of a single token * Move to unit test --- src/harness/unittests/extractMethods.ts | 2 ++ src/services/refactors/extractMethod.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/harness/unittests/extractMethods.ts b/src/harness/unittests/extractMethods.ts index 5cab0289d67..ca77af61c75 100644 --- a/src/harness/unittests/extractMethods.ts +++ b/src/harness/unittests/extractMethods.ts @@ -410,6 +410,8 @@ function test(x: number) { "Statement or expression expected." ]); + testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, ["Select more than a single token."]); + testExtractMethod("extractMethod1", `namespace A { let x = 1; diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index 4c24d22a2f3..9ea3d9d11bd 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -231,7 +231,7 @@ namespace ts.refactor.extractMethod { } function checkRootNode(node: Node): Diagnostic[] | undefined { - if (isToken(node)) { + if (isToken(isExpressionStatement(node) ? node.expression : node)) { return [createDiagnosticForNode(node, Messages.InsufficientSelection)]; } return undefined;