message-compiler.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import type { BaseError } from '@intlify/shared';
  2. import type { RawSourceMap } from 'source-map-js';
  3. export declare function baseCompile(source: string, options?: CompileOptions): CompilerResult;
  4. export declare type CacheKeyHandler = (source: string) => string;
  5. export declare interface CodeGenOptions {
  6. location?: boolean;
  7. mode?: 'normal' | 'arrow';
  8. breakLineCode?: '\n' | ';';
  9. needIndent?: boolean;
  10. onError?: CompileErrorHandler;
  11. sourceMap?: boolean;
  12. filename?: string;
  13. }
  14. declare interface CodeGenResult {
  15. code: string;
  16. ast: ResourceNode;
  17. map?: RawSourceMap;
  18. }
  19. export declare type CompileDomain = 'tokenizer' | 'parser' | 'generator' | 'transformer' | 'optimizer' | 'minifier';
  20. export declare interface CompileError extends BaseError, SyntaxError {
  21. domain?: CompileDomain;
  22. location?: SourceLocation;
  23. }
  24. export declare const CompileErrorCodes: {
  25. readonly EXPECTED_TOKEN: 1;
  26. readonly INVALID_TOKEN_IN_PLACEHOLDER: 2;
  27. readonly UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3;
  28. readonly UNKNOWN_ESCAPE_SEQUENCE: 4;
  29. readonly INVALID_UNICODE_ESCAPE_SEQUENCE: 5;
  30. readonly UNBALANCED_CLOSING_BRACE: 6;
  31. readonly UNTERMINATED_CLOSING_BRACE: 7;
  32. readonly EMPTY_PLACEHOLDER: 8;
  33. readonly NOT_ALLOW_NEST_PLACEHOLDER: 9;
  34. readonly INVALID_LINKED_FORMAT: 10;
  35. readonly MUST_HAVE_MESSAGES_IN_PLURAL: 11;
  36. readonly UNEXPECTED_EMPTY_LINKED_MODIFIER: 12;
  37. readonly UNEXPECTED_EMPTY_LINKED_KEY: 13;
  38. readonly UNEXPECTED_LEXICAL_ANALYSIS: 14;
  39. readonly UNHANDLED_CODEGEN_NODE_TYPE: 15;
  40. readonly UNHANDLED_MINIFIER_NODE_TYPE: 16;
  41. readonly __EXTEND_POINT__: 17;
  42. };
  43. export declare type CompileErrorCodes = (typeof CompileErrorCodes)[keyof typeof CompileErrorCodes];
  44. export declare type CompileErrorHandler = (error: CompileError) => void;
  45. export declare interface CompileErrorOptions {
  46. domain?: CompileDomain;
  47. messages?: {
  48. [code: number]: string;
  49. };
  50. args?: unknown[];
  51. }
  52. export declare type CompileOptions = {
  53. optimize?: boolean;
  54. minify?: boolean;
  55. jit?: boolean;
  56. } & TransformOptions & CodeGenOptions & ParserOptions & TokenizeOptions;
  57. export declare type CompilerResult = CodeGenResult;
  58. export declare function createCompileError<T extends number>(code: T, loc: SourceLocation | null, options?: CompileErrorOptions): CompileError;
  59. export declare function createLocation(start: Position, end: Position, source?: string): SourceLocation;
  60. export declare function createParser(options?: ParserOptions): Parser;
  61. export declare function createPosition(line: number, column: number, offset: number): Position;
  62. /* Excluded from this release type: defaultOnError */
  63. export declare const detectHtmlTag: (source: string) => boolean;
  64. export declare const ERROR_DOMAIN = "parser";
  65. /* Excluded from this release type: errorMessages */
  66. export declare const enum HelperNameMap {
  67. LIST = "list",
  68. NAMED = "named",
  69. PLURAL = "plural",
  70. LINKED = "linked",
  71. MESSAGE = "message",
  72. TYPE = "type",
  73. INTERPOLATE = "interpolate",
  74. NORMALIZE = "normalize",
  75. VALUES = "values"
  76. }
  77. export declare type Identifier = string;
  78. export declare interface LinkedKeyNode extends Node_2 {
  79. type: NodeTypes.LinkedKey;
  80. value: string;
  81. /* Excluded from this release type: v */
  82. }
  83. export declare interface LinkedModifierNode extends Node_2 {
  84. type: NodeTypes.LinkedModifier;
  85. value: Identifier;
  86. /* Excluded from this release type: v */
  87. }
  88. export declare interface LinkedNode extends Node_2 {
  89. type: NodeTypes.Linked;
  90. modifier?: LinkedModifierNode;
  91. /* Excluded from this release type: m */
  92. key: LinkedKeyNode | NamedNode | ListNode | LiteralNode;
  93. /* Excluded from this release type: k */
  94. }
  95. export declare interface ListNode extends Node_2 {
  96. type: NodeTypes.List;
  97. index: number;
  98. /* Excluded from this release type: i */
  99. }
  100. export declare interface LiteralNode extends Node_2 {
  101. type: NodeTypes.Literal;
  102. value?: string;
  103. /* Excluded from this release type: v */
  104. }
  105. export declare const LOCATION_STUB: SourceLocation;
  106. declare type MessageElementNode = TextNode | NamedNode | ListNode | LiteralNode | LinkedNode;
  107. export declare interface MessageNode extends Node_2 {
  108. type: NodeTypes.Message;
  109. static?: string;
  110. /* Excluded from this release type: s */
  111. items: MessageElementNode[];
  112. /* Excluded from this release type: i */
  113. }
  114. export declare interface NamedNode extends Node_2 {
  115. type: NodeTypes.Named;
  116. key: Identifier;
  117. /* Excluded from this release type: k */
  118. }
  119. declare interface Node_2 {
  120. type: NodeTypes;
  121. /* Excluded from this release type: t */
  122. start?: number;
  123. end?: number;
  124. loc?: SourceLocation;
  125. }
  126. export { Node_2 as Node }
  127. export declare const enum NodeTypes {
  128. Resource = 0,
  129. Plural = 1,
  130. Message = 2,
  131. Text = 3,
  132. Named = 4,
  133. List = 5,
  134. Linked = 6,
  135. LinkedKey = 7,
  136. LinkedModifier = 8,
  137. Literal = 9
  138. }
  139. export declare interface Parser {
  140. parse(source: string): ResourceNode;
  141. }
  142. export declare interface ParserOptions {
  143. location?: boolean;
  144. onCacheKey?: (source: string) => string;
  145. onError?: CompileErrorHandler;
  146. }
  147. export declare interface PluralNode extends Node_2 {
  148. type: NodeTypes.Plural;
  149. cases: MessageNode[];
  150. /* Excluded from this release type: c */
  151. }
  152. export declare interface Position {
  153. offset: number;
  154. line: number;
  155. column: number;
  156. }
  157. export declare interface ResourceNode extends Node_2 {
  158. type: NodeTypes.Resource;
  159. body: MessageNode | PluralNode;
  160. /* Excluded from this release type: b */
  161. cacheKey?: string;
  162. helpers?: string[];
  163. }
  164. export declare interface SourceLocation {
  165. start: Position;
  166. end: Position;
  167. source?: string;
  168. }
  169. export declare interface TextNode extends Node_2 {
  170. type: NodeTypes.Text;
  171. value?: string;
  172. /* Excluded from this release type: v */
  173. }
  174. export declare interface TokenizeOptions {
  175. location?: boolean;
  176. onError?: CompileErrorHandler;
  177. }
  178. export declare interface TransformOptions {
  179. onError?: CompileErrorHandler;
  180. }
  181. export { }