stringham / move-ts

A Visual Studio Code plugin for updating relative imports when moving typescript files and folders in your workspace.
MIT License
52 stars 15 forks source link

Do you plan to support TypeScript paths option? #16

Closed vitalysoftech closed 6 years ago

vitalysoftech commented 6 years ago

Hello,

TypeScript 2+ supports baseUrl and paths options that allow to reference files via "absolute-like" path, whereas it is actually implicitly relative to one of the paths specified in tsconfig.json. See "Path mapping" by the following link: http://www.typescriptlang.org/docs/handbook/module-resolution.html

Currently the extension doesn't affect paths relative to "paths" from tsconfig. But that would be nice to have. Do you plan to support that?

Example:

src
    folder1
        file1.ts
    folder2
        folder2_1
            file2.ts

tsconfig.json:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "*": [
                "./src/folder2/*"
            ]
        }
    }
}

file1.ts: import { something } from "folder2_1/file2"

Note that folder2_1/file2 should be resolved to src/folder2/folder2_1/file2.ts. If I move file2.ts to another location, it would be good if import statement above updates as well.

Regards, Vitaly

stringham commented 6 years ago

The extension currently does support the typescript paths option in many cases, but it is not following the spec and could be improved.

https://github.com/stringham/move-ts/blob/master/src/index/referenceindexer.ts#L537-L548

Right now when resolving relative paths, it checks to see if the module specifier starts with everything before the *, and only works if your path mapping array is of length one since it skips doing a file existence check. It also assumes that the baseUrl is ".".

As implemented it is working correctly on the projects I use. Do you have a project on github that it is not working on that I can test against?

vitalysoftech commented 6 years ago

Yes, here it is: https://github.com/vitalysoftech/paths-move

I use monorepo approach, where I add "library-folders" to paths. I see that the code you pointed to supports only a single element for each mapping's array: https://github.com/stringham/move-ts/blob/master/src/index/referenceindexer.ts#L540

stringham commented 6 years ago

It should work in version 1.11.2

vitalysoftech commented 6 years ago

Thanks for the fix, but this still doesn't work. Take a look:

movets_paths

vitalysoftech commented 6 years ago

I've also added a branch which uses configuration that was originally supported - each paths item array has a single element. But moving file still doesn't work correctly - references in file1 are not updated. May be extension updates only relative imports?

"paths": {
    "folder1/*": [
        "src/folder1/*"
    ],
    "folder2/*": [
        "src/folder2/*"
    ],
    "folder3/*": [
        "src/folder3/*"
    ]
}

New branch with more expected configuration is: https://github.com/vitalysoftech/paths-move/tree/diffrentConfig

stringham commented 6 years ago

Interesting, I'm not able to reproduce that. Have you tried reloading vscode?

move-paths

vitalysoftech commented 6 years ago

Yes, sure. BTW, I see that you are working on either Linux or MacOS. I am on Windows. May be that is the reason.

stringham commented 6 years ago

It turns out that https://github.com/stringham/move-ts/pull/3/files wasn't sufficient for fixing path mapping on windows.

I just published version 11.1.3 with updated support for Windows paths.

vitalysoftech commented 6 years ago

Thanks a lot! This works now!