tsd.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. declare module 'jsep' {
  2. namespace jsep {
  3. export type baseTypes = string | number | boolean | RegExp | null | undefined | object;
  4. export interface Expression {
  5. type: string;
  6. [key: string]: baseTypes | Expression | Array<baseTypes | Expression>;
  7. }
  8. export interface ArrayExpression extends Expression {
  9. type: 'ArrayExpression';
  10. elements: Expression[];
  11. }
  12. export interface BinaryExpression extends Expression {
  13. type: 'BinaryExpression';
  14. operator: string;
  15. left: Expression;
  16. right: Expression;
  17. }
  18. export interface CallExpression extends Expression {
  19. type: 'CallExpression';
  20. arguments: Expression[];
  21. callee: Expression;
  22. }
  23. export interface Compound extends Expression {
  24. type: 'Compound';
  25. body: Expression[];
  26. }
  27. export interface ConditionalExpression extends Expression {
  28. type: 'ConditionalExpression';
  29. test: Expression;
  30. consequent: Expression;
  31. alternate: Expression;
  32. }
  33. export interface Identifier extends Expression {
  34. type: 'Identifier';
  35. name: string;
  36. }
  37. export interface Literal extends Expression {
  38. type: 'Literal';
  39. value: boolean | number | string | RegExp | null;
  40. raw: string;
  41. }
  42. export interface MemberExpression extends Expression {
  43. type: 'MemberExpression';
  44. computed: boolean;
  45. object: Expression;
  46. property: Expression;
  47. optional?: boolean;
  48. }
  49. export interface ThisExpression extends Expression {
  50. type: 'ThisExpression';
  51. }
  52. export interface UnaryExpression extends Expression {
  53. type: 'UnaryExpression';
  54. operator: string;
  55. argument: Expression;
  56. prefix: boolean;
  57. }
  58. export type ExpressionType =
  59. 'Compound'
  60. | 'Identifier'
  61. | 'MemberExpression'
  62. | 'Literal'
  63. | 'ThisExpression'
  64. | 'CallExpression'
  65. | 'UnaryExpression'
  66. | 'BinaryExpression'
  67. | 'ConditionalExpression'
  68. | 'ArrayExpression';
  69. export type CoreExpression =
  70. ArrayExpression
  71. | BinaryExpression
  72. | CallExpression
  73. | Compound
  74. | ConditionalExpression
  75. | Identifier
  76. | Literal
  77. | MemberExpression
  78. | ThisExpression
  79. | UnaryExpression;
  80. export type PossibleExpression = Expression | undefined;
  81. export interface HookScope {
  82. index: number;
  83. readonly expr: string;
  84. readonly char: string; // current character of the expression
  85. readonly code: number; // current character code of the expression
  86. gobbleSpaces: () => void;
  87. gobbleExpressions: (untilICode?: number) => Expression[];
  88. gobbleExpression: () => Expression;
  89. gobbleBinaryOp: () => PossibleExpression;
  90. gobbleBinaryExpression: () => PossibleExpression;
  91. gobbleToken: () => PossibleExpression;
  92. gobbleTokenProperty: (node: Expression) => Expression
  93. gobbleNumericLiteral: () => PossibleExpression;
  94. gobbleStringLiteral: () => PossibleExpression;
  95. gobbleIdentifier: () => PossibleExpression;
  96. gobbleArguments: (untilICode: number) => PossibleExpression;
  97. gobbleGroup: () => Expression;
  98. gobbleArray: () => PossibleExpression;
  99. throwError: (msg: string) => void;
  100. }
  101. export type HookType = 'gobble-expression' | 'after-expression' | 'gobble-token' | 'after-token' | 'gobble-spaces';
  102. export type HookCallback = (this: HookScope, env: { node?: Expression }) => void;
  103. type HookTypeObj = Partial<{ [key in HookType]: HookCallback}>
  104. export interface IHooks extends HookTypeObj {
  105. add(name: HookType, cb: HookCallback, first?: boolean): void;
  106. add(obj: { [name in HookType]: HookCallback }, first?: boolean): void;
  107. run(name: string, env: { context?: typeof jsep, node?: Expression }): void;
  108. }
  109. let hooks: IHooks;
  110. export interface IPlugin {
  111. name: string;
  112. init: (this: typeof jsep) => void;
  113. }
  114. export interface IPlugins {
  115. registered: { [name: string]: IPlugin };
  116. register: (...plugins: IPlugin[]) => void;
  117. }
  118. let plugins: IPlugins;
  119. let unary_ops: { [op: string]: any };
  120. let binary_ops: { [op: string]: number };
  121. let right_associative: Set<string>;
  122. let additional_identifier_chars: Set<string>;
  123. let literals: { [literal: string]: any };
  124. let this_str: string;
  125. function addBinaryOp(operatorName: string, precedence: number, rightToLeft?: boolean): void;
  126. function addUnaryOp(operatorName: string): void;
  127. function removeBinaryOp(operatorName: string): void;
  128. function removeUnaryOp(operatorName: string): void;
  129. function addIdentifierChar(identifierName: string): void;
  130. function removeIdentifierChar(identifierName: string): void;
  131. const version: string;
  132. }
  133. function jsep(val: string | jsep.Expression): jsep.Expression;
  134. export = jsep;
  135. }