ast.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.Token =
  6. exports.QueryDocumentKeys =
  7. exports.OperationTypeNode =
  8. exports.Location =
  9. void 0;
  10. exports.isNode = isNode;
  11. /**
  12. * Contains a range of UTF-8 character offsets and token references that
  13. * identify the region of the source from which the AST derived.
  14. */
  15. class Location {
  16. /**
  17. * The character offset at which this Node begins.
  18. */
  19. /**
  20. * The character offset at which this Node ends.
  21. */
  22. /**
  23. * The Token at which this Node begins.
  24. */
  25. /**
  26. * The Token at which this Node ends.
  27. */
  28. /**
  29. * The Source document the AST represents.
  30. */
  31. constructor(startToken, endToken, source) {
  32. this.start = startToken.start;
  33. this.end = endToken.end;
  34. this.startToken = startToken;
  35. this.endToken = endToken;
  36. this.source = source;
  37. }
  38. get [Symbol.toStringTag]() {
  39. return 'Location';
  40. }
  41. toJSON() {
  42. return {
  43. start: this.start,
  44. end: this.end,
  45. };
  46. }
  47. }
  48. /**
  49. * Represents a range of characters represented by a lexical token
  50. * within a Source.
  51. */
  52. exports.Location = Location;
  53. class Token {
  54. /**
  55. * The kind of Token.
  56. */
  57. /**
  58. * The character offset at which this Node begins.
  59. */
  60. /**
  61. * The character offset at which this Node ends.
  62. */
  63. /**
  64. * The 1-indexed line number on which this Token appears.
  65. */
  66. /**
  67. * The 1-indexed column number at which this Token begins.
  68. */
  69. /**
  70. * For non-punctuation tokens, represents the interpreted value of the token.
  71. *
  72. * Note: is undefined for punctuation tokens, but typed as string for
  73. * convenience in the parser.
  74. */
  75. /**
  76. * Tokens exist as nodes in a double-linked-list amongst all tokens
  77. * including ignored tokens. <SOF> is always the first node and <EOF>
  78. * the last.
  79. */
  80. constructor(kind, start, end, line, column, value) {
  81. this.kind = kind;
  82. this.start = start;
  83. this.end = end;
  84. this.line = line;
  85. this.column = column; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  86. this.value = value;
  87. this.prev = null;
  88. this.next = null;
  89. }
  90. get [Symbol.toStringTag]() {
  91. return 'Token';
  92. }
  93. toJSON() {
  94. return {
  95. kind: this.kind,
  96. value: this.value,
  97. line: this.line,
  98. column: this.column,
  99. };
  100. }
  101. }
  102. /**
  103. * The list of all possible AST node types.
  104. */
  105. exports.Token = Token;
  106. /**
  107. * @internal
  108. */
  109. const QueryDocumentKeys = {
  110. Name: [],
  111. Document: ['definitions'],
  112. OperationDefinition: [
  113. 'name',
  114. 'variableDefinitions',
  115. 'directives',
  116. 'selectionSet',
  117. ],
  118. VariableDefinition: ['variable', 'type', 'defaultValue', 'directives'],
  119. Variable: ['name'],
  120. SelectionSet: ['selections'],
  121. Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
  122. Argument: ['name', 'value'],
  123. FragmentSpread: ['name', 'directives'],
  124. InlineFragment: ['typeCondition', 'directives', 'selectionSet'],
  125. FragmentDefinition: [
  126. 'name', // Note: fragment variable definitions are deprecated and will removed in v17.0.0
  127. 'variableDefinitions',
  128. 'typeCondition',
  129. 'directives',
  130. 'selectionSet',
  131. ],
  132. IntValue: [],
  133. FloatValue: [],
  134. StringValue: [],
  135. BooleanValue: [],
  136. NullValue: [],
  137. EnumValue: [],
  138. ListValue: ['values'],
  139. ObjectValue: ['fields'],
  140. ObjectField: ['name', 'value'],
  141. Directive: ['name', 'arguments'],
  142. NamedType: ['name'],
  143. ListType: ['type'],
  144. NonNullType: ['type'],
  145. SchemaDefinition: ['description', 'directives', 'operationTypes'],
  146. OperationTypeDefinition: ['type'],
  147. ScalarTypeDefinition: ['description', 'name', 'directives'],
  148. ObjectTypeDefinition: [
  149. 'description',
  150. 'name',
  151. 'interfaces',
  152. 'directives',
  153. 'fields',
  154. ],
  155. FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'],
  156. InputValueDefinition: [
  157. 'description',
  158. 'name',
  159. 'type',
  160. 'defaultValue',
  161. 'directives',
  162. ],
  163. InterfaceTypeDefinition: [
  164. 'description',
  165. 'name',
  166. 'interfaces',
  167. 'directives',
  168. 'fields',
  169. ],
  170. UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
  171. EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
  172. EnumValueDefinition: ['description', 'name', 'directives'],
  173. InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
  174. DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
  175. SchemaExtension: ['directives', 'operationTypes'],
  176. ScalarTypeExtension: ['name', 'directives'],
  177. ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
  178. InterfaceTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
  179. UnionTypeExtension: ['name', 'directives', 'types'],
  180. EnumTypeExtension: ['name', 'directives', 'values'],
  181. InputObjectTypeExtension: ['name', 'directives', 'fields'],
  182. };
  183. exports.QueryDocumentKeys = QueryDocumentKeys;
  184. const kindValues = new Set(Object.keys(QueryDocumentKeys));
  185. /**
  186. * @internal
  187. */
  188. function isNode(maybeNode) {
  189. const maybeKind =
  190. maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.kind;
  191. return typeof maybeKind === 'string' && kindValues.has(maybeKind);
  192. }
  193. /** Name */
  194. var OperationTypeNode;
  195. exports.OperationTypeNode = OperationTypeNode;
  196. (function (OperationTypeNode) {
  197. OperationTypeNode['QUERY'] = 'query';
  198. OperationTypeNode['MUTATION'] = 'mutation';
  199. OperationTypeNode['SUBSCRIPTION'] = 'subscription';
  200. })(OperationTypeNode || (exports.OperationTypeNode = OperationTypeNode = {}));