stripIgnoredCharacters.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.stripIgnoredCharacters = stripIgnoredCharacters;
  6. var _source = require("../language/source.js");
  7. var _tokenKind = require("../language/tokenKind.js");
  8. var _lexer = require("../language/lexer.js");
  9. var _blockString = require("../language/blockString.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. * query SomeQuery($foo: String!, $bar: String) {
  33. * someField(foo: $foo, bar: $bar) {
  34. * a
  35. * b {
  36. * c
  37. * d
  38. * }
  39. * }
  40. * }
  41. *
  42. * Becomes:
  43. *
  44. * query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}
  45. *
  46. * SDL example:
  47. *
  48. * """
  49. * Type description
  50. * """
  51. * type Foo {
  52. * """
  53. * Field description
  54. * """
  55. * bar: String
  56. * }
  57. *
  58. * Becomes:
  59. *
  60. * """Type description""" type Foo{"""Field description""" bar:String}
  61. */
  62. function stripIgnoredCharacters(source) {
  63. var sourceObj = (0, _source.isSource)(source) ? source : new _source.Source(source);
  64. var body = sourceObj.body;
  65. var lexer = new _lexer.Lexer(sourceObj);
  66. var strippedBody = '';
  67. var wasLastAddedTokenNonPunctuator = false;
  68. while (lexer.advance().kind !== _tokenKind.TokenKind.EOF) {
  69. var currentToken = lexer.token;
  70. var tokenKind = currentToken.kind;
  71. /**
  72. * Every two non-punctuator tokens should have space between them.
  73. * Also prevent case of non-punctuator token following by spread resulting
  74. * in invalid token (e.g. `1...` is invalid Float token).
  75. */
  76. var isNonPunctuator = !(0, _lexer.isPunctuatorTokenKind)(currentToken.kind);
  77. if (wasLastAddedTokenNonPunctuator) {
  78. if (isNonPunctuator || currentToken.kind === _tokenKind.TokenKind.SPREAD) {
  79. strippedBody += ' ';
  80. }
  81. }
  82. var tokenBody = body.slice(currentToken.start, currentToken.end);
  83. if (tokenKind === _tokenKind.TokenKind.BLOCK_STRING) {
  84. strippedBody += dedentBlockString(tokenBody);
  85. } else {
  86. strippedBody += tokenBody;
  87. }
  88. wasLastAddedTokenNonPunctuator = isNonPunctuator;
  89. }
  90. return strippedBody;
  91. }
  92. function dedentBlockString(blockStr) {
  93. // skip leading and trailing triple quotations
  94. var rawStr = blockStr.slice(3, -3);
  95. var body = (0, _blockString.dedentBlockStringValue)(rawStr);
  96. if ((0, _blockString.getBlockStringIndentation)(body) > 0) {
  97. body = '\n' + body;
  98. }
  99. var lastChar = body[body.length - 1];
  100. var hasTrailingQuote = lastChar === '"' && body.slice(-4) !== '\\"""';
  101. if (hasTrailingQuote || lastChar === '\\') {
  102. body += '\n';
  103. }
  104. return '"""' + body + '"""';
  105. }