babel-parser.source.d.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = import("../src/typings").PluginConfig;
  110. export type {
  111. ParserPluginWithOptions,
  112. DecoratorsPluginOptions,
  113. PipelineOperatorPluginOptions,
  114. RecordAndTuplePluginOptions,
  115. FlowPluginOptions,
  116. TypeScriptPluginOptions,
  117. } from "../src/typings";
  118. export const tokTypes: {
  119. // todo(flow->ts) real token type
  120. [name: string]: any;
  121. };
  122. export interface ParseError {
  123. code: string;
  124. reasonCode: string;
  125. }
  126. type ParseResult<Result> = Result & {
  127. errors: ParseError[];
  128. };