1 | import inspect from '../jsutils/inspect';
|
2 | import { Source } from '../language/source';
|
3 | import { TokenKind } from '../language/tokenKind';
|
4 | import { createLexer, isPunctuatorToken } from '../language/lexer';
|
5 | import { dedentBlockStringValue, getBlockStringIndentation } from '../language/blockString';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | export function stripIgnoredCharacters(source) {
|
60 | var sourceObj = typeof source === 'string' ? new Source(source) : source;
|
61 |
|
62 | if (!(sourceObj instanceof Source)) {
|
63 | throw new TypeError("Must provide string or Source. Received: ".concat(inspect(sourceObj)));
|
64 | }
|
65 |
|
66 | var body = sourceObj.body;
|
67 | var lexer = createLexer(sourceObj);
|
68 | var strippedBody = '';
|
69 | var wasLastAddedTokenNonPunctuator = false;
|
70 |
|
71 | while (lexer.advance().kind !== TokenKind.EOF) {
|
72 | var currentToken = lexer.token;
|
73 | var tokenKind = currentToken.kind;
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | var isNonPunctuator = !isPunctuatorToken(currentToken);
|
81 |
|
82 | if (wasLastAddedTokenNonPunctuator) {
|
83 | if (isNonPunctuator || currentToken.kind === TokenKind.SPREAD) {
|
84 | strippedBody += ' ';
|
85 | }
|
86 | }
|
87 |
|
88 | var tokenBody = body.slice(currentToken.start, currentToken.end);
|
89 |
|
90 | if (tokenKind === TokenKind.BLOCK_STRING) {
|
91 | strippedBody += dedentBlockString(tokenBody);
|
92 | } else {
|
93 | strippedBody += tokenBody;
|
94 | }
|
95 |
|
96 | wasLastAddedTokenNonPunctuator = isNonPunctuator;
|
97 | }
|
98 |
|
99 | return strippedBody;
|
100 | }
|
101 |
|
102 | function dedentBlockString(blockStr) {
|
103 |
|
104 | var rawStr = blockStr.slice(3, -3);
|
105 | var body = dedentBlockStringValue(rawStr);
|
106 | var lines = body.split(/\r\n|[\n\r]/g);
|
107 |
|
108 | if (getBlockStringIndentation(lines) > 0) {
|
109 | body = '\n' + body;
|
110 | }
|
111 |
|
112 | var lastChar = body[body.length - 1];
|
113 | var hasTrailingQuote = lastChar === '"' && body.slice(-4) !== '\\"""';
|
114 |
|
115 | if (hasTrailingQuote || lastChar === '\\') {
|
116 | body += '\n';
|
117 | }
|
118 |
|
119 | return '"""' + body + '"""';
|
120 | }
|