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

Feature request: import/extensions autofix

Open csvan opened this issue 5 years ago • 20 comments

With Node 13.2 and up now having near-stable support for ESM modules, existing projects will need to update their imports in order to make use of it. According to the docs:

A file extension must be provided when using the import keyword. Directory indexes (e.g. './startup/index.js') must also be fully specified. This behavior matches how import behaves in browser environments, assuming a typically configured server.

(https://nodejs.org/api/esm.html#esm_mandatory_file_extensions)

Currently, while the import/extensions plugin can be configured to throw if an import doesn't have a proper ending, it does not provide a way to autofix it. Having that would be a tremendeous benefits to existing, large codebases migrating to native modules.

csvan avatar May 05 '20 12:05 csvan

Existing projects don’t need to do anything; there’s no particular reason anyone needs to switch to native ESM, and published packages likely shouldn’t.

That said, it seems like a reasonable autofix when it’s unambiguous, but only then.

ljharb avatar May 05 '20 15:05 ljharb

@ljharb no, of course no one needs to switch to native ESM. However if they want to do so, they need to, as per the spec, use file extensions in their imports. This FR makes such migrations much easier, as well as related cases. It's not a commentary on which module system to use.

csvan avatar May 05 '20 18:05 csvan

@csvan take a look at the node/file-extension-in-import rule, it can autofix:

https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/file-extension-in-import.md

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

jaydenseric avatar May 12 '20 11:05 jaydenseric

Thanks @jaydenseric, I will check it out

csvan avatar May 12 '20 12:05 csvan

I also hope to has an 'autofix' function for the rule 'import/extensions'. Except for the case @csvan mentioned, there are many other cases that need the 'autofix'. For example, when using react/vue or other framework that may has different extensions.

cat-walk avatar Jul 31 '20 03:07 cat-walk

Existing projects don’t need to do anything; there’s no particular reason anyone needs to switch to native ESM, and published packages likely shouldn’t.

There are reasons now, many are already switching...

jimmywarting avatar Nov 16 '21 11:11 jimmywarting

There still are no reasons, except that a tiny number of authors (only one of whom unfortunately represents a large number of packages) have decided to be user-hostile and switch to ESM-only. That will only force people to stay on old versions or find a better package, but in the meantime it will cause a lot of pain to their users.

ljharb avatar Nov 16 '21 16:11 ljharb

There still are no reasons, except that a tiny number of authors (only one of whom unfortunately represents a large number of packages) have decided to be user-hostile and switch to ESM-only. That will only force people to stay on old versions or find a better package, but in the meantime it will cause a lot of pain to their users.

ignorePackages?

DMXL avatar Feb 18 '22 08:02 DMXL

so eslint-plugin-node has an autofix but it can't handle directory imports. It would be great if eslint-plugin-import could do this, since it's so much smarter.

ESM is slowly becoming inevitable. Node-fetch v3 and Microsoft's pnpjs are two examples of ESM-only packages with no alternatives.

wmertens avatar Mar 31 '22 19:03 wmertens

@wmertens i don't think 2 packages represents an indication that anything is becoming "inevitable". node-fetch has many alternatives, including an experimental one in node itself, for example.

Once resolve supports "exports" (which i'm steadily working on), we'll be able to do a proper autofix here for many of these scenarios.

ljharb avatar Mar 31 '22 21:03 ljharb

@csvan take a look at the node/file-extension-in-import rule, it can autofix:

https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/file-extension-in-import.md

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

This is replacing with .ts instead of .js like it's supposed to

ntucker avatar Mar 09 '23 20:03 ntucker

No autofix, this rule lose half of meaning

tjx666 avatar Mar 21 '23 20:03 tjx666

@tjx666 any rule is valuable with or without autofixing; but certainly autofixing is nice to have!

ljharb avatar Mar 21 '23 21:03 ljharb