Skip to content

Commit b1e9446

Browse files
Andymhegazy
Andy
authored andcommitted
extractMethod: Don't try to extract a single token (#18090)
* extractMethod: Don't try to extract a single token * Update tests
1 parent 1c7a29e commit b1e9446

File tree

5 files changed

+16
-34
lines changed

5 files changed

+16
-34
lines changed

src/services/refactors/extractMethod.ts

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

229229
function checkRootNode(node: Node): Diagnostic[] | undefined {
230-
if (isIdentifier(node)) {
230+
if (isToken(node)) {
231231
return [createDiagnosticForNode(node, Messages.InsufficientSelection)];
232232
}
233233
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: 6 additions & 6 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

@@ -16,12 +16,12 @@ edit.applyRefactor({
1616
actionDescription: "Extract function into class 'C'",
1717
newContent:
1818
`class C {
19-
static j = 100;
19+
static j = 1 + 1;
2020
constructor(q: string = C./*RENAME*/newFunction()) {
2121
}
2222
2323
private static newFunction(): string {
24-
return "hello";
24+
return "a" + "b";
2525
}
2626
}`
2727
});
@@ -38,11 +38,11 @@ edit.applyRefactor({
3838
}
3939
4040
private static newFunction(): string {
41-
return "hello";
41+
return "a" + "b";
4242
}
4343
4444
private static newFunction_1() {
45-
return 100;
45+
return 1 + 1;
4646
}
4747
}`
4848
});

tests/cases/fourslash/extract-method5.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

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,7 +15,7 @@ edit.applyRefactor({
1515
`function fn(x = /*RENAME*/newFunction()) {
1616
}
1717
function newFunction() {
18-
return 3;
18+
return 1 + 1;
1919
}
2020
`
2121
});

0 commit comments

Comments
 (0)