or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configurations.mdhelpers.mdindex.mdprocessors.mdrules.md
tile.json

helpers.mddocs/

Helpers

Utility functions for managing ESLint configurations, globals conversion, and cross-environment compatibility. These helpers provide essential functionality for configuration management and module loading.

Capabilities

Get Globals

Generic utility function for converting sources to ESLint globals object with proper typing.

function getGlobals<
  T extends Record<string, unknown> | string[],
  G extends Record<string, boolean>,
  R extends G = G & Record<T extends Array<infer R> ? R : keyof T, false>
>(sources: T, initialGlobals?: G): R;

The getGlobals function:

  • Converts arrays or objects to ESLint globals format
  • Sets all global variables to false (read-only)
  • Supports both string arrays and object keys
  • Preserves existing globals through initialGlobals parameter
  • Provides full TypeScript type safety

Usage Examples:

import { getGlobals } from "eslint-plugin-mdx";

// From array of strings
const globals1 = getGlobals(['React', 'process', 'global']);
// Result: { React: false, process: false, global: false }

// From object keys
const globals2 = getGlobals({ React: 'any', process: 'any' });
// Result: { React: false, process: false }

// With initial globals
const globals3 = getGlobals(['React'], { window: true });
// Result: { window: true, React: false }

// In ESLint config
export default [
  {
    languageOptions: {
      globals: getGlobals(['React', 'JSX'])
    }
  }
];

CommonJS Require

Cross-environment CommonJS require function for ESM compatibility.

declare const cjsRequire: CjsRequire;

interface CjsRequire {
  <T = any>(id: string): T;
  resolve(id: string): string;
}

The cjsRequire function:

  • Provides CommonJS require functionality in ESM modules
  • Uses createRequire when available (Node.js ESM)
  • Falls back to global require in CommonJS environments
  • Supports module resolution and loading
  • Used internally for dynamic module loading

Usage Examples:

import { cjsRequire } from "eslint-plugin-mdx";

// Load a CommonJS module
const packageJson = cjsRequire('./package.json');

// Resolve module path
const resolvedPath = cjsRequire.resolve('eslint');

// Load optional dependencies
try {
  const prettier = cjsRequire('prettier');
  // Use prettier if available
} catch {
  // Handle missing dependency
}

Type Definitions

// Generic helper types
type ArrayElement<T> = T extends Array<infer U> ? U : never;

type ObjectKeys<T> = T extends Record<string, unknown> ? keyof T : never;

// Globals type mapping
type GlobalsFromArray<T extends string[]> = Record<ArrayElement<T>, false>;

type GlobalsFromObject<T extends Record<string, unknown>> = Record<keyof T, false>;

// CommonJS require interface
interface CjsRequire {
  <T = any>(id: string): T;
  resolve(id: string): string;
}

Implementation Details

Cross-Environment Compatibility

The helpers are designed to work across different JavaScript environments:

  • ESM Modules: Uses import.meta.url and createRequire
  • CommonJS: Falls back to global require
  • TypeScript: Full type safety with generic constraints
  • Browser: Compatible with bundlers that polyfill Node.js APIs

Performance Considerations

  • Lazy Loading: Modules are loaded only when needed
  • Caching: Leverages Node.js module cache
  • Type Safety: Compile-time guarantees prevent runtime errors
  • Memory Efficient: Minimal overhead for utility functions

Advanced Usage:

// Custom globals with type safety
interface CustomGlobals {
  myGlobal: boolean;
  anotherGlobal: boolean;
}

const customGlobals: CustomGlobals = {
  myGlobal: true,
  anotherGlobal: false
};

// Merge with library globals
const allGlobals = getGlobals(
  ['React', 'process'], 
  customGlobals
);
// Type: CustomGlobals & { React: false, process: false }