mysticatea / eslint-plugin-node

Additional ESLint's rules for Node.js
MIT License
958 stars 167 forks source link

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

Open giladgd opened 2 years ago

giladgd commented 2 years ago

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 commented 2 years ago

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.

rosskevin commented 2 years ago

@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

giladgd commented 2 years ago

Thanks @rosskevin!

SamuelGaona commented 1 year ago

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

voxpelli commented 11 months ago

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.