dsherret / dsherret/ts-morph

Add support for module specifier "aliases" (tsconfig.paths mappings) when moving files/folders.

Open
#1,494 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**tl;dr** Add support for module specifier "aliases" (tsconfig.paths mappings) when moving files/folders.

---

Let's say your tsconfig contains a paths mapping like this:
```
"paths": {
"Root/*": ["./*"]
},
```

I noticed that

```ts
project.createDirectory('someDir');
const srcFile = project.getSourceFileOrThrow('someFile.ts');
srcFile.moveToDirectory('someDir');
```

will update all relative import/export specifiers referencing 'someFile.ts' but _not_ if the specifier used an alias such as

```ts
import * from 'Root/someFile';
```

I expected `ts-morph` to update the specifier to `import * from 'Root/someDir/someFile';` but it doesn't.
I think it would be nice feature to support this. VSCode can handle paths mapped import/exports when moving files.

**Describe the solution you'd like**

I think the problem is that [`updateStringLiteralReferences(nodeReferences: ReadonlyArray<[StringLiteral, SourceFile]>) {`](https://github.com/dsherret/ts-morph/blob/930779a0d3699be35ab17bbe9dd26ec00350fe53/packages/ts-morph/src/compiler/ast/module/SourceFile.ts#L1025C1-L1028C4) only updates the module specifier if it's a relative path like `'../someFile'` not if it's an module specifier alias like `'Root/someFile'`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/ts-morph/src/compiler/ast/module/SourceFile.ts at updateStringLiteralReferences, then trace the moveToDirectory flow and how tsconfig paths mappings are represented. Reproduce the relative-specifier update with an alias such as Root/someFile; done means moving a file or folder also updates matching aliased import/export specifiers.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.