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(eslint-plugin): [no-restricted-imports]: report type-only imports properly #3996

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Expand Up @@ -4,13 +4,13 @@ import {
ArrayOfStringOrObjectPatterns,
} from 'eslint/lib/rules/no-restricted-imports';
import ignore, { Ignore } from 'ignore';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import {
createRule,
deepMerge,
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../util';
import { getESLintCoreRule } from '../util/getESLintCoreRule';

const baseRule = getESLintCoreRule('no-restricted-imports');

Expand Down Expand Up @@ -150,9 +150,12 @@ export default createRule<Options, MessageIds>({
}
}
function isAllowedTypeImportPattern(importSource: string): boolean {
return allowedImportTypeMatchers.every(matcher => {
return matcher.ignores(importSource);
});
return (
allowedImportTypeMatchers.length > 0 &&
allowedImportTypeMatchers.every(matcher => {
return matcher.ignores(importSource);
})
Comment on lines +154 to +157
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that allowedImportTypeMatchers remains empty when you don't specify allowTypeImports: true in options... and every returns true even for an empty array (ref: https://262.ecma-international.org/6.0/#sec-array.prototype.every).

);
}

return {
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts
Expand Up @@ -535,5 +535,19 @@ ruleTester.run('no-restricted-imports', rule, {
},
],
},
{
code: "import type { InvalidTestCase } from '@typescript-eslint/experimental-utils/dist/ts-eslint';",
options: [
{
patterns: ['@typescript-eslint/experimental-utils/dist/*'],
},
],
errors: [
{
messageId: 'patterns',
type: AST_NODE_TYPES.ImportDeclaration,
},
],
},
],
});