Skip to content

The open alternative to Salesforce, designed for AI.

1.9 KB
Raw Blame
import ts from 'typescript';

import { isDefined } from '../src/utilities/utils/isDefined';

const COMPONENT_NAME_PATTERN = /^[A-Z][a-z]/;
const COMPONENT_SOURCE_PATTERN = /\.(tsx|d\.ts)$/;

export const getPublicComponentExports = ({
  checker,
  entryPoints,
}: {
  checker: ts.TypeChecker;
  entryPoints: { name: string; source: ts.SourceFile }[];
}) => {
  const components = new Map<
    string,
    { name: string; entryPoints: string[]; symbol: ts.Symbol }
  >();

  for (const { name: entryPoint, source } of entryPoints) {
    const moduleSymbol = checker.getSymbolAtLocation(source);

    if (!isDefined(moduleSymbol)) {
      throw new Error(`Could not read exports from ${entryPoint}`);
    }

    for (const exported of checker.getExportsOfModule(moduleSymbol)) {
      const symbol =
        exported.flags & ts.SymbolFlags.Alias
          ? checker.getAliasedSymbol(exported)
          : exported;
      const declaration = symbol.valueDeclaration;

      if (
        !isDefined(declaration) ||
        !COMPONENT_NAME_PATTERN.test(exported.name) ||
        !COMPONENT_SOURCE_PATTERN.test(declaration.getSourceFile().fileName)
      ) {
        continue;
      }

      const type = checker.getTypeOfSymbolAtLocation(symbol, declaration);
      const isCallable = type.getCallSignatures().length > 0;
      const hasComponentParts = type
        .getProperties()
        .some(
          (part) =>
            COMPONENT_NAME_PATTERN.test(part.name) &&
            checker
              .getTypeOfSymbolAtLocation(part, declaration)
              .getCallSignatures().length > 0,
        );

      if (isCallable || hasComponentParts) {
        const component = components.get(exported.name) ?? {
          name: exported.name,
          entryPoints: [],
          symbol,
        };

        component.entryPoints.push(entryPoint);
        components.set(exported.name, component);
      }
    }
  }

  return [...components.values()];
};