Skip to content

⬅️ Back to Table of Contents

📄 no-var-requires

📊 Analysis Summary

Metric Count
🔧 Functions 3
📦 Imports 5
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/no-var-requires.ts

📤 Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-var-requires'
meta.type 'problem'
meta.deprecated.deprecatedSince '8.0.0'
meta.deprecated.replacedBy [ { rule: { name: '@typescript-eslint/no-require-imports', url: 'https://typescript-eslint.io/rules/no-require-import...
meta.deprecated.url 'https://github.com/typescript-eslint/typescript-eslint/pull/8334'
meta.docs.description 'Disallow require statements except in import statements'
meta.messages.noVarReqs 'Require statement not part of import statement.'
meta.replacedBy ['@typescript-eslint/no-require-imports']
meta.schema [ { type: 'object', additionalProperties: false, properties: { allow: { type: 'array', description: 'Patterns of impo...
defaultOptions [{ allow: [] }]

Entry point: create — documented under Functions.


📦 Imports

Name Source
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
ASTUtils @typescript-eslint/utils
createRule ../util
getStaticStringValue ../util

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; }

Calls:

  • options[0].allow.map
  • allowPatterns.some
  • importPath.match
  • isStringOrTemplateLiteral
  • getStaticStringValue (from ../util)
  • isImportPathAllowed
  • [ AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.MemberExpression, AST_NODE_TYPES.NewExpression, AST_NODE_TYPES.TSAsExpression, AST_NODE_TYPES.TSTypeAssertion, AST_NODE_TYPES.VariableDeclarator, ].includes
  • ASTUtils.findVariable
  • context.sourceCode.getScope
  • context.report
Code
create(context, options) {
    const allowPatterns = options[0].allow.map(
      pattern => new RegExp(pattern, 'u'),
    );
    function isImportPathAllowed(importPath: string): boolean {
      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 = getStaticStringValue(node.arguments[0]);
          if (typeof argValue === 'string' && isImportPathAllowed(argValue)) {
            return;
          }
        }
        const parent =
          node.parent.type === AST_NODE_TYPES.ChainExpression
            ? node.parent.parent
            : node.parent;

        if (
          [
            AST_NODE_TYPES.CallExpression,
            AST_NODE_TYPES.MemberExpression,
            AST_NODE_TYPES.NewExpression,
            AST_NODE_TYPES.TSAsExpression,
            AST_NODE_TYPES.TSTypeAssertion,
            AST_NODE_TYPES.VariableDeclarator,
          ].includes(parent.type)
        ) {
          const variable = ASTUtils.findVariable(
            context.sourceCode.getScope(node),
            'require',
          );

          if (!variable?.identifiers.length) {
            context.report({
              node,
              messageId: 'noVarReqs',
            });
          }
        }
      },
    };
  }

Internal helpers

Declared inside another function in this file.

isImportPathAllowed(importPath: string): boolean

Parameters:

  • importPath string

Returns: boolean

Calls:

  • allowPatterns.some
  • importPath.match
Code
function isImportPathAllowed(importPath: string): boolean {
      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[];
  },
];

MessageIds

type MessageIds = 'noVarReqs';

Generated by Syntax Scribe