-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Refactor params for interface #42652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4ff1f2d
e125769
2a2fca6
26b53e4
2cc21f5
1a53903
8727653
a289977
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 {}, | ||
};` | ||
}); |
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 {}, | ||
};` | ||
}); |
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 {}, | ||
};` | ||
}); |
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"); |
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens with unions? Do we even get the error that triggers the codefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test refactorConvertParamsToDestructuredObject_interfaceNoUnion.ts: It's a refactor so we just don't offer it. |
||
//// method(/*a*/x: string, y: string/*b*/): void {}, | ||
////}; | ||
|
||
goTo.select("a", "b"); | ||
verify.not.refactorAvailable("Convert parameters to destructured object"); |
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"); |
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 {}, | ||
};` | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these branches be mutually exclusive? This looks suspicious to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the signature is defined, that means we were able to connect the method declaration back to it. That is,
functionDeclaration
implementssignature
and both should be changed. The absence ofsignature
simply meansfunctionDeclaration
does not implement a method declared elsewhere (that we know of).