Skip to content

⬅️ Back to Table of Contents

📄 flat-config-schema

📊 Analysis Summary

Metric Count
🔧 Functions 10
🧱 Classes 4
📦 Imports 3
📊 Variables & Constants 12
📐 Interfaces 1
📑 Type Aliases 3

📚 Table of Contents

🛠️ File Location:

📂 packages/rule-tester/src/utils/flat-config-schema.ts

📦 Imports

Name Source
Processor @typescript-eslint/utils/ts-eslint
SharedConfig @typescript-eslint/utils/ts-eslint
normalizeSeverityToNumber ./severity

Variables & Constants

Name Type Kind Value Exported
ruleSeverities Map<SharedConfig.RuleLevel, SharedCon... const new Map<SharedConfig.RuleLevel, SharedConfig.Severity>([ ['error', 2], ['off'...
booleanSchema { merge: string; validate: string; } const { merge: 'replace', validate: 'boolean', } satisfies ObjectPropertySchema
ALLOWED_SEVERITIES Set<string \| number> const new Set([0, 1, 2, 'error', 'off', 'warn'])
disableDirectiveSeveritySchema ObjectPropertySchema<SharedConfig.Rul... const { merge( first: boolean \| SharedConfig.RuleLevel \| undefined, second: boole...
deepObjectAssignSchema { merge<First extends ObjectLike, Sec... const { merge<First extends ObjectLike, Second extends ObjectLike>( first = {} as F...
languageOptionsSchema { merge(first?: ObjectLike, second?: ... const { merge(first: ObjectLike = {}, second: ObjectLike = {}): object { const resu...
languageSchema ObjectPropertySchema<PluginMemberName> const { merge: 'replace', validate: assertIsPluginMemberName, }
pluginsSchema { merge(first?: ObjectLike, second?: ... const { merge(first: ObjectLike = {}, second: ObjectLike = {}): object { const keys...
processorSchema ObjectPropertySchema<Processor.LooseP... const { merge: 'replace', validate(value: unknown) { if (typeof value === 'string')...
rulesSchema { merge(first?: ConfigRules, second?:... const { merge(first: ConfigRules = {}, second: ConfigRules = {}): ConfigRules { con...
eslintrcKeys string[] const [ 'env', 'extends', 'globals', 'ignorePatterns', 'noInlineConfig', 'overrides...
flatConfigSchema { language: ObjectPropertySchema<${s...| const |, // Original ESLint schemas from flat-config-sc...`

Functions

isNonNullObject(value: unknown): boolean

Check if a value is a non-null object.

Parameters:

  • value any: The value to check.

Returns: undefined true if the value is a non-null object.

Raw JSDoc
/**
 * Check if a value is a non-null object.
 * @param value The value to check.
 * @returns `true` if the value is a non-null object.
 */

Internal Comments:

// eslint-disable-next-line eqeqeq, @typescript-eslint/internal/eqeq-nullish

Code
function isNonNullObject(value: unknown): boolean {
  // eslint-disable-next-line eqeqeq, @typescript-eslint/internal/eqeq-nullish
  return typeof value === 'object' && value !== null;
}

isNonArrayObject(value: unknown): boolean

Check if a value is a non-null non-array object.

Parameters:

  • value any: The value to check.

Returns: undefined true if the value is a non-null non-array object.

Raw JSDoc
/**
 * Check if a value is a non-null non-array object.
 * @param value The value to check.
 * @returns `true` if the value is a non-null non-array object.
 */

Calls:

  • isNonNullObject
  • Array.isArray
Code
function isNonArrayObject(value: unknown): boolean {
  return isNonNullObject(value) && !Array.isArray(value);
}

deepMerge(first: First, second: Second, mergeMap: Map<First | Second, Map<First | Second,…): First & Second

Deeply merges two non-array objects.

Parameters:

  • first any: The base object.
  • second any: The overrides object.
  • mergeMap any: Maps the combination of first and second arguments to a merged result.

Returns: undefined An object with properties from both first and second.

Raw JSDoc
/**
 * Deeply merges two non-array objects.
 * @param first The base object.
 * @param second The overrides object.
 * @param mergeMap Maps the combination of first and second arguments to a merged result.
 * @returns An object with properties from both first and second.
 */

Calls:

  • mergeMap.get
  • secondMergeMap.get
  • mergeMap.set
  • secondMergeMap.set
  • Object.keys
  • Object.prototype.propertyIsEnumerable.call
  • isNonArrayObject
  • deepMerge

Internal Comments:

// If this combination of first and second arguments has been already visited, return the previously created result.
/*
   * First create a result object where properties from the second object
   * overwrite properties from the first. This sets up a baseline to use
   * later rather than needing to inspect and change every property
   * individually.
   */ (x2)
// Store the pending result for this combination of first and second arguments. (x4)
// avoid hairy edge case
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion (x4)

Code
function deepMerge<First extends object, Second extends object>(
  first: First,
  second: Second,
  mergeMap = new Map<First | Second, Map<First | Second, First & Second>>(),
): First & Second {
  let secondMergeMap = mergeMap.get(first);

  if (secondMergeMap) {
    const result = secondMergeMap.get(second);

    if (result) {
      // If this combination of first and second arguments has been already visited, return the previously created result.
      return result;
    }
  } else {
    secondMergeMap = new Map();
    mergeMap.set(first, secondMergeMap);
  }

  /*
   * First create a result object where properties from the second object
   * overwrite properties from the first. This sets up a baseline to use
   * later rather than needing to inspect and change every property
   * individually.
   */
  const result = {
    ...first,
    ...second,
  } as First & ObjectLike & Second;

  delete (result as ObjectLike).__proto__; // don't merge own property "__proto__"

  // Store the pending result for this combination of first and second arguments.
  secondMergeMap.set(second, result);

  for (const key of Object.keys(second)) {
    // avoid hairy edge case
    if (
      key === '__proto__' ||
      !Object.prototype.propertyIsEnumerable.call(first, key)
    ) {
      continue;
    }

    const firstValue = (first as ObjectLike)[key] as object | undefined;
    const secondValue = (second as ObjectLike)[key] as object | undefined;

    if (isNonArrayObject(firstValue) && isNonArrayObject(secondValue)) {
      (result as ObjectLike)[key] = deepMerge(
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        firstValue!,
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        secondValue!,
        mergeMap,
      );
      // eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
    } else if (secondValue === undefined) {
      (result as ObjectLike)[key] = firstValue;
    }
  }

  return result;
}

normalizeRuleOptions(ruleOptions: SharedConfig.RuleLevel | SharedConfig.R…): SharedConfig.RuleLevelAndOptions

Normalizes the rule options config for a given rule by ensuring that it is an array and that the first item is 0, 1, or 2.

Parameters:

  • ruleOptions any: The rule options config.

Returns: undefined An array of rule options.

Raw JSDoc
/**
 * Normalizes the rule options config for a given rule by ensuring that
 * it is an array and that the first item is 0, 1, or 2.
 * @param ruleOptions The rule options config.
 * @returns An array of rule options.
 */

Calls:

  • Array.isArray
  • ruleSeverities.get
  • structuredClone
Code
function normalizeRuleOptions(
  ruleOptions: SharedConfig.RuleLevel | SharedConfig.RuleLevelAndOptions,
): SharedConfig.RuleLevelAndOptions {
  const finalOptions = Array.isArray(ruleOptions)
    ? [...ruleOptions]
    : [ruleOptions];

  finalOptions[0] = ruleSeverities.get(
    finalOptions[0] as SharedConfig.RuleLevel,
  );

  return structuredClone(finalOptions as SharedConfig.RuleLevelAndOptions);
}

hasMethod(object: Record<string, unknown>): boolean

Determines if an object has any methods.

Parameters:

  • object any: The object to check.

Returns: undefined true if the object has any methods.

Raw JSDoc
/**
 * Determines if an object has any methods.
 * @param object The object to check.
 * @returns `true` if the object has any methods.
 */

Calls:

  • Object.keys
Code
function hasMethod(object: Record<string, unknown>): boolean {
  for (const key of Object.keys(object)) {
    if (typeof object[key] === 'function') {
      return true;
    }
  }

  return false;
}

assertIsRuleOptions(ruleId: string, value: unknown): void

Validates that a value is a valid rule options entry.

Parameters:

  • ruleId any: Rule name being configured.
  • value any: The value to check.

Throws:

  • InvalidRuleOptionsError: If the value isn't a valid rule options.
Raw JSDoc
/**
 * Validates that a value is a valid rule options entry.
 * @param ruleId Rule name being configured.
 * @param value The value to check.
 * @throws {InvalidRuleOptionsError} If the value isn't a valid rule options.
 */

Calls:

  • Array.isArray
Code
function assertIsRuleOptions(ruleId: string, value: unknown): void {
  if (
    typeof value !== 'string' &&
    typeof value !== 'number' &&
    !Array.isArray(value)
  ) {
    throw new InvalidRuleOptionsError(ruleId, value);
  }
}

assertIsRuleSeverity(ruleId: string, value: unknown): void

Validates that a value is valid rule severity.

Parameters:

  • ruleId any: Rule name being configured.
  • value any: The value to check.

Throws:

  • InvalidRuleSeverityError: If the value isn't a valid rule severity.
Raw JSDoc
/**
 * Validates that a value is valid rule severity.
 * @param ruleId Rule name being configured.
 * @param value The value to check.
 * @throws {InvalidRuleSeverityError} If the value isn't a valid rule severity.
 */

Calls:

  • ruleSeverities.get
Code
function assertIsRuleSeverity(ruleId: string, value: unknown): void {
  const severity = ruleSeverities.get(value as SharedConfig.RuleLevel);

  if (severity == null) {
    throw new InvalidRuleSeverityError(ruleId, value);
  }
}

assertIsPluginMemberName(value: unknown): asserts value is PluginMemberName

Validates that a given string is the form pluginName/objectName.

Parameters:

  • value any: The string to check.
Raw JSDoc
/**
 * Validates that a given string is the form pluginName/objectName.
 * @param value The string to check.
 */

Calls:

  • /[@\w$-]+(?:\/[\w$-]+)+$/iu.test

Internal Comments:

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions (x2)

Code
function assertIsPluginMemberName(
  value: unknown,
): asserts value is PluginMemberName {
  if (typeof value !== 'string' || !/[@\w$-]+(?:\/[\w$-]+)+$/iu.test(value)) {
    throw new TypeError(
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
      `Expected string in the form "pluginName/objectName" but found "${value}".`,
    );
  }
}

assertIsObject(value: unknown): void

Validates that a value is an object.

Parameters:

  • value any: The value to check.
Raw JSDoc
/**
 * Validates that a value is an object.
 * @param value The value to check.
 */

Calls:

  • isNonNullObject
Code
function assertIsObject(value: unknown): void {
  if (!isNonNullObject(value)) {
    throw new TypeError('Expected an object.');
  }
}

createEslintrcErrorSchema(key: string): ObjectPropertySchema

Creates a schema that always throws an error. Useful for warning about eslintrc-style keys.

Parameters:

  • key any: The eslintrc key to create a schema for.
Raw JSDoc
/**
 * Creates a schema that always throws an error. Useful for warning
 * about eslintrc-style keys.
 * @param key The eslintrc key to create a schema for.
 */
Code
function createEslintrcErrorSchema(key: string): ObjectPropertySchema {
  return {
    merge: 'replace',
    validate(): void {
      throw new IncompatibleKeyError(key);
    },
  };
}

Classes

InvalidRuleOptionsError

The error type when a rule's options are configured with an invalid type.

Extends: Error

Class Code
class InvalidRuleOptionsError extends Error {
  readonly messageData: { ruleId: string; value: unknown };
  readonly messageTemplate: string;

  constructor(ruleId: string, value: unknown) {
    super(
      `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`,
    );
    this.messageTemplate = 'invalid-rule-options';
    this.messageData = { ruleId, value };
  }
}

InvalidRuleSeverityError

The error type when a rule's severity is invalid.

Extends: Error

Class Code
class InvalidRuleSeverityError extends Error {
  readonly messageData: { ruleId: string; value: unknown };
  readonly messageTemplate: string;

  constructor(ruleId: string, value: unknown) {
    super(
      `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`,
    );
    this.messageTemplate = 'invalid-rule-severity';
    this.messageData = { ruleId, value };
  }
}

IncompatibleKeyError

The error type when there's an eslintrc-style options in a flat config.

Extends: Error

Class Code
class IncompatibleKeyError extends Error {
  readonly messageData: { key: string };
  readonly messageTemplate: string;

  /**
   * @param key The invalid key.
   */
  constructor(key: string) {
    super(
      'This appears to be in eslintrc format rather than flat config format.',
    );
    this.messageTemplate = 'eslintrc-incompat';
    this.messageData = { key };
  }
}

IncompatiblePluginsError

The error type when there's an eslintrc-style plugins array found.

Extends: Error

Class Code
class IncompatiblePluginsError extends Error {
  readonly messageData: { plugins: string[] };
  readonly messageTemplate: string;

  constructor(plugins: string[]) {
    super(
      'This appears to be in eslintrc format (array of strings) rather than flat config format (object).',
    );
    this.messageTemplate = 'eslintrc-plugins';
    this.messageData = { plugins };
  }
}

Interfaces

ObjectPropertySchema<T = unknown>

Interface Code
interface ObjectPropertySchema<T = unknown> {
  merge: string | ((a: T, b: T) => T);
  validate: string | ((value: unknown) => asserts value is T);
}

Properties

Name Type Optional Description
merge string \| ((a: T, b: T) => T) not shown
validate string \| ((value: unknown) => asserts value is T) not shown

Type Aliases

PluginMemberName

type PluginMemberName = `${string}/${string}`;

ObjectLike

type ObjectLike = Record<string, unknown>;

ConfigRules

type ConfigRules = Record<string, SharedConfig.RuleLevelAndOptions>;

Generated by Syntax Scribe