export function createStandardLibraryTypeResolver()

in server/src/standardLibrary/standardLibraryTypeResolver.ts [12:48]


export function createStandardLibraryTypeResolver(
    libraryDefinitions: PQLS.Library.LibraryDefinitions,
): PQLS.Inspection.ExternalType.TExternalTypeResolverFn {
    return (request: PQLS.Inspection.ExternalType.TExternalTypeRequest) => {
        const maybeLibraryType: Type.TPowerQueryType | undefined = libraryDefinitions.get(
            request.identifierLiteral,
        )?.asPowerQueryType;

        if (maybeLibraryType === undefined) {
            return undefined;
        }
        // It's asking for a value, which we already have.
        else if (request.kind === PQLS.Inspection.ExternalType.ExternalTypeRequestKind.Value) {
            return maybeLibraryType;
        } else {
            const key: string = TypeUtils.nameOf(maybeLibraryType);
            const maybeSmartTypeResolverFn: SmartTypeResolverFn | undefined = SmartTypeResolverFns.get(key);

            if (maybeSmartTypeResolverFn === undefined) {
                return undefined;
            }

            const typeChecked: TypeUtils.CheckedInvocation = TypeUtils.typeCheckInvocation(
                request.args,
                // If it's an invocation type then it's assumed we
                // already confirmed the request is about a DefinedFunction.
                TypeUtils.assertAsDefinedFunction(maybeLibraryType),
            );

            if (!isValidInvocation(typeChecked)) {
                return undefined;
            }

            return maybeSmartTypeResolverFn(request.args);
        }
    };
}