scope.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Code, Name } from "./code";
  2. interface NameGroup {
  3. prefix: string;
  4. index: number;
  5. }
  6. export interface NameValue {
  7. ref: ValueReference;
  8. key?: unknown;
  9. code?: Code;
  10. }
  11. export declare type ValueReference = unknown;
  12. interface ScopeOptions {
  13. prefixes?: Set<string>;
  14. parent?: Scope;
  15. }
  16. interface ValueScopeOptions extends ScopeOptions {
  17. scope: ScopeStore;
  18. es5?: boolean;
  19. lines?: boolean;
  20. }
  21. export declare type ScopeStore = Record<string, ValueReference[] | undefined>;
  22. declare type ScopeValues = {
  23. [Prefix in string]?: Map<unknown, ValueScopeName>;
  24. };
  25. export declare type ScopeValueSets = {
  26. [Prefix in string]?: Set<ValueScopeName>;
  27. };
  28. export declare const varKinds: {
  29. const: Name;
  30. let: Name;
  31. var: Name;
  32. };
  33. export declare class Scope {
  34. protected readonly _names: {
  35. [Prefix in string]?: NameGroup;
  36. };
  37. protected readonly _prefixes?: Set<string>;
  38. protected readonly _parent?: Scope;
  39. constructor({ prefixes, parent }?: ScopeOptions);
  40. toName(nameOrPrefix: Name | string): Name;
  41. name(prefix: string): Name;
  42. protected _newName(prefix: string): string;
  43. private _nameGroup;
  44. }
  45. interface ScopePath {
  46. property: string;
  47. itemIndex: number;
  48. }
  49. export declare class ValueScopeName extends Name {
  50. readonly prefix: string;
  51. value?: NameValue;
  52. scopePath?: Code;
  53. constructor(prefix: string, nameStr: string);
  54. setValue(value: NameValue, { property, itemIndex }: ScopePath): void;
  55. }
  56. interface VSOptions extends ValueScopeOptions {
  57. _n: Code;
  58. }
  59. export declare class ValueScope extends Scope {
  60. protected readonly _values: ScopeValues;
  61. protected readonly _scope: ScopeStore;
  62. readonly opts: VSOptions;
  63. constructor(opts: ValueScopeOptions);
  64. get(): ScopeStore;
  65. name(prefix: string): ValueScopeName;
  66. value(nameOrPrefix: ValueScopeName | string, value: NameValue): ValueScopeName;
  67. getValue(prefix: string, keyOrRef: unknown): ValueScopeName | undefined;
  68. scopeRefs(scopeName: Name, values?: ScopeValues | ScopeValueSets): Code;
  69. scopeCode(values?: ScopeValues | ScopeValueSets, usedValues?: ScopeValueSets, getCode?: (n: ValueScopeName) => Code | undefined): Code;
  70. private _reduceValues;
  71. }
  72. export {};