sourceFile.moveDeclaration()
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 238
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
Similar to `SourceFile` methods getClass(), getStatement(), etc. I would like to have methods `moveClass()`, or more general `moveDeclaration()` to be able to move a top-level source file's declaration to another source file. Features:
* should support only declarations that are declared as children of the source file (top-level)
* should support class, interface, function, enum, variable, type alias kinds
* the target source file could be an existing one or a non existing one in which case it will be created
* all files in the project should be updated so references to the moved node in import statements point to the new location after the operation completes.
* If the declaration referenced nodes that are not exported they must end up being exported in sourcefile so the new declaration can import them from the new file
* If the existing target sourcefile already has a top-level declaration with the same name, then a new declaration will be created for the moved node with a different name. For example, if the target node is called "I" and the target source file already has a top level declaration with that name, then the operation will create a new top-level declaration called "I2"
* it should throw if target node has no name
* it should support references to the target node imported using named, default or namespace import kinds.
* it should not leave the project invalid, this is have some kind of validation before the operation or some rollback mechanism in case something fails at the last moment
* (nice to have) should not leave any forgotten nodes
* Alternatively, if after the operation a source file is empty, user can provide an option so it get's removed from the project.
* Alternatively user can provide a new name for the new declaration (moveAndRename)
P / D : I have a working implementation that apply with most of mentioned items here :
* https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/ts-simple-ast-extra/src/refactor/moveDeclaration.ts
* specs : https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/ts-simple-ast-extra/spec/moveDeclarationSpec.ts
I'm testing it now on big projects and it's working fine, although it's not fast and still missing the following features:
* don't support variable declarations yet.
* some file nodes are forgotten (because I'm using organizeImports())
* Don't support default or namespace imports in which case it throws.
Contributor guide
Assessment
This issue has not been assessed yet.