Skip to content
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

[no-missing-imports]: add tryExtensions option #33

Closed
piranna opened this issue Jul 11, 2022 · 11 comments · Fixed by #228
Closed

[no-missing-imports]: add tryExtensions option #33

piranna opened this issue Jul 11, 2022 · 11 comments · Fixed by #228

Comments

@piranna
Copy link

piranna commented Jul 11, 2022

Similar to https://github.com/mysticatea/eslint-plugin-node/blob/master/docs/rules/no-missing-import.md, add support for the tryExtensions option.

@aladdin-add
Copy link

if using esm, technically it's a bug to use "tryExtensions" - the ext is always required, just like the web.

to support the nodejs esm, we have removed the option in d24be36.

@piranna
Copy link
Author

piranna commented Jul 12, 2022

ESM requires to have extensions always, and I agree on that, but Typescript on the other hand forbids them, so without extensions, files are not found...

@aladdin-add
Copy link

well, we could reintroduce the option to support something like ts.

@piranna
Copy link
Author

piranna commented Jul 13, 2022

that would be really nice :-) Maybe disabled by default, enforcing extensions to be added.

@jordanbtucker
Copy link

jordanbtucker commented Oct 3, 2022

ESM requires to have extensions always, and I agree on that, but Typescript on the other hand forbids them, so without extensions, files are not found...

@piranna Can you clarify what you mean? TypeScript allows you to have extensions in your module specifiers because it just copies them verbatim.

import util from './util.js'

transpiles to

const util = require('./util.js')

So you shouldn't use .ts file extensions in module specifiers like this.

import util from './util.ts'

because it just transpiles to

const util = require('./util.ts')

which breaks because Node.js and browsers can't parse TypeScript.

Note that you can still have a util.ts file, you just need to import it as if it is a .js file that exists relative to the transpiled .js file that will be importing it. TypeScript will import the .d.ts file for that .js file and everything works.

@piranna
Copy link
Author

piranna commented Oct 4, 2022

@piranna Can you clarify what you mean? TypeScript allows you to have extensions in your module specifiers because it just copies them verbatim.

Yes, but that feels like a nasty hack, because in source code there's no .js files, only .ts ones, being the .js just only on the transpiled code, so this plugin will also fails because there's no .js files at all on source files. Correct solution should be Typescript following ESM rules and allow to have .ts extensions on imports, but that's not the case. So, the alternative is to have back the tryExtensions option, and set it to .ts files.

@jordanbtucker
Copy link

Thanks for clarifying. I agree that adding the tryExtensions option for imports will fix the vast majority of use cases for TypeScript users.

@Lioness100
Copy link

Any progress on this? I'm using --experimental-specifier-resolution=node flag, which allows for extensions to be omitted in esm, which is breaking this rule.

@ashercoren
Copy link

@aladdin-add Any progress on this?

@uiolee
Copy link

uiolee commented Nov 5, 2023

I'm experiencing the same issue. It is blocking me from migrating from eslint-plugin-node to eslint-plugin-n.

@scagood
Copy link

scagood commented Nov 26, 2023

mmm, it looks like this is supported from the settings block, but not from the rule's option 🤔

https://github.com/eslint-community/eslint-plugin-n/blob/master/lib/util/visit-import.js#L35

So this in theory is as simple as adding tryExtensions: getTryExtensions.schema here:
https://github.com/eslint-community/eslint-plugin-n/blob/master/lib/rules/no-missing-import.js#L31

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

Successfully merging a pull request may close this issue.

7 participants