ast.js.flow 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // @flow strict
  2. import defineInspect from '../jsutils/defineInspect';
  3. import type { Source } from './source';
  4. import type { TokenKindEnum } from './tokenKind';
  5. /**
  6. * Contains a range of UTF-8 character offsets and token references that
  7. * identify the region of the source from which the AST derived.
  8. */
  9. export class Location {
  10. /**
  11. * The character offset at which this Node begins.
  12. */
  13. +start: number;
  14. /**
  15. * The character offset at which this Node ends.
  16. */
  17. +end: number;
  18. /**
  19. * The Token at which this Node begins.
  20. */
  21. +startToken: Token;
  22. /**
  23. * The Token at which this Node ends.
  24. */
  25. +endToken: Token;
  26. /**
  27. * The Source document the AST represents.
  28. */
  29. +source: Source;
  30. constructor(startToken: Token, endToken: Token, source: Source) {
  31. this.start = startToken.start;
  32. this.end = endToken.end;
  33. this.startToken = startToken;
  34. this.endToken = endToken;
  35. this.source = source;
  36. }
  37. toJSON(): {| start: number, end: number |} {
  38. return { start: this.start, end: this.end };
  39. }
  40. }
  41. // Print a simplified form when appearing in `inspect` and `util.inspect`.
  42. defineInspect(Location);
  43. /**
  44. * Represents a range of characters represented by a lexical token
  45. * within a Source.
  46. */
  47. export class Token {
  48. /**
  49. * The kind of Token.
  50. */
  51. +kind: TokenKindEnum;
  52. /**
  53. * The character offset at which this Node begins.
  54. */
  55. +start: number;
  56. /**
  57. * The character offset at which this Node ends.
  58. */
  59. +end: number;
  60. /**
  61. * The 1-indexed line number on which this Token appears.
  62. */
  63. +line: number;
  64. /**
  65. * The 1-indexed column number at which this Token begins.
  66. */
  67. +column: number;
  68. /**
  69. * For non-punctuation tokens, represents the interpreted value of the token.
  70. */
  71. +value: string | void;
  72. /**
  73. * Tokens exist as nodes in a double-linked-list amongst all tokens
  74. * including ignored tokens. <SOF> is always the first node and <EOF>
  75. * the last.
  76. */
  77. +prev: Token | null;
  78. +next: Token | null;
  79. constructor(
  80. kind: TokenKindEnum,
  81. start: number,
  82. end: number,
  83. line: number,
  84. column: number,
  85. prev: Token | null,
  86. value?: string,
  87. ) {
  88. this.kind = kind;
  89. this.start = start;
  90. this.end = end;
  91. this.line = line;
  92. this.column = column;
  93. this.value = value;
  94. this.prev = prev;
  95. this.next = null;
  96. }
  97. toJSON(): {|
  98. kind: TokenKindEnum,
  99. value: string | void,
  100. line: number,
  101. column: number,
  102. |} {
  103. return {
  104. kind: this.kind,
  105. value: this.value,
  106. line: this.line,
  107. column: this.column,
  108. };
  109. }
  110. }
  111. // Print a simplified form when appearing in `inspect` and `util.inspect`.
  112. defineInspect(Token);
  113. /**
  114. * @internal
  115. */
  116. export function isNode(maybeNode: mixed): boolean %checks {
  117. return maybeNode != null && typeof maybeNode.kind === 'string';
  118. }
  119. /**
  120. * The list of all possible AST node types.
  121. */
  122. export type ASTNode =
  123. | NameNode
  124. | DocumentNode
  125. | OperationDefinitionNode
  126. | VariableDefinitionNode
  127. | VariableNode
  128. | SelectionSetNode
  129. | FieldNode
  130. | ArgumentNode
  131. | FragmentSpreadNode
  132. | InlineFragmentNode
  133. | FragmentDefinitionNode
  134. | IntValueNode
  135. | FloatValueNode
  136. | StringValueNode
  137. | BooleanValueNode
  138. | NullValueNode
  139. | EnumValueNode
  140. | ListValueNode
  141. | ObjectValueNode
  142. | ObjectFieldNode
  143. | DirectiveNode
  144. | NamedTypeNode
  145. | ListTypeNode
  146. | NonNullTypeNode
  147. | SchemaDefinitionNode
  148. | OperationTypeDefinitionNode
  149. | ScalarTypeDefinitionNode
  150. | ObjectTypeDefinitionNode
  151. | FieldDefinitionNode
  152. | InputValueDefinitionNode
  153. | InterfaceTypeDefinitionNode
  154. | UnionTypeDefinitionNode
  155. | EnumTypeDefinitionNode
  156. | EnumValueDefinitionNode
  157. | InputObjectTypeDefinitionNode
  158. | DirectiveDefinitionNode
  159. | SchemaExtensionNode
  160. | ScalarTypeExtensionNode
  161. | ObjectTypeExtensionNode
  162. | InterfaceTypeExtensionNode
  163. | UnionTypeExtensionNode
  164. | EnumTypeExtensionNode
  165. | InputObjectTypeExtensionNode;
  166. /**
  167. * Utility type listing all nodes indexed by their kind.
  168. */
  169. export type ASTKindToNode = {|
  170. Name: NameNode,
  171. Document: DocumentNode,
  172. OperationDefinition: OperationDefinitionNode,
  173. VariableDefinition: VariableDefinitionNode,
  174. Variable: VariableNode,
  175. SelectionSet: SelectionSetNode,
  176. Field: FieldNode,
  177. Argument: ArgumentNode,
  178. FragmentSpread: FragmentSpreadNode,
  179. InlineFragment: InlineFragmentNode,
  180. FragmentDefinition: FragmentDefinitionNode,
  181. IntValue: IntValueNode,
  182. FloatValue: FloatValueNode,
  183. StringValue: StringValueNode,
  184. BooleanValue: BooleanValueNode,
  185. NullValue: NullValueNode,
  186. EnumValue: EnumValueNode,
  187. ListValue: ListValueNode,
  188. ObjectValue: ObjectValueNode,
  189. ObjectField: ObjectFieldNode,
  190. Directive: DirectiveNode,
  191. NamedType: NamedTypeNode,
  192. ListType: ListTypeNode,
  193. NonNullType: NonNullTypeNode,
  194. SchemaDefinition: SchemaDefinitionNode,
  195. OperationTypeDefinition: OperationTypeDefinitionNode,
  196. ScalarTypeDefinition: ScalarTypeDefinitionNode,
  197. ObjectTypeDefinition: ObjectTypeDefinitionNode,
  198. FieldDefinition: FieldDefinitionNode,
  199. InputValueDefinition: InputValueDefinitionNode,
  200. InterfaceTypeDefinition: InterfaceTypeDefinitionNode,
  201. UnionTypeDefinition: UnionTypeDefinitionNode,
  202. EnumTypeDefinition: EnumTypeDefinitionNode,
  203. EnumValueDefinition: EnumValueDefinitionNode,
  204. InputObjectTypeDefinition: InputObjectTypeDefinitionNode,
  205. DirectiveDefinition: DirectiveDefinitionNode,
  206. SchemaExtension: SchemaExtensionNode,
  207. ScalarTypeExtension: ScalarTypeExtensionNode,
  208. ObjectTypeExtension: ObjectTypeExtensionNode,
  209. InterfaceTypeExtension: InterfaceTypeExtensionNode,
  210. UnionTypeExtension: UnionTypeExtensionNode,
  211. EnumTypeExtension: EnumTypeExtensionNode,
  212. InputObjectTypeExtension: InputObjectTypeExtensionNode,
  213. |};
  214. // Name
  215. export type NameNode = {|
  216. +kind: 'Name',
  217. +loc?: Location,
  218. +value: string,
  219. |};
  220. // Document
  221. export type DocumentNode = {|
  222. +kind: 'Document',
  223. +loc?: Location,
  224. +definitions: $ReadOnlyArray<DefinitionNode>,
  225. |};
  226. export type DefinitionNode =
  227. | ExecutableDefinitionNode
  228. | TypeSystemDefinitionNode
  229. | TypeSystemExtensionNode;
  230. export type ExecutableDefinitionNode =
  231. | OperationDefinitionNode
  232. | FragmentDefinitionNode;
  233. export type OperationDefinitionNode = {|
  234. +kind: 'OperationDefinition',
  235. +loc?: Location,
  236. +operation: OperationTypeNode,
  237. +name?: NameNode,
  238. +variableDefinitions?: $ReadOnlyArray<VariableDefinitionNode>,
  239. +directives?: $ReadOnlyArray<DirectiveNode>,
  240. +selectionSet: SelectionSetNode,
  241. |};
  242. export type OperationTypeNode = 'query' | 'mutation' | 'subscription';
  243. export type VariableDefinitionNode = {|
  244. +kind: 'VariableDefinition',
  245. +loc?: Location,
  246. +variable: VariableNode,
  247. +type: TypeNode,
  248. +defaultValue?: ValueNode,
  249. +directives?: $ReadOnlyArray<DirectiveNode>,
  250. |};
  251. export type VariableNode = {|
  252. +kind: 'Variable',
  253. +loc?: Location,
  254. +name: NameNode,
  255. |};
  256. export type SelectionSetNode = {|
  257. kind: 'SelectionSet',
  258. loc?: Location,
  259. selections: $ReadOnlyArray<SelectionNode>,
  260. |};
  261. export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode;
  262. export type FieldNode = {|
  263. +kind: 'Field',
  264. +loc?: Location,
  265. +alias?: NameNode,
  266. +name: NameNode,
  267. +arguments?: $ReadOnlyArray<ArgumentNode>,
  268. +directives?: $ReadOnlyArray<DirectiveNode>,
  269. +selectionSet?: SelectionSetNode,
  270. |};
  271. export type ArgumentNode = {|
  272. +kind: 'Argument',
  273. +loc?: Location,
  274. +name: NameNode,
  275. +value: ValueNode,
  276. |};
  277. // Fragments
  278. export type FragmentSpreadNode = {|
  279. +kind: 'FragmentSpread',
  280. +loc?: Location,
  281. +name: NameNode,
  282. +directives?: $ReadOnlyArray<DirectiveNode>,
  283. |};
  284. export type InlineFragmentNode = {|
  285. +kind: 'InlineFragment',
  286. +loc?: Location,
  287. +typeCondition?: NamedTypeNode,
  288. +directives?: $ReadOnlyArray<DirectiveNode>,
  289. +selectionSet: SelectionSetNode,
  290. |};
  291. export type FragmentDefinitionNode = {|
  292. +kind: 'FragmentDefinition',
  293. +loc?: Location,
  294. +name: NameNode,
  295. // Note: fragment variable definitions are experimental and may be changed
  296. // or removed in the future.
  297. +variableDefinitions?: $ReadOnlyArray<VariableDefinitionNode>,
  298. +typeCondition: NamedTypeNode,
  299. +directives?: $ReadOnlyArray<DirectiveNode>,
  300. +selectionSet: SelectionSetNode,
  301. |};
  302. // Values
  303. export type ValueNode =
  304. | VariableNode
  305. | IntValueNode
  306. | FloatValueNode
  307. | StringValueNode
  308. | BooleanValueNode
  309. | NullValueNode
  310. | EnumValueNode
  311. | ListValueNode
  312. | ObjectValueNode;
  313. export type IntValueNode = {|
  314. +kind: 'IntValue',
  315. +loc?: Location,
  316. +value: string,
  317. |};
  318. export type FloatValueNode = {|
  319. +kind: 'FloatValue',
  320. +loc?: Location,
  321. +value: string,
  322. |};
  323. export type StringValueNode = {|
  324. +kind: 'StringValue',
  325. +loc?: Location,
  326. +value: string,
  327. +block?: boolean,
  328. |};
  329. export type BooleanValueNode = {|
  330. +kind: 'BooleanValue',
  331. +loc?: Location,
  332. +value: boolean,
  333. |};
  334. export type NullValueNode = {|
  335. +kind: 'NullValue',
  336. +loc?: Location,
  337. |};
  338. export type EnumValueNode = {|
  339. +kind: 'EnumValue',
  340. +loc?: Location,
  341. +value: string,
  342. |};
  343. export type ListValueNode = {|
  344. +kind: 'ListValue',
  345. +loc?: Location,
  346. +values: $ReadOnlyArray<ValueNode>,
  347. |};
  348. export type ObjectValueNode = {|
  349. +kind: 'ObjectValue',
  350. +loc?: Location,
  351. +fields: $ReadOnlyArray<ObjectFieldNode>,
  352. |};
  353. export type ObjectFieldNode = {|
  354. +kind: 'ObjectField',
  355. +loc?: Location,
  356. +name: NameNode,
  357. +value: ValueNode,
  358. |};
  359. // Directives
  360. export type DirectiveNode = {|
  361. +kind: 'Directive',
  362. +loc?: Location,
  363. +name: NameNode,
  364. +arguments?: $ReadOnlyArray<ArgumentNode>,
  365. |};
  366. // Type Reference
  367. export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode;
  368. export type NamedTypeNode = {|
  369. +kind: 'NamedType',
  370. +loc?: Location,
  371. +name: NameNode,
  372. |};
  373. export type ListTypeNode = {|
  374. +kind: 'ListType',
  375. +loc?: Location,
  376. +type: TypeNode,
  377. |};
  378. export type NonNullTypeNode = {|
  379. +kind: 'NonNullType',
  380. +loc?: Location,
  381. +type: NamedTypeNode | ListTypeNode,
  382. |};
  383. // Type System Definition
  384. export type TypeSystemDefinitionNode =
  385. | SchemaDefinitionNode
  386. | TypeDefinitionNode
  387. | DirectiveDefinitionNode;
  388. export type SchemaDefinitionNode = {|
  389. +kind: 'SchemaDefinition',
  390. +loc?: Location,
  391. +description?: StringValueNode,
  392. +directives?: $ReadOnlyArray<DirectiveNode>,
  393. +operationTypes: $ReadOnlyArray<OperationTypeDefinitionNode>,
  394. |};
  395. export type OperationTypeDefinitionNode = {|
  396. +kind: 'OperationTypeDefinition',
  397. +loc?: Location,
  398. +operation: OperationTypeNode,
  399. +type: NamedTypeNode,
  400. |};
  401. // Type Definition
  402. export type TypeDefinitionNode =
  403. | ScalarTypeDefinitionNode
  404. | ObjectTypeDefinitionNode
  405. | InterfaceTypeDefinitionNode
  406. | UnionTypeDefinitionNode
  407. | EnumTypeDefinitionNode
  408. | InputObjectTypeDefinitionNode;
  409. export type ScalarTypeDefinitionNode = {|
  410. +kind: 'ScalarTypeDefinition',
  411. +loc?: Location,
  412. +description?: StringValueNode,
  413. +name: NameNode,
  414. +directives?: $ReadOnlyArray<DirectiveNode>,
  415. |};
  416. export type ObjectTypeDefinitionNode = {|
  417. +kind: 'ObjectTypeDefinition',
  418. +loc?: Location,
  419. +description?: StringValueNode,
  420. +name: NameNode,
  421. +interfaces?: $ReadOnlyArray<NamedTypeNode>,
  422. +directives?: $ReadOnlyArray<DirectiveNode>,
  423. +fields?: $ReadOnlyArray<FieldDefinitionNode>,
  424. |};
  425. export type FieldDefinitionNode = {|
  426. +kind: 'FieldDefinition',
  427. +loc?: Location,
  428. +description?: StringValueNode,
  429. +name: NameNode,
  430. +arguments?: $ReadOnlyArray<InputValueDefinitionNode>,
  431. +type: TypeNode,
  432. +directives?: $ReadOnlyArray<DirectiveNode>,
  433. |};
  434. export type InputValueDefinitionNode = {|
  435. +kind: 'InputValueDefinition',
  436. +loc?: Location,
  437. +description?: StringValueNode,
  438. +name: NameNode,
  439. +type: TypeNode,
  440. +defaultValue?: ValueNode,
  441. +directives?: $ReadOnlyArray<DirectiveNode>,
  442. |};
  443. export type InterfaceTypeDefinitionNode = {|
  444. +kind: 'InterfaceTypeDefinition',
  445. +loc?: Location,
  446. +description?: StringValueNode,
  447. +name: NameNode,
  448. +interfaces?: $ReadOnlyArray<NamedTypeNode>,
  449. +directives?: $ReadOnlyArray<DirectiveNode>,
  450. +fields?: $ReadOnlyArray<FieldDefinitionNode>,
  451. |};
  452. export type UnionTypeDefinitionNode = {|
  453. +kind: 'UnionTypeDefinition',
  454. +loc?: Location,
  455. +description?: StringValueNode,
  456. +name: NameNode,
  457. +directives?: $ReadOnlyArray<DirectiveNode>,
  458. +types?: $ReadOnlyArray<NamedTypeNode>,
  459. |};
  460. export type EnumTypeDefinitionNode = {|
  461. +kind: 'EnumTypeDefinition',
  462. +loc?: Location,
  463. +description?: StringValueNode,
  464. +name: NameNode,
  465. +directives?: $ReadOnlyArray<DirectiveNode>,
  466. +values?: $ReadOnlyArray<EnumValueDefinitionNode>,
  467. |};
  468. export type EnumValueDefinitionNode = {|
  469. +kind: 'EnumValueDefinition',
  470. +loc?: Location,
  471. +description?: StringValueNode,
  472. +name: NameNode,
  473. +directives?: $ReadOnlyArray<DirectiveNode>,
  474. |};
  475. export type InputObjectTypeDefinitionNode = {|
  476. +kind: 'InputObjectTypeDefinition',
  477. +loc?: Location,
  478. +description?: StringValueNode,
  479. +name: NameNode,
  480. +directives?: $ReadOnlyArray<DirectiveNode>,
  481. +fields?: $ReadOnlyArray<InputValueDefinitionNode>,
  482. |};
  483. // Directive Definitions
  484. export type DirectiveDefinitionNode = {|
  485. +kind: 'DirectiveDefinition',
  486. +loc?: Location,
  487. +description?: StringValueNode,
  488. +name: NameNode,
  489. +arguments?: $ReadOnlyArray<InputValueDefinitionNode>,
  490. +repeatable: boolean,
  491. +locations: $ReadOnlyArray<NameNode>,
  492. |};
  493. // Type System Extensions
  494. export type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode;
  495. export type SchemaExtensionNode = {|
  496. +kind: 'SchemaExtension',
  497. +loc?: Location,
  498. +directives?: $ReadOnlyArray<DirectiveNode>,
  499. +operationTypes?: $ReadOnlyArray<OperationTypeDefinitionNode>,
  500. |};
  501. // Type Extensions
  502. export type TypeExtensionNode =
  503. | ScalarTypeExtensionNode
  504. | ObjectTypeExtensionNode
  505. | InterfaceTypeExtensionNode
  506. | UnionTypeExtensionNode
  507. | EnumTypeExtensionNode
  508. | InputObjectTypeExtensionNode;
  509. export type ScalarTypeExtensionNode = {|
  510. +kind: 'ScalarTypeExtension',
  511. +loc?: Location,
  512. +name: NameNode,
  513. +directives?: $ReadOnlyArray<DirectiveNode>,
  514. |};
  515. export type ObjectTypeExtensionNode = {|
  516. +kind: 'ObjectTypeExtension',
  517. +loc?: Location,
  518. +name: NameNode,
  519. +interfaces?: $ReadOnlyArray<NamedTypeNode>,
  520. +directives?: $ReadOnlyArray<DirectiveNode>,
  521. +fields?: $ReadOnlyArray<FieldDefinitionNode>,
  522. |};
  523. export type InterfaceTypeExtensionNode = {|
  524. +kind: 'InterfaceTypeExtension',
  525. +loc?: Location,
  526. +name: NameNode,
  527. +interfaces?: $ReadOnlyArray<NamedTypeNode>,
  528. +directives?: $ReadOnlyArray<DirectiveNode>,
  529. +fields?: $ReadOnlyArray<FieldDefinitionNode>,
  530. |};
  531. export type UnionTypeExtensionNode = {|
  532. +kind: 'UnionTypeExtension',
  533. +loc?: Location,
  534. +name: NameNode,
  535. +directives?: $ReadOnlyArray<DirectiveNode>,
  536. +types?: $ReadOnlyArray<NamedTypeNode>,
  537. |};
  538. export type EnumTypeExtensionNode = {|
  539. +kind: 'EnumTypeExtension',
  540. +loc?: Location,
  541. +name: NameNode,
  542. +directives?: $ReadOnlyArray<DirectiveNode>,
  543. +values?: $ReadOnlyArray<EnumValueDefinitionNode>,
  544. |};
  545. export type InputObjectTypeExtensionNode = {|
  546. +kind: 'InputObjectTypeExtension',
  547. +loc?: Location,
  548. +name: NameNode,
  549. +directives?: $ReadOnlyArray<DirectiveNode>,
  550. +fields?: $ReadOnlyArray<InputValueDefinitionNode>,
  551. |};