UNPKG

@babel/generator

Version:
1,935 lines 125 kB
import { GenMapping, setSourceContent, toEncodedMap, toDecodedMap, allMappings, maybeAddMapping } from '@jridgewell/gen-mapping'; import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'; import * as _t from '@babel/types'; import jsesc from 'jsesc'; class SourceMap { _map; _rawMappings; _sourceFileName; _lastGenLine = 0; _lastSourceLine = 0; _lastSourceColumn = 0; _inputMap = null; constructor(opts, code) { const map = this._map = new GenMapping({ sourceRoot: opts.sourceRoot }); this._sourceFileName = opts.sourceFileName?.replace(/\\/g, "/"); this._rawMappings = undefined; if (opts.inputSourceMap) { this._inputMap = new TraceMap(opts.inputSourceMap); const resolvedSources = this._inputMap.resolvedSources; if (resolvedSources.length) { for (let i = 0; i < resolvedSources.length; i++) { setSourceContent(map, resolvedSources[i], this._inputMap.sourcesContent?.[i]); } } } if (typeof code === "string" && !opts.inputSourceMap) { setSourceContent(map, this._sourceFileName, code); } else if (typeof code === "object") { for (const sourceFileName of Object.keys(code)) { setSourceContent(map, sourceFileName.replace(/\\/g, "/"), code[sourceFileName]); } } } get() { return toEncodedMap(this._map); } getDecoded() { return toDecodedMap(this._map); } getRawMappings() { return this._rawMappings ||= allMappings(this._map); } mark(generated, generatedIdentifierName, line, column, identifierName, identifierNamePos, filename) { this._rawMappings = undefined; let originalMapping; if (line != null) { if (this._inputMap) { originalMapping = originalPositionFor(this._inputMap, { line, column: column }); if (originalMapping.name && (identifierNamePos || identifierName != null && originalMapping.column === column)) { identifierName = originalMapping.name; } else if (identifierNamePos) { const originalIdentifierMapping = originalPositionFor(this._inputMap, identifierNamePos); if (originalIdentifierMapping.name) { identifierName = originalIdentifierMapping.name; } } } else { originalMapping = { name: null, source: filename?.replace(/\\/g, "/") || this._sourceFileName, line: line, column: column }; } } if (identifierName != null && identifierName === generatedIdentifierName) { identifierName = null; } maybeAddMapping(this._map, { name: identifierName, generated, source: originalMapping?.source, original: originalMapping }); } } const spaceIndents = []; for (let i = 0; i < 32; i++) { spaceIndents.push(" ".repeat(i * 2)); } class Buffer { constructor(map, indentChar) { this._map = map; this._indentChar = indentChar; } _map = null; _buf = ""; _str = ""; _appendCount = 0; _last = 0; _canMarkIdName = true; _indentChar = ""; _queuedChar = 0; _position = { line: 1, column: 0 }; _sourcePosition = { identifierName: undefined, identifierNamePos: undefined, line: undefined, column: undefined, filename: undefined }; get() { const { _map, _last } = this; if (this._queuedChar !== 32) { this._flush(); } const code = _last === 10 ? (this._buf + this._str).trimRight() : this._buf + this._str; if (_map === null) { return { code: code, decodedMap: undefined, map: null, rawMappings: undefined }; } const result = { code: code, decodedMap: _map.getDecoded(), get map() { const resultMap = _map.get(); result.map = resultMap; return resultMap; }, set map(value) { Object.defineProperty(result, "map", { value, writable: true }); }, get rawMappings() { const mappings = _map.getRawMappings(); result.rawMappings = mappings; return mappings; }, set rawMappings(value) { Object.defineProperty(result, "rawMappings", { value, writable: true }); } }; return result; } append(str, maybeNewline, ignoreMapping = false) { this._flush(); this._append(str, maybeNewline, ignoreMapping); } appendChar(char) { this._flush(); this._appendChar(char, 1, true); } queue(char) { this._flush(); this._queuedChar = char; } _flush() { const queuedChar = this._queuedChar; if (queuedChar !== 0) { this._appendChar(queuedChar, 1, true); this._queuedChar = 0; } } _appendChar(char, repeat, useSourcePos) { this._last = char; if (char === -1) { const indent = repeat >= 64 ? this._indentChar.repeat(repeat) : spaceIndents[repeat / 2]; this._str += indent; } else { this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char); } const isSpace = char === 32; const position = this._position; if (char !== 10) { if (this._map) { const sourcePos = this._sourcePosition; if (useSourcePos && sourcePos) { this._map.mark(position, null, sourcePos.line, sourcePos.column, isSpace ? undefined : sourcePos.identifierName, isSpace ? undefined : sourcePos.identifierNamePos, sourcePos.filename); if (!isSpace && this._canMarkIdName) { sourcePos.identifierName = undefined; sourcePos.identifierNamePos = undefined; } } else { this._map.mark(position, null); } } position.column += repeat; } else { position.line++; position.column = 0; } } _append(str, maybeNewline, ignoreMapping) { const len = str.length; const position = this._position; const sourcePos = this._sourcePosition; this._last = -1; if (++this._appendCount > 4096) { +this._str; this._buf += this._str; this._str = str; this._appendCount = 0; } else { this._str += str; } const hasMap = !ignoreMapping && this._map !== null; if (!maybeNewline && !hasMap) { position.column += len; return; } const { column, identifierName, identifierNamePos, filename } = sourcePos; let line = sourcePos.line; if ((identifierName != null || identifierNamePos != null) && this._canMarkIdName) { sourcePos.identifierName = undefined; sourcePos.identifierNamePos = undefined; } let i = str.indexOf("\n"); let last = 0; if (hasMap && i !== 0) { this._map.mark(position, str, line, column, identifierName, identifierNamePos, filename); } while (i !== -1) { position.line++; position.column = 0; last = i + 1; if (last < len && line !== undefined) { line++; if (hasMap) { this._map.mark(position, str, line, 0, undefined, undefined, filename); } } i = str.indexOf("\n", last); } position.column += len - last; } removeLastSemicolon() { if (this._queuedChar === 59) { this._queuedChar = 0; } } getLastChar(checkQueue) { if (!checkQueue) { return this._last; } const queuedChar = this._queuedChar; return queuedChar !== 0 ? queuedChar : this._last; } getNewlineCount() { return this._queuedChar === 0 && this._last === 10 ? 1 : 0; } hasContent() { return this._last !== 0; } exactSource(loc, cb) { if (!this._map) { cb(); return; } this.source("start", loc); const identifierName = loc.identifierName; const sourcePos = this._sourcePosition; if (identifierName != null) { this._canMarkIdName = false; sourcePos.identifierName = identifierName; } cb(); if (identifierName != null) { this._canMarkIdName = true; sourcePos.identifierName = undefined; sourcePos.identifierNamePos = undefined; } this.source("end", loc); } source(prop, loc) { if (!this._map) return; this._normalizePosition(prop, loc, 0); } sourceWithOffset(prop, loc, columnOffset) { if (!this._map) return; this._normalizePosition(prop, loc, columnOffset); } _normalizePosition(prop, loc, columnOffset) { this._flush(); const pos = loc[prop]; if (pos) { this.setSourcePosition(pos.line, Math.max(pos.column + columnOffset, 0)); this._sourcePosition.filename = loc.filename; } } setSourcePosition(line, column) { const target = this._sourcePosition; target.line = line; target.column = column; } getCurrentColumn() { return this._position.column + (this._queuedChar ? 1 : 0); } getCurrentLine() { return this._position.line; } } const { isMemberExpression: isMemberExpression$1, isOptionalMemberExpression, isYieldExpression, isStatement: isStatement$4 } = _t; const PRECEDENCE = new Map([["||", 0], ["??", 1], ["&&", 2], ["|", 3], ["^", 4], ["&", 5], ["==", 6], ["===", 6], ["!=", 6], ["!==", 6], ["<", 7], [">", 7], ["<=", 7], [">=", 7], ["in", 7], ["instanceof", 7], [">>", 8], ["<<", 8], [">>>", 8], ["+", 9], ["-", 9], ["*", 10], ["/", 10], ["%", 10], ["**", 11]]); function isTSTypeExpression(nodeId) { return nodeId === 154 || nodeId === 199 || nodeId === 207; } const isClassExtendsClause = (node, parent, parentId) => { return (parentId === 22 || parentId === 23) && parent.superClass === node; }; const hasPostfixPart = (node, parent, parentId) => { switch (parentId) { case 109: case 133: return parent.object === node; case 18: case 131: case 113: return parent.callee === node; case 220: return parent.tag === node; case 189: return true; } return false; }; function NullableTypeAnnotation$1(node, parent, parentId) { return parentId === 4; } function FunctionTypeAnnotation$1(node, parent, parentId, tokenContext) { return parentId === 237 || parentId === 91 || parentId === 4 || (tokenContext & 8) > 0; } function UpdateExpression$1(node, parent, parentId) { return hasPostfixPart(node, parent, parentId) || isClassExtendsClause(node, parent, parentId); } function needsParenBeforeExpressionBrace(tokenContext) { return (tokenContext & (1 | 2)) > 0; } function ObjectExpression$1(node, parent, parentId, tokenContext) { return needsParenBeforeExpressionBrace(tokenContext); } function DoExpression$1(node, parent, parentId, tokenContext) { return (tokenContext & 1) > 0 && !node.async; } function BinaryLike(node, parent, parentId, nodeType) { if (isClassExtendsClause(node, parent, parentId)) { return true; } if (hasPostfixPart(node, parent, parentId) || parentId === 236 || parentId === 143 || parentId === 8) { return true; } let parentPos; switch (parentId) { case 11: case 108: parentPos = PRECEDENCE.get(parent.operator); break; case 154: case 199: parentPos = 7; } if (parentPos !== undefined) { const nodePos = nodeType === 2 ? 7 : PRECEDENCE.get(node.operator); if (parentPos > nodePos) return true; if (parentPos === nodePos && parentId === 11 && (nodePos === 11 ? parent.left === node : parent.right === node)) { return true; } if (nodeType === 1 && parentId === 108 && (nodePos === 1 && parentPos !== 1 || parentPos === 1 && nodePos !== 1)) { return true; } } return false; } function UnionTypeAnnotation$1(node, parent, parentId) { switch (parentId) { case 4: case 116: case 91: case 237: return true; } return false; } function OptionalIndexedAccessType$1(node, parent, parentId) { return parentId === 85 && parent.objectType === node; } function TSAsExpression$1(node, parent, parentId) { if ((parentId === 6 || parentId === 7) && parent.left === node) { return true; } if (parentId === 11 && (parent.operator === "|" || parent.operator === "&") && node === parent.left) { return true; } return BinaryLike(node, parent, parentId, 2); } function TSConditionalType$1(node, parent, parentId) { switch (parentId) { case 153: case 193: case 209: case 210: case 179: case 217: return true; case 173: return parent.objectType === node; case 159: return parent.checkType === node || parent.extendsType === node; } return false; } function TSUnionType$1(node, parent, parentId) { switch (parentId) { case 179: case 209: case 153: case 193: return true; case 173: return parent.objectType === node; } return false; } function TSIntersectionType$1(node, parent, parentId) { return parentId === 209 || TSTypeOperator$1(node, parent, parentId); } function TSInferType$1(node, parent, parentId) { if (TSTypeOperator$1(node, parent, parentId)) { return true; } if ((parentId === 179 || parentId === 217) && node.typeParameter.constraint != null) { return true; } return false; } function TSTypeOperator$1(node, parent, parentId) { switch (parentId) { case 153: case 193: return true; case 173: if (parent.objectType === node) { return true; } } return false; } function TSInstantiationExpression$1(node, parent, parentId) { switch (parentId) { case 18: case 131: case 113: case 175: return parent.typeArguments != null; } return false; } function TSFunctionType$1(node, parent, parentId) { if (TSUnionType$1(node, parent, parentId)) return true; return parentId === 217 || parentId === 159 && (parent.checkType === node || parent.extendsType === node); } function BinaryExpression$1(node, parent, parentId, tokenContext) { if (BinaryLike(node, parent, parentId, 0)) return true; return (tokenContext & 128) > 0 && node.operator === "in"; } function LogicalExpression(node, parent, parentId) { return BinaryLike(node, parent, parentId, 1); } function SequenceExpression$1(node, parent, parentId) { if (parentId === 142 || parentId === 134 || parentId === 109 && parent.property === node || parentId === 133 && parent.property === node || parentId === 222) { return false; } if (parentId === 22) { return true; } if (parentId === 69) { return parent.right === node; } if (parentId === 61) { return true; } return !isStatement$4(parent); } function YieldExpression$1(node, parent, parentId) { return parentId === 11 || parentId === 108 || parentId === 236 || parentId === 143 || hasPostfixPart(node, parent, parentId) || parentId === 8 && isYieldExpression(node) || parentId === 29 && node === parent.test || isClassExtendsClause(node, parent, parentId) || isTSTypeExpression(parentId); } function ClassExpression(node, parent, parentId, tokenContext) { return (tokenContext & (1 | 4)) > 0; } function UnaryLike(node, parent, parentId) { return hasPostfixPart(node, parent, parentId) || parentId === 11 && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent, parentId); } function FunctionExpression$1(node, parent, parentId, tokenContext) { return (tokenContext & (1 | 4)) > 0; } function ConditionalExpression$1(node, parent, parentId) { switch (parentId) { case 236: case 143: case 11: case 108: case 8: return true; case 29: if (parent.test === node) { return true; } } if (isTSTypeExpression(parentId)) { return true; } return UnaryLike(node, parent, parentId); } function OptionalMemberExpression$1(node, parent, parentId) { switch (parentId) { case 18: return parent.callee === node; case 109: return parent.object === node; } return false; } function AssignmentExpression$1(node, parent, parentId, tokenContext) { if (needsParenBeforeExpressionBrace(tokenContext) && node.left.type === "ObjectPattern") { return true; } return ConditionalExpression$1(node, parent, parentId); } function Identifier$1(node, parent, parentId, tokenContext, getRawIdentifier) { if (getRawIdentifier && getRawIdentifier(node) !== node.name) { return false; } if (parentId === 6 && node.extra?.parenthesized && parent.left === node) { const rightType = parent.right.type; if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) { return true; } } if (tokenContext & 64 || (parentId === 109 || parentId === 133) && tokenContext & (1 | 16 | 32)) { if (node.name === "let") { const isFollowedByBracket = isMemberExpression$1(parent, { object: node, computed: true }) || isOptionalMemberExpression(parent, { object: node, computed: true, optional: false }); if (isFollowedByBracket && tokenContext & (1 | 16 | 32)) { return true; } return (tokenContext & 64) > 0; } } return parentId === 69 && parent.left === node && node.name === "async" && !parent.await; } const parens = /*#__PURE__*/Object.defineProperty({ __proto__: null, ArrowFunctionExpression: ConditionalExpression$1, AssignmentExpression: AssignmentExpression$1, AwaitExpression: YieldExpression$1, BinaryExpression: BinaryExpression$1, ClassExpression, ConditionalExpression: ConditionalExpression$1, DoExpression: DoExpression$1, FunctionExpression: FunctionExpression$1, FunctionTypeAnnotation: FunctionTypeAnnotation$1, Identifier: Identifier$1, IntersectionTypeAnnotation: UnionTypeAnnotation$1, LogicalExpression, NullableTypeAnnotation: NullableTypeAnnotation$1, ObjectExpression: ObjectExpression$1, OptionalCallExpression: OptionalMemberExpression$1, OptionalIndexedAccessType: OptionalIndexedAccessType$1, OptionalMemberExpression: OptionalMemberExpression$1, SequenceExpression: SequenceExpression$1, SpreadElement: UnaryLike, TSAsExpression: TSAsExpression$1, TSConditionalType: TSConditionalType$1, TSConstructorType: TSFunctionType$1, TSFunctionType: TSFunctionType$1, TSInferType: TSInferType$1, TSInstantiationExpression: TSInstantiationExpression$1, TSIntersectionType: TSIntersectionType$1, TSSatisfiesExpression: TSAsExpression$1, TSTypeAssertion: UnaryLike, TSTypeOperator: TSTypeOperator$1, TSUnionType: TSUnionType$1, UnaryExpression: UnaryLike, UnionTypeAnnotation: UnionTypeAnnotation$1, UpdateExpression: UpdateExpression$1, YieldExpression: YieldExpression$1 }, Symbol.toStringTag, { value: 'Module' }); function TaggedTemplateExpression(node) { this.print(node.tag); this.print(node.typeArguments); this.print(node.quasi); } function TemplateElement() { throw new Error("TemplateElement printing is handled in TemplateLiteral"); } function _printTemplate(node, substitutions) { const quasis = node.quasis; let partRaw = "`"; for (let i = 0; i < quasis.length - 1; i++) { partRaw += quasis[i].value.raw; this.token(partRaw + "${", true); this.print(substitutions[i]); partRaw = "}"; } partRaw += quasis[quasis.length - 1].value.raw; this.token(partRaw + "`", true); } function TemplateLiteral(node) { _printTemplate.call(this, node, node.expressions); } const { isCallExpression, isLiteral, isMemberExpression, isNewExpression, isPattern } = _t; function UnaryExpression(node) { const { operator } = node; const firstChar = operator.charCodeAt(0); if (firstChar >= 97 && firstChar <= 122) { this.word(operator); this.space(); } else { this.tokenChar(firstChar); } this.print(node.argument); } function DoExpression(node) { if (node.async) { this.word("async", true); this.space(); } this.word("do"); this.space(); this.print(node.body); } function ParenthesizedExpression(node) { this.tokenChar(40); const oldNoLineTerminatorAfterNode = this.enterDelimited(); this.print(node.expression, undefined, true); this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; this.rightParens(node); } function UpdateExpression(node) { if (node.prefix) { this.token(node.operator, false, 0, true); this.print(node.argument); } else { this.print(node.argument, true); this.token(node.operator, false, 0, true); } } function ConditionalExpression(node) { this.print(node.test); this.space(); this.tokenChar(63); this.space(); this.print(node.consequent); this.space(); this.tokenChar(58); this.space(); this.print(node.alternate); } function _printExpressionArguments(node) { this.tokenChar(40); const oldNoLineTerminatorAfterNode = this.enterDelimited(); this.printList(node.arguments, this.shouldPrintTrailingComma(")"), undefined, undefined, undefined, true); this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; this.rightParens(node); } function NewExpression(node, parent) { this.word("new"); this.space(); this.print(node.callee); if (this.format.minified && node.arguments.length === 0 && !isCallExpression(parent, { callee: node }) && !isMemberExpression(parent) && !isNewExpression(parent)) { return; } this.print(node.typeArguments); if (node.arguments.length === 0 && this.tokenMap && !this.tokenMap.endMatches(node, ")")) { return; } _printExpressionArguments.call(this, node); } function SequenceExpression(node) { this.printList(node.expressions); } function ThisExpression() { this.word("this"); } function Super() { this.word("super"); } function _shouldPrintDecoratorsBeforeExport(node) { return typeof node.start === "number" && node.start === node.declaration.start; } function Decorator(node) { this.tokenChar(64); const { expression } = node; this.print(expression); this.newline(); } function OptionalMemberExpression(node) { let { computed } = node; const { optional, property } = node; this.print(node.object); if (!computed && isMemberExpression(property)) { throw new TypeError("Got a MemberExpression for MemberExpression property"); } if (isLiteral(property) && typeof property.value === "number") { computed = true; } if (optional) { this.token("?."); } if (computed) { this.tokenChar(91); this.print(property); this.tokenChar(93); } else { if (!optional) { this.tokenChar(46); } this.print(property); } } function OptionalCallExpression(node) { this.print(node.callee); if (node.optional) { this.token("?."); } this.print(node.typeArguments); _printExpressionArguments.call(this, node); } function CallExpression(node) { this.print(node.callee); this.print(node.typeArguments); _printExpressionArguments.call(this, node); } function Import() { this.word("import"); } function AwaitExpression(node) { this.word("await"); this.space(); this.print(node.argument); } function YieldExpression(node) { if (node.delegate) { this.word("yield", true); this.tokenChar(42); if (node.argument) { this.space(); this.print(node.argument); } } else if (node.argument) { this.word("yield", true); this.space(); this.print(node.argument); } else { this.word("yield"); } } function EmptyStatement() { this.semicolon(true); } function ExpressionStatement(node) { this.tokenContext |= 1; this.print(node.expression); this.semicolon(); } function AssignmentPattern(node) { this.print(node.left); if (node.left.type === "Identifier" || isPattern(node.left)) { if (node.left.optional) this.tokenChar(63); this.print(node.left.typeAnnotation); } this.space(); this.tokenChar(61); this.space(); this.print(node.right); } function AssignmentExpression(node) { this.print(node.left); this.space(); this.token(node.operator, false, 0, true); this.space(); this.print(node.right); } function BinaryExpression(node) { this.print(node.left); this.space(); const { operator } = node; if (operator.charCodeAt(0) === 105) { this.word(operator); } else { this.token(operator, false, 0, true); this.setLastChar(operator.charCodeAt(operator.length - 1)); } this.space(); this.print(node.right); } function BindExpression(node) { this.print(node.object); this.token("::"); this.print(node.callee); } function MemberExpression(node) { this.print(node.object); if (!node.computed && isMemberExpression(node.property)) { throw new TypeError("Got a MemberExpression for MemberExpression property"); } let computed = node.computed; if (isLiteral(node.property) && typeof node.property.value === "number") { computed = true; } if (computed) { const oldNoLineTerminatorAfterNode = this.enterDelimited(); this.tokenChar(91); this.print(node.property, undefined, true); this.tokenChar(93); this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; } else { this.tokenChar(46); this.print(node.property); } } function MetaProperty(node) { this.print(node.meta); this.tokenChar(46); this.print(node.property); } function PrivateName(node) { this.tokenChar(35); this.print(node.id); } function V8IntrinsicIdentifier(node) { this.tokenChar(37); this.word(node.name); } function ModuleExpression(node) { this.word("module", true); this.space(); this.tokenChar(123); this.indent(); const { body } = node; if (body.body.length || body.directives.length) { this.newline(); } this.print(body); this.dedent(); this.rightBrace(node); } const { isFor, isIfStatement, isStatement: isStatement$3, isVoidPattern } = _t; function WithStatement(node) { this.word("with"); this.space(); this.tokenChar(40); this.print(node.object); this.tokenChar(41); this.printBlock(node.body); } function IfStatement(node) { this.word("if"); this.space(); this.tokenChar(40); this.print(node.test); this.tokenChar(41); this.space(); const needsBlock = node.alternate && isIfStatement(getLastStatement(node.consequent)); if (needsBlock) { this.tokenChar(123); this.newline(); this.indent(); } this.printAndIndentOnComments(node.consequent); if (needsBlock) { this.dedent(); this.newline(); this.tokenChar(125); } if (node.alternate) { if (this.endsWith(125)) this.space(); this.word("else"); this.space(); this.printAndIndentOnComments(node.alternate); } } function getLastStatement(statement) { const { body } = statement; if (isStatement$3(body) === false) { return statement; } return getLastStatement(body); } function ForStatement(node) { this.word("for"); this.space(); this.tokenChar(40); this.tokenContext |= 16 | 128; this.print(node.init); this.tokenContext = 0; this.tokenChar(59); if (node.test) { this.space(); this.print(node.test); } this.tokenChar(59, 1); if (node.update) { this.space(); this.print(node.update); } this.tokenChar(41); this.printBlock(node.body); } function WhileStatement(node) { this.word("while"); this.space(); this.tokenChar(40); this.print(node.test); this.tokenChar(41); this.printBlock(node.body); } function ForInStatement(node) { this.word("for"); this.space(); this.noIndentInnerCommentsHere(); this.tokenChar(40); this.tokenContext |= 32 | 128; this.print(node.left); this.tokenContext = 0; this.space(); this.word("in"); this.space(); this.print(node.right); this.tokenChar(41); this.printBlock(node.body); } function ForOfStatement(node) { this.word("for"); this.space(); if (node.await) { this.word("await"); this.space(); } this.noIndentInnerCommentsHere(); this.tokenChar(40); this.tokenContext |= 64; this.print(node.left); this.space(); this.word("of"); this.space(); this.print(node.right); this.tokenChar(41); this.printBlock(node.body); } function DoWhileStatement(node) { this.word("do"); this.space(); this.print(node.body); this.space(); this.word("while"); this.space(); this.tokenChar(40); this.print(node.test); this.tokenChar(41); this.semicolon(); } function printStatementAfterKeyword(printer, node) { if (node) { printer.space(); printer.printTerminatorless(node); } printer.semicolon(); } function BreakStatement(node) { this.word("break"); printStatementAfterKeyword(this, node.label); } function ContinueStatement(node) { this.word("continue"); printStatementAfterKeyword(this, node.label); } function ReturnStatement(node) { this.word("return"); printStatementAfterKeyword(this, node.argument); } function ThrowStatement(node) { this.word("throw"); printStatementAfterKeyword(this, node.argument); } function LabeledStatement(node) { this.print(node.label); this.tokenChar(58); this.space(); this.print(node.body); } function TryStatement(node) { this.word("try"); this.space(); this.print(node.block); this.space(); if (node.handlers) { this.print(node.handlers[0]); } else { this.print(node.handler); } if (node.finalizer) { this.space(); this.word("finally"); this.space(); this.print(node.finalizer); } } function CatchClause(node) { this.word("catch"); this.space(); if (node.param) { this.tokenChar(40); this.print(node.param); this.print(node.param.typeAnnotation); this.tokenChar(41); this.space(); } this.print(node.body); } function SwitchStatement(node) { this.word("switch"); this.space(); this.tokenChar(40); this.print(node.discriminant); this.tokenChar(41); this.space(); this.tokenChar(123); this.printSequence(node.cases, true); this.rightBrace(node); } function SwitchCase(node) { if (node.test) { this.word("case"); this.space(); this.print(node.test); this.tokenChar(58); } else { this.word("default"); this.tokenChar(58); } if (node.consequent.length) { this.newline(); this.printSequence(node.consequent, true); } } function DebuggerStatement() { this.word("debugger"); this.semicolon(); } function commaSeparatorWithNewline(occurrenceCount) { this.tokenChar(44, occurrenceCount); this.newline(); } function VariableDeclaration(node, parent) { if (node.declare) { this.word("declare"); this.space(); } const { kind } = node; switch (kind) { case "await using": this.word("await"); this.space(); case "using": this.word("using", true); break; default: this.word(kind); } this.space(); let hasInits = false; if (!isFor(parent)) { for (const declar of node.declarations) { if (declar.init) { hasInits = true; break; } } } this.printList(node.declarations, undefined, undefined, node.declarations.length > 1, hasInits ? commaSeparatorWithNewline : undefined); if (parent != null) { switch (parent.type) { case "ForStatement": if (parent.init === node) { return; } break; case "ForInStatement": case "ForOfStatement": if (parent.left === node) { return; } } } this.semicolon(); } function VariableDeclarator(node) { this.print(node.id); if (node.definite) this.tokenChar(33); if (!isVoidPattern(node.id)) { this.print(node.id.typeAnnotation); } if (node.init) { this.space(); this.tokenChar(61); this.space(); this.print(node.init); } } const { isIdentifier: isIdentifier$1 } = _t; function _params(node, noLineTerminator, idNode, parentNode) { this.print(node.typeParameters); if (idNode !== undefined || parentNode !== undefined) { const nameInfo = _getFuncIdName.call(this, idNode, parentNode); if (nameInfo) { this.sourceIdentifierName(nameInfo.name, nameInfo.pos); } } this.tokenChar(40); _parameters.call(this, node.params, 41); this.print(node.returnType, noLineTerminator); this._noLineTerminator = noLineTerminator; } function _parameters(parameters, endToken) { const oldNoLineTerminatorAfterNode = this.enterDelimited(); const trailingComma = this.shouldPrintTrailingComma(endToken); const paramLength = parameters.length; for (let i = 0; i < paramLength; i++) { _param.call(this, parameters[i]); if (trailingComma || i < paramLength - 1) { this.tokenChar(44, i); this.space(); } } this.tokenChar(endToken); this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; } function _param(parameter) { this.printJoin(parameter.decorators, undefined, undefined, undefined, undefined, true); this.print(parameter, undefined, true); if (parameter.optional) { this.tokenChar(63); } this.print(parameter.typeAnnotation, undefined, true); } function _methodHead(node) { const kind = node.kind; const key = node.key; if (kind === "get" || kind === "set") { this.word(kind); this.space(); } if (node.async) { this.word("async", true); this.space(); } if (kind === "method" || kind === "init") { if (node.generator) { this.tokenChar(42); } } if (node.computed) { this.tokenChar(91); this.print(key); this.tokenChar(93); } else { this.print(key); } if (node.optional) { this.tokenChar(63); } if (this._buf._map) { _params.call(this, node, false, node.computed && node.key.type !== "StringLiteral" ? undefined : node.key); } else { _params.call(this, node, false); } } function _predicate(node, noLineTerminatorAfter) { if (node.predicate) { if (!node.returnType) { this.tokenChar(58); } this.space(); this.print(node.predicate, noLineTerminatorAfter); } } function _functionHead(node, parent, hasPredicate) { if (node.async) { this.word("async"); if (!this.format.preserveFormat) { this._innerCommentsState = 0; } this.space(); } this.word("function"); if (node.generator) { if (!this.format.preserveFormat) { this._innerCommentsState = 0; } this.tokenChar(42); } this.space(); if (node.id) { this.print(node.id); } if (this._buf._map) { _params.call(this, node, false, node.id, parent); } else { _params.call(this, node, false); } if (hasPredicate) { _predicate.call(this, node); } } function FunctionExpression(node, parent) { _functionHead.call(this, node, parent, true); this.space(); this.print(node.body); } function ArrowFunctionExpression(node, parent) { if (node.async) { this.word("async", true); this.space(); } if (_shouldPrintArrowParamsParens.call(this, node)) { _params.call(this, node, true, undefined, this._buf._map ? parent : undefined); } else { this.print(node.params[0], true); } _predicate.call(this, node, true); this.space(); this.printInnerComments(); this.token("=>"); this.space(); this.tokenContext |= 2; this.print(node.body); } function _shouldPrintArrowParamsParens(node) { if (node.params.length !== 1) return true; if (node.typeParameters || node.returnType || node.predicate) { return true; } const firstParam = node.params[0]; if (!isIdentifier$1(firstParam) || firstParam.typeAnnotation || firstParam.optional || firstParam.leadingComments?.length || firstParam.trailingComments?.length) { return true; } if (this.tokenMap) { if (node.loc == null) return true; if (this.tokenMap.findMatching(node, "(") !== null) return true; const arrowToken = this.tokenMap.findMatching(node, "=>"); if (arrowToken?.loc == null) return true; return arrowToken.loc.start.line !== node.loc.start.line; } if (this.format.retainLines) return true; return false; } function isRenamedIdentifier(id) { return !!id.loc?.identifierName && id.loc.identifierName !== id.name; } function _getFuncIdName(idNode, parent) { let id = idNode; if (!id && parent) { const parentType = parent.type; if (parentType === "VariableDeclarator") { id = parent.id; } else if (parentType === "AssignmentExpression" || parentType === "AssignmentPattern") { id = parent.left; } else if (parentType === "ObjectProperty" || parentType === "ClassProperty") { if (!parent.computed || parent.key.type === "StringLiteral") { id = parent.key; } } else if (parentType === "ClassPrivateProperty" || parentType === "ClassAccessorProperty") { id = parent.key; } } if (!id?.loc) return; let nameInfo; if (id.type === "Identifier") { if (!isRenamedIdentifier(id)) return; nameInfo = { pos: id.loc.start, name: id.loc.identifierName }; } else if (id.type === "PrivateName") { if (!isRenamedIdentifier(id.id)) return; nameInfo = { pos: id.loc.start, name: "#" + id.id.loc.identifierName }; } else if (id.type === "StringLiteral") { nameInfo = { pos: id.loc.start, name: id.value }; } return nameInfo; } function TSTypeAnnotation(node, parent) { this.token((parent.type === "TSFunctionType" || parent.type === "TSConstructorType") && parent.returnType === node ? "=>" : ":"); this.space(); if (node.optional) this.tokenChar(63); this.print(node.typeAnnotation); } function TSTypeParameterInstantiation(node, parent) { this.tokenChar(60); let printTrailingSeparator = parent.type === "ArrowFunctionExpression" && node.params.length === 1; if (this.tokenMap && node.start != null && node.end != null) { printTrailingSeparator &&= !!this.tokenMap.find(node, t => this.tokenMap.matchesOriginal(t, ",")); printTrailingSeparator ||= this.shouldPrintTrailingComma(">"); } this.printList(node.params, printTrailingSeparator); this.tokenChar(62); } function TSTypeParameter(node) { if (node.const) { this.word("const"); this.space(); } if (node.in) { this.word("in"); this.space(); } if (node.out) { this.word("out"); this.space(); } this.word(node.name.name); if (node.constraint) { this.space(); this.word("extends"); this.space(); this.print(node.constraint); } if (node.default) { this.space(); this.tokenChar(61); this.space(); this.print(node.default); } } function TSParameterProperty(node) { if (node.accessibility) { this.word(node.accessibility); this.space(); } if (node.readonly) { this.word("readonly"); this.space(); } _param.call(this, node.parameter); } function TSDeclareFunction(node, parent) { if (node.declare) { this.word("declare"); this.space(); } _functionHead.call(this, node, parent, false); this.semicolon(); } function TSDeclareMethod(node) { _classMethodHead.call(this, node, false); this.semicolon(); } function TSQualifiedName(node) { this.print(node.left); this.tokenChar(46); this.print(node.right); } function TSCallSignatureDeclaration(node) { tsPrintSignatureDeclarationBase.call(this, node); maybePrintTrailingCommaOrSemicolon(this, node); } function maybePrintTrailingCommaOrSemicolon(printer, node) { if (!printer.tokenMap || !node.start || !node.end) { printer.semicolon(); return; } if (printer.tokenMap.endMatches(node, ",")) { printer.token(","); } else if (printer.tokenMap.endMatches(node, ";")) { printer.semicolon(); } } function TSConstructSignatureDeclaration(node) { this.word("new"); this.space(); tsPrintSignatureDeclarationBase.call(this, node); maybePrintTrailingCommaOrSemicolon(this, node); } function TSPropertySignature(node) { const { readonly } = node; if (readonly) { this.word("readonly"); this.space(); } tsPrintPropertyOrMethodName.call(this, node); this.print(node.typeAnnotation); maybePrintTrailingCommaOrSemicolon(this, node); } function tsPrintPropertyOrMethodName(node) { if (node.computed) { this.tokenChar(91); } this.print(node.key); if (node.computed) { this.tokenChar(93); } if (node.optional) { this.tokenChar(63); } } function TSMethodSignature(node) { const { kind } = node; if (kind === "set" || kind === "get") { this.word(kind); this.space(); } tsPrintPropertyOrMethodName.call(this, node); tsPrintSignatureDeclarationBase.call(this, node); maybePrintTrailingCommaOrSemicolon(this, node); } function TSIndexSignature(node) { const { readonly, static: isStatic } = node; if (isStatic) { this.word("static"); this.space(); } if (readonly) { this.word("readonly"); this.space(); } this.tokenChar(91); _parameters.call(this, node.parameters, 93); this.print(node.typeAnnotation); maybePrintTrailingCommaOrSemicolon(this, node); } function TSAnyKeyword() { this.word("any"); } function TSBigIntKeyword() { this.word("bigint"); } function TSUnknownKeyword() { this.word("unknown"); } function TSNumberKeyword() { this.word("number"); } function TSObjectKeyword() { this.word("object"); } function TSBooleanKeyword() { this.word("boolean"); } function TSStringKeyword() { this.word("string"); } function TSSymbolKeyword() { this.word("symbol"); } function TSVoidKeyword() { this.word("void"); } function TSUndefinedKeyword() { this.word("undefined"); } function TSNullKeyword() { this.word("null"); } function TSNeverKeyword() { this.word("never"); } function TSIntrinsicKeyword() { this.word("intrinsic"); } function TSThisType() { this.word("this"); } function TSFunctionType(node) { tsPrintFunctionOrConstructorType.call(this, node); } function TSConstructorType(node) { if (node.abstract) { this.word("abstract"); this.space(); } this.word("new"); this.space(); tsPrintFunctionOrConstructorType.call(this, node); } function tsPrintFunctionOrConstructorType(node) { const { typeParameters } = node; const parameters = node.params; this.print(typeParameters); this.tokenChar(40); _parameters.call(this, parameters, 41); this.space(); const returnType = node.returnType; this.print(returnType); } function TSTypeReference(node) { const typeArguments = node.typeArguments; this.print(node.typeName, !!typeArguments); this.print(typeArguments); } function TSTypePredicate(node) { if (node.asserts) { this.word("asserts"); this.space(); } this.print(node.parameterName); if (node.typeAnnotation) { this.space(); this.word("is"); this.space(); this.print(node.typeAnnotation.typeAnnotation); } } function TSTypeQuery(node) { this.word("typeof"); this.space(); this.print(node.exprName); const typeArguments = node.typeArguments; if (typeArguments) { this.print(typeArguments); } } function TSTypeLiteral(node) { printBraced(this, node, () => this.printJoin(node.members, true, true, undefined, undefined, true)); } function TSArrayType(node) { this.print(node.elementType, true); this.tokenChar(91); this.tokenChar(93); } function TSTupleType(node) { this.tokenChar(91); this.printList(node.elementTypes, this.shouldPrintTrailingComma("]")); this.tokenChar(93); } function TSOptionalType(node) { this.print(node.typeAnnotation); this.tokenChar(63); } function TSRestType(node) { this.token("..."); this.print(node.typeAnnotation); } function TSNamedTupleMember(node) { this.print(node.label); if (node.optional) this.tokenChar(63); this.tokenChar(58); this.space(); this.print(node.elementType); } function TSUnionType(node) { tsPrintUnionOrIntersectionType(this, node, "|"); } function TSIntersectionType(node) { tsPrintUnionOrIntersectionType(this, node, "&"); } function tsPrintUnionOrIntersectionType(printer, node, sep) { let hasLeadingToken = 0; if (printer.tokenMap?.startMatches(node, sep)) { hasLeadingToken = 1; printer.token(sep); } printer.printJoin(node.types, undefined, undefined, function (i) { this.space(); this.token(sep, undefined, i + hasLeadingToken); this.space(); }); } function TSConditionalType(node) { this.print(node.checkType); this.space(); this.word("extends"); this.space(); this.print(node.extendsType); this.space(); this.tokenChar(63); this.space(); this.print(node.trueType); this.space(); this.tokenChar(58); this.space(); this.print(node.falseType); } function TSInferType(node) { this.word("infer"); this.print(node.typeParameter); } function TSParenthesizedType(node) { this.tokenChar(40); this.print(node.typeAnnotation); this.tokenChar(41); } function TSTypeOperator(node) { this.word(node.operator); this.space(); this.print(node.typeAnnotation); } function TSIndexedAccessType(node) { this.print(node.objectType, true); this.tokenChar(91); this.print(node.indexType); this.tokenChar(93); } function TSMappedType(node) { const { nameType, optional, readonly, typeAnnotation } = node; this.tokenChar(123); const oldNoLineTerminatorAfterNode = this.enterDelimited(); this.space(); if (readonly) { tokenIfPlusMinus(this, readonly); this.word("readonly"); this.space(); } this.tokenChar(91); this.word(node.key.name); this.space(); this.word("in"); this.space(); this.print(node.constraint); if (nameType) { this.space(); this.word("as"); this.space(); this.print(nameType, undefined, true); } this.tokenChar(93); if (optional) { tokenIfPlusMinus(this, optional); this.tokenChar(63); } if (typeAnnotation) { this.tokenChar(58); this.space(); this.print(typeAnnotation, undefined, true); } this.space(); this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; this.tokenChar(125); } function tokenIfPlusMinus(self, tok) { if (tok !== true) { self.token(tok); } } function TSTemplateLiteralType(node) { _printTemplate.call(this, node, node.types); } function TSLiteralType(node) { this.print(node.literal); } function TSClassImplements(node) { this.print(node.expression); this.print(node.typeArguments); } function TSInterfaceDeclaration(node) { const { declare, id, typeParameters, extends: extendz, body } = node; if (declare) { this.word("declare"); this.space(); } this.word("interface"); this.space(); this.print(id); this.print(typeParameters); if (extendz?.length) { this.space(); this.word("extends"); this.space(); this.printList(extendz); } this.space(); this.print(body); } function TSInterfaceBody(node) { printBraced(this, node, () => this.printJoin(node.body, true, true, undefined, undefined, true)); } function TSTypeAliasDeclaration(node) { const { declare, id, typeParameters, typeAnnotation } = node; if (declare) { this.word("declare"); this.space(); } this.word("type"); this.space(); this.print(id); this.print(typeParameters); this.space(); this.tokenChar(61); this.space(); this.print(typeAnnotation); this.semicolon(); } function TSAsExpression(node) { const { expression, typeAnnotation } = node; this.print(expression, true); this.space(); this.word("as"); this.space(); this.print(typeAnnotation); } function TSSatisfiesExpression(node) { const { expression, typeAnnotation } = node; this.print(expression, true); this.space(); this.word("satisfies"); this.space(); this.print(typeAnnotation); } function TSTypeAssertion(node) { const { typeAnnotation, expression } = node; this.tokenChar(60); this.print(typeAnnotation); this.tokenChar(62); this.space(); this.print(expression); } function TSInstantiationExpression(node) { this.print(node.expression); this.print(node.typeArguments); } function TSEnumDeclaration(node) { const { declare, const: isConst, id } = node; if (declare) { this.word("declare"); this.space(); } if (isConst) { this.word("const"); this.space(); } this.word("enum"); this.space(); this.print(id); this.space(); this.print(node.body); } function TSEnumBody(node) { printBraced(this, node, () => this.printList(node.members, this.shouldPrintTrailingComma("}") ?? false, true, true, undefined, true)); } function TSEnumMember(node) { const { id, initializer } = node; this.print(id); if (initializer) { this.space(); this.tokenChar(61); this.space(); this.print(initializer); } } function TSModuleDeclaration(node) { const { declare, kind } = node; if (declare) { this.word("declare"); this.space(); } if (kind !== "global") { this.word(kind); this.space(); } this.print(node.id); if (!node.body) { this.semicolon(); return; } this.space(); this.print(node.body); } function TSModuleBlock(node) { printBraced(this, node, () => this.printSequence(node.body, true, true)); } function TSImportType(node) { const { qualifier, options } = node; this.word("import"); this.tokenChar(40); this.print(node.source); if (options) { this.tokenChar(44); this.print(options); } this.tokenChar(41); if (qualifier) { this.tokenChar(46); this.print(qualifier); } const typeArguments = node.typeArguments; if (typeArguments) { this.print(typeArguments); } } function TSImportEqualsDeclaration(node) { const { id, moduleReference } = node; this.word("import"); this.space(); this.print(id); this.space(); this.tokenChar(61); this.space(); this.print(moduleReference); this.semicolon(); } function TSExternalModuleReference(node) { this.token("require("); this.print(node.expression); this.tokenChar(41); } function TSNonNullExpression(node) { this.print(node.expression); this.tokenChar(33); this.setLastChar(33); } function TSExportAssignment(node) { this.word("export"); this.space(); this.tokenChar(61); this.space(); this.print(node.expression); this.semicolon(); } function TSNamespaceExportDeclaration(node) { this.w