📄 no-restricted-imports¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 11 |
| 📦 Imports | 14 |
| 📊 Variables & Constants | 5 |
| 📑 Type Aliases | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/rules/no-restricted-imports.ts
📤 Default Export¶
| Property | Value |
|---|---|
name |
'no-restricted-imports' |
meta.type |
'suggestion' |
meta.deprecated.deprecatedSince |
'8.64.0' |
meta.deprecated.replacedBy |
[ { rule: { name: 'no-restricted-imports', url: 'https://eslint.org/docs/latest/rules/no-restricted-imports', }, }, ] |
meta.deprecated.url |
'https://github.com/typescript-eslint/typescript-eslint/pull/12527' |
meta.docs.description |
'Disallow specified modules when loaded by import' |
meta.docs.extendsBaseRule |
true |
meta.fixable |
baseRule.meta.fixable |
meta.messages |
baseRule.meta.messages |
defaultOptions |
[] |
Entry point: create — documented under Functions.
📦 Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
JSONSchema4AnyOfSchema |
@typescript-eslint/utils/json-schema |
JSONSchema4ArraySchema |
@typescript-eslint/utils/json-schema |
JSONSchema4ObjectSchema |
@typescript-eslint/utils/json-schema |
ArrayOfStringOrObject |
eslint/lib/rules/no-restricted-imports |
ArrayOfStringOrObjectPatterns |
eslint/lib/rules/no-restricted-imports |
RuleListener |
eslint/lib/rules/no-restricted-imports |
Ignore |
ignore |
AST_NODE_TYPES |
@typescript-eslint/utils |
ignore |
ignore |
InferMessageIdsTypeFromRule |
../util |
InferOptionsTypeFromRule |
../util |
createRule |
../util |
getESLintCoreRule |
../util/getESLintCoreRule |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
baseSchema |
{ anyOf: [unknown, { items: [{ proper... |
const | baseRule.meta.schema as { anyOf: [ unknown, { items: [ { properties: { paths:... |
✗ |
allowTypeImportsOptionSchema |
JSONSchema4ObjectSchema['properties'] |
const | { allowTypeImports: { type: 'boolean', description: 'Whether to allow type-on... |
✗ |
arrayOfStringsOrObjects |
JSONSchema4ArraySchema |
const | { type: 'array', items: { anyOf: [ { type: 'string' }, { type: 'object', addi... |
✗ |
arrayOfStringsOrObjectPatterns |
JSONSchema4AnyOfSchema |
const | { anyOf: [ { type: 'array', items: { type: 'string', }, uniqueItems: true, },... |
✗ |
schema |
JSONSchema4AnyOfSchema |
const | { anyOf: [ arrayOfStringsOrObjects, { type: 'array', additionalItems: false, ... |
✗ |
Functions¶
create(context: any): { ExportAllDeclaration?: undefined; 'ExportNamedDeclaration…¶
Parameters:
contextany
Returns: { ExportAllDeclaration?: undefined; 'ExportNamedDeclaration[source]'?: undefined; ImportDeclaration?: undefined; TSImportEqualsDeclaration?: undefined; } | { ExportAllDeclaration: any; 'ExportNamedDeclaration[source]'(node: { source: NonNullable<TSESTree.ExportNamedDeclaration["source"]>; } & TSESTree.ExportNamedDeclaration): void; ImportDeclaration: (node: TSESTree.ImportDeclaration) => void; TSImportEqualsDeclaration(node: TSESTree.TSImportEqualsDeclaration): void; }
Calls:
baseRule.createshouldCreateRulegetRestrictedPathsallowedTypeImportPathNameSet.addallowedTypeImportPathNameSet.hasgetRestrictedPatternsallowedImportTypeMatchers.pushignore({ allowRelativePaths: true, ignoreCase: !restrictedPattern.caseSensitive, }).addallowedImportTypeRegexMatchers.pushallowedImportTypeMatchers.somematcher.ignoresallowedImportTypeRegexMatchers.someregex.testnode.specifiers.everynode.source.value.trimisAllowedTypeImportPathisAllowedTypeImportPatternrules.ImportDeclarationrules.ExportNamedDeclarationcheckImportNode
Internal Comments:
// Following how ignore is configured in the base rule
// As long as there's one matching pattern that allows type import (x4)
// @ts-expect-error -- parent types are incompatible but it's fine for the purposes of this extension (x2)
Code
create(context) {
const rules = baseRule.create(context);
const { options } = context;
if (!shouldCreateRule(rules, options)) {
return {};
}
const restrictedPaths = getRestrictedPaths(options);
const allowedTypeImportPathNameSet = new Set<string>();
for (const restrictedPath of restrictedPaths) {
if (
typeof restrictedPath === 'object' &&
restrictedPath.allowTypeImports
) {
allowedTypeImportPathNameSet.add(restrictedPath.name);
}
}
function isAllowedTypeImportPath(importSource: string): boolean {
return allowedTypeImportPathNameSet.has(importSource);
}
const restrictedPatterns = getRestrictedPatterns(options);
const allowedImportTypeMatchers: Ignore[] = [];
const allowedImportTypeRegexMatchers: RegExp[] = [];
for (const restrictedPattern of restrictedPatterns) {
if (
typeof restrictedPattern === 'object' &&
restrictedPattern.allowTypeImports
) {
// Following how ignore is configured in the base rule
if (restrictedPattern.group) {
allowedImportTypeMatchers.push(
ignore({
allowRelativePaths: true,
ignoreCase: !restrictedPattern.caseSensitive,
}).add(restrictedPattern.group),
);
}
if (restrictedPattern.regex) {
allowedImportTypeRegexMatchers.push(
new RegExp(
restrictedPattern.regex,
restrictedPattern.caseSensitive ? 'u' : 'iu',
),
);
}
}
}
function isAllowedTypeImportPattern(importSource: string): boolean {
return (
// As long as there's one matching pattern that allows type import
allowedImportTypeMatchers.some(matcher =>
matcher.ignores(importSource),
) ||
allowedImportTypeRegexMatchers.some(regex => regex.test(importSource))
);
}
function checkImportNode(node: TSESTree.ImportDeclaration): void {
if (
node.importKind === 'type' ||
(node.specifiers.length > 0 &&
node.specifiers.every(
specifier =>
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.importKind === 'type',
))
) {
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
!isAllowedTypeImportPattern(importSource)
) {
return rules.ImportDeclaration(node);
}
} else {
return rules.ImportDeclaration(node);
}
}
return {
ExportAllDeclaration: rules.ExportAllDeclaration,
'ExportNamedDeclaration[source]'(
node: {
source: NonNullable<TSESTree.ExportNamedDeclaration['source']>;
} & TSESTree.ExportNamedDeclaration,
): void {
if (
node.exportKind === 'type' ||
(node.specifiers.length > 0 &&
node.specifiers.every(specifier => specifier.exportKind === 'type'))
) {
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
!isAllowedTypeImportPattern(importSource)
) {
return rules.ExportNamedDeclaration(node);
}
} else {
return rules.ExportNamedDeclaration(node);
}
},
ImportDeclaration: checkImportNode,
TSImportEqualsDeclaration(
node: TSESTree.TSImportEqualsDeclaration,
): void {
if (
node.moduleReference.type === AST_NODE_TYPES.TSExternalModuleReference
) {
const synthesizedImport: TSESTree.ImportDeclaration = {
...node,
type: AST_NODE_TYPES.ImportDeclaration,
assertions: [],
attributes: [],
source: node.moduleReference.expression,
specifiers: [
{
...node.id,
type: AST_NODE_TYPES.ImportDefaultSpecifier,
local: node.id,
// @ts-expect-error -- parent types are incompatible but it's fine for the purposes of this extension
parent: node.id.parent,
},
],
};
return checkImportNode(synthesizedImport);
}
},
};
}
tryAccess(getter: () => T, fallback: T): T¶
Parameters:
getter() => TfallbackT
Returns: T
Calls:
getter
Code
isObjectOfPaths(obj: unknown): obj is { paths: ArrayOfStringOrObject }¶
Parameters:
objunknown
Returns: obj is { paths: ArrayOfStringOrObject }
Calls:
Object.hasOwn
Code
isObjectOfPatterns(obj: unknown): obj is { patterns: ArrayOfStringOrObjectPatterns }¶
Parameters:
objunknown
Returns: obj is { patterns: ArrayOfStringOrObjectPatterns }
Calls:
Object.hasOwn
Code
isOptionsArrayOfStringOrObject(options: Options): options is ArrayOfStringOrObject¶
Parameters:
optionsOptions
Returns: options is ArrayOfStringOrObject
Calls:
isObjectOfPathsisObjectOfPatterns
Code
getRestrictedPaths(options: Options): ArrayOfStringOrObject¶
Parameters:
optionsOptions
Returns: ArrayOfStringOrObject
Calls:
isOptionsArrayOfStringOrObjectisObjectOfPaths
Code
getRestrictedPatterns(options: Options): ArrayOfStringOrObjectPatterns¶
Parameters:
optionsOptions
Returns: ArrayOfStringOrObjectPatterns
Calls:
isObjectOfPatterns
Code
shouldCreateRule(baseRules: RuleListener, options: Options): baseRules is Exclude<RuleListener, Record<string, never>>¶
Parameters:
baseRulesRuleListeneroptionsOptions
Returns: baseRules is Exclude<RuleListener, Record<string, never>>
Calls:
Object.keysisOptionsArrayOfStringOrObject
Code
function shouldCreateRule(
baseRules: RuleListener,
options: Options,
): baseRules is Exclude<RuleListener, Record<string, never>> {
if (Object.keys(baseRules).length === 0 || options.length === 0) {
return false;
}
if (!isOptionsArrayOfStringOrObject(options)) {
return !!(options[0].paths?.length || options[0].patterns?.length);
}
return true;
}
Internal helpers¶
Declared inside another function in this file.
isAllowedTypeImportPath(importSource: string): boolean¶
Parameters:
importSourcestring
Returns: boolean
Calls:
allowedTypeImportPathNameSet.has
Code
isAllowedTypeImportPattern(importSource: string): boolean¶
Parameters:
importSourcestring
Returns: boolean
Calls:
allowedImportTypeMatchers.somematcher.ignoresallowedImportTypeRegexMatchers.someregex.test
Internal Comments:
Code
checkImportNode(node: TSESTree.ImportDeclaration): void¶
Parameters:
nodeTSESTree.ImportDeclaration
Returns: void
Calls:
node.specifiers.everynode.source.value.trimisAllowedTypeImportPathisAllowedTypeImportPatternrules.ImportDeclaration
Code
function checkImportNode(node: TSESTree.ImportDeclaration): void {
if (
node.importKind === 'type' ||
(node.specifiers.length > 0 &&
node.specifiers.every(
specifier =>
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.importKind === 'type',
))
) {
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
!isAllowedTypeImportPattern(importSource)
) {
return rules.ImportDeclaration(node);
}
} else {
return rules.ImportDeclaration(node);
}
}
Type Aliases¶
Options¶
MessageIds¶
Generated by Syntax Scribe