Skip to content

⬅️ Back to Table of Contents

📄 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

export default util.createRule<Options, MessageIds>({ ... })
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:

  • context any
  • options any

Returns: { 'CallExpression[callee.name="require"]'(node: TSESTree.CallExpression): void; TSExternalModuleReference(node: any): void; }

Calls:

  • options[0].allow?.map
  • allowPatterns?.some
  • importPath.match
  • isStringOrTemplateLiteral
  • util.getStaticStringValue
  • isImportPathAllowed
  • ASTUtils.findVariable
  • context.sourceCode.getScope
  • context.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:

  • importPath string

Returns: boolean | undefined

Calls:

  • allowPatterns?.some
  • importPath.match
Code
function isImportPathAllowed(importPath: string): boolean | undefined {
      return allowPatterns?.some(pattern => importPath.match(pattern));
    }

isStringOrTemplateLiteral(node: TSESTree.Node): boolean

Parameters:

  • node TSESTree.Node

Returns: boolean

Code
function isStringOrTemplateLiteral(node: TSESTree.Node): boolean {
      return (
        (node.type === AST_NODE_TYPES.Literal &&
          typeof node.value === 'string') ||
        node.type === AST_NODE_TYPES.TemplateLiteral
      );
    }

Type Aliases

Options

type Options = [
  {
    allow?: string[];
    allowAsImport?: boolean;
  },
];

MessageIds

type MessageIds = 'noRequireImports';

Generated by Syntax Scribe