eslint-plugin-node icon indicating copy to clipboard operation
eslint-plugin-node copied to clipboard

🐞 [bugfix] Add proper file extensions when importing a typescript file from a typescript file

Open giladgd opened this issue 3 years ago • 5 comments

The current situation

Given this configuration:

{
    "node/file-extension-in-import": ["error", "always"]
}

And this file: (a typescript file that imports from another typescript file)

// file1.ts
import something from "./file2"

eslint will suggest adding .ts extension to the import statement, which is invalid and breaks the compilation of typescript.

The fix

When a typescript file (.ts / .cts / .mts files, .cts and .mts will be supported as of TypeScript 4.5) imports another typescript file, eslint will now use the proper matching file extension instead of the original referenced file extension.

This means that, for example, when a .ts file imports another .ts file, eslint will suggest using a .js extension in the import statement.

giladgd avatar Nov 18 '21 02:11 giladgd

This would fix the problem that caused https://github.com/xojs/xo/issues/522. This would also provide an elegant solution to https://github.com/microsoft/TypeScript/issues/16577.

giladgd avatar Nov 18 '21 02:11 giladgd

@giladgd I have cherry picked and submitted your fix to the new repo which is being maintained: https://github.com/weiran-zsd/eslint-plugin-node/pull/20

rosskevin avatar Jun 01 '22 19:06 rosskevin

Thanks @rosskevin!

giladgd avatar Jun 01 '22 19:06 giladgd

Is this working? Because i'm still getting .ts extensions on --fix.

SamuelGaona avatar Jun 08 '23 05:06 SamuelGaona

eslint-plugin-n is the maintained version of this module.

We switched to it in eg. eslint-config-standard / standard and it is maintained by me and other members of the official ESLint community organization.

Try that module out and if it isn't fixed there, then open a new PR in that project.

voxpelli avatar Aug 13 '23 12:08 voxpelli