|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +from ..language import Lexer, Source, TokenKind |
| 4 | +from ..language.block_string import ( |
| 5 | + dedent_block_string_value, |
| 6 | + get_block_string_indentation, |
| 7 | +) |
| 8 | +from ..language.lexer import is_punctuator_token |
| 9 | +from ..pyutils import inspect |
| 10 | + |
| 11 | + |
| 12 | +def strip_ignored_characters(source: Union[str, Source]) -> str: |
| 13 | + """Strip characters that are ignored anyway. |
| 14 | +
|
| 15 | + Strips characters that are not significant to the validity or execution |
| 16 | + of a GraphQL document: |
| 17 | + - UnicodeBOM |
| 18 | + - WhiteSpace |
| 19 | + - LineTerminator |
| 20 | + - Comment |
| 21 | + - Comma |
| 22 | + - BlockString indentation |
| 23 | +
|
| 24 | + Note: It is required to have a delimiter character between neighboring |
| 25 | + non-punctuator tokes and this function always uses single space as delimiter. |
| 26 | +
|
| 27 | + It is guaranteed that both input and output documents if parsed would result |
| 28 | + in the exact same AST except for nodes location. |
| 29 | +
|
| 30 | + Warning: It is guaranteed that this function will always produce stable results. |
| 31 | + However, it's not guaranteed that it will stay the same between different |
| 32 | + releases due to bugfixes or changes in the GraphQL specification. |
| 33 | + """ ''' |
| 34 | +
|
| 35 | + Query example:: |
| 36 | +
|
| 37 | + query SomeQuery($foo: String!, $bar: String) { |
| 38 | + someField(foo: $foo, bar: $bar) { |
| 39 | + a |
| 40 | + b { |
| 41 | + c |
| 42 | + d |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +
|
| 47 | + Becomes:: |
| 48 | +
|
| 49 | + query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}} |
| 50 | +
|
| 51 | + SDL example: |
| 52 | +
|
| 53 | + """ |
| 54 | + Type description |
| 55 | + """ |
| 56 | + type Foo { |
| 57 | + """ |
| 58 | + Field description |
| 59 | + """ |
| 60 | + bar: String |
| 61 | + } |
| 62 | +
|
| 63 | + Becomes:: |
| 64 | +
|
| 65 | + """Type description""" type Foo{"""Field description""" bar:String} |
| 66 | + ''' |
| 67 | + source_obj = Source(source) if isinstance(source, str) else source |
| 68 | + if not isinstance(source_obj, Source): |
| 69 | + raise TypeError( |
| 70 | + f"Must provide string or Source. Received: {inspect(source_obj)}" |
| 71 | + ) |
| 72 | + |
| 73 | + body = source_obj.body |
| 74 | + lexer = Lexer(source_obj) |
| 75 | + stripped_body = "" |
| 76 | + was_last_added_token_non_punctuator = False |
| 77 | + while lexer.advance().kind != TokenKind.EOF: |
| 78 | + current_token = lexer.token |
| 79 | + token_kind = current_token.kind |
| 80 | + |
| 81 | + # Every two non-punctuator tokens should have space between them. |
| 82 | + # Also prevent case of non-punctuator token following by spread resulting |
| 83 | + # in invalid token (e.g.`1...` is invalid Float token). |
| 84 | + is_non_punctuator = not is_punctuator_token(current_token) |
| 85 | + if was_last_added_token_non_punctuator: |
| 86 | + if is_non_punctuator or current_token.kind == TokenKind.SPREAD: |
| 87 | + stripped_body += " " |
| 88 | + |
| 89 | + token_body = body[current_token.start : current_token.end] |
| 90 | + if token_kind == TokenKind.BLOCK_STRING: |
| 91 | + stripped_body += dedent_block_string(token_body) |
| 92 | + else: |
| 93 | + stripped_body += token_body |
| 94 | + |
| 95 | + was_last_added_token_non_punctuator = is_non_punctuator |
| 96 | + |
| 97 | + return stripped_body |
| 98 | + |
| 99 | + |
| 100 | +def dedent_block_string(block_str: str) -> str: |
| 101 | + """Skip leading and trailing triple quotations""" |
| 102 | + raw_str = block_str[3:-3] |
| 103 | + body = dedent_block_string_value(raw_str) |
| 104 | + |
| 105 | + lines = body.splitlines() |
| 106 | + if get_block_string_indentation(lines) > 0: |
| 107 | + body = "\n" + body |
| 108 | + |
| 109 | + last_char = body[-1:] |
| 110 | + has_trailing_quote = last_char == '"' and body[-4:] != '\\"""' |
| 111 | + if has_trailing_quote or last_char == "\\": |
| 112 | + body += "\n" |
| 113 | + |
| 114 | + return '"""' + body + '"""' |
0 commit comments