dsherret / dsherret/ts-morph

moveToDirectory loses the import statements' extension

Open
#1,612 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 25.0.1

When `moveToDirectory` is used, the import statements' extensions are lost.

**To Reproduce**

```ts
import { Project } from "ts-morph";

const project = new Project({
tsConfigFilePath: path.join(packagePath, 'tsconfig.json'),
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
quoteKind: QuoteKind.Double,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
useTrailingCommas: true,
},
});

sourceFile.addImportDeclaration({
moduleSpecifier: '../App.js',
namedImports: ['App'],
});

sourceFile.moveToDirectory('subDir');
```

**Expected behavior**

Without `moveToDirectory` is used, import statement looks like this.

```ts
import { App } from '../App.js';
```

After

```ts
import { App } from '../../App'; // .js extension is lost.
```

Other move methods like `moveImmediately` also seems to be affected.

tsconfig

```json
{
"compilerOptions": {
"lib": ["ES2022"],
"target": "ES2022",
"module": "Node16",
"moduleResolution": "node16",
"declaration": true,
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noEmitOnError": true
}
}
```

Contributor guide

Open the contributing guide

Research direction

Read CONTRIBUTING.md, then run the supplied TypeScript reproduction and trace sourceFile.moveToDirectory and the related moveImmediately path. Compare the import declaration before and after moving; done means relative imports retain their explicit .js extension.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.