0477622ad67adb59723660afb68d8a683f89b12e3fee8932a02f2fe713eae6ead9cb5828bf57bfe3e006cb0608f112d353c6e732931d2be14a15c28d73ab0e 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. // This definition file follows a somewhat unusual format. ESTree allows
  2. // runtime type checks based on the `type` parameter. In order to explain this
  3. // to typescript we want to use discriminated union types:
  4. // https://github.com/Microsoft/TypeScript/pull/9163
  5. //
  6. // For ESTree this is a bit tricky because the high level interfaces like
  7. // Node or Function are pulling double duty. We want to pass common fields down
  8. // to the interfaces that extend them (like Identifier or
  9. // ArrowFunctionExpression), but you can't extend a type union or enforce
  10. // common fields on them. So we've split the high level interfaces into two
  11. // types, a base type which passes down inherited fields, and a type union of
  12. // all types which extend the base type. Only the type union is exported, and
  13. // the union is how other types refer to the collection of inheriting types.
  14. //
  15. // This makes the definitions file here somewhat more difficult to maintain,
  16. // but it has the notable advantage of making ESTree much easier to use as
  17. // an end user.
  18. export interface BaseNodeWithoutComments {
  19. // Every leaf interface that extends BaseNode must specify a type property.
  20. // The type property should be a string literal. For example, Identifier
  21. // has: `type: "Identifier"`
  22. type: string;
  23. loc?: SourceLocation | null | undefined;
  24. range?: [number, number] | undefined;
  25. }
  26. export interface BaseNode extends BaseNodeWithoutComments {
  27. leadingComments?: Comment[] | undefined;
  28. trailingComments?: Comment[] | undefined;
  29. }
  30. export interface NodeMap {
  31. AssignmentProperty: AssignmentProperty;
  32. CatchClause: CatchClause;
  33. Class: Class;
  34. ClassBody: ClassBody;
  35. Expression: Expression;
  36. Function: Function;
  37. Identifier: Identifier;
  38. Literal: Literal;
  39. MethodDefinition: MethodDefinition;
  40. ModuleDeclaration: ModuleDeclaration;
  41. ModuleSpecifier: ModuleSpecifier;
  42. Pattern: Pattern;
  43. PrivateIdentifier: PrivateIdentifier;
  44. Program: Program;
  45. Property: Property;
  46. PropertyDefinition: PropertyDefinition;
  47. SpreadElement: SpreadElement;
  48. Statement: Statement;
  49. Super: Super;
  50. SwitchCase: SwitchCase;
  51. TemplateElement: TemplateElement;
  52. VariableDeclarator: VariableDeclarator;
  53. }
  54. export type Node = NodeMap[keyof NodeMap];
  55. export interface Comment extends BaseNodeWithoutComments {
  56. type: "Line" | "Block";
  57. value: string;
  58. }
  59. export interface SourceLocation {
  60. source?: string | null | undefined;
  61. start: Position;
  62. end: Position;
  63. }
  64. export interface Position {
  65. /** >= 1 */
  66. line: number;
  67. /** >= 0 */
  68. column: number;
  69. }
  70. export interface Program extends BaseNode {
  71. type: "Program";
  72. sourceType: "script" | "module";
  73. body: Array<Directive | Statement | ModuleDeclaration>;
  74. comments?: Comment[] | undefined;
  75. }
  76. export interface Directive extends BaseNode {
  77. type: "ExpressionStatement";
  78. expression: Literal;
  79. directive: string;
  80. }
  81. export interface BaseFunction extends BaseNode {
  82. params: Pattern[];
  83. generator?: boolean | undefined;
  84. async?: boolean | undefined;
  85. // The body is either BlockStatement or Expression because arrow functions
  86. // can have a body that's either. FunctionDeclarations and
  87. // FunctionExpressions have only BlockStatement bodies.
  88. body: BlockStatement | Expression;
  89. }
  90. export type Function = FunctionDeclaration | FunctionExpression | ArrowFunctionExpression;
  91. export type Statement =
  92. | ExpressionStatement
  93. | BlockStatement
  94. | StaticBlock
  95. | EmptyStatement
  96. | DebuggerStatement
  97. | WithStatement
  98. | ReturnStatement
  99. | LabeledStatement
  100. | BreakStatement
  101. | ContinueStatement
  102. | IfStatement
  103. | SwitchStatement
  104. | ThrowStatement
  105. | TryStatement
  106. | WhileStatement
  107. | DoWhileStatement
  108. | ForStatement
  109. | ForInStatement
  110. | ForOfStatement
  111. | Declaration;
  112. export interface BaseStatement extends BaseNode {}
  113. export interface EmptyStatement extends BaseStatement {
  114. type: "EmptyStatement";
  115. }
  116. export interface BlockStatement extends BaseStatement {
  117. type: "BlockStatement";
  118. body: Statement[];
  119. innerComments?: Comment[] | undefined;
  120. }
  121. export interface StaticBlock extends Omit<BlockStatement, "type"> {
  122. type: "StaticBlock";
  123. }
  124. export interface ExpressionStatement extends BaseStatement {
  125. type: "ExpressionStatement";
  126. expression: Expression;
  127. }
  128. export interface IfStatement extends BaseStatement {
  129. type: "IfStatement";
  130. test: Expression;
  131. consequent: Statement;
  132. alternate?: Statement | null | undefined;
  133. }
  134. export interface LabeledStatement extends BaseStatement {
  135. type: "LabeledStatement";
  136. label: Identifier;
  137. body: Statement;
  138. }
  139. export interface BreakStatement extends BaseStatement {
  140. type: "BreakStatement";
  141. label?: Identifier | null | undefined;
  142. }
  143. export interface ContinueStatement extends BaseStatement {
  144. type: "ContinueStatement";
  145. label?: Identifier | null | undefined;
  146. }
  147. export interface WithStatement extends BaseStatement {
  148. type: "WithStatement";
  149. object: Expression;
  150. body: Statement;
  151. }
  152. export interface SwitchStatement extends BaseStatement {
  153. type: "SwitchStatement";
  154. discriminant: Expression;
  155. cases: SwitchCase[];
  156. }
  157. export interface ReturnStatement extends BaseStatement {
  158. type: "ReturnStatement";
  159. argument?: Expression | null | undefined;
  160. }
  161. export interface ThrowStatement extends BaseStatement {
  162. type: "ThrowStatement";
  163. argument: Expression;
  164. }
  165. export interface TryStatement extends BaseStatement {
  166. type: "TryStatement";
  167. block: BlockStatement;
  168. handler?: CatchClause | null | undefined;
  169. finalizer?: BlockStatement | null | undefined;
  170. }
  171. export interface WhileStatement extends BaseStatement {
  172. type: "WhileStatement";
  173. test: Expression;
  174. body: Statement;
  175. }
  176. export interface DoWhileStatement extends BaseStatement {
  177. type: "DoWhileStatement";
  178. body: Statement;
  179. test: Expression;
  180. }
  181. export interface ForStatement extends BaseStatement {
  182. type: "ForStatement";
  183. init?: VariableDeclaration | Expression | null | undefined;
  184. test?: Expression | null | undefined;
  185. update?: Expression | null | undefined;
  186. body: Statement;
  187. }
  188. export interface BaseForXStatement extends BaseStatement {
  189. left: VariableDeclaration | Pattern;
  190. right: Expression;
  191. body: Statement;
  192. }
  193. export interface ForInStatement extends BaseForXStatement {
  194. type: "ForInStatement";
  195. }
  196. export interface DebuggerStatement extends BaseStatement {
  197. type: "DebuggerStatement";
  198. }
  199. export type Declaration = FunctionDeclaration | VariableDeclaration | ClassDeclaration;
  200. export interface BaseDeclaration extends BaseStatement {}
  201. export interface MaybeNamedFunctionDeclaration extends BaseFunction, BaseDeclaration {
  202. type: "FunctionDeclaration";
  203. /** It is null when a function declaration is a part of the `export default function` statement */
  204. id: Identifier | null;
  205. body: BlockStatement;
  206. }
  207. export interface FunctionDeclaration extends MaybeNamedFunctionDeclaration {
  208. id: Identifier;
  209. }
  210. export interface VariableDeclaration extends BaseDeclaration {
  211. type: "VariableDeclaration";
  212. declarations: VariableDeclarator[];
  213. kind: "var" | "let" | "const" | "using" | "await using";
  214. }
  215. export interface VariableDeclarator extends BaseNode {
  216. type: "VariableDeclarator";
  217. id: Pattern;
  218. init?: Expression | null | undefined;
  219. }
  220. export interface ExpressionMap {
  221. ArrayExpression: ArrayExpression;
  222. ArrowFunctionExpression: ArrowFunctionExpression;
  223. AssignmentExpression: AssignmentExpression;
  224. AwaitExpression: AwaitExpression;
  225. BinaryExpression: BinaryExpression;
  226. CallExpression: CallExpression;
  227. ChainExpression: ChainExpression;
  228. ClassExpression: ClassExpression;
  229. ConditionalExpression: ConditionalExpression;
  230. FunctionExpression: FunctionExpression;
  231. Identifier: Identifier;
  232. ImportExpression: ImportExpression;
  233. Literal: Literal;
  234. LogicalExpression: LogicalExpression;
  235. MemberExpression: MemberExpression;
  236. MetaProperty: MetaProperty;
  237. NewExpression: NewExpression;
  238. ObjectExpression: ObjectExpression;
  239. SequenceExpression: SequenceExpression;
  240. TaggedTemplateExpression: TaggedTemplateExpression;
  241. TemplateLiteral: TemplateLiteral;
  242. ThisExpression: ThisExpression;
  243. UnaryExpression: UnaryExpression;
  244. UpdateExpression: UpdateExpression;
  245. YieldExpression: YieldExpression;
  246. }
  247. export type Expression = ExpressionMap[keyof ExpressionMap];
  248. export interface BaseExpression extends BaseNode {}
  249. export type ChainElement = SimpleCallExpression | MemberExpression;
  250. export interface ChainExpression extends BaseExpression {
  251. type: "ChainExpression";
  252. expression: ChainElement;
  253. }
  254. export interface ThisExpression extends BaseExpression {
  255. type: "ThisExpression";
  256. }
  257. export interface ArrayExpression extends BaseExpression {
  258. type: "ArrayExpression";
  259. elements: Array<Expression | SpreadElement | null>;
  260. }
  261. export interface ObjectExpression extends BaseExpression {
  262. type: "ObjectExpression";
  263. properties: Array<Property | SpreadElement>;
  264. }
  265. export interface PrivateIdentifier extends BaseNode {
  266. type: "PrivateIdentifier";
  267. name: string;
  268. }
  269. export interface Property extends BaseNode {
  270. type: "Property";
  271. key: Expression | PrivateIdentifier;
  272. value: Expression | Pattern; // Could be an AssignmentProperty
  273. kind: "init" | "get" | "set";
  274. method: boolean;
  275. shorthand: boolean;
  276. computed: boolean;
  277. }
  278. export interface PropertyDefinition extends BaseNode {
  279. type: "PropertyDefinition";
  280. key: Expression | PrivateIdentifier;
  281. value?: Expression | null | undefined;
  282. computed: boolean;
  283. static: boolean;
  284. }
  285. export interface FunctionExpression extends BaseFunction, BaseExpression {
  286. id?: Identifier | null | undefined;
  287. type: "FunctionExpression";
  288. body: BlockStatement;
  289. }
  290. export interface SequenceExpression extends BaseExpression {
  291. type: "SequenceExpression";
  292. expressions: Expression[];
  293. }
  294. export interface UnaryExpression extends BaseExpression {
  295. type: "UnaryExpression";
  296. operator: UnaryOperator;
  297. prefix: true;
  298. argument: Expression;
  299. }
  300. export interface BinaryExpression extends BaseExpression {
  301. type: "BinaryExpression";
  302. operator: BinaryOperator;
  303. left: Expression | PrivateIdentifier;
  304. right: Expression;
  305. }
  306. export interface AssignmentExpression extends BaseExpression {
  307. type: "AssignmentExpression";
  308. operator: AssignmentOperator;
  309. left: Pattern | MemberExpression;
  310. right: Expression;
  311. }
  312. export interface UpdateExpression extends BaseExpression {
  313. type: "UpdateExpression";
  314. operator: UpdateOperator;
  315. argument: Expression;
  316. prefix: boolean;
  317. }
  318. export interface LogicalExpression extends BaseExpression {
  319. type: "LogicalExpression";
  320. operator: LogicalOperator;
  321. left: Expression;
  322. right: Expression;
  323. }
  324. export interface ConditionalExpression extends BaseExpression {
  325. type: "ConditionalExpression";
  326. test: Expression;
  327. alternate: Expression;
  328. consequent: Expression;
  329. }
  330. export interface BaseCallExpression extends BaseExpression {
  331. callee: Expression | Super;
  332. arguments: Array<Expression | SpreadElement>;
  333. }
  334. export type CallExpression = SimpleCallExpression | NewExpression;
  335. export interface SimpleCallExpression extends BaseCallExpression {
  336. type: "CallExpression";
  337. optional: boolean;
  338. }
  339. export interface NewExpression extends BaseCallExpression {
  340. type: "NewExpression";
  341. }
  342. export interface MemberExpression extends BaseExpression, BasePattern {
  343. type: "MemberExpression";
  344. object: Expression | Super;
  345. property: Expression | PrivateIdentifier;
  346. computed: boolean;
  347. optional: boolean;
  348. }
  349. export type Pattern = Identifier | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern | MemberExpression;
  350. export interface BasePattern extends BaseNode {}
  351. export interface SwitchCase extends BaseNode {
  352. type: "SwitchCase";
  353. test?: Expression | null | undefined;
  354. consequent: Statement[];
  355. }
  356. export interface CatchClause extends BaseNode {
  357. type: "CatchClause";
  358. param: Pattern | null;
  359. body: BlockStatement;
  360. }
  361. export interface Identifier extends BaseNode, BaseExpression, BasePattern {
  362. type: "Identifier";
  363. name: string;
  364. }
  365. export type Literal = SimpleLiteral | RegExpLiteral | BigIntLiteral;
  366. export interface SimpleLiteral extends BaseNode, BaseExpression {
  367. type: "Literal";
  368. value: string | boolean | number | null;
  369. raw?: string | undefined;
  370. }
  371. export interface RegExpLiteral extends BaseNode, BaseExpression {
  372. type: "Literal";
  373. value?: RegExp | null | undefined;
  374. regex: {
  375. pattern: string;
  376. flags: string;
  377. };
  378. raw?: string | undefined;
  379. }
  380. export interface BigIntLiteral extends BaseNode, BaseExpression {
  381. type: "Literal";
  382. value?: bigint | null | undefined;
  383. bigint: string;
  384. raw?: string | undefined;
  385. }
  386. export type UnaryOperator = "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
  387. export type BinaryOperator =
  388. | "=="
  389. | "!="
  390. | "==="
  391. | "!=="
  392. | "<"
  393. | "<="
  394. | ">"
  395. | ">="
  396. | "<<"
  397. | ">>"
  398. | ">>>"
  399. | "+"
  400. | "-"
  401. | "*"
  402. | "/"
  403. | "%"
  404. | "**"
  405. | "|"
  406. | "^"
  407. | "&"
  408. | "in"
  409. | "instanceof";
  410. export type LogicalOperator = "||" | "&&" | "??";
  411. export type AssignmentOperator =
  412. | "="
  413. | "+="
  414. | "-="
  415. | "*="
  416. | "/="
  417. | "%="
  418. | "**="
  419. | "<<="
  420. | ">>="
  421. | ">>>="
  422. | "|="
  423. | "^="
  424. | "&="
  425. | "||="
  426. | "&&="
  427. | "??=";
  428. export type UpdateOperator = "++" | "--";
  429. export interface ForOfStatement extends BaseForXStatement {
  430. type: "ForOfStatement";
  431. await: boolean;
  432. }
  433. export interface Super extends BaseNode {
  434. type: "Super";
  435. }
  436. export interface SpreadElement extends BaseNode {
  437. type: "SpreadElement";
  438. argument: Expression;
  439. }
  440. export interface ArrowFunctionExpression extends BaseExpression, BaseFunction {
  441. type: "ArrowFunctionExpression";
  442. expression: boolean;
  443. body: BlockStatement | Expression;
  444. }
  445. export interface YieldExpression extends BaseExpression {
  446. type: "YieldExpression";
  447. argument?: Expression | null | undefined;
  448. delegate: boolean;
  449. }
  450. export interface TemplateLiteral extends BaseExpression {
  451. type: "TemplateLiteral";
  452. quasis: TemplateElement[];
  453. expressions: Expression[];
  454. }
  455. export interface TaggedTemplateExpression extends BaseExpression {
  456. type: "TaggedTemplateExpression";
  457. tag: Expression;
  458. quasi: TemplateLiteral;
  459. }
  460. export interface TemplateElement extends BaseNode {
  461. type: "TemplateElement";
  462. tail: boolean;
  463. value: {
  464. /** It is null when the template literal is tagged and the text has an invalid escape (e.g. - tag`\unicode and \u{55}`) */
  465. cooked?: string | null | undefined;
  466. raw: string;
  467. };
  468. }
  469. export interface AssignmentProperty extends Property {
  470. value: Pattern;
  471. kind: "init";
  472. method: boolean; // false
  473. }
  474. export interface ObjectPattern extends BasePattern {
  475. type: "ObjectPattern";
  476. properties: Array<AssignmentProperty | RestElement>;
  477. }
  478. export interface ArrayPattern extends BasePattern {
  479. type: "ArrayPattern";
  480. elements: Array<Pattern | null>;
  481. }
  482. export interface RestElement extends BasePattern {
  483. type: "RestElement";
  484. argument: Pattern;
  485. }
  486. export interface AssignmentPattern extends BasePattern {
  487. type: "AssignmentPattern";
  488. left: Pattern;
  489. right: Expression;
  490. }
  491. export type Class = ClassDeclaration | ClassExpression;
  492. export interface BaseClass extends BaseNode {
  493. superClass?: Expression | null | undefined;
  494. body: ClassBody;
  495. }
  496. export interface ClassBody extends BaseNode {
  497. type: "ClassBody";
  498. body: Array<MethodDefinition | PropertyDefinition | StaticBlock>;
  499. }
  500. export interface MethodDefinition extends BaseNode {
  501. type: "MethodDefinition";
  502. key: Expression | PrivateIdentifier;
  503. value: FunctionExpression;
  504. kind: "constructor" | "method" | "get" | "set";
  505. computed: boolean;
  506. static: boolean;
  507. }
  508. export interface MaybeNamedClassDeclaration extends BaseClass, BaseDeclaration {
  509. type: "ClassDeclaration";
  510. /** It is null when a class declaration is a part of the `export default class` statement */
  511. id: Identifier | null;
  512. }
  513. export interface ClassDeclaration extends MaybeNamedClassDeclaration {
  514. id: Identifier;
  515. }
  516. export interface ClassExpression extends BaseClass, BaseExpression {
  517. type: "ClassExpression";
  518. id?: Identifier | null | undefined;
  519. }
  520. export interface MetaProperty extends BaseExpression {
  521. type: "MetaProperty";
  522. meta: Identifier;
  523. property: Identifier;
  524. }
  525. export type ModuleDeclaration =
  526. | ImportDeclaration
  527. | ExportNamedDeclaration
  528. | ExportDefaultDeclaration
  529. | ExportAllDeclaration;
  530. export interface BaseModuleDeclaration extends BaseNode {}
  531. export type ModuleSpecifier = ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier;
  532. export interface BaseModuleSpecifier extends BaseNode {
  533. local: Identifier;
  534. }
  535. export interface ImportDeclaration extends BaseModuleDeclaration {
  536. type: "ImportDeclaration";
  537. specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
  538. attributes: ImportAttribute[];
  539. source: Literal;
  540. }
  541. export interface ImportSpecifier extends BaseModuleSpecifier {
  542. type: "ImportSpecifier";
  543. imported: Identifier | Literal;
  544. }
  545. export interface ImportAttribute extends BaseNode {
  546. type: "ImportAttribute";
  547. key: Identifier | Literal;
  548. value: Literal;
  549. }
  550. export interface ImportExpression extends BaseExpression {
  551. type: "ImportExpression";
  552. source: Expression;
  553. options?: Expression | null | undefined;
  554. }
  555. export interface ImportDefaultSpecifier extends BaseModuleSpecifier {
  556. type: "ImportDefaultSpecifier";
  557. }
  558. export interface ImportNamespaceSpecifier extends BaseModuleSpecifier {
  559. type: "ImportNamespaceSpecifier";
  560. }
  561. export interface ExportNamedDeclaration extends BaseModuleDeclaration {
  562. type: "ExportNamedDeclaration";
  563. declaration?: Declaration | null | undefined;
  564. specifiers: ExportSpecifier[];
  565. attributes: ImportAttribute[];
  566. source?: Literal | null | undefined;
  567. }
  568. export interface ExportSpecifier extends Omit<BaseModuleSpecifier, "local"> {
  569. type: "ExportSpecifier";
  570. local: Identifier | Literal;
  571. exported: Identifier | Literal;
  572. }
  573. export interface ExportDefaultDeclaration extends BaseModuleDeclaration {
  574. type: "ExportDefaultDeclaration";
  575. declaration: MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | Expression;
  576. }
  577. export interface ExportAllDeclaration extends BaseModuleDeclaration {
  578. type: "ExportAllDeclaration";
  579. exported: Identifier | Literal | null;
  580. attributes: ImportAttribute[];
  581. source: Literal;
  582. }
  583. export interface AwaitExpression extends BaseExpression {
  584. type: "AwaitExpression";
  585. argument: Expression;
  586. }