Skip to content

moveToDirectory loses the import statements' extension #1612

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

Open
shinebayar-g opened this issue Feb 6, 2025 · 2 comments
Open

moveToDirectory loses the import statements' extension #1612

shinebayar-g opened this issue Feb 6, 2025 · 2 comments

Comments

@shinebayar-g
Copy link

shinebayar-g commented Feb 6, 2025

Describe the bug

Version: 25.0.1

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

To Reproduce

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.

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

After

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

Other move methods like moveImmediately also seems to be affected.

tsconfig

{
  "compilerOptions": {
    "lib": ["ES2022"],
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "node16",
    "declaration": true,
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,
    "noEmitOnError": true
  }
}
@shinebayar-g
Copy link
Author

It became a blocker for users who have "moduleResolution": "nodenext" in their tsconfig.

@kingston
Copy link

Yup also encountered this issue. I narrowed it down to these lines where it seems extensions are stripped:

https://github.com/dsherret/ts-morph/blob/a1c61c7e04928de5d8fc12f32a76e3ed5b7ffea1/packages/ts-morph/src/fileSystem/Directory.ts#L876C1-L879C1

I can try to make a PR for this if helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants