Skip to content

Commit b533b24

Browse files
author
Andy
authored
extractMethod: Don't try to extract a single token (#18090)
* extractMethod: Don't try to extract a single token * Update tests
1 parent b3c87aa commit b533b24

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

src/services/refactors/extractMethod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace ts.refactor.extractMethod {
9595
export const CannotExtractRangeThatContainsWritesToReferencesLocatedOutsideOfTheTargetRangeInGenerators: DiagnosticMessage = createMessage("Cannot extract range containing writes to references located outside of the target range in generators.");
9696
export const TypeWillNotBeVisibleInTheNewScope = createMessage("Type will not visible in the new scope.");
9797
export const FunctionWillNotBeVisibleInTheNewScope = createMessage("Function will not visible in the new scope.");
98-
export const InsufficientSelection = createMessage("Select more than a single identifier.");
98+
export const InsufficientSelection = createMessage("Select more than a single token.");
9999
export const CannotExtractExportedEntity = createMessage("Cannot extract exported declaration");
100100
export const CannotCombineWritesAndReturns = createMessage("Cannot combine writes and returns");
101101
export const CannotExtractReadonlyPropertyInitializerOutsideConstructor = createMessage("Cannot move initialization of read-only class property outside of the constructor");
@@ -239,7 +239,7 @@ namespace ts.refactor.extractMethod {
239239
}
240240

241241
function checkRootNode(node: Node): Diagnostic[] | undefined {
242-
if (isIdentifier(node)) {
242+
if (isToken(node)) {
243243
return [createDiagnosticForNode(node, Messages.InsufficientSelection)];
244244
}
245245
return undefined;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////"/**/foo";
4+
5+
goTo.marker("");
6+
verify.not.refactorAvailable('Extract Method');

tests/cases/fourslash/extract-method13.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// Also checks that we correctly find non-conflicting names in static contexts.
55

66
//// class C {
7-
//// static j = /*c*/100/*d*/;
8-
//// constructor(q: string = /*a*/"hello"/*b*/) {
7+
//// static j = /*c*/1 + 1/*d*/;
8+
//// constructor(q: string = /*a*/"a" + "b"/*b*/) {
99
//// }
1010
//// }
1111

@@ -29,10 +29,10 @@ verify.currentFileContentIs(`class C {
2929
}
3030
3131
private static newFunction(): string {
32-
return "hello";
32+
return "a" + "b";
3333
}
3434
3535
private static newFunction_1() {
36-
return 100;
36+
return 1 + 1;
3737
}
3838
}`);

tests/cases/fourslash/extract-method5.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// annotation in the extracted function
66

77
//// function f() {
8-
//// var x: 1 | 2 | 3 = /*start*/2/*end*/;
8+
//// var x: 1 | 2 | 3 = /*start*/1 + 1 === 2 ? 1 : 2/*end*/;
99
//// }
1010

1111
goTo.select('start', 'end');
@@ -14,11 +14,12 @@ edit.applyRefactor({
1414
actionName: "scope_0",
1515
actionDescription: "Extract function into function 'f'",
1616
});
17+
// TODO: GH#18091 (fix formatting to use `2 ? 1 :` and not `2?1:`)
1718
verify.currentFileContentIs(
1819
`function f() {
1920
var x: 1 | 2 | 3 = newFunction();
2021
2122
function newFunction(): 1 | 2 | 3 {
22-
return 2;
23+
return 1 + 1 === 2?1: 2;
2324
}
2425
}`);

tests/cases/fourslash/extract-method7.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// You cannot extract a function initializer into the function's body.
44
// The innermost scope (scope_0) is the sibling of the function, not the function itself.
55

6-
//// function fn(x = /*a*/3/*b*/) {
6+
//// function fn(x = /*a*/1 + 1/*b*/) {
77
//// }
88

99
goTo.select('a', 'b');
@@ -15,6 +15,6 @@ edit.applyRefactor({
1515
verify.currentFileContentIs(`function fn(x = newFunction()) {
1616
}
1717
function newFunction() {
18-
return 3;
18+
return 1 + 1;
1919
}
2020
`);

0 commit comments

Comments
 (0)