ESLint InterlaceESLint Interlace
Plugin: maintainabilityRules

consistent-function-scoping

ESLint rule documentation for consistent-function-scoping

๐Ÿ“ก Live from GitHub โ€” This documentation is fetched directly from consistent-function-scoping.md and cached for 6 hours.

Keywords: function scope, hoisting, nested functions, ESLint rule, performance, LLM-optimized

Move functions to the highest possible scope

Move functions to the highest possible scope. This rule is part of eslint-plugin-maintainability.

Quick Summary

AspectDetails
SeverityWarning (code quality)
Auto-FixโŒ No (requires manual move)
CategoryQuality
ESLint MCPโœ… Optimized for ESLint MCP integration
Best ForPerformance optimization, code organization

Rule Details

Functions that don't use variables from their parent scope should be moved to a higher scope.

Why This Matters

IssueImpactSolution
โšก PerformanceRecreated on each callMove to outer scope
๐Ÿ“– ReadabilityDeep nestingFlatten structure
๐Ÿงช TestabilityHard to test nested functionsExtract for testing

Examples

โŒ Incorrect

function outer() {
  // This function doesn't use any variables from outer
  function helper(x: number) {
    return x * 2;
  }
  
  return [1, 2, 3].map(helper);
}

โœ… Correct

// Moved to module scope
function helper(x: number) {
  return x * 2;
}

function outer() {
  return [1, 2, 3].map(helper);
}

// Or when closure is needed (correct nesting)
function createMultiplier(factor: number) {
  return function multiply(x: number) {  // Uses factor from parent
    return x * factor;
  };
}

Configuration Examples

Basic Usage

{
  rules: {
    'architecture/consistent-function-scoping': 'warn'
  }
}

Further Reading

Known False Negatives

The following patterns are not detected due to static analysis limitations:

Dynamic Variable References

Why: Static analysis cannot trace values stored in variables or passed through function parameters.

// โŒ NOT DETECTED - Value from variable
const value = externalSource();
processValue(value); // Variable origin not tracked

Mitigation: Implement runtime validation and review code manually. Consider using TypeScript branded types for validated inputs.

Imported Values

Why: When values come from imports, the rule cannot analyze their origin or construction.

// โŒ NOT DETECTED - Value from import
import { getValue } from './helpers';
processValue(getValue()); // Cross-file not tracked

Mitigation: Ensure imported values follow the same constraints. Use TypeScript for type safety.

On this page

No Headings