lexer.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. export declare enum ImportType {
  2. /**
  3. * A normal static using any syntax variations
  4. * import .. from 'module'
  5. */
  6. Static = 1,
  7. /**
  8. * A dynamic import expression `import(specifier)`
  9. * or `import(specifier, opts)`
  10. */
  11. Dynamic = 2,
  12. /**
  13. * An import.meta expression
  14. */
  15. ImportMeta = 3,
  16. /**
  17. * A source phase import
  18. * import source x from 'module'
  19. */
  20. StaticSourcePhase = 4,
  21. /**
  22. * A dynamic source phase import
  23. * import.source('module')
  24. */
  25. DynamicSourcePhase = 5
  26. }
  27. export interface ImportSpecifier {
  28. /**
  29. * Module name
  30. *
  31. * To handle escape sequences in specifier strings, the .n field of imported specifiers will be provided where possible.
  32. *
  33. * For dynamic import expressions, this field will be empty if not a valid JS string.
  34. *
  35. * @example
  36. * const [imports1, exports1] = parse(String.raw`import './\u0061\u0062.js'`);
  37. * imports1[0].n;
  38. * // Returns "./ab.js"
  39. *
  40. * const [imports2, exports2] = parse(`import("./ab.js")`);
  41. * imports2[0].n;
  42. * // Returns "./ab.js"
  43. *
  44. * const [imports3, exports3] = parse(`import("./" + "ab.js")`);
  45. * imports3[0].n;
  46. * // Returns undefined
  47. */
  48. readonly n: string | undefined;
  49. /**
  50. * Type of import statement
  51. */
  52. readonly t: ImportType;
  53. /**
  54. * Start of module specifier
  55. *
  56. * @example
  57. * const source = `import { a } from 'asdf'`;
  58. * const [imports, exports] = parse(source);
  59. * source.substring(imports[0].s, imports[0].e);
  60. * // Returns "asdf"
  61. */
  62. readonly s: number;
  63. /**
  64. * End of module specifier
  65. */
  66. readonly e: number;
  67. /**
  68. * Start of import statement
  69. *
  70. * @example
  71. * const source = `import { a } from 'asdf'`;
  72. * const [imports, exports] = parse(source);
  73. * source.substring(imports[0].ss, imports[0].se);
  74. * // Returns "import { a } from 'asdf';"
  75. */
  76. readonly ss: number;
  77. /**
  78. * End of import statement
  79. */
  80. readonly se: number;
  81. /**
  82. * If this import keyword is a dynamic import, this is the start value.
  83. * If this import keyword is a static import, this is -1.
  84. * If this import keyword is an import.meta expresion, this is -2.
  85. */
  86. readonly d: number;
  87. /**
  88. * If this import has an import assertion, this is the start value.
  89. * Otherwise this is `-1`.
  90. */
  91. readonly a: number;
  92. }
  93. export interface ExportSpecifier {
  94. /**
  95. * Exported name
  96. *
  97. * @example
  98. * const source = `export default []`;
  99. * const [imports, exports] = parse(source);
  100. * exports[0].n;
  101. * // Returns "default"
  102. *
  103. * @example
  104. * const source = `export const asdf = 42`;
  105. * const [imports, exports] = parse(source);
  106. * exports[0].n;
  107. * // Returns "asdf"
  108. */
  109. readonly n: string;
  110. /**
  111. * Local name, or undefined.
  112. *
  113. * @example
  114. * const source = `export default []`;
  115. * const [imports, exports] = parse(source);
  116. * exports[0].ln;
  117. * // Returns undefined
  118. *
  119. * @example
  120. * const asdf = 42;
  121. * const source = `export { asdf as a }`;
  122. * const [imports, exports] = parse(source);
  123. * exports[0].ln;
  124. * // Returns "asdf"
  125. */
  126. readonly ln: string | undefined;
  127. /**
  128. * Start of exported name
  129. *
  130. * @example
  131. * const source = `export default []`;
  132. * const [imports, exports] = parse(source);
  133. * source.substring(exports[0].s, exports[0].e);
  134. * // Returns "default"
  135. *
  136. * @example
  137. * const source = `export { 42 as asdf }`;
  138. * const [imports, exports] = parse(source);
  139. * source.substring(exports[0].s, exports[0].e);
  140. * // Returns "asdf"
  141. */
  142. readonly s: number;
  143. /**
  144. * End of exported name
  145. */
  146. readonly e: number;
  147. /**
  148. * Start of local name, or -1.
  149. *
  150. * @example
  151. * const asdf = 42;
  152. * const source = `export { asdf as a }`;
  153. * const [imports, exports] = parse(source);
  154. * source.substring(exports[0].ls, exports[0].le);
  155. * // Returns "asdf"
  156. */
  157. readonly ls: number;
  158. /**
  159. * End of local name, or -1.
  160. */
  161. readonly le: number;
  162. }
  163. export interface ParseError extends Error {
  164. idx: number;
  165. }
  166. /**
  167. * Outputs the list of exports and locations of import specifiers,
  168. * including dynamic import and import meta handling.
  169. *
  170. * @param source Source code to parser
  171. * @param name Optional sourcename
  172. * @returns Tuple contaning imports list and exports list.
  173. */
  174. export declare function parse(source: string, name?: string): readonly [
  175. imports: ReadonlyArray<ImportSpecifier>,
  176. exports: ReadonlyArray<ExportSpecifier>,
  177. facade: boolean,
  178. hasModuleSyntax: boolean
  179. ];
  180. /**
  181. * Wait for init to resolve before calling `parse`.
  182. */
  183. export declare const init: Promise<void>;