extractMethod: Don't try to extract a single token (#18090)

* extractMethod: Don't try to extract a single token

* Update tests
This commit is contained in:
Andy
2017-09-07 07:28:12 -07:00
committed by GitHub
parent b3c87aa919
commit b533b24686
5 changed files with 17 additions and 10 deletions
+2 -2
View File
@@ -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;
@@ -0,0 +1,6 @@
/// <reference path='fourslash.ts' />
////"/**/foo";
goTo.marker("");
verify.not.refactorAvailable('Extract Method');
+4 -4
View File
@@ -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;
}
}`);
+3 -2
View File
@@ -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;
}
}`);
+2 -2
View File
@@ -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;
}
`);