stripIgnoredCharacters.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.stripIgnoredCharacters = stripIgnoredCharacters;
  6. var _blockString = require('../language/blockString.js');
  7. var _lexer = require('../language/lexer.js');
  8. var _source = require('../language/source.js');
  9. var _tokenKind = require('../language/tokenKind.js');
  10. /**
  11. * Strips characters that are not significant to the validity or execution
  12. * of a GraphQL document:
  13. * - UnicodeBOM
  14. * - WhiteSpace
  15. * - LineTerminator
  16. * - Comment
  17. * - Comma
  18. * - BlockString indentation
  19. *
  20. * Note: It is required to have a delimiter character between neighboring
  21. * non-punctuator tokens and this function always uses single space as delimiter.
  22. *
  23. * It is guaranteed that both input and output documents if parsed would result
  24. * in the exact same AST except for nodes location.
  25. *
  26. * Warning: It is guaranteed that this function will always produce stable results.
  27. * However, it's not guaranteed that it will stay the same between different
  28. * releases due to bugfixes or changes in the GraphQL specification.
  29. *
  30. * Query example:
  31. *
  32. * ```graphql
  33. * query SomeQuery($foo: String!, $bar: String) {
  34. * someField(foo: $foo, bar: $bar) {
  35. * a
  36. * b {
  37. * c
  38. * d
  39. * }
  40. * }
  41. * }
  42. * ```
  43. *
  44. * Becomes:
  45. *
  46. * ```graphql
  47. * query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}
  48. * ```
  49. *
  50. * SDL example:
  51. *
  52. * ```graphql
  53. * """
  54. * Type description
  55. * """
  56. * type Foo {
  57. * """
  58. * Field description
  59. * """
  60. * bar: String
  61. * }
  62. * ```
  63. *
  64. * Becomes:
  65. *
  66. * ```graphql
  67. * """Type description""" type Foo{"""Field description""" bar:String}
  68. * ```
  69. */
  70. function stripIgnoredCharacters(source) {
  71. const sourceObj = (0, _source.isSource)(source)
  72. ? source
  73. : new _source.Source(source);
  74. const body = sourceObj.body;
  75. const lexer = new _lexer.Lexer(sourceObj);
  76. let strippedBody = '';
  77. let wasLastAddedTokenNonPunctuator = false;
  78. while (lexer.advance().kind !== _tokenKind.TokenKind.EOF) {
  79. const currentToken = lexer.token;
  80. const tokenKind = currentToken.kind;
  81. /**
  82. * Every two non-punctuator tokens should have space between them.
  83. * Also prevent case of non-punctuator token following by spread resulting
  84. * in invalid token (e.g. `1...` is invalid Float token).
  85. */
  86. const isNonPunctuator = !(0, _lexer.isPunctuatorTokenKind)(
  87. currentToken.kind,
  88. );
  89. if (wasLastAddedTokenNonPunctuator) {
  90. if (
  91. isNonPunctuator ||
  92. currentToken.kind === _tokenKind.TokenKind.SPREAD
  93. ) {
  94. strippedBody += ' ';
  95. }
  96. }
  97. const tokenBody = body.slice(currentToken.start, currentToken.end);
  98. if (tokenKind === _tokenKind.TokenKind.BLOCK_STRING) {
  99. strippedBody += (0, _blockString.printBlockString)(currentToken.value, {
  100. minimize: true,
  101. });
  102. } else {
  103. strippedBody += tokenBody;
  104. }
  105. wasLastAddedTokenNonPunctuator = isNonPunctuator;
  106. }
  107. return strippedBody;
  108. }