Skip to content

⬅️ Back to Table of Contents

📄 TypeOrValueSpecifier

📊 Analysis Summary

Metric Count
🔧 Functions 6
📦 Imports 7
📊 Variables & Constants 1
📐 Interfaces 3
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/type-utils/src/TypeOrValueSpecifier.ts

📦 Imports

Name Source
TSESTree @typescript-eslint/types
JSONSchema4 @typescript-eslint/utils/json-schema
AST_NODE_TYPES @typescript-eslint/types
specifierNameMatches ./typeOrValueSpecifiers/specifierNameMatches
typeDeclaredInFile ./typeOrValueSpecifiers/typeDeclaredInFile
typeDeclaredInLib ./typeOrValueSpecifiers/typeDeclaredInLib
typeDeclaredInPackageDeclarationFile ./typeOrValueSpecifiers/typeDeclaredInPackageDeclarationFile

Variables & Constants

Name Type Kind Value Exported
typeOrValueSpecifiersSchema JSONSchema4 const { items: { oneOf: [ { type: 'string', }, { additionalProperties: false, prope...

Functions

typeMatchesSpecifier(type: ts.Type, specifier: TypeOrValueSpecifier, program: ts.Program): boolean

Parameters:

  • type ts.Type
  • specifier TypeOrValueSpecifier
  • program ts.Program

Returns: boolean

Calls:

  • tsutils.isUnionType
  • type.types.every
  • typeMatchesSpecifier
  • complex_call_4343
  • tsutils.isIntrinsicErrorType
  • specifierNameMatches (from ./typeOrValueSpecifiers/specifierNameMatches)
  • type.getSymbol
  • symbol?.getDeclarations
  • declarations.map
  • declaration.getSourceFile
  • typeDeclaredInFile (from ./typeOrValueSpecifiers/typeDeclaredInFile)
  • typeDeclaredInLib (from ./typeOrValueSpecifiers/typeDeclaredInLib)
  • typeDeclaredInPackageDeclarationFile (from ./typeOrValueSpecifiers/typeDeclaredInPackageDeclarationFile)
  • tsutils.isIntersectionType
  • tsutils .intersectionConstituents(type) .some
Code
export function typeMatchesSpecifier(
  type: ts.Type,
  specifier: TypeOrValueSpecifier,
  program: ts.Program,
): boolean {
  if (tsutils.isUnionType(type)) {
    return type.types.every(t => typeMatchesSpecifier(t, specifier, program));
  }

  const wholeTypeMatches = ((): boolean => {
    if (tsutils.isIntrinsicErrorType(type)) {
      return false;
    }
    if (typeof specifier === 'string') {
      return specifierNameMatches(type, specifier);
    }
    if (!specifierNameMatches(type, specifier.name)) {
      return false;
    }
    const symbol = type.getSymbol() ?? type.aliasSymbol;
    const declarations = symbol?.getDeclarations() ?? [];
    const declarationFiles = declarations.map(declaration =>
      declaration.getSourceFile(),
    );
    switch (specifier.from) {
      case 'file':
        return typeDeclaredInFile(specifier.path, declarationFiles, program);
      case 'lib':
        return typeDeclaredInLib(declarationFiles, program);
      case 'package':
        return typeDeclaredInPackageDeclarationFile(
          specifier.package,
          declarations,
          declarationFiles,
          program,
        );
    }
  })();

  if (wholeTypeMatches) {
    return true;
  }

  if (
    tsutils.isIntersectionType(type) &&
    tsutils
      .intersectionConstituents(type)
      .some(part => typeMatchesSpecifier(part, specifier, program))
  ) {
    return true;
  }

  return false;
}

typeMatchesSomeSpecifier(type: ts.Type, specifiers: TypeOrValueSpecifier[], program: ts.Program): boolean

Parameters:

  • type ts.Type
  • specifiers TypeOrValueSpecifier[]
  • program ts.Program

Returns: boolean

Calls:

  • specifiers.some
Code
(
  type: ts.Type,
  specifiers: TypeOrValueSpecifier[] = [],
  program: ts.Program,
): boolean =>
  specifiers.some(specifier => typeMatchesSpecifier(type, specifier, program))

valueMatchesSpecifier(node: TSESTree.Node, specifier: TypeOrValueSpecifier, program: ts.Program, type: ts.Type): boolean

Parameters:

  • node TSESTree.Node
  • specifier TypeOrValueSpecifier
  • program ts.Program
  • type ts.Type

Returns: boolean

Calls:

  • getStaticName
  • getSpecifierNames(specifier.name).includes
  • type.getSymbol
  • symbol?.getDeclarations
  • declarations.map
  • declaration.getSourceFile
  • typeDeclaredInPackageDeclarationFile (from ./typeOrValueSpecifiers/typeDeclaredInPackageDeclarationFile)
Code
export function valueMatchesSpecifier(
  node: TSESTree.Node,
  specifier: TypeOrValueSpecifier,
  program: ts.Program,
  type: ts.Type,
): boolean {
  const staticName = getStaticName(node);
  if (!staticName) {
    return false;
  }

  if (typeof specifier === 'string') {
    return specifier === staticName;
  }

  if (!getSpecifierNames(specifier.name).includes(staticName)) {
    return false;
  }

  if (specifier.from === 'package') {
    const symbol = type.getSymbol() ?? type.aliasSymbol;
    const declarations = symbol?.getDeclarations() ?? [];
    const declarationFiles = declarations.map(declaration =>
      declaration.getSourceFile(),
    );
    return typeDeclaredInPackageDeclarationFile(
      specifier.package,
      declarations,
      declarationFiles,
      program,
    );
  }

  return true;
}

valueMatchesSomeSpecifier(node: TSESTree.Node, specifiers: TypeOrValueSpecifier[], program: ts.Program, type: ts.Type): boolean

Parameters:

  • node TSESTree.Node
  • specifiers TypeOrValueSpecifier[]
  • program ts.Program
  • type ts.Type

Returns: boolean

Calls:

  • specifiers.some
Code
(
  node: TSESTree.Node,
  specifiers: TypeOrValueSpecifier[] = [],
  program: ts.Program,
  type: ts.Type,
): boolean =>
  specifiers.some(specifier =>
    valueMatchesSpecifier(node, specifier, program, type),
  )

getSpecifierNames(specifierName: string | string[]): string[]

Parameters:

  • specifierName string | string[]

Returns: string[]

Code
(specifierName: string | string[]): string[] => {
  return typeof specifierName === 'string' ? [specifierName] : specifierName;
}

getStaticName(node: TSESTree.Node): string | undefined

Parameters:

  • node TSESTree.Node

Returns: string | undefined

Code
(node: TSESTree.Node): string | undefined => {
  if (
    node.type === AST_NODE_TYPES.Identifier ||
    node.type === AST_NODE_TYPES.JSXIdentifier ||
    node.type === AST_NODE_TYPES.PrivateIdentifier
  ) {
    return node.name;
  }

  if (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') {
    return node.value;
  }

  return undefined;
}

Interfaces

FileSpecifier

Interface Code
export interface FileSpecifier {
  from: 'file';

  /**
   * Type or value name(s) to match on.
   */
  name: string | string[];

  /**
   * A specific file the types or values must be declared in.
   */
  path?: string;
}

Properties

Name Type Optional Description
from 'file' not shown
name string \| string[] not shown
path string not shown

LibSpecifier

Interface Code
export interface LibSpecifier {
  from: 'lib';

  /**
   * Type or value name(s) to match on.
   */
  name: string | string[];
}

Properties

Name Type Optional Description
from 'lib' not shown
name string \| string[] not shown

PackageSpecifier

Interface Code
export interface PackageSpecifier {
  from: 'package';

  /**
   * Type or value name(s) to match on.
   */
  name: string | string[];

  /**
   * Package name the type or value must be declared in.
   */
  package: string;
}

Properties

Name Type Optional Description
from 'package' not shown
name string \| string[] not shown
package string not shown

Type Aliases

TypeOrValueSpecifier

/* * A centralized format for rule options to describe specific types and/or values. * See TypeOrValueSpecifier. /

type TypeOrValueSpecifier = string | FileSpecifier | LibSpecifier | PackageSpecifier;

Generated by Syntax Scribe