UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 4.69 kB
{"version":3,"file":"stripIgnoredCharacters.js","sourceRoot":"","sources":["../../src/utilities/stripIgnoredCharacters.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,oCAAmC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,8BAA6B;AACpE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,+BAA8B;AACzD,OAAO,EAAE,SAAS,EAAE,kCAAiC;AAoErD,MAAM,UAAU,sBAAsB,CAAC,MAAuB;IAC5D,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;IAEjE,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC;IAC5B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IACnC,IAAI,YAAY,GAAG,EAAE,CAAC;IAEtB,IAAI,8BAA8B,GAAG,KAAK,CAAC;IAC3C,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC,IAAI,KAAK,SAAS,CAAC,GAAG,EAAE,CAAC;QAC9C,MAAM,YAAY,GAAG,KAAK,CAAC,KAAK,CAAC;QACjC,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC;QASpC,MAAM,eAAe,GAAG,CAAC,qBAAqB,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QAClE,IAAI,8BAA8B,EAAE,CAAC;YACnC,IAAI,eAAe,IAAI,YAAY,CAAC,IAAI,KAAK,SAAS,CAAC,MAAM,EAAE,CAAC;gBAC9D,YAAY,IAAI,GAAG,CAAC;YACtB,CAAC;QACH,CAAC;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC;QACnE,IAAI,SAAS,KAAK,SAAS,CAAC,YAAY,EAAE,CAAC;YACzC,YAAY,IAAI,gBAAgB,CAAC,YAAY,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3E,CAAC;aAAM,CAAC;YACN,YAAY,IAAI,SAAS,CAAC;QAC5B,CAAC;QAED,8BAA8B,GAAG,eAAe,CAAC;IACnD,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC","sourcesContent":["/** @category AST Utilities */\n\nimport { printBlockString } from '../language/blockString.ts';\nimport { isPunctuatorTokenKind, Lexer } from '../language/lexer.ts';\nimport { isSource, Source } from '../language/source.ts';\nimport { TokenKind } from '../language/tokenKind.ts';\n\n/**\n * Strips characters that are not significant to the validity or execution\n * of a GraphQL document:\n * - UnicodeBOM\n * - WhiteSpace\n * - LineTerminator\n * - Comment\n * - Comma\n * - BlockString indentation\n *\n * Note: It is required to have a delimiter character between neighboring\n * non-punctuator tokens and this function always uses single space as delimiter.\n *\n * It is guaranteed that both input and output documents if parsed would result\n * in the exact same AST except for nodes location.\n *\n * Warning: It is guaranteed that this function will always produce stable results.\n * However, it's not guaranteed that it will stay the same between different\n * releases due to bugfixes or changes in the GraphQL specification.\n * @param source - The GraphQL source text or source object.\n * @returns A semantically equivalent GraphQL source string without ignored characters.\n * @example Query source\n * ```graphql\n * query SomeQuery($foo: String!, $bar: String) {\n * someField(foo: $foo, bar: $bar) {\n * a\n * b {\n * c\n * d\n * }\n * }\n * }\n * ```\n *\n * Becomes:\n *\n * ```graphql prettier-ignore\n * query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}\n * ```\n * @example SDL source\n * ```graphql\n * \"\"\"\n * Type description\n * \"\"\"\n * type Foo {\n * \"\"\"\n * Field description\n * \"\"\"\n * bar: String\n * }\n * ```\n *\n * Becomes:\n *\n * ```graphql prettier-ignore\n * \"\"\"Type description\"\"\" type Foo{\"\"\"Field description\"\"\" bar:String}\n * ```\n * @example\n * ```ts\n * import { stripIgnoredCharacters } from 'graphql/utilities';\n *\n * const source = stripIgnoredCharacters('query Example { name }');\n *\n * source; // => 'query Example{name}'\n * ```\n */\nexport function stripIgnoredCharacters(source: string | Source): string {\n const sourceObj = isSource(source) ? source : new Source(source);\n\n const body = sourceObj.body;\n const lexer = new Lexer(sourceObj);\n let strippedBody = '';\n\n let wasLastAddedTokenNonPunctuator = false;\n while (lexer.advance().kind !== TokenKind.EOF) {\n const currentToken = lexer.token;\n const tokenKind = currentToken.kind;\n\n /**\n * Every two non-punctuator tokens should have space between them.\n * Also prevent case of non-punctuator token following by spread resulting\n * in invalid token (e.g. `1...` is invalid Float token).\n *\n * @internal\n */\n const isNonPunctuator = !isPunctuatorTokenKind(currentToken.kind);\n if (wasLastAddedTokenNonPunctuator) {\n if (isNonPunctuator || currentToken.kind === TokenKind.SPREAD) {\n strippedBody += ' ';\n }\n }\n\n const tokenBody = body.slice(currentToken.start, currentToken.end);\n if (tokenKind === TokenKind.BLOCK_STRING) {\n strippedBody += printBlockString(currentToken.value, { minimize: true });\n } else {\n strippedBody += tokenBody;\n }\n\n wasLastAddedTokenNonPunctuator = isNonPunctuator;\n }\n\n return strippedBody;\n}\n"]}