1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.stripIgnoredCharacters = stripIgnoredCharacters;
|
7 |
|
8 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
9 |
|
10 | var _source = require("../language/source");
|
11 |
|
12 | var _tokenKind = require("../language/tokenKind");
|
13 |
|
14 | var _lexer = require("../language/lexer");
|
15 |
|
16 | var _blockString = require("../language/blockString");
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
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 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function stripIgnoredCharacters(source) {
|
73 | var sourceObj = typeof source === 'string' ? new _source.Source(source) : source;
|
74 |
|
75 | if (!(sourceObj instanceof _source.Source)) {
|
76 | throw new TypeError("Must provide string or Source. Received: ".concat((0, _inspect.default)(sourceObj)));
|
77 | }
|
78 |
|
79 | var body = sourceObj.body;
|
80 | var lexer = (0, _lexer.createLexer)(sourceObj);
|
81 | var strippedBody = '';
|
82 | var wasLastAddedTokenNonPunctuator = false;
|
83 |
|
84 | while (lexer.advance().kind !== _tokenKind.TokenKind.EOF) {
|
85 | var currentToken = lexer.token;
|
86 | var tokenKind = currentToken.kind;
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | var isNonPunctuator = !(0, _lexer.isPunctuatorToken)(currentToken);
|
94 |
|
95 | if (wasLastAddedTokenNonPunctuator) {
|
96 | if (isNonPunctuator || currentToken.kind === _tokenKind.TokenKind.SPREAD) {
|
97 | strippedBody += ' ';
|
98 | }
|
99 | }
|
100 |
|
101 | var tokenBody = body.slice(currentToken.start, currentToken.end);
|
102 |
|
103 | if (tokenKind === _tokenKind.TokenKind.BLOCK_STRING) {
|
104 | strippedBody += dedentBlockString(tokenBody);
|
105 | } else {
|
106 | strippedBody += tokenBody;
|
107 | }
|
108 |
|
109 | wasLastAddedTokenNonPunctuator = isNonPunctuator;
|
110 | }
|
111 |
|
112 | return strippedBody;
|
113 | }
|
114 |
|
115 | function dedentBlockString(blockStr) {
|
116 |
|
117 | var rawStr = blockStr.slice(3, -3);
|
118 | var body = (0, _blockString.dedentBlockStringValue)(rawStr);
|
119 | var lines = body.split(/\r\n|[\n\r]/g);
|
120 |
|
121 | if ((0, _blockString.getBlockStringIndentation)(lines) > 0) {
|
122 | body = '\n' + body;
|
123 | }
|
124 |
|
125 | var lastChar = body[body.length - 1];
|
126 | var hasTrailingQuote = lastChar === '"' && body.slice(-4) !== '\\"""';
|
127 |
|
128 | if (hasTrailingQuote || lastChar === '\\') {
|
129 | body += '\n';
|
130 | }
|
131 |
|
132 | return '"""' + body + '"""';
|
133 | }
|