-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor params for interface (#42652)
* apply refactor to interface methods * handle calls * no available refactors for union type * add tests * Update src/services/refactors/convertParamsToDestructuredObject.ts Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com> * address comments * Update src/services/refactors/convertParamsToDestructuredObject.ts Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com> * Update src/services/refactors/convertParamsToDestructuredObject.ts Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com> Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
- Loading branch information
1 parent
664ed17
commit 1b19af0
Showing
8 changed files
with
214 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////const x: IFoo = { | ||
//// method(/*a*/x: string, y: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert parameters to destructured object", | ||
actionName: "Convert parameters to destructured object", | ||
actionDescription: "Convert parameters to destructured object", | ||
newContent: `interface IFoo { | ||
method({ x, y }: { x: string; y: string; }): void; | ||
} | ||
const x: IFoo = { | ||
method({ x, y }: { x: string; y: string; }): void {}, | ||
};` | ||
}); |
21 changes: 21 additions & 0 deletions
21
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_interfaceAssignability.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////const x: IFoo = { | ||
//// method(/*a*/x: string, y: string, z?: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert parameters to destructured object", | ||
actionName: "Convert parameters to destructured object", | ||
actionDescription: "Convert parameters to destructured object", | ||
newContent: `interface IFoo { | ||
method({ x, y }: { x: string; y: string; }): void; | ||
} | ||
const x: IFoo = { | ||
method({ x, y, z }: { x: string; y: string; z?: string; }): void {}, | ||
};` | ||
}); |
28 changes: 28 additions & 0 deletions
28
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_interfaceContextualParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////const x: IFoo = { | ||
//// method(/*a*/x, y/*b*/): void {}, | ||
////}; | ||
|
||
/* | ||
When there are no type annotations on the params in the implementation, we ultimately | ||
would like to handle them like we do for calls resulting in `method({x, y}) {}`. | ||
Note that simply adding the annotations from the signature can fail as the implementation | ||
can take more paramters than the signatures. | ||
*/ | ||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert parameters to destructured object", | ||
actionName: "Convert parameters to destructured object", | ||
actionDescription: "Convert parameters to destructured object", | ||
newContent: `interface IFoo { | ||
method({ x, y }: { x: string; y: string; }): void; | ||
} | ||
const x: IFoo = { | ||
method({ x, y }: { x; y; }): void {}, | ||
};` | ||
}); |
14 changes: 14 additions & 0 deletions
14
.../cases/fourslash/refactorConvertParamsToDestructuredObject_interfaceMultipleSignatures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
//// method(x: number, y: string): void; | ||
////} | ||
////const x: IFoo = { | ||
//// method(/*a*/x: string | number, y: string/*b*/): void {}, | ||
////}; | ||
|
||
// For multiple signatures, we don't have a reliable way to determine | ||
// which signature to match to or if all signatures should be changed. | ||
goTo.select("a", "b"); | ||
verify.not.refactorAvailable("Convert parameters to destructured object"); |
14 changes: 14 additions & 0 deletions
14
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_interfaceNoIntersection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////interface IBar { | ||
//// method(x: number): void; | ||
////} | ||
////const x: IFoo & IBar = { | ||
//// method(/*a*/x: string, y: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
verify.not.refactorAvailable("Convert parameters to destructured object"); |
14 changes: 14 additions & 0 deletions
14
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_interfaceNoUnion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////interface IFoo { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////interface IBar { | ||
//// method(x: number): void; | ||
////} | ||
////const x: IFoo | IBar = { | ||
//// method(/*a*/x: string, y: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
verify.not.refactorAvailable("Convert parameters to destructured object"); |
21 changes: 21 additions & 0 deletions
21
tests/cases/fourslash/refactorConvertParamsToDestructuredObject_typeLiteral.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////type Foo = { | ||
//// method(x: string, y: string): void; | ||
////} | ||
////const x: Foo = { | ||
//// method(/*a*/x: string, y: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert parameters to destructured object", | ||
actionName: "Convert parameters to destructured object", | ||
actionDescription: "Convert parameters to destructured object", | ||
newContent: `type Foo = { | ||
method({ x, y }: { x: string; y: string; }): void; | ||
} | ||
const x: Foo = { | ||
method({ x, y }: { x: string; y: string; }): void {}, | ||
};` | ||
}); |