Skip to content

⬅️ Back to Table of Contents

📄 typeDeclaredInPackageDeclarationFile

📊 Analysis Summary

Metric Count
🔧 Functions 4

📚 Table of Contents

🛠️ File Location:

📂 packages/type-utils/src/typeOrValueSpecifiers/typeDeclaredInPackageDeclarationFile.ts

Functions

typeDeclaredInPackageDeclarationFile(packageName: string, declarations: ts.Node[], declarationFiles: ts.SourceFile[], program: ts.Program): boolean

Parameters:

  • packageName string
  • declarations ts.Node[]
  • declarationFiles ts.SourceFile[]
  • program ts.Program

Returns: boolean

Calls:

  • typeDeclaredInDeclareModule
  • typeDeclaredInDeclarationFile
Code
export function typeDeclaredInPackageDeclarationFile(
  packageName: string,
  declarations: ts.Node[],
  declarationFiles: ts.SourceFile[],
  program: ts.Program,
): boolean {
  return (
    typeDeclaredInDeclareModule(packageName, declarations) ||
    typeDeclaredInDeclarationFile(packageName, declarationFiles, program)
  );
}

findParentModuleDeclaration(node: ts.Node): ts.ModuleDeclaration | undefined

Parameters:

  • node ts.Node

Returns: ts.ModuleDeclaration | undefined

Calls:

  • ts.isStringLiteral
  • findParentModuleDeclaration

Internal Comments:

// "namespace x {...}" should be ignored here

Code
function findParentModuleDeclaration(
  node: ts.Node,
): ts.ModuleDeclaration | undefined {
  switch (node.kind) {
    case ts.SyntaxKind.ModuleDeclaration:
      // "namespace x {...}" should be ignored here
      if (node.flags & ts.NodeFlags.Namespace) {
        break;
      }
      return ts.isStringLiteral((node as ts.ModuleDeclaration).name)
        ? (node as ts.ModuleDeclaration)
        : undefined;
    case ts.SyntaxKind.SourceFile:
      return undefined;
  }
  return findParentModuleDeclaration(node.parent);
}

typeDeclaredInDeclareModule(packageName: string, declarations: ts.Node[]): boolean

Parameters:

  • packageName string
  • declarations ts.Node[]

Returns: boolean

Calls:

  • declarations.some
  • findParentModuleDeclaration
Code
function typeDeclaredInDeclareModule(
  packageName: string,
  declarations: ts.Node[],
): boolean {
  return declarations.some(
    declaration =>
      findParentModuleDeclaration(declaration)?.name.text === packageName,
  );
}

typeDeclaredInDeclarationFile(packageName: string, declarationFiles: ts.SourceFile[], program: ts.Program): boolean

Parameters:

  • packageName string
  • declarationFiles ts.SourceFile[]
  • program ts.Program

Returns: boolean

Calls:

  • packageName.replace
  • declarationFiles.some
  • program.sourceFileToPackageName.get
  • matcher.test
  • program.isSourceFileFromExternalLibrary

Internal Comments:

// Handle scoped packages: if the name starts with @, remove it and replace / with __ (x2)

Code
function typeDeclaredInDeclarationFile(
  packageName: string,
  declarationFiles: ts.SourceFile[],
  program: ts.Program,
): boolean {
  // Handle scoped packages: if the name starts with @, remove it and replace / with __
  const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__');

  const matcher = new RegExp(`${packageName}|${typesPackageName}`);
  return declarationFiles.some(declaration => {
    const packageIdName = program.sourceFileToPackageName.get(declaration.path);
    return (
      packageIdName != null &&
      matcher.test(packageIdName) &&
      program.isSourceFileFromExternalLibrary(declaration)
    );
  });
}

Generated by Syntax Scribe