β¬ οΈ Back to Table of Contents
π no-restricted-types¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 5 |
| π¦ Imports | 5 |
| π Variables & Constants | 1 |
| π Type Aliases | 3 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-restricted-types.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'no-restricted-types' |
meta.type |
'suggestion' |
meta.docs.description |
'Disallow certain types' |
meta.fixable |
'code' |
meta.hasSuggestions |
true |
meta.messages.bannedTypeMessage |
"Don't use {{name}} as a type.{{customMessage}}" |
meta.messages.bannedTypeReplacement |
'Replace {{name}} with {{replacement}}.' |
meta.schema |
[ { type: 'object', $defs: { banConfig: { oneOf: [ { type: 'boolean', description: 'Bans the type with the default me... |
defaultOptions |
[{}] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESLint |
@typescript-eslint/utils |
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
createRule |
../util |
objectReduceKey |
../util |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
TYPE_KEYWORDS |
{ bigint: any; boolean: any; never: a... |
const | { bigint: AST_NODE_TYPES.TSBigIntKeyword, boolean: AST_NODE_TYPES.TSBooleanKe... |
β |
Functions¶
create(context: any, [{ types = {} }]: any): any¶
Parameters:
contextany[{ types = {} }]any
Returns: any
Calls:
Object.entries(types).mapremoveSpacesstringifyNodebannedTypes.getgetCustomMessagecontext.reportfixer.replaceTextsuggest?.mapobjectReduceKey (from ../util)bannedTypes.hascheckBannedTypes
Code
create(context, [{ types = {} }]) {
const bannedTypes = new Map(
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
);
function checkBannedTypes(
typeNode: TSESTree.Node,
name = stringifyNode(typeNode, context.sourceCode),
): void {
const bannedType = bannedTypes.get(name);
if (bannedType == null || bannedType === false) {
return;
}
const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
const suggest =
bannedType && typeof bannedType === 'object'
? bannedType.suggest
: undefined;
context.report({
node: typeNode,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
: null,
suggest: suggest?.map(replacement => ({
messageId: 'bannedTypeReplacement',
data: {
name,
replacement,
},
fix: (fixer): TSESLint.RuleFix =>
fixer.replaceText(typeNode, replacement),
})),
});
}
const keywordSelectors = objectReduceKey(
TYPE_KEYWORDS,
(acc: TSESLint.RuleListener, keyword) => {
if (bannedTypes.has(keyword)) {
acc[TYPE_KEYWORDS[keyword]] = (node: TSESTree.Node): void =>
checkBannedTypes(node, keyword);
}
return acc;
},
{},
);
return {
...keywordSelectors,
TSClassImplements(node): void {
checkBannedTypes(node.expression);
if (node.typeArguments) {
checkBannedTypes(node);
}
},
TSInterfaceHeritage(node): void {
checkBannedTypes(node.expression);
if (node.typeArguments) {
checkBannedTypes(node);
}
},
TSTupleType(node): void {
if (!node.elementTypes.length) {
checkBannedTypes(node);
}
},
TSTypeLiteral(node): void {
if (!node.members.length) {
checkBannedTypes(node);
}
},
TSTypeReference(node): void {
checkBannedTypes(node.typeName);
if (node.typeArguments) {
checkBannedTypes(node);
}
},
};
}
removeSpaces(str: string): string¶
Parameters:
strstring
Returns: string
Calls:
str.replaceAll
stringifyNode(node: TSESTree.Node, sourceCode: TSESLint.SourceCode): string¶
Parameters:
nodeTSESTree.NodesourceCodeTSESLint.SourceCode
Returns: string
Calls:
removeSpacessourceCode.getText
Code
getCustomMessage(bannedType: string | true | { fixWith?: string; mesβ¦): string¶
Parameters:
bannedTypestring | true | { fixWith?: string; message?: string } | null
Returns: string
Code
function getCustomMessage(
bannedType: string | true | { fixWith?: string; message?: string } | null,
): string {
if (!bannedType || bannedType === true) {
return '';
}
if (typeof bannedType === 'string') {
return ` ${bannedType}`;
}
if (bannedType.message) {
return ` ${bannedType.message}`;
}
return '';
}
Internal helpers¶
Declared inside another function in this file.
checkBannedTypes(typeNode: TSESTree.Node, name: string): void¶
Parameters:
typeNodeTSESTree.Nodenamestring
Returns: void
Calls:
bannedTypes.getgetCustomMessagecontext.reportfixer.replaceTextsuggest?.map
Code
function checkBannedTypes(
typeNode: TSESTree.Node,
name = stringifyNode(typeNode, context.sourceCode),
): void {
const bannedType = bannedTypes.get(name);
if (bannedType == null || bannedType === false) {
return;
}
const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
const suggest =
bannedType && typeof bannedType === 'object'
? bannedType.suggest
: undefined;
context.report({
node: typeNode,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
: null,
suggest: suggest?.map(replacement => ({
messageId: 'bannedTypeReplacement',
data: {
name,
replacement,
},
fix: (fixer): TSESLint.RuleFix =>
fixer.replaceText(typeNode, replacement),
})),
});
}
Type Aliases¶
Types¶
type Types = Record<
string,
| boolean
| string
| {
fixWith?: string;
message: string;
suggest?: readonly string[];
}
| null
>;
Options¶
MessageIds¶
Generated by Syntax Scribe