Skip to content

⬅️ Back to Table of Contents

📄 test-utils.ts

📊 Analysis Summary

Metric Count
🔧 Functions 8
📦 Imports 3
📊 Variables & Constants 3
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/typescript-estree/tests/test-utils/test-utils.ts

📦 Imports

Name Source
ParseAndGenerateServicesResult ../../src
TSESTreeOptions ../../src
parseAndGenerateServices ../../src

Variables & Constants

Name Type Kind Value Exported
node { [x: string]: unknown; } const { ...oNode }
child UnknownObject | UnknownObject[] const node[prop] as UnknownObject | UnknownObject[]
value any[] const []

Functions

parseCodeAndGenerateServices(code: string, config: TSESTreeOptions): ParseAndGenerateServicesResult<TSESTreeOptions>

Code
export function parseCodeAndGenerateServices(
  code: string,
  config: TSESTreeOptions,
): ParseAndGenerateServicesResult<TSESTreeOptions> {
  return parseAndGenerateServices(code, config);
}
  • Parameters:
  • code: string
  • config: TSESTreeOptions
  • Return Type: ParseAndGenerateServicesResult<TSESTreeOptions>
  • Calls:
  • parseAndGenerateServices (from ../../src)

formatSnapshotName(filename: string, fixturesDir: string, fileExtension: string): string

Code
export function formatSnapshotName(
  filename: string,
  fixturesDir: string,
  fileExtension = '.js',
): string {
  return `fixtures/${filename
    .replace(`${fixturesDir}/`, '')
    .replace(fileExtension, '')}`;
}
  • Parameters:
  • filename: string
  • fixturesDir: string
  • fileExtension: string
  • Return Type: string
  • Calls:
  • filename .replace(${fixturesDir}/, '') .replace

isJSXFileType(fileType: string): boolean

Code
export function isJSXFileType(fileType: string): boolean {
  if (fileType.startsWith('.')) {
    fileType = fileType.slice(1);
  }
  return fileType === 'js' || fileType === 'jsx' || fileType === 'tsx';
}
  • JSDoc:

    /**
     * Check if file extension is one used for jsx
     */
    

  • Parameters:

  • fileType: string
  • Return Type: boolean
  • Calls:
  • fileType.startsWith
  • fileType.slice

deeplyCopy(ast: T): T

Code
export function deeplyCopy<T extends NonNullable<unknown>>(ast: T): T {
  return omitDeep(ast) as T;
}
  • JSDoc:

    /**
     * Returns a raw copy of the typescript AST
     * @param ast the AST object
     * @returns copy of the AST object
     */
    

  • Parameters:

  • ast: T
  • Return Type: T
  • Calls:
  • omitDeep

isObjectLike(value: unknown): boolean

Code
function isObjectLike(value: unknown): boolean {
  return (
    typeof value === 'object' && !(value instanceof RegExp) && value != null
  );
}
  • Parameters:
  • value: unknown
  • Return Type: boolean

`omitDeep(root: UnknownObject, keysToOmit: { key: string; predicate: (value: unknown) => boolean }[], selectors: Record<

string,
(node: UnknownObject, parent: UnknownObject | null) => void

): UnknownObject`

Code
export function omitDeep(
  root: UnknownObject,
  keysToOmit: { key: string; predicate: (value: unknown) => boolean }[] = [],
  selectors: Record<
    string,
    (node: UnknownObject, parent: UnknownObject | null) => void
  > = {},
): UnknownObject {
  function shouldOmit(keyName: string, val: unknown): boolean {
    if (keysToOmit.length) {
      return keysToOmit.some(
        keyConfig => keyConfig.key === keyName && keyConfig.predicate(val),
      );
    }
    return false;
  }

  function visit(
    oNode: UnknownObject,
    parent: UnknownObject | null,
  ): UnknownObject {
    if (!Array.isArray(oNode) && !isObjectLike(oNode)) {
      return oNode;
    }

    const node = { ...oNode };

    for (const prop in node) {
      if (Object.hasOwn(node, prop)) {
        // eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
        if (shouldOmit(prop, node[prop]) || node[prop] === undefined) {
          // Filter out omitted and undefined props from the node
          // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
          delete node[prop];
          continue;
        }

        const child = node[prop] as UnknownObject | UnknownObject[];
        if (Array.isArray(child)) {
          const value = [];
          for (const el of child) {
            value.push(visit(el, node));
          }
          node[prop] = value;
        } else if (isObjectLike(child)) {
          node[prop] = visit(child, node);
        }
      }
    }

    if (typeof node.type === 'string' && node.type in selectors) {
      selectors[node.type](node, parent);
    }

    return node;
  }

  return visit(root, null);
}
  • JSDoc:

    /**
     * Removes the given keys from the given AST object recursively
     * @param root A JavaScript object to remove keys from
     * @param keysToOmit Names and predicate functions use to determine what keys to omit from the final object
     * @param selectors advance ast modifications
     * @returns formatted object
     */
    

  • Parameters:

  • root: UnknownObject
  • keysToOmit: { key: string; predicate: (value: unknown) => boolean }[]
  • `selectors: Record< string, (node: UnknownObject, parent: UnknownObject | null) => void

    `

  • Return Type: UnknownObject
  • Calls:
  • keysToOmit.some
  • keyConfig.predicate
  • Array.isArray
  • isObjectLike
  • Object.hasOwn
  • shouldOmit
  • value.push
  • visit
  • complex_call_3100
  • Internal Comments:
    // eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
    // Filter out omitted and undefined props from the node (x2)
    // eslint-disable-next-line @typescript-eslint/no-dynamic-delete (x2)
    

shouldOmit(keyName: string, val: unknown): boolean

Code
function shouldOmit(keyName: string, val: unknown): boolean {
    if (keysToOmit.length) {
      return keysToOmit.some(
        keyConfig => keyConfig.key === keyName && keyConfig.predicate(val),
      );
    }
    return false;
  }
  • Parameters:
  • keyName: string
  • val: unknown
  • Return Type: boolean
  • Calls:
  • keysToOmit.some
  • keyConfig.predicate

visit(oNode: UnknownObject, parent: UnknownObject | null): UnknownObject

Code
function visit(
    oNode: UnknownObject,
    parent: UnknownObject | null,
  ): UnknownObject {
    if (!Array.isArray(oNode) && !isObjectLike(oNode)) {
      return oNode;
    }

    const node = { ...oNode };

    for (const prop in node) {
      if (Object.hasOwn(node, prop)) {
        // eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
        if (shouldOmit(prop, node[prop]) || node[prop] === undefined) {
          // Filter out omitted and undefined props from the node
          // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
          delete node[prop];
          continue;
        }

        const child = node[prop] as UnknownObject | UnknownObject[];
        if (Array.isArray(child)) {
          const value = [];
          for (const el of child) {
            value.push(visit(el, node));
          }
          node[prop] = value;
        } else if (isObjectLike(child)) {
          node[prop] = visit(child, node);
        }
      }
    }

    if (typeof node.type === 'string' && node.type in selectors) {
      selectors[node.type](node, parent);
    }

    return node;
  }
  • Parameters:
  • oNode: UnknownObject
  • parent: UnknownObject | null
  • Return Type: UnknownObject
  • Calls:
  • Array.isArray
  • isObjectLike
  • Object.hasOwn
  • shouldOmit
  • value.push
  • visit
  • complex_call_3100
  • Internal Comments:
    // eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
    // Filter out omitted and undefined props from the node (x2)
    // eslint-disable-next-line @typescript-eslint/no-dynamic-delete (x2)
    

Type Aliases

UnknownObject

type UnknownObject = Record<string, unknown>;