| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | var _buffer = require("./buffer.js");
|
|---|
| 8 | var _index = require("./node/index.js");
|
|---|
| 9 | var _nodes = require("./nodes.js");
|
|---|
| 10 | var _t = require("@babel/types");
|
|---|
| 11 | var _tokenMap = require("./token-map.js");
|
|---|
| 12 | var _types2 = require("./generators/types.js");
|
|---|
| 13 | const {
|
|---|
| 14 | isExpression,
|
|---|
| 15 | isFunction,
|
|---|
| 16 | isStatement,
|
|---|
| 17 | isClassBody,
|
|---|
| 18 | isTSInterfaceBody,
|
|---|
| 19 | isTSEnumMember
|
|---|
| 20 | } = _t;
|
|---|
| 21 | const SCIENTIFIC_NOTATION = /e/i;
|
|---|
| 22 | const ZERO_DECIMAL_INTEGER = /\.0+$/;
|
|---|
| 23 | const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
|
|---|
| 24 | const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
|
|---|
| 25 | function commentIsNewline(c) {
|
|---|
| 26 | return c.type === "CommentLine" || HAS_NEWLINE.test(c.value);
|
|---|
| 27 | }
|
|---|
| 28 | class Printer {
|
|---|
| 29 | constructor(format, map, tokens = null, originalCode = null) {
|
|---|
| 30 | this.tokenContext = _index.TokenContext.normal;
|
|---|
| 31 | this._tokens = null;
|
|---|
| 32 | this._originalCode = null;
|
|---|
| 33 | this._currentNode = null;
|
|---|
| 34 | this._currentTypeId = null;
|
|---|
| 35 | this._indent = 0;
|
|---|
| 36 | this._indentRepeat = 0;
|
|---|
| 37 | this._insideAux = false;
|
|---|
| 38 | this._noLineTerminator = false;
|
|---|
| 39 | this._noLineTerminatorAfterNode = null;
|
|---|
| 40 | this._printAuxAfterOnNextUserNode = false;
|
|---|
| 41 | this._printedComments = new Set();
|
|---|
| 42 | this._lastCommentLine = 0;
|
|---|
| 43 | this._innerCommentsState = 0;
|
|---|
| 44 | this._flags = 0;
|
|---|
| 45 | this.tokenMap = null;
|
|---|
| 46 | this._boundGetRawIdentifier = null;
|
|---|
| 47 | this._printSemicolonBeforeNextNode = -1;
|
|---|
| 48 | this._printSemicolonBeforeNextToken = -1;
|
|---|
| 49 | this.format = format;
|
|---|
| 50 | this._tokens = tokens;
|
|---|
| 51 | this._originalCode = originalCode;
|
|---|
| 52 | this._indentRepeat = format.indent.style.length;
|
|---|
| 53 | this._inputMap = (map == null ? void 0 : map._inputMap) || null;
|
|---|
| 54 | this._buf = new _buffer.default(map, format.indent.style[0]);
|
|---|
| 55 | const {
|
|---|
| 56 | preserveFormat,
|
|---|
| 57 | compact,
|
|---|
| 58 | concise,
|
|---|
| 59 | retainLines,
|
|---|
| 60 | retainFunctionParens
|
|---|
| 61 | } = format;
|
|---|
| 62 | if (preserveFormat) {
|
|---|
| 63 | this._flags |= 1;
|
|---|
| 64 | }
|
|---|
| 65 | if (compact) {
|
|---|
| 66 | this._flags |= 2;
|
|---|
| 67 | }
|
|---|
| 68 | if (concise) {
|
|---|
| 69 | this._flags |= 4;
|
|---|
| 70 | }
|
|---|
| 71 | if (retainLines) {
|
|---|
| 72 | this._flags |= 8;
|
|---|
| 73 | }
|
|---|
| 74 | if (retainFunctionParens) {
|
|---|
| 75 | this._flags |= 16;
|
|---|
| 76 | }
|
|---|
| 77 | if (format.auxiliaryCommentBefore || format.auxiliaryCommentAfter) {
|
|---|
| 78 | this._flags |= 32;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | enterDelimited() {
|
|---|
| 82 | const oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
|
|---|
| 83 | if (oldNoLineTerminatorAfterNode !== null) {
|
|---|
| 84 | this._noLineTerminatorAfterNode = null;
|
|---|
| 85 | }
|
|---|
| 86 | return oldNoLineTerminatorAfterNode;
|
|---|
| 87 | }
|
|---|
| 88 | generate(ast) {
|
|---|
| 89 | if (this.format.preserveFormat) {
|
|---|
| 90 | this.tokenMap = new _tokenMap.TokenMap(ast, this._tokens, this._originalCode);
|
|---|
| 91 | this._boundGetRawIdentifier = _types2._getRawIdentifier.bind(this);
|
|---|
| 92 | }
|
|---|
| 93 | this.print(ast);
|
|---|
| 94 | this._maybeAddAuxComment();
|
|---|
| 95 | return this._buf.get();
|
|---|
| 96 | }
|
|---|
| 97 | indent(flags = this._flags) {
|
|---|
| 98 | if (flags & (1 | 2 | 4)) {
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | this._indent += this._indentRepeat;
|
|---|
| 102 | }
|
|---|
| 103 | dedent(flags = this._flags) {
|
|---|
| 104 | if (flags & (1 | 2 | 4)) {
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 | this._indent -= this._indentRepeat;
|
|---|
| 108 | }
|
|---|
| 109 | semicolon(force = false) {
|
|---|
| 110 | const flags = this._flags;
|
|---|
| 111 | if (flags & 32) {
|
|---|
| 112 | this._maybeAddAuxComment();
|
|---|
| 113 | }
|
|---|
| 114 | if (flags & 1) {
|
|---|
| 115 | const node = this._currentNode;
|
|---|
| 116 | if (node.start != null && node.end != null) {
|
|---|
| 117 | if (!this.tokenMap.endMatches(node, ";")) {
|
|---|
| 118 | this._printSemicolonBeforeNextNode = this._buf.getCurrentLine();
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 | const indexes = this.tokenMap.getIndexes(this._currentNode);
|
|---|
| 122 | this._catchUpTo(this._tokens[indexes[indexes.length - 1]].loc.start);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | if (force) {
|
|---|
| 126 | this._appendChar(59);
|
|---|
| 127 | } else {
|
|---|
| 128 | this._queue(59);
|
|---|
| 129 | }
|
|---|
| 130 | this._noLineTerminator = false;
|
|---|
| 131 | }
|
|---|
| 132 | rightBrace(node) {
|
|---|
| 133 | if (this.format.minified) {
|
|---|
| 134 | this._buf.removeLastSemicolon();
|
|---|
| 135 | }
|
|---|
| 136 | this.sourceWithOffset("end", node.loc, -1);
|
|---|
| 137 | this.tokenChar(125);
|
|---|
| 138 | }
|
|---|
| 139 | rightParens(node) {
|
|---|
| 140 | this.sourceWithOffset("end", node.loc, -1);
|
|---|
| 141 | this.tokenChar(41);
|
|---|
| 142 | }
|
|---|
| 143 | space(force = false) {
|
|---|
| 144 | if (this._flags & (1 | 2)) {
|
|---|
| 145 | return;
|
|---|
| 146 | }
|
|---|
| 147 | if (force) {
|
|---|
| 148 | this._space();
|
|---|
| 149 | } else {
|
|---|
| 150 | const lastCp = this.getLastChar(true);
|
|---|
| 151 | if (lastCp !== 0 && lastCp !== 32 && lastCp !== 10) {
|
|---|
| 152 | this._space();
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | word(str, noLineTerminatorAfter = false) {
|
|---|
| 157 | this.tokenContext &= _index.TokenContext.forInOrInitHeadAccumulatePassThroughMask;
|
|---|
| 158 | this._maybePrintInnerComments(str);
|
|---|
| 159 | const flags = this._flags;
|
|---|
| 160 | if (flags & 32) {
|
|---|
| 161 | this._maybeAddAuxComment();
|
|---|
| 162 | }
|
|---|
| 163 | if (flags & 1) this._catchUpToCurrentToken(str);
|
|---|
| 164 | const lastChar = this.getLastChar();
|
|---|
| 165 | if (lastChar === -2 || lastChar === -3 || lastChar === 47 && str.charCodeAt(0) === 47) {
|
|---|
| 166 | this._space();
|
|---|
| 167 | }
|
|---|
| 168 | this._append(str, false);
|
|---|
| 169 | this.setLastChar(-3);
|
|---|
| 170 | this._noLineTerminator = noLineTerminatorAfter;
|
|---|
| 171 | }
|
|---|
| 172 | number(str, number) {
|
|---|
| 173 | function isNonDecimalLiteral(str) {
|
|---|
| 174 | if (str.length > 2 && str.charCodeAt(0) === 48) {
|
|---|
| 175 | const secondChar = str.charCodeAt(1);
|
|---|
| 176 | return secondChar === 98 || secondChar === 111 || secondChar === 120;
|
|---|
| 177 | }
|
|---|
| 178 | return false;
|
|---|
| 179 | }
|
|---|
| 180 | this.word(str);
|
|---|
| 181 | if (Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46) {
|
|---|
| 182 | this.setLastChar(-2);
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | token(str, maybeNewline = false, occurrenceCount = 0, mayNeedSpace = false) {
|
|---|
| 186 | this.tokenContext &= _index.TokenContext.forInOrInitHeadAccumulatePassThroughMask;
|
|---|
| 187 | this._maybePrintInnerComments(str, occurrenceCount);
|
|---|
| 188 | const flags = this._flags;
|
|---|
| 189 | if (flags & 32) {
|
|---|
| 190 | this._maybeAddAuxComment();
|
|---|
| 191 | }
|
|---|
| 192 | if (flags & 1) {
|
|---|
| 193 | this._catchUpToCurrentToken(str, occurrenceCount);
|
|---|
| 194 | }
|
|---|
| 195 | if (mayNeedSpace) {
|
|---|
| 196 | const strFirst = str.charCodeAt(0);
|
|---|
| 197 | if ((strFirst === 45 && str === "--" || strFirst === 61) && this.getLastChar() === 33 || strFirst === 43 && this.getLastChar() === 43 || strFirst === 45 && this.getLastChar() === 45 || strFirst === 46 && this.getLastChar() === -2) {
|
|---|
| 198 | this._space();
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | this._append(str, maybeNewline);
|
|---|
| 202 | this._noLineTerminator = false;
|
|---|
| 203 | }
|
|---|
| 204 | tokenChar(char, occurrenceCount = 0) {
|
|---|
| 205 | this.tokenContext &= _index.TokenContext.forInOrInitHeadAccumulatePassThroughMask;
|
|---|
| 206 | this._maybePrintInnerComments(char, occurrenceCount);
|
|---|
| 207 | const flags = this._flags;
|
|---|
| 208 | if (flags & 32) {
|
|---|
| 209 | this._maybeAddAuxComment();
|
|---|
| 210 | }
|
|---|
| 211 | if (flags & 1) {
|
|---|
| 212 | this._catchUpToCurrentToken(char, occurrenceCount);
|
|---|
| 213 | }
|
|---|
| 214 | if (char === 43 && this.getLastChar() === 43 || char === 45 && this.getLastChar() === 45 || char === 46 && this.getLastChar() === -2) {
|
|---|
| 215 | this._space();
|
|---|
| 216 | }
|
|---|
| 217 | this._appendChar(char);
|
|---|
| 218 | this._noLineTerminator = false;
|
|---|
| 219 | }
|
|---|
| 220 | newline(i = 1, flags = this._flags) {
|
|---|
| 221 | if (i <= 0) return;
|
|---|
| 222 | if (flags & (8 | 2)) {
|
|---|
| 223 | return;
|
|---|
| 224 | }
|
|---|
| 225 | if (flags & 4) {
|
|---|
| 226 | this.space();
|
|---|
| 227 | return;
|
|---|
| 228 | }
|
|---|
| 229 | if (i > 2) i = 2;
|
|---|
| 230 | i -= this._buf.getNewlineCount();
|
|---|
| 231 | for (let j = 0; j < i; j++) {
|
|---|
| 232 | this._newline();
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | endsWith(char) {
|
|---|
| 236 | return this.getLastChar(true) === char;
|
|---|
| 237 | }
|
|---|
| 238 | getLastChar(checkQueue) {
|
|---|
| 239 | return this._buf.getLastChar(checkQueue);
|
|---|
| 240 | }
|
|---|
| 241 | setLastChar(char) {
|
|---|
| 242 | this._buf._last = char;
|
|---|
| 243 | }
|
|---|
| 244 | exactSource(loc, cb) {
|
|---|
| 245 | if (!loc) {
|
|---|
| 246 | cb();
|
|---|
| 247 | return;
|
|---|
| 248 | }
|
|---|
| 249 | this._catchUp("start", loc);
|
|---|
| 250 | this._buf.exactSource(loc, cb);
|
|---|
| 251 | }
|
|---|
| 252 | source(prop, loc) {
|
|---|
| 253 | if (!loc) return;
|
|---|
| 254 | this._catchUp(prop, loc);
|
|---|
| 255 | this._buf.source(prop, loc);
|
|---|
| 256 | }
|
|---|
| 257 | sourceWithOffset(prop, loc, columnOffset) {
|
|---|
| 258 | if (!loc || this.format.preserveFormat) return;
|
|---|
| 259 | this._catchUp(prop, loc);
|
|---|
| 260 | this._buf.sourceWithOffset(prop, loc, columnOffset);
|
|---|
| 261 | }
|
|---|
| 262 | sourceIdentifierName(identifierName, pos) {
|
|---|
| 263 | if (!this._buf._canMarkIdName) return;
|
|---|
| 264 | const sourcePosition = this._buf._sourcePosition;
|
|---|
| 265 | sourcePosition.identifierNamePos = pos;
|
|---|
| 266 | sourcePosition.identifierName = identifierName;
|
|---|
| 267 | }
|
|---|
| 268 | _space() {
|
|---|
| 269 | this._queue(32);
|
|---|
| 270 | }
|
|---|
| 271 | _newline() {
|
|---|
| 272 | if (this._buf._queuedChar === 32) this._buf._queuedChar = 0;
|
|---|
| 273 | this._appendChar(10, true);
|
|---|
| 274 | }
|
|---|
| 275 | _catchUpToCurrentToken(str, occurrenceCount = 0) {
|
|---|
| 276 | const token = this.tokenMap.findMatching(this._currentNode, str, occurrenceCount);
|
|---|
| 277 | if (token) this._catchUpTo(token.loc.start);
|
|---|
| 278 | if (this._printSemicolonBeforeNextToken !== -1 && this._printSemicolonBeforeNextToken === this._buf.getCurrentLine()) {
|
|---|
| 279 | this._appendChar(59, true);
|
|---|
| 280 | }
|
|---|
| 281 | this._printSemicolonBeforeNextToken = -1;
|
|---|
| 282 | this._printSemicolonBeforeNextNode = -1;
|
|---|
| 283 | }
|
|---|
| 284 | _append(str, maybeNewline) {
|
|---|
| 285 | this._maybeIndent();
|
|---|
| 286 | this._buf.append(str, maybeNewline);
|
|---|
| 287 | }
|
|---|
| 288 | _appendChar(char, noIndent) {
|
|---|
| 289 | if (!noIndent) {
|
|---|
| 290 | this._maybeIndent();
|
|---|
| 291 | }
|
|---|
| 292 | this._buf.appendChar(char);
|
|---|
| 293 | }
|
|---|
| 294 | _queue(char) {
|
|---|
| 295 | this._buf.queue(char);
|
|---|
| 296 | this.setLastChar(-1);
|
|---|
| 297 | }
|
|---|
| 298 | _maybeIndent() {
|
|---|
| 299 | const indent = this._shouldIndent();
|
|---|
| 300 | if (indent > 0) {
|
|---|
| 301 | this._buf._appendChar(-1, indent, false);
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 | _shouldIndent() {
|
|---|
| 305 | return this.endsWith(10) ? this._indent : 0;
|
|---|
| 306 | }
|
|---|
| 307 | catchUp(line) {
|
|---|
| 308 | if (!this.format.retainLines) return;
|
|---|
| 309 | const count = line - this._buf.getCurrentLine();
|
|---|
| 310 | for (let i = 0; i < count; i++) {
|
|---|
| 311 | this._newline();
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 | _catchUp(prop, loc) {
|
|---|
| 315 | const flags = this._flags;
|
|---|
| 316 | if ((flags & 1) === 0) {
|
|---|
| 317 | if (flags & 8 && loc != null && loc[prop]) {
|
|---|
| 318 | this.catchUp(loc[prop].line);
|
|---|
| 319 | }
|
|---|
| 320 | return;
|
|---|
| 321 | }
|
|---|
| 322 | const pos = loc == null ? void 0 : loc[prop];
|
|---|
| 323 | if (pos != null) this._catchUpTo(pos);
|
|---|
| 324 | }
|
|---|
| 325 | _catchUpTo({
|
|---|
| 326 | line,
|
|---|
| 327 | column,
|
|---|
| 328 | index
|
|---|
| 329 | }) {
|
|---|
| 330 | const count = line - this._buf.getCurrentLine();
|
|---|
| 331 | if (count > 0 && this._noLineTerminator) {
|
|---|
| 332 | return;
|
|---|
| 333 | }
|
|---|
| 334 | for (let i = 0; i < count; i++) {
|
|---|
| 335 | this._newline();
|
|---|
| 336 | }
|
|---|
| 337 | const spacesCount = count > 0 ? column : column - this._buf.getCurrentColumn();
|
|---|
| 338 | if (spacesCount > 0) {
|
|---|
| 339 | const spaces = this._originalCode ? this._originalCode.slice(index - spacesCount, index).replace(/[^\t\x0B\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]/gu, " ") : " ".repeat(spacesCount);
|
|---|
| 340 | this._append(spaces, false);
|
|---|
| 341 | this.setLastChar(32);
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 | printTerminatorless(node) {
|
|---|
| 345 | this._noLineTerminator = true;
|
|---|
| 346 | this.print(node);
|
|---|
| 347 | }
|
|---|
| 348 | print(node, noLineTerminatorAfter = false, resetTokenContext = false, trailingCommentsLineOffset) {
|
|---|
| 349 | var _node$leadingComments, _node$leadingComments2;
|
|---|
| 350 | if (!node) return;
|
|---|
| 351 | this._innerCommentsState = 0;
|
|---|
| 352 | const {
|
|---|
| 353 | type,
|
|---|
| 354 | loc,
|
|---|
| 355 | extra
|
|---|
| 356 | } = node;
|
|---|
| 357 | const flags = this._flags;
|
|---|
| 358 | let changedFlags = false;
|
|---|
| 359 | if (node._compact) {
|
|---|
| 360 | this._flags |= 4;
|
|---|
| 361 | changedFlags = true;
|
|---|
| 362 | }
|
|---|
| 363 | const nodeInfo = _nodes.generatorInfosMap.get(type);
|
|---|
| 364 | if (nodeInfo === undefined) {
|
|---|
| 365 | throw new ReferenceError(`unknown node of type ${JSON.stringify(type)} with constructor ${JSON.stringify(node.constructor.name)}`);
|
|---|
| 366 | }
|
|---|
| 367 | const [printMethod, nodeId, needsParens] = nodeInfo;
|
|---|
| 368 | const parent = this._currentNode;
|
|---|
| 369 | const parentId = this._currentTypeId;
|
|---|
| 370 | this._currentNode = node;
|
|---|
| 371 | this._currentTypeId = nodeId;
|
|---|
| 372 | if (flags & 1) {
|
|---|
| 373 | this._printSemicolonBeforeNextToken = this._printSemicolonBeforeNextNode;
|
|---|
| 374 | }
|
|---|
| 375 | let oldInAux;
|
|---|
| 376 | if (flags & 32) {
|
|---|
| 377 | oldInAux = this._insideAux;
|
|---|
| 378 | this._insideAux = loc == null;
|
|---|
| 379 | this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
|---|
| 380 | }
|
|---|
| 381 | let oldTokenContext = 0;
|
|---|
| 382 | if (resetTokenContext) {
|
|---|
| 383 | oldTokenContext = this.tokenContext;
|
|---|
| 384 | if (oldTokenContext & _index.TokenContext.forInOrInitHeadAccumulate) {
|
|---|
| 385 | this.tokenContext = 0;
|
|---|
| 386 | } else {
|
|---|
| 387 | oldTokenContext = 0;
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|
| 390 | const parenthesized = extra != null && extra.parenthesized;
|
|---|
| 391 | let shouldPrintParens = parenthesized && flags & 1 || parenthesized && flags & 16 && nodeId === 71 || parent && ((0, _index.parentNeedsParens)(node, parent, parentId) || needsParens != null && needsParens(node, parent, parentId, this.tokenContext, flags & 1 ? this._boundGetRawIdentifier : undefined));
|
|---|
| 392 | if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") {
|
|---|
| 393 | switch (parentId) {
|
|---|
| 394 | case 65:
|
|---|
| 395 | case 243:
|
|---|
| 396 | case 6:
|
|---|
| 397 | case 143:
|
|---|
| 398 | break;
|
|---|
| 399 | case 17:
|
|---|
| 400 | case 130:
|
|---|
| 401 | case 112:
|
|---|
| 402 | if (parent.callee !== node) break;
|
|---|
| 403 | default:
|
|---|
| 404 | shouldPrintParens = true;
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 | let indentParenthesized = false;
|
|---|
| 408 | if (!shouldPrintParens && this._noLineTerminator && ((_node$leadingComments2 = node.leadingComments) != null && _node$leadingComments2.some(commentIsNewline) || flags & 8 && loc && loc.start.line > this._buf.getCurrentLine())) {
|
|---|
| 409 | shouldPrintParens = true;
|
|---|
| 410 | indentParenthesized = true;
|
|---|
| 411 | }
|
|---|
| 412 | let oldNoLineTerminatorAfterNode;
|
|---|
| 413 | if (!shouldPrintParens) {
|
|---|
| 414 | noLineTerminatorAfter || (noLineTerminatorAfter = !!parent && this._noLineTerminatorAfterNode === parent && (0, _index.isLastChild)(parent, node));
|
|---|
| 415 | if (noLineTerminatorAfter) {
|
|---|
| 416 | var _node$trailingComment;
|
|---|
| 417 | if ((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.some(commentIsNewline)) {
|
|---|
| 418 | if (isExpression(node)) shouldPrintParens = true;
|
|---|
| 419 | } else {
|
|---|
| 420 | oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
|
|---|
| 421 | this._noLineTerminatorAfterNode = node;
|
|---|
| 422 | }
|
|---|
| 423 | }
|
|---|
| 424 | }
|
|---|
| 425 | if (shouldPrintParens) {
|
|---|
| 426 | this.tokenChar(40);
|
|---|
| 427 | if (indentParenthesized) this.indent();
|
|---|
| 428 | this._innerCommentsState = 0;
|
|---|
| 429 | if (!resetTokenContext) {
|
|---|
| 430 | oldTokenContext = this.tokenContext;
|
|---|
| 431 | }
|
|---|
| 432 | if (oldTokenContext & _index.TokenContext.forInOrInitHeadAccumulate) {
|
|---|
| 433 | this.tokenContext = 0;
|
|---|
| 434 | }
|
|---|
| 435 | oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
|
|---|
| 436 | this._noLineTerminatorAfterNode = null;
|
|---|
| 437 | }
|
|---|
| 438 | this._printLeadingComments(node, parent);
|
|---|
| 439 | this.exactSource(nodeId === 139 || nodeId === 66 ? null : loc, printMethod.bind(this, node, parent));
|
|---|
| 440 | if (shouldPrintParens) {
|
|---|
| 441 | this._printTrailingComments(node, parent);
|
|---|
| 442 | if (indentParenthesized) {
|
|---|
| 443 | this.dedent();
|
|---|
| 444 | this.newline();
|
|---|
| 445 | }
|
|---|
| 446 | this.tokenChar(41);
|
|---|
| 447 | this._noLineTerminator = noLineTerminatorAfter;
|
|---|
| 448 | } else if (noLineTerminatorAfter && !this._noLineTerminator) {
|
|---|
| 449 | this._noLineTerminator = true;
|
|---|
| 450 | this._printTrailingComments(node, parent);
|
|---|
| 451 | } else {
|
|---|
| 452 | this._printTrailingComments(node, parent, trailingCommentsLineOffset);
|
|---|
| 453 | }
|
|---|
| 454 | if (oldTokenContext) this.tokenContext = oldTokenContext;
|
|---|
| 455 | this._currentNode = parent;
|
|---|
| 456 | this._currentTypeId = parentId;
|
|---|
| 457 | if (changedFlags) {
|
|---|
| 458 | this._flags = flags;
|
|---|
| 459 | }
|
|---|
| 460 | if (flags & 32) {
|
|---|
| 461 | this._insideAux = oldInAux;
|
|---|
| 462 | }
|
|---|
| 463 | if (oldNoLineTerminatorAfterNode != null) {
|
|---|
| 464 | this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
|
|---|
| 465 | }
|
|---|
| 466 | this._innerCommentsState = 0;
|
|---|
| 467 | }
|
|---|
| 468 | _maybeAddAuxComment(enteredPositionlessNode) {
|
|---|
| 469 | if (enteredPositionlessNode) this._printAuxBeforeComment();
|
|---|
| 470 | if (!this._insideAux) this._printAuxAfterComment();
|
|---|
| 471 | }
|
|---|
| 472 | _printAuxBeforeComment() {
|
|---|
| 473 | if (this._printAuxAfterOnNextUserNode) return;
|
|---|
| 474 | this._printAuxAfterOnNextUserNode = true;
|
|---|
| 475 | const comment = this.format.auxiliaryCommentBefore;
|
|---|
| 476 | if (comment) {
|
|---|
| 477 | this._printComment({
|
|---|
| 478 | type: "CommentBlock",
|
|---|
| 479 | value: comment
|
|---|
| 480 | }, 0);
|
|---|
| 481 | }
|
|---|
| 482 | }
|
|---|
| 483 | _printAuxAfterComment() {
|
|---|
| 484 | if (!this._printAuxAfterOnNextUserNode) return;
|
|---|
| 485 | this._printAuxAfterOnNextUserNode = false;
|
|---|
| 486 | const comment = this.format.auxiliaryCommentAfter;
|
|---|
| 487 | if (comment) {
|
|---|
| 488 | this._printComment({
|
|---|
| 489 | type: "CommentBlock",
|
|---|
| 490 | value: comment
|
|---|
| 491 | }, 0);
|
|---|
| 492 | }
|
|---|
| 493 | }
|
|---|
| 494 | getPossibleRaw(node) {
|
|---|
| 495 | const extra = node.extra;
|
|---|
| 496 | if ((extra == null ? void 0 : extra.raw) != null && extra.rawValue != null && node.value === extra.rawValue) {
|
|---|
| 497 | return extra.raw;
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 | printJoin(nodes, statement, indent, separator, printTrailingSeparator, resetTokenContext, trailingCommentsLineOffset) {
|
|---|
| 501 | if (!(nodes != null && nodes.length)) return;
|
|---|
| 502 | const flags = this._flags;
|
|---|
| 503 | if (indent == null && flags & 8) {
|
|---|
| 504 | var _nodes$0$loc;
|
|---|
| 505 | const startLine = (_nodes$0$loc = nodes[0].loc) == null ? void 0 : _nodes$0$loc.start.line;
|
|---|
| 506 | if (startLine != null && startLine !== this._buf.getCurrentLine()) {
|
|---|
| 507 | indent = true;
|
|---|
| 508 | }
|
|---|
| 509 | }
|
|---|
| 510 | if (indent) this.indent(flags);
|
|---|
| 511 | const len = nodes.length;
|
|---|
| 512 | for (let i = 0; i < len; i++) {
|
|---|
| 513 | const node = nodes[i];
|
|---|
| 514 | if (!node) continue;
|
|---|
| 515 | if (statement && i === 0 && this._buf.hasContent()) {
|
|---|
| 516 | this.newline(1, flags);
|
|---|
| 517 | }
|
|---|
| 518 | this.print(node, false, resetTokenContext, trailingCommentsLineOffset || 0);
|
|---|
| 519 | if (separator != null) {
|
|---|
| 520 | if (i < len - 1) separator.call(this, i, false);else if (printTrailingSeparator) separator.call(this, i, true);
|
|---|
| 521 | }
|
|---|
| 522 | if (statement) {
|
|---|
| 523 | if (i + 1 === len) {
|
|---|
| 524 | this.newline(1, flags);
|
|---|
| 525 | } else {
|
|---|
| 526 | const lastCommentLine = this._lastCommentLine;
|
|---|
| 527 | if (lastCommentLine > 0) {
|
|---|
| 528 | var _nodes$loc;
|
|---|
| 529 | const offset = (((_nodes$loc = nodes[i + 1].loc) == null ? void 0 : _nodes$loc.start.line) || 0) - lastCommentLine;
|
|---|
| 530 | if (offset >= 0) {
|
|---|
| 531 | this.newline(offset || 1, flags);
|
|---|
| 532 | continue;
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 | this.newline(1, flags);
|
|---|
| 536 | }
|
|---|
| 537 | }
|
|---|
| 538 | }
|
|---|
| 539 | if (indent) this.dedent(flags);
|
|---|
| 540 | }
|
|---|
| 541 | printAndIndentOnComments(node) {
|
|---|
| 542 | const indent = node.leadingComments && node.leadingComments.length > 0;
|
|---|
| 543 | if (indent) this.indent();
|
|---|
| 544 | this.print(node);
|
|---|
| 545 | if (indent) this.dedent();
|
|---|
| 546 | }
|
|---|
| 547 | printBlock(body) {
|
|---|
| 548 | if (body.type !== "EmptyStatement") {
|
|---|
| 549 | this.space();
|
|---|
| 550 | }
|
|---|
| 551 | this.print(body);
|
|---|
| 552 | }
|
|---|
| 553 | _printTrailingComments(node, parent, lineOffset) {
|
|---|
| 554 | const {
|
|---|
| 555 | innerComments,
|
|---|
| 556 | trailingComments
|
|---|
| 557 | } = node;
|
|---|
| 558 | if (innerComments != null && innerComments.length) {
|
|---|
| 559 | this._printComments(2, innerComments, node, parent, lineOffset);
|
|---|
| 560 | }
|
|---|
| 561 | if (trailingComments != null && trailingComments.length) {
|
|---|
| 562 | this._printComments(2, trailingComments, node, parent, lineOffset);
|
|---|
| 563 | } else {
|
|---|
| 564 | this._lastCommentLine = 0;
|
|---|
| 565 | }
|
|---|
| 566 | }
|
|---|
| 567 | _printLeadingComments(node, parent) {
|
|---|
| 568 | const comments = node.leadingComments;
|
|---|
| 569 | if (!(comments != null && comments.length)) return;
|
|---|
| 570 | this._printComments(0, comments, node, parent);
|
|---|
| 571 | }
|
|---|
| 572 | _maybePrintInnerComments(nextTokenStr, nextTokenOccurrenceCount) {
|
|---|
| 573 | var _this$tokenMap;
|
|---|
| 574 | const state = this._innerCommentsState;
|
|---|
| 575 | switch (state & 3) {
|
|---|
| 576 | case 0:
|
|---|
| 577 | this._innerCommentsState = 1 | 4;
|
|---|
| 578 | return;
|
|---|
| 579 | case 1:
|
|---|
| 580 | this.printInnerComments((state & 4) > 0, (_this$tokenMap = this.tokenMap) == null ? void 0 : _this$tokenMap.findMatching(this._currentNode, nextTokenStr, nextTokenOccurrenceCount));
|
|---|
| 581 | }
|
|---|
| 582 | }
|
|---|
| 583 | printInnerComments(indent = true, nextToken) {
|
|---|
| 584 | const node = this._currentNode;
|
|---|
| 585 | const comments = node.innerComments;
|
|---|
| 586 | if (!(comments != null && comments.length)) {
|
|---|
| 587 | this._innerCommentsState = 2;
|
|---|
| 588 | return;
|
|---|
| 589 | }
|
|---|
| 590 | const hasSpace = this.endsWith(32);
|
|---|
| 591 | if (indent) this.indent();
|
|---|
| 592 | switch (this._printComments(1, comments, node, undefined, undefined, nextToken)) {
|
|---|
| 593 | case 2:
|
|---|
| 594 | this._innerCommentsState = 2;
|
|---|
| 595 | case 1:
|
|---|
| 596 | if (hasSpace) this.space();
|
|---|
| 597 | }
|
|---|
| 598 | if (indent) this.dedent();
|
|---|
| 599 | }
|
|---|
| 600 | noIndentInnerCommentsHere() {
|
|---|
| 601 | this._innerCommentsState &= ~4;
|
|---|
| 602 | }
|
|---|
| 603 | printSequence(nodes, indent, resetTokenContext, trailingCommentsLineOffset) {
|
|---|
| 604 | this.printJoin(nodes, true, indent != null ? indent : false, undefined, undefined, resetTokenContext, trailingCommentsLineOffset);
|
|---|
| 605 | }
|
|---|
| 606 | printList(items, printTrailingSeparator, statement, indent, separator, resetTokenContext) {
|
|---|
| 607 | this.printJoin(items, statement, indent, separator != null ? separator : commaSeparator, printTrailingSeparator, resetTokenContext);
|
|---|
| 608 | }
|
|---|
| 609 | shouldPrintTrailingComma(listEnd) {
|
|---|
| 610 | if (!this.tokenMap) return null;
|
|---|
| 611 | const listEndIndex = this.tokenMap.findLastIndex(this._currentNode, token => this.tokenMap.matchesOriginal(token, typeof listEnd === "number" ? String.fromCharCode(listEnd) : listEnd));
|
|---|
| 612 | if (listEndIndex <= 0) return null;
|
|---|
| 613 | return this.tokenMap.matchesOriginal(this._tokens[listEndIndex - 1], ",");
|
|---|
| 614 | }
|
|---|
| 615 | _shouldPrintComment(comment, nextToken) {
|
|---|
| 616 | if (comment.ignore) return 0;
|
|---|
| 617 | if (this._printedComments.has(comment)) return 0;
|
|---|
| 618 | if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) {
|
|---|
| 619 | return 2;
|
|---|
| 620 | }
|
|---|
| 621 | if (nextToken && this.tokenMap) {
|
|---|
| 622 | const commentTok = this.tokenMap.find(this._currentNode, token => token.value === comment.value);
|
|---|
| 623 | if (commentTok && commentTok.start > nextToken.start) {
|
|---|
| 624 | return 2;
|
|---|
| 625 | }
|
|---|
| 626 | }
|
|---|
| 627 | this._printedComments.add(comment);
|
|---|
| 628 | if (!this.format.shouldPrintComment(comment.value)) {
|
|---|
| 629 | return 0;
|
|---|
| 630 | }
|
|---|
| 631 | return 1;
|
|---|
| 632 | }
|
|---|
| 633 | _printComment(comment, skipNewLines) {
|
|---|
| 634 | const noLineTerminator = this._noLineTerminator;
|
|---|
| 635 | const isBlockComment = comment.type === "CommentBlock";
|
|---|
| 636 | const printNewLines = isBlockComment && skipNewLines !== 1 && !noLineTerminator;
|
|---|
| 637 | if (printNewLines && this._buf.hasContent() && skipNewLines !== 2) {
|
|---|
| 638 | this.newline(1);
|
|---|
| 639 | }
|
|---|
| 640 | switch (this.getLastChar(true)) {
|
|---|
| 641 | case 47:
|
|---|
| 642 | this._space();
|
|---|
| 643 | case 91:
|
|---|
| 644 | case 123:
|
|---|
| 645 | case 40:
|
|---|
| 646 | break;
|
|---|
| 647 | default:
|
|---|
| 648 | this.space();
|
|---|
| 649 | }
|
|---|
| 650 | let val;
|
|---|
| 651 | if (isBlockComment) {
|
|---|
| 652 | val = `/*${comment.value}*/`;
|
|---|
| 653 | if (this.format.indent.adjustMultilineComment) {
|
|---|
| 654 | var _comment$loc;
|
|---|
| 655 | const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
|
|---|
| 656 | if (offset) {
|
|---|
| 657 | const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
|---|
| 658 | val = val.replace(newlineRegex, "\n");
|
|---|
| 659 | }
|
|---|
| 660 | if (this._flags & 4) {
|
|---|
| 661 | val = val.replace(/\n(?!$)/g, `\n`);
|
|---|
| 662 | } else {
|
|---|
| 663 | let indentSize = this.format.retainLines ? 0 : this._buf.getCurrentColumn();
|
|---|
| 664 | if (this._shouldIndent() || this.format.retainLines) {
|
|---|
| 665 | indentSize += this._indent;
|
|---|
| 666 | }
|
|---|
| 667 | val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
|
|---|
| 668 | }
|
|---|
| 669 | }
|
|---|
| 670 | } else if (!noLineTerminator) {
|
|---|
| 671 | val = `//${comment.value}`;
|
|---|
| 672 | } else {
|
|---|
| 673 | val = `/*${comment.value}*/`;
|
|---|
| 674 | }
|
|---|
| 675 | this.source("start", comment.loc);
|
|---|
| 676 | this._append(val, isBlockComment);
|
|---|
| 677 | if (!isBlockComment && !noLineTerminator) {
|
|---|
| 678 | this._newline();
|
|---|
| 679 | }
|
|---|
| 680 | if (printNewLines && skipNewLines !== 3) {
|
|---|
| 681 | this.newline(1);
|
|---|
| 682 | }
|
|---|
| 683 | }
|
|---|
| 684 | _printComments(type, comments, node, parent, lineOffset = 0, nextToken) {
|
|---|
| 685 | const nodeLoc = node.loc;
|
|---|
| 686 | const len = comments.length;
|
|---|
| 687 | let hasLoc = !!nodeLoc;
|
|---|
| 688 | const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
|
|---|
| 689 | const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
|
|---|
| 690 | let lastLine = 0;
|
|---|
| 691 | let leadingCommentNewline = 0;
|
|---|
| 692 | const {
|
|---|
| 693 | _noLineTerminator,
|
|---|
| 694 | _flags
|
|---|
| 695 | } = this;
|
|---|
| 696 | for (let i = 0; i < len; i++) {
|
|---|
| 697 | const comment = comments[i];
|
|---|
| 698 | const shouldPrint = this._shouldPrintComment(comment, nextToken);
|
|---|
| 699 | if (shouldPrint === 2) {
|
|---|
| 700 | return i === 0 ? 0 : 1;
|
|---|
| 701 | }
|
|---|
| 702 | if (hasLoc && comment.loc && shouldPrint === 1) {
|
|---|
| 703 | const commentStartLine = comment.loc.start.line;
|
|---|
| 704 | const commentEndLine = comment.loc.end.line;
|
|---|
| 705 | if (type === 0) {
|
|---|
| 706 | let offset = 0;
|
|---|
| 707 | if (i === 0) {
|
|---|
| 708 | if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine !== commentEndLine)) {
|
|---|
| 709 | offset = leadingCommentNewline = 1;
|
|---|
| 710 | }
|
|---|
| 711 | } else {
|
|---|
| 712 | offset = commentStartLine - lastLine;
|
|---|
| 713 | }
|
|---|
| 714 | lastLine = commentEndLine;
|
|---|
| 715 | if (offset > 0 && !_noLineTerminator) {
|
|---|
| 716 | this.newline(offset, _flags);
|
|---|
| 717 | }
|
|---|
| 718 | this._printComment(comment, 1);
|
|---|
| 719 | if (i + 1 === len) {
|
|---|
| 720 | const count = Math.max(nodeStartLine - lastLine, leadingCommentNewline);
|
|---|
| 721 | if (count > 0 && !_noLineTerminator) {
|
|---|
| 722 | this.newline(count, _flags);
|
|---|
| 723 | }
|
|---|
| 724 | lastLine = nodeStartLine;
|
|---|
| 725 | }
|
|---|
| 726 | } else if (type === 1) {
|
|---|
| 727 | const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine);
|
|---|
| 728 | lastLine = commentEndLine;
|
|---|
| 729 | if (offset > 0 && !_noLineTerminator) {
|
|---|
| 730 | this.newline(offset, _flags);
|
|---|
| 731 | }
|
|---|
| 732 | this._printComment(comment, 1);
|
|---|
| 733 | if (i + 1 === len) {
|
|---|
| 734 | const count = Math.min(1, nodeEndLine - lastLine);
|
|---|
| 735 | if (count > 0 && !_noLineTerminator) {
|
|---|
| 736 | this.newline(count, _flags);
|
|---|
| 737 | }
|
|---|
| 738 | lastLine = nodeEndLine;
|
|---|
| 739 | }
|
|---|
| 740 | } else {
|
|---|
| 741 | const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
|
|---|
| 742 | lastLine = commentEndLine;
|
|---|
| 743 | if (offset > 0 && !_noLineTerminator) {
|
|---|
| 744 | this.newline(offset, _flags);
|
|---|
| 745 | }
|
|---|
| 746 | this._printComment(comment, 1);
|
|---|
| 747 | }
|
|---|
| 748 | } else {
|
|---|
| 749 | hasLoc = false;
|
|---|
| 750 | if (shouldPrint !== 1) {
|
|---|
| 751 | continue;
|
|---|
| 752 | }
|
|---|
| 753 | if (len === 1) {
|
|---|
| 754 | const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line : !HAS_NEWLINE.test(comment.value);
|
|---|
| 755 | const shouldSkipNewline = singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent) && !isTSEnumMember(node);
|
|---|
| 756 | if (type === 0) {
|
|---|
| 757 | this._printComment(comment, shouldSkipNewline && node.type !== "ObjectExpression" || singleLine && isFunction(parent) && parent.body === node ? 1 : 0);
|
|---|
| 758 | } else if (shouldSkipNewline && type === 2) {
|
|---|
| 759 | this._printComment(comment, 1);
|
|---|
| 760 | } else {
|
|---|
| 761 | this._printComment(comment, 0);
|
|---|
| 762 | }
|
|---|
| 763 | } else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody" && node.type !== "TSInterfaceBody") {
|
|---|
| 764 | this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0);
|
|---|
| 765 | } else {
|
|---|
| 766 | this._printComment(comment, 0);
|
|---|
| 767 | }
|
|---|
| 768 | }
|
|---|
| 769 | }
|
|---|
| 770 | if (type === 2 && hasLoc && lastLine) {
|
|---|
| 771 | this._lastCommentLine = lastLine;
|
|---|
| 772 | }
|
|---|
| 773 | return 2;
|
|---|
| 774 | }
|
|---|
| 775 | }
|
|---|
| 776 | var _default = exports.default = Printer;
|
|---|
| 777 | function commaSeparator(occurrenceCount, last) {
|
|---|
| 778 | this.tokenChar(44, occurrenceCount);
|
|---|
| 779 | if (!last) this.space();
|
|---|
| 780 | }
|
|---|
| 781 |
|
|---|
| 782 | //# sourceMappingURL=printer.js.map
|
|---|