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

import/extensions: ignore imports starting with #

Open dobesv opened this issue 6 months ago • 4 comments

Feature Request

It would be nice if it was easy to exclude '#' based imports from the "import/extensions" rule, similar to how it is easy to exclude imports from other packages for this rule.

Background

When an import starts with '#' it is resolved by looking for a matching entry in "imports". These import rules are very similar to the rules applied when you are importing from another package, especially one that specifies "exports" to translate import paths.

As such, it is normal that these kinds of imports may not require a file extension even if relative imports do.

Workaround

You can define an override for imports starting with #, e.g.

'import/extensions': [
          'error',
          'ignorePackages',
          {
            pathGroupOverrides: [
              {
                pattern: '#**',
                action: 'ignore',
              },
            ],
          },
        ],

dobesv avatar May 27 '25 01:05 dobesv

Why it should be ignored? Node doesn't support ignore its extension. And we've already have pattern support to ignore such case.

https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/extensions.md#rule-details

JounQin avatar May 27 '25 01:05 JounQin

The proper way to support imports is to require that they exactly match what the “exports” field has in it, but we don’t support “exports” yet, and thus not yet “imports” either.

ljharb avatar May 27 '25 01:05 ljharb

This could likely be fixed as part of fixing https://github.com/import-js/eslint-plugin-import/issues/3189

dobesv avatar May 27 '25 03:05 dobesv

If the "imports" subpath mapping is set up to add extensions, then we don't want to put the extension when we import it. However, we can of course just not do things that way and have the extension when we import and modify the extension in the imports mapping.

dobesv avatar May 27 '25 05:05 dobesv