Skip to content
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

Suggested refactoring: move function/type/class to new file #13859

Closed
markusjohnsson opened this issue Feb 3, 2017 · 4 comments
Closed

Suggested refactoring: move function/type/class to new file #13859

markusjohnsson opened this issue Feb 3, 2017 · 4 comments
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@markusjohnsson
Copy link
Contributor

markusjohnsson commented Feb 3, 2017

I suggest adding a refactoring/code fix that helps with the task of moving a class/function/type or other top level declaration out of the current file and into another file, updating references as needed.

Motivation
Often when starting out with a new feature it makes sense to start to write code in a single file while working out the design. However, as soon as long method bodies start getting implemented or when code gets checked in to version control, it makes less sense and gets harder to work with and you want to move top level members into separate files.

I've found CodeRush and ReSharper's "Move Type to File" tool (for VS/C#) very useful in this workflow and I miss it greatly when working with TypeScript/VS Code.

Example

before:

// MyView.ts

class MyListItem { 
   // ...
} 

export default class MyView {
   myItems: MyListItem[];
   // ...
}

after invoking "Move declaration to file" on MyListItem:

// MyListItem.ts
export default class MyListItem { 
   // ...
} 

// MyView.ts
import MyListItem from "./MyListItem";

export default class MyView {
   myItems: MyListItem[];
   // ...
}

Design

  • I suggest that this type of feature should require as little input as possible, ideally just be a single shortcut without dialogs, generating an appropriately named file.
  • It needs to take module configuration into consideration (i.e. should import/export be generated or not)
  • Should generate .tsx extension as appropriate, either based on the file it is invoked in or the contents of the code transferred.

Edit: removed parenthesis after class names

@markusjohnsson
Copy link
Contributor Author

Is this something that could be considered for inclusion in the compiler, or is it out of scope and should instead be suggested as a plug-in to editors/IDE's?

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol Duplicate An existing issue was already created and removed Domain: Refactorings e.g. extract to constant or function, rename symbol Suggestion An idea for TypeScript labels Feb 27, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2017

Should be covered by #6487

@markusjohnsson
Copy link
Contributor Author

Well, it's not about moving a file but moving a declaration out of one file into another. I guess a lot of the mechanics of updating references will be the same, but is it the same?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2017

will leave both then.

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol and removed Duplicate An existing issue was already created labels Feb 27, 2017
@mhegazy mhegazy assigned ghost May 2, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone May 2, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 8, 2018
@mhegazy mhegazy closed this as completed May 10, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants