babel-parser.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Type definitions for @babel/parser
  2. // Project: https://github.com/babel/babel/tree/main/packages/babel-parser
  3. // Definitions by: Troy Gerwien <https://github.com/yortus>
  4. // Marvin Hagemeister <https://github.com/marvinhagemeister>
  5. // Avi Vahl <https://github.com/AviVahl>
  6. // TypeScript Version: 2.9
  7. /**
  8. * Parse the provided code as an entire ECMAScript program.
  9. */
  10. export function parse(
  11. input: string,
  12. options?: ParserOptions
  13. ): ParseResult<import("@babel/types").File>;
  14. /**
  15. * Parse the provided code as a single expression.
  16. */
  17. export function parseExpression(
  18. input: string,
  19. options?: ParserOptions
  20. ): ParseResult<import("@babel/types").Expression>;
  21. export interface ParserOptions {
  22. /**
  23. * By default, import and export declarations can only appear at a program's top level.
  24. * Setting this option to true allows them anywhere where a statement is allowed.
  25. */
  26. allowImportExportEverywhere?: boolean;
  27. /**
  28. * By default, await use is not allowed outside of an async function.
  29. * Set this to true to accept such code.
  30. */
  31. allowAwaitOutsideFunction?: boolean;
  32. /**
  33. * By default, a return statement at the top level raises an error.
  34. * Set this to true to accept such code.
  35. */
  36. allowReturnOutsideFunction?: boolean;
  37. allowSuperOutsideMethod?: boolean;
  38. /**
  39. * By default, exported identifiers must refer to a declared variable.
  40. * Set this to true to allow export statements to reference undeclared variables.
  41. */
  42. allowUndeclaredExports?: boolean;
  43. /**
  44. * By default, Babel attaches comments to adjacent AST nodes.
  45. * When this option is set to false, comments are not attached.
  46. * It can provide up to 30% performance improvement when the input code has many comments.
  47. * @babel/eslint-parser will set it for you.
  48. * It is not recommended to use attachComment: false with Babel transform,
  49. * as doing so removes all the comments in output code, and renders annotations such as
  50. * /* istanbul ignore next *\/ nonfunctional.
  51. */
  52. attachComment?: boolean;
  53. /**
  54. * By default, Babel always throws an error when it finds some invalid code.
  55. * When this option is set to true, it will store the parsing error and
  56. * try to continue parsing the invalid input file.
  57. */
  58. errorRecovery?: boolean;
  59. /**
  60. * Indicate the mode the code should be parsed in.
  61. * Can be one of "script", "module", or "unambiguous". Defaults to "script".
  62. * "unambiguous" will make @babel/parser attempt to guess, based on the presence
  63. * of ES6 import or export statements.
  64. * Files with ES6 imports and exports are considered "module" and are otherwise "script".
  65. */
  66. sourceType?: "script" | "module" | "unambiguous";
  67. /**
  68. * Correlate output AST nodes with their source filename.
  69. * Useful when generating code and source maps from the ASTs of multiple input files.
  70. */
  71. sourceFilename?: string;
  72. /**
  73. * By default, the first line of code parsed is treated as line 1.
  74. * You can provide a line number to alternatively start with.
  75. * Useful for integration with other source tools.
  76. */
  77. startLine?: number;
  78. /**
  79. * Array containing the plugins that you want to enable.
  80. */
  81. plugins?: ParserPlugin[];
  82. /**
  83. * Should the parser work in strict mode.
  84. * Defaults to true if sourceType === 'module'. Otherwise, false.
  85. */
  86. strictMode?: boolean;
  87. /**
  88. * Adds a ranges property to each node: [node.start, node.end]
  89. */
  90. ranges?: boolean;
  91. /**
  92. * Adds all parsed tokens to a tokens property on the File node.
  93. */
  94. tokens?: boolean;
  95. /**
  96. * By default, the parser adds information about parentheses by setting
  97. * `extra.parenthesized` to `true` as needed.
  98. * When this option is `true` the parser creates `ParenthesizedExpression`
  99. * AST nodes instead of using the `extra` property.
  100. */
  101. createParenthesizedExpressions?: boolean;
  102. }
  103. export type ParserPlugin =
  104. | "asyncDoExpressions"
  105. | "asyncGenerators"
  106. | "bigInt"
  107. | "classPrivateMethods"
  108. | "classPrivateProperties"
  109. | "classProperties"
  110. | "classStaticBlock" // Enabled by default
  111. | "decimal"
  112. | "decorators"
  113. | "decorators-legacy"
  114. | "decoratorAutoAccessors"
  115. | "destructuringPrivate"
  116. | "doExpressions"
  117. | "dynamicImport"
  118. | "estree"
  119. | "exportDefaultFrom"
  120. | "exportNamespaceFrom" // deprecated
  121. | "flow"
  122. | "flowComments"
  123. | "functionBind"
  124. | "functionSent"
  125. | "importMeta"
  126. | "jsx"
  127. | "logicalAssignment"
  128. | "importAssertions"
  129. | "moduleBlocks"
  130. | "moduleStringNames"
  131. | "nullishCoalescingOperator"
  132. | "numericSeparator"
  133. | "objectRestSpread"
  134. | "optionalCatchBinding"
  135. | "optionalChaining"
  136. | "partialApplication"
  137. | "pipelineOperator"
  138. | "placeholders"
  139. | "privateIn" // Enabled by default
  140. | "regexpUnicodeSets"
  141. | "throwExpressions"
  142. | "topLevelAwait"
  143. | "typescript"
  144. | "v8intrinsic"
  145. | ParserPluginWithOptions;
  146. export type ParserPluginWithOptions =
  147. | ["decorators", DecoratorsPluginOptions]
  148. | ["pipelineOperator", PipelineOperatorPluginOptions]
  149. | ["recordAndTuple", RecordAndTuplePluginOptions]
  150. | ["flow", FlowPluginOptions]
  151. | ["typescript", TypeScriptPluginOptions];
  152. export interface DecoratorsPluginOptions {
  153. decoratorsBeforeExport?: boolean;
  154. }
  155. export interface PipelineOperatorPluginOptions {
  156. proposal: "minimal" | "fsharp" | "hack" | "smart";
  157. topicToken?: "%" | "#" | "@@" | "^^";
  158. }
  159. export interface RecordAndTuplePluginOptions {
  160. syntaxType: "bar" | "hash";
  161. }
  162. export interface FlowPluginOptions {
  163. all?: boolean;
  164. }
  165. export interface TypeScriptPluginOptions {
  166. dts?: boolean;
  167. disallowAmbiguousJSXLike?: boolean;
  168. }
  169. export const tokTypes: {
  170. // todo(flow->ts) real token type
  171. [name: string]: any;
  172. };
  173. export interface ParseError {
  174. code: string;
  175. reasonCode: string;
  176. }
  177. type ParseResult<Result> = Result & {
  178. errors: ParseError[];
  179. };