UNPKG

@jfconley/di-compiler

Version:

A Custom Transformer for Typescript that enables compile-time Dependency Injection

79 lines (78 loc) 5.74 kB
import { TransformOptions, TransformResult } from "./transform-options.js"; import { TS } from "./type.js"; import { DiOptions } from "./di-options.js"; import { BaseVisitorContext, VisitorContext } from "./visitor-context.js"; import { VisitorOptions } from "./visitor-options.js"; import { ImportedSymbol } from "./imported-symbol.js"; /** * CustomTransformer that associates constructor arguments with any given class declaration */ declare function transform(source: string, options?: TransformOptions): TransformResult; declare function transform(source: string, filename: string, options?: TransformOptions): TransformResult; /** * CustomTransformer that associates constructor arguments with any given class declaration */ declare function di(options: DiOptions): TS.CustomTransformers; declare function getBaseVisitorContext({ typescript, ...rest }?: DiOptions): BaseVisitorContext; declare function afterTransformer(context: BaseVisitorContext): TS.TransformerFactory<TS.SourceFile>; type RootBlock = TS.Node & { statements: TS.NodeArray<TS.Statement>; }; interface AfterVisitorOptions<T extends TS.Node> extends VisitorOptions<T> { defineArrayLiteralExpression: TS.ArrayLiteralExpression | undefined; rootBlock: RootBlock; } declare function visitNode<T extends TS.Node>(options: AfterVisitorOptions<T>): TS.VisitResult<TS.Node>; declare function visitDefineArrayLiteralExpression(options: AfterVisitorOptions<TS.ArrayLiteralExpression>): TS.ArrayLiteralExpression; declare function visitRootBlockBlock(options: AfterVisitorOptions<TS.Block>): TS.VisitResult<TS.Node>; declare function visitRootBlockSourceFile(options: AfterVisitorOptions<TS.SourceFile>): TS.VisitResult<TS.Node>; declare function visitRootBlock(options: AfterVisitorOptions<RootBlock>): TS.Statement[]; declare function beforeTransformer(context: BaseVisitorContext): TS.TransformerFactory<TS.SourceFile>; declare function transformSourceFile(sourceFile: TS.SourceFile, context: VisitorContext): TS.SourceFile; interface BeforeVisitorOptions<T extends TS.Node> extends VisitorOptions<T> { requireImportedSymbol(importedSymbol: ImportedSymbol): void; addTslibDefinition(): void; } declare function visitNode$0<T extends TS.Node>(options: BeforeVisitorOptions<T>): TS.VisitResult<TS.Node>; declare function visitCallExpression(options: BeforeVisitorOptions<TS.CallExpression>): TS.VisitResult<TS.Node>; type TSWithHelpers = typeof TS & { importDefaultHelper?: TS.EmitHelper; importStarHelper?: TS.EmitHelper; }; declare function needsImportPreservationLogic(context: Pick<BaseVisitorContext, "compilerOptions" | "typescript">): boolean; declare function needsImportPreservationLogic(typescript: typeof TS, compilerOptions: TS.CompilerOptions): boolean; declare function getImportDefaultHelper(typescript: TSWithHelpers): TS.EmitHelper; declare function getImportStarHelper(typescript: TSWithHelpers): TS.EmitHelper; declare function moduleKindSupportsImportHelpers(moduleKind: TS.ModuleKind | undefined, typescript: typeof TS): boolean; declare function moduleKindDefinesDependencies(moduleKind: TS.ModuleKind | undefined, typescript: typeof TS): boolean; declare function getUnscopedHelperName(context: VisitorContext, helperName: string): TS.Identifier; declare function getRootBlockInsertionPosition(rootBlock: RootBlock, typescript: typeof TS): number; declare function getDefineArrayLiteralExpression(sourceFile: TS.SourceFile, context: VisitorContext): TS.ArrayLiteralExpression | undefined; declare function getRootBlock(sourceFile: TS.SourceFile, context: VisitorContext): RootBlock; declare function isImportedSymbolImported(importedSymbol: ImportedSymbol, rootBlock: RootBlock, context: VisitorContext): boolean; declare function generateImportStatementForImportedSymbolInContext(importedSymbol: ImportedSymbol, context: VisitorContext): TS.Statement | undefined; declare function visitClassLikeDeclaration(options: BeforeVisitorOptions<TS.ClassLikeDeclaration>): TS.VisitResult<TS.Node>; /** * A TypeNode such as IFoo<string> should still yield the service name "IFoo". * This helper generates a proper service name from a TypeNode */ declare function pickServiceOrImplementationName(node: TS.Expression | TS.TypeNode | TS.EntityName, context: VisitorContext): string; declare function getModifierLikes(node: TS.Node): readonly TS.ModifierLike[] | undefined; declare const CONSTRUCTOR_ARGUMENTS_SYMBOL_IDENTIFIER = "___CTOR_ARGS___"; declare const DI_CONTAINER_NAME = "DIContainer"; type DiMethodName = "get" | "has" | "registerSingleton" | "registerTransient"; interface FileCacheOptions { cacheName: string; ttl: number; } declare class FileCache<T> extends Map<string, T> { private readonly cacheFiles; private readonly options; constructor({ cacheName, ttl }?: Partial<FileCacheOptions>); private get cacheDirectory(); private readTransformResult; get(key: string): NonNullable<T> | undefined; set(key: string, value: T): this; expireDiskCache(): void; } export { transform, di, getBaseVisitorContext, afterTransformer, visitNode, visitDefineArrayLiteralExpression, visitRootBlockBlock, visitRootBlockSourceFile, visitRootBlock, beforeTransformer, transformSourceFile, visitCallExpression, needsImportPreservationLogic, getImportDefaultHelper, getImportStarHelper, moduleKindSupportsImportHelpers, moduleKindDefinesDependencies, getUnscopedHelperName, getRootBlockInsertionPosition, getDefineArrayLiteralExpression, getRootBlock, isImportedSymbolImported, generateImportStatementForImportedSymbolInContext, visitClassLikeDeclaration, pickServiceOrImplementationName, getModifierLikes, CONSTRUCTOR_ARGUMENTS_SYMBOL_IDENTIFIER, DI_CONTAINER_NAME, DiMethodName, FileCache };