📄 no-require-imports¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 3 |
| 📦 Imports | 3 |
| 📑 Type Aliases | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/rules/no-require-imports.ts
📤 Default Export¶
| Property | Value |
|---|---|
name |
'no-require-imports' |
meta.type |
'problem' |
meta.docs.description |
'Disallow invocation of require()' |
meta.docs.recommended |
'recommended' |
meta.messages.noRequireImports |
'A require() style import is forbidden.' |
meta.schema |
[ { type: 'object', additionalProperties: false, properties: { allow: { type: 'array', description: 'Patterns of impo... |
defaultOptions |
[{ allow: [], allowAsImport: false }] |
Entry point: create — documented under Functions.
📦 Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
ASTUtils |
@typescript-eslint/utils |
Functions¶
create(context: any, options: any): { 'CallExpression[callee.name="require"]'(node: TSESTree.Ca…¶
Parameters:
contextanyoptionsany
Returns: { 'CallExpression[callee.name="require"]'(node: TSESTree.CallExpression): void; TSExternalModuleReference(node: any): void; }
Calls:
options[0].allow?.mapallowPatterns?.someimportPath.matchisStringOrTemplateLiteralutil.getStaticStringValueisImportPathAllowedASTUtils.findVariablecontext.sourceCode.getScopecontext.report
Internal Comments:
// ignore non-global require usage as it's something user-land custom instead
// of the commonjs standard
Code
create(context, options) {
const allowAsImport = options[0].allowAsImport;
const allowPatterns = options[0].allow?.map(
pattern => new RegExp(pattern, 'u'),
);
function isImportPathAllowed(importPath: string): boolean | undefined {
return allowPatterns?.some(pattern => importPath.match(pattern));
}
function isStringOrTemplateLiteral(node: TSESTree.Node): boolean {
return (
(node.type === AST_NODE_TYPES.Literal &&
typeof node.value === 'string') ||
node.type === AST_NODE_TYPES.TemplateLiteral
);
}
return {
'CallExpression[callee.name="require"]'(
node: TSESTree.CallExpression,
): void {
if (node.arguments[0] && isStringOrTemplateLiteral(node.arguments[0])) {
const argValue = util.getStaticStringValue(node.arguments[0]);
if (typeof argValue === 'string' && isImportPathAllowed(argValue)) {
return;
}
}
const variable = ASTUtils.findVariable(
context.sourceCode.getScope(node),
'require',
);
// ignore non-global require usage as it's something user-land custom instead
// of the commonjs standard
if (!variable?.identifiers.length) {
context.report({
node,
messageId: 'noRequireImports',
});
}
},
TSExternalModuleReference(node): void {
if (isStringOrTemplateLiteral(node.expression)) {
const argValue = util.getStaticStringValue(node.expression);
if (typeof argValue === 'string' && isImportPathAllowed(argValue)) {
return;
}
}
if (
allowAsImport &&
node.parent.type === AST_NODE_TYPES.TSImportEqualsDeclaration
) {
return;
}
context.report({
node,
messageId: 'noRequireImports',
});
},
};
}
Internal helpers¶
Declared inside another function in this file.
isImportPathAllowed(importPath: string): boolean | undefined¶
Parameters:
importPathstring
Returns: boolean | undefined
Calls:
allowPatterns?.someimportPath.match
Code
isStringOrTemplateLiteral(node: TSESTree.Node): boolean¶
Parameters:
nodeTSESTree.Node
Returns: boolean
Code
Type Aliases¶
Options¶
MessageIds¶
Generated by Syntax Scribe