message-compiler.d.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 const COMPILE_ERROR_CODES_EXTEND_POINT = 17;
  20. export declare type CompileDomain = 'tokenizer' | 'parser' | 'generator' | 'transformer' | 'optimizer' | 'minifier';
  21. export declare interface CompileError extends BaseError, SyntaxError {
  22. domain?: CompileDomain;
  23. location?: SourceLocation;
  24. }
  25. export declare const CompileErrorCodes: {
  26. readonly EXPECTED_TOKEN: 1;
  27. readonly INVALID_TOKEN_IN_PLACEHOLDER: 2;
  28. readonly UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3;
  29. readonly UNKNOWN_ESCAPE_SEQUENCE: 4;
  30. readonly INVALID_UNICODE_ESCAPE_SEQUENCE: 5;
  31. readonly UNBALANCED_CLOSING_BRACE: 6;
  32. readonly UNTERMINATED_CLOSING_BRACE: 7;
  33. readonly EMPTY_PLACEHOLDER: 8;
  34. readonly NOT_ALLOW_NEST_PLACEHOLDER: 9;
  35. readonly INVALID_LINKED_FORMAT: 10;
  36. readonly MUST_HAVE_MESSAGES_IN_PLURAL: 11;
  37. readonly UNEXPECTED_EMPTY_LINKED_MODIFIER: 12;
  38. readonly UNEXPECTED_EMPTY_LINKED_KEY: 13;
  39. readonly UNEXPECTED_LEXICAL_ANALYSIS: 14;
  40. readonly UNHANDLED_CODEGEN_NODE_TYPE: 15;
  41. readonly UNHANDLED_MINIFIER_NODE_TYPE: 16;
  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. modulo?: boolean;
  118. /* Excluded from this release type: k */
  119. }
  120. declare interface Node_2 {
  121. type: NodeTypes;
  122. /* Excluded from this release type: t */
  123. start?: number;
  124. end?: number;
  125. loc?: SourceLocation;
  126. }
  127. export { Node_2 as Node }
  128. export declare const enum NodeTypes {
  129. Resource = 0,// 0
  130. Plural = 1,
  131. Message = 2,
  132. Text = 3,
  133. Named = 4,
  134. List = 5,// 5
  135. Linked = 6,
  136. LinkedKey = 7,
  137. LinkedModifier = 8,
  138. Literal = 9
  139. }
  140. export declare interface Parser {
  141. parse(source: string): ResourceNode;
  142. }
  143. export declare interface ParserOptions {
  144. location?: boolean;
  145. onCacheKey?: (source: string) => string;
  146. onError?: CompileErrorHandler;
  147. }
  148. export declare interface PluralNode extends Node_2 {
  149. type: NodeTypes.Plural;
  150. cases: MessageNode[];
  151. /* Excluded from this release type: c */
  152. }
  153. export declare interface Position {
  154. offset: number;
  155. line: number;
  156. column: number;
  157. }
  158. export declare interface ResourceNode extends Node_2 {
  159. type: NodeTypes.Resource;
  160. body: MessageNode | PluralNode;
  161. /* Excluded from this release type: b */
  162. cacheKey?: string;
  163. helpers?: string[];
  164. }
  165. export declare interface SourceLocation {
  166. start: Position;
  167. end: Position;
  168. source?: string;
  169. }
  170. export declare interface TextNode extends Node_2 {
  171. type: NodeTypes.Text;
  172. value?: string;
  173. /* Excluded from this release type: v */
  174. }
  175. export declare interface TokenizeOptions {
  176. location?: boolean;
  177. onError?: CompileErrorHandler;
  178. }
  179. export declare interface TransformOptions {
  180. onError?: CompileErrorHandler;
  181. }
  182. export { }