babel-parser.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * By default, the parsed code is treated as if it starts from line 1, column 0.
  80. * You can provide a column number to alternatively start with.
  81. * Useful for integration with other source tools.
  82. */
  83. startColumn?: number;
  84. /**
  85. * Array containing the plugins that you want to enable.
  86. */
  87. plugins?: ParserPlugin[];
  88. /**
  89. * Should the parser work in strict mode.
  90. * Defaults to true if sourceType === 'module'. Otherwise, false.
  91. */
  92. strictMode?: boolean;
  93. /**
  94. * Adds a ranges property to each node: [node.start, node.end]
  95. */
  96. ranges?: boolean;
  97. /**
  98. * Adds all parsed tokens to a tokens property on the File node.
  99. */
  100. tokens?: boolean;
  101. /**
  102. * By default, the parser adds information about parentheses by setting
  103. * `extra.parenthesized` to `true` as needed.
  104. * When this option is `true` the parser creates `ParenthesizedExpression`
  105. * AST nodes instead of using the `extra` property.
  106. */
  107. createParenthesizedExpressions?: boolean;
  108. }
  109. export type ParserPlugin =
  110. | "asyncDoExpressions"
  111. | "asyncGenerators"
  112. | "bigInt"
  113. | "classPrivateMethods"
  114. | "classPrivateProperties"
  115. | "classProperties"
  116. | "classStaticBlock" // Enabled by default
  117. | "decimal"
  118. | "decorators"
  119. | "decorators-legacy"
  120. | "decoratorAutoAccessors"
  121. | "destructuringPrivate"
  122. | "doExpressions"
  123. | "dynamicImport"
  124. | "estree"
  125. | "exportDefaultFrom"
  126. | "exportNamespaceFrom" // deprecated
  127. | "flow"
  128. | "flowComments"
  129. | "functionBind"
  130. | "functionSent"
  131. | "importMeta"
  132. | "jsx"
  133. | "logicalAssignment"
  134. | "importAssertions"
  135. | "moduleBlocks"
  136. | "moduleStringNames"
  137. | "nullishCoalescingOperator"
  138. | "numericSeparator"
  139. | "objectRestSpread"
  140. | "optionalCatchBinding"
  141. | "optionalChaining"
  142. | "partialApplication"
  143. | "pipelineOperator"
  144. | "placeholders"
  145. | "privateIn" // Enabled by default
  146. | "regexpUnicodeSets"
  147. | "throwExpressions"
  148. | "topLevelAwait"
  149. | "typescript"
  150. | "v8intrinsic"
  151. | ParserPluginWithOptions;
  152. export type ParserPluginWithOptions =
  153. | ["decorators", DecoratorsPluginOptions]
  154. | ["pipelineOperator", PipelineOperatorPluginOptions]
  155. | ["recordAndTuple", RecordAndTuplePluginOptions]
  156. | ["flow", FlowPluginOptions]
  157. | ["typescript", TypeScriptPluginOptions];
  158. export interface DecoratorsPluginOptions {
  159. decoratorsBeforeExport?: boolean;
  160. }
  161. export interface PipelineOperatorPluginOptions {
  162. proposal: "minimal" | "fsharp" | "hack" | "smart";
  163. topicToken?: "%" | "#" | "@@" | "^^";
  164. }
  165. export interface RecordAndTuplePluginOptions {
  166. syntaxType: "bar" | "hash";
  167. }
  168. export interface FlowPluginOptions {
  169. all?: boolean;
  170. }
  171. export interface TypeScriptPluginOptions {
  172. dts?: boolean;
  173. disallowAmbiguousJSXLike?: boolean;
  174. }
  175. export const tokTypes: {
  176. // todo(flow->ts) real token type
  177. [name: string]: any;
  178. };
  179. export interface ParseError {
  180. code: string;
  181. reasonCode: string;
  182. }
  183. type ParseResult<Result> = Result & {
  184. errors: ParseError[];
  185. };