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

[Fix] importType: fix isExternalModule calculation #2282

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,10 +8,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Fixed
- [`extensions`]: ignore unresolveable type-only imports ([#2270], [#2271], [@jablko])
- `importType`: fix `isExternalModule` calculation ([#2282], [@mx-bernhard])

### Changed
- [Docs] [`order`]: add type to the default groups ([#2272], [@charpeni])
- [readme] Add note to TypeScript docs to install appropriate resolver ([#2279], [@johnthagen])
- [Refactor] `importType`: combine redundant `isScoped` and `isScopedModule` ([@ljharb])

## [2.25.2] - 2021-10-12

Expand Down Expand Up @@ -936,6 +938,8 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2282]: https://github.com/import-js/eslint-plugin-import/pull/2282
[#2279]: https://github.com/import-js/eslint-plugin-import/pull/2279
[#2272]: https://github.com/import-js/eslint-plugin-import/pull/2272
[#2271]: https://github.com/import-js/eslint-plugin-import/pull/2271
[#2270]: https://github.com/import-js/eslint-plugin-import/pull/2270
Expand Down Expand Up @@ -1542,6 +1546,7 @@ for info on changes for earlier releases.
[@MikeyBeLike]: https://github.com/MikeyBeLike
[@mplewis]: https://github.com/mplewis
[@mrmckeb]: https://github.com/mrmckeb
[@mx-bernhard]: https://github.com/mx-bernhard
[@nickofthyme]: https://github.com/nickofthyme
[@nicolashenry]: https://github.com/nicolashenry
[@noelebrun]: https://github.com/noelebrun
Expand Down
6 changes: 1 addition & 5 deletions src/core/importType.js
Expand Up @@ -29,7 +29,7 @@ export function isExternalModule(name, settings, path, context) {
if (arguments.length < 4) {
throw new TypeError('isExternalModule: name, settings, path, and context are all required');
}
return isModule(name) && isExternalPath(name, settings, path, getContextPackagePath(context));
return (isModule(name) || isScoped(name)) && isExternalPath(name, settings, path, getContextPackagePath(context));
}

export function isExternalModuleMain(name, settings, path, context) {
Expand Down Expand Up @@ -101,10 +101,6 @@ function typeTest(name, context, path) {
return 'unknown';
}

export function isScopedModule(name) {
return name.indexOf('@') === 0 && !name.startsWith('@/');
}

export default function resolveImportType(name, context) {
return typeTest(name, context, resolve(name, context));
}
6 changes: 3 additions & 3 deletions src/rules/extensions.js
@@ -1,7 +1,7 @@
import path from 'path';

import resolve from 'eslint-module-utils/resolve';
import { isBuiltIn, isExternalModule, isScoped, isScopedModule } from '../core/importType';
import { isBuiltIn, isExternalModule, isScoped } from '../core/importType';
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
import docsUrl from '../docsUrl';

Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = {
const slashCount = file.split('/').length - 1;

if (slashCount === 0) return true;
if (isScopedModule(file) && slashCount <= 1) return true;
if (isScoped(file) && slashCount <= 1) return true;
return false;
}

Expand Down Expand Up @@ -161,7 +161,7 @@ module.exports = {
importPath,
context.settings,
resolve(importPath, context),
context
context,
) || isScoped(importPath);

if (!extension || !importPath.endsWith(`.${extension}`)) {
Expand Down
15 changes: 9 additions & 6 deletions tests/src/core/importType.js
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as path from 'path';

import importType, { isExternalModule, isScopedModule, isScoped } from 'core/importType';
import importType, { isExternalModule, isScoped } from 'core/importType';

import { testContext, testFilePath } from '../utils';

Expand Down Expand Up @@ -234,16 +234,19 @@ describe('importType(name)', function () {
it('`isExternalModule` works with windows directory separator', function () {
const context = testContext();
expect(isExternalModule('foo', {}, 'E:\\path\\to\\node_modules\\foo', context)).to.equal(true);
expect(isExternalModule('@foo/bar', {}, 'E:\\path\\to\\node_modules\\@foo\\bar', context)).to.equal(true);
expect(isExternalModule('foo', {
'import/external-module-folders': ['E:\\path\\to\\node_modules'],
}, 'E:\\path\\to\\node_modules\\foo', context)).to.equal(true);
});

it('correctly identifies scoped modules with `isScopedModule`', () => {
expect(isScopedModule('@/abc')).to.equal(false);
expect(isScopedModule('@/abc/def')).to.equal(false);
expect(isScopedModule('@a/abc')).to.equal(true);
expect(isScopedModule('@a/abc/def')).to.equal(true);
it('`isExternalModule` works with unix directory separator', function () {
const context = testContext();
expect(isExternalModule('foo', {}, '/path/to/node_modules/foo', context)).to.equal(true);
expect(isExternalModule('@foo/bar', {}, '/path/to/node_modules/@foo/bar', context)).to.equal(true);
expect(isExternalModule('foo', {
'import/external-module-folders': ['/path/to/node_modules'],
}, '/path/to/node_modules/foo', context)).to.equal(true);
});

it('correctly identifies scoped modules with `isScoped`', () => {
Expand Down