import { Expression } from './expressions'; import { ThingSymbol } from './symbol'; import { TypeDeclaration } from './type-declaration'; /** * Interface for classes that are scopes/namespaces */ export interface IScope { readonly fqn: string; /** * The set of type declarations in this scope */ readonly typeMap: ReadonlyMap; /** * Turn a declaration name into a declaration FQN */ qualifyName(name: string): string; /** * Register a type to the scope */ registerType(type: TypeDeclaration): void; /** * Try find a type by FQN */ tryFindType(fqn: string): TypeDeclaration | undefined; /** * Register a visible other scope into this scope * * Associate a resolver with all symbols from `scope`. When a symbol from * the given scope is referenced, the `resolver` will be called to come up * with the expression that should be used to do so. * * This affects the result of `symbolToExpression`. * * If the scopes in question are `Module`s, you should call something like: * * ```ts * module1.addImport(new AliasedModuleImport(module2, pathToModule, 'xyz')); * ``` * * This will make all symbols from `module2` available in `module1. */ linkScope(scope: IScope, resolver: ISymbolResolver): void; /** * Register a visible symbol in this scope * * Associate a specific symbol with a given expression by which it can be * referenced in the current scope. * * This affects the result of `symbolToExpression`. * * If the scopes in question are `Module`s, you should call something like: * * ```ts * module1.addImport(new AliasedModuleImport(module2, pathToModule, 'xyz')); * ``` * * This will make all symbols from `module2` available in `module1. */ linkSymbol(sym: ThingSymbol, alias: Expression): void; /** * Turn a symbol into an expression that can be used to reference the symbol in this scope. */ symbolToExpression(symbol: ThingSymbol): Expression | undefined; } /** * An implementation of the IScope methods * * Classes can either inherit from this class to automatically satisfy the `IScope` requirements, * mix this class into themselves, or declare a private member and delegate all methods. */ export declare class ScopeImpl implements IScope { readonly fqn: string; private readonly _typeMap; private readonly linkMap; private readonly symbolMap; readonly typeMap: ReadonlyMap; /** * Create a Scope with a given FQN * * All elements added to this scope will have this FQN as a prefix of their own FQN. */ constructor(fqn: string); qualifyName(name: string): string; /** * All direct types in this scope */ get types(): readonly TypeDeclaration[]; /** * Register a type to the scope */ registerType(type: TypeDeclaration): void; tryFindType(fqnOrName: string): TypeDeclaration | undefined; linkScope(scope: IScope, theImport: IScopeLink): void; linkSymbol(sym: ThingSymbol, alias: Expression): void; /** * Try to resolve this symbol here */ symbolToExpression(sym: ThingSymbol): Expression | undefined; } /** * A global scope for things that are already in the target environment */ export declare const AMBIENT_SCOPE: ScopeImpl; /** * Link one scope to another (make symbols from the linked scope visible in the current one) */ export interface ISymbolResolver { /** * Return an expression that will reference the given symbol */ referenceSymbol(sym: ThingSymbol): Expression; } /** * Deprecated alias of ISymbolResolver */ export type IScopeLink = ISymbolResolver; /** * All things that have a scope */ export interface IScoped { readonly scope: IScope; } export declare function isScoped(x: unknown): x is IScoped; /** * Common operations that can be performed on anything that's an IScope */ export declare class RichScope { private readonly scope; constructor(scope: IScope); /** * Find type by FQN, throwing an error if not found */ findType(fqn: string): TypeDeclaration; /** * Find a type by name in this current scope */ tryFindTypeByName(name: string): TypeDeclaration | undefined; } /** * A scope that is never meant to be added to the syntax tree. Useful for * when you have to add something to a scope, but don't want it to be * rendered automatically by the renderer when traversing the tree. * * Example: if you want to render an inner class, inside a method, wrap * the class in a TypeDeclarationStatement. If you add the class to the * dummy scope, it will only be rendered as part of the statement. */ export declare class DummyScope extends ScopeImpl { constructor(); }