{"version":3,"file":"index.cjs","names":["formatOptions: Options","pluginTypescript","ts","output: string"],"sources":["../src/format.ts","../src/print.ts"],"sourcesContent":["import type { Options } from 'prettier'\nimport { format as prettierFormat } from 'prettier'\nimport pluginTypescript from 'prettier/plugins/typescript'\n\nconst formatOptions: Options = {\n  tabWidth: 2,\n  printWidth: 160,\n  parser: 'typescript',\n  singleQuote: true,\n  semi: false,\n  bracketSameLine: false,\n  endOfLine: 'auto',\n  plugins: [pluginTypescript],\n}\nexport function format(source?: string): Promise<string> {\n  if (!source) {\n    return Promise.resolve('')\n  }\n\n  try {\n    return prettierFormat(source, formatOptions)\n  } catch (_e) {\n    return Promise.resolve(source)\n  }\n}\n","import ts from 'typescript'\n\nconst { factory } = ts\n\nexport type PrintOptions = {\n  source?: string\n  baseName?: string\n  scriptKind?: ts.ScriptKind\n}\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */')\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n */\nconst restoreNewLines = (code: string) => code.replace(/\\/\\* :newline: \\*\\//g, '\\n')\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n * Ensures consistent output across environments.\n * Also works as a formatter when `source` is provided without `elements`.\n */\nexport function print(elements: Array<ts.Node> = [], { source = '', baseName = 'print.tsx', scriptKind = ts.ScriptKind.TSX }: PrintOptions = {}): string {\n  const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind)\n\n  const printer = ts.createPrinter({\n    omitTrailingSemicolon: true,\n    newLine: ts.NewLineKind.LineFeed,\n    removeComments: false,\n    noEmitHelpers: true,\n  })\n\n  let output: string\n\n  if (elements.length > 0) {\n    // Print only provided nodes\n    const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))\n    output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)\n  } else {\n    // Format the whole file\n    output = printer.printFile(sourceFile)\n  }\n\n  return restoreNewLines(output).replace(/\\r\\n/g, '\\n')\n}\n"],"mappings":";;;;;;;;;AAIA,MAAMA,gBAAyB;CAC7B,UAAU;CACV,YAAY;CACZ,QAAQ;CACR,aAAa;CACb,MAAM;CACN,iBAAiB;CACjB,WAAW;CACX,SAAS,CAACC,oCAAiB;CAC5B;AACD,SAAgB,OAAO,QAAkC;AACvD,KAAI,CAAC,OACH,QAAO,QAAQ,QAAQ,GAAG;AAG5B,KAAI;AACF,8BAAsB,QAAQ,cAAc;UACrC,IAAI;AACX,SAAO,QAAQ,QAAQ,OAAO;;;;;;ACpBlC,MAAM,EAAE,YAAYC;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS,oBAAoB;;;;AAKnF,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB,KAAK;;;;;;AAOpF,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAaA,mBAAG,WAAW,QAAsB,EAAE,EAAU;CACvJ,MAAM,aAAaA,mBAAG,iBAAiB,UAAU,eAAe,OAAO,EAAEA,mBAAG,aAAa,QAAQ,MAAM,WAAW;CAElH,MAAM,UAAUA,mBAAG,cAAc;EAC/B,uBAAuB;EACvB,SAASA,mBAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC;CAEF,IAAIC;AAEJ,KAAI,SAAS,SAAS,GAAG;EAEvB,MAAM,QAAQ,SAAS,OAAO,QAAQ,CAAC,MAAM,GAAG,OAAO,EAAE,OAAO,MAAM,EAAE,OAAO,GAAG;AAClF,WAAS,QAAQ,UAAUD,mBAAG,WAAW,WAAW,QAAQ,gBAAgB,MAAM,EAAE,WAAW;OAG/F,UAAS,QAAQ,UAAU,WAAW;AAGxC,QAAO,gBAAgB,OAAO,CAAC,QAAQ,SAAS,KAAK"}