@kubb/parser-ts
Version:
TypeScript parsing and manipulation utilities for Kubb, enabling code generation with proper TypeScript syntax and formatting.
1 lines • 3.99 kB
Source Map (JSON)
{"version":3,"sources":["../src/print.ts","../src/format.ts"],"names":["ts","pluginTypescript","prettierFormat"],"mappings":";;;;;;;;;;;;AAIA,IAAM,EAAE,SAAY,GAAAA,mBAAA;AAapB,IAAM,iBAAiB,CAAC,IAAA,KAAiB,IAAK,CAAA,OAAA,CAAQ,SAAS,mBAAmB,CAAA;AAOlF,IAAM,kBAAkB,CAAC,IAAA,KAAiB,IAAK,CAAA,OAAA,CAAQ,wBAAwB,IAAI,CAAA;AAM5E,SAAS,MACd,QACA,EAAA,EAAE,SAAS,EAAI,EAAA,QAAA,GAAW,YAAY,cAAgB,EAAA,aAAA,EAAe,UAAUA,mBAAG,CAAA,WAAA,CAAY,UAAU,UAAa,GAAAA,mBAAA,CAAG,WAAW,EAAG,EAAA,GAAkB,EAChJ,EAAA;AACR,EAAM,MAAA,UAAA,GAAaA,mBAAG,CAAA,gBAAA,CAAiB,QAAU,EAAA,cAAA,CAAe,MAAM,CAAA,EAAGA,mBAAG,CAAA,YAAA,CAAa,MAAQ,EAAA,KAAA,EAAO,UAAU,CAAA;AAElH,EAAM,MAAA,OAAA,GAAUA,oBAAG,aAAc,CAAA;AAAA,IAC/B,qBAAuB,EAAA,IAAA;AAAA,IACvB,OAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACD,CAAA;AAED,EAAM,MAAA,KAAA,GAAA,CAAS,MAAM,OAAQ,CAAA,QAAQ,IAAI,QAAW,GAAA,CAAC,QAAQ,CAAG,EAAA,MAAA,CAAO,OAAO,CAAE,CAAA,IAAA,CAAK,CAAC,CAAG,EAAA,CAAA,KAAA,CAAO,EAAE,GAAO,IAAA,CAAA,KAAM,CAAE,CAAA,GAAA,IAAO,CAAE,CAAA,CAAA;AAE1H,EAAM,MAAA,MAAA,GAAS,OAAQ,CAAA,SAAA,CAAUA,mBAAG,CAAA,UAAA,CAAW,WAAW,OAAQ,CAAA,eAAA,CAAgB,KAAK,CAAA,EAAG,UAAU,CAAA;AAEpG,EAAA,OAAO,eAAgB,CAAA,MAAM,CAAE,CAAA,OAAA,CAAQ,SAAS,IAAI,CAAA;AACtD;AC3CA,IAAM,aAAyB,GAAA;AAAA,EAC7B,QAAU,EAAA,CAAA;AAAA,EACV,UAAY,EAAA,GAAA;AAAA,EACZ,MAAQ,EAAA,YAAA;AAAA,EACR,WAAa,EAAA,IAAA;AAAA,EACb,IAAM,EAAA,KAAA;AAAA,EACN,eAAiB,EAAA,KAAA;AAAA,EACjB,SAAW,EAAA,MAAA;AAAA,EACX,OAAA,EAAS,CAACC,iCAAgB;AAC5B,CAAA;AAKA,eAAsB,OAAO,MAAgB,EAAA;AAE3C,EAAA,IAAI,CAAC,MAAQ,EAAA;AACX,IAAO,OAAA,EAAA;AAAA;AAGT,EAAO,OAAAC,eAAA,CAAe,QAAQ,aAAa,CAAA;AAC7C","file":"index.cjs","sourcesContent":["import ts from 'typescript'\n\nimport type { PrinterOptions } from 'typescript'\n\nconst { factory } = ts\n\nexport type PrintOptions = {\n source?: string\n baseName?: string\n scriptKind?: ts.ScriptKind\n} & PrinterOptions\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n * @param code The code to escape new lines in\n * @returns The same code but with new lines escaped using block comments\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */')\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n * @param code The code with escaped new lines\n * @returns The same code with new lines restored\n */\nconst restoreNewLines = (code: string) => code.replace(/\\/\\* :newline: \\*\\//g, '\\n')\n\n/**\n * Convert AST TypeScript nodes to a string based on the TypeScript printer.\n * Ensures consistent output across environments.\n */\nexport function print(\n elements: Array<ts.Node>,\n { source = '', baseName = 'print.ts', removeComments, noEmitHelpers, newLine = ts.NewLineKind.LineFeed, scriptKind = ts.ScriptKind.TS }: PrintOptions = {},\n): string {\n const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, false, scriptKind)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine,\n removeComments,\n noEmitHelpers,\n })\n // make sure that the nodes have the same order on every machine\n const nodes = (Array.isArray(elements) ? elements : [elements]).filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)\n\n return restoreNewLines(output).replace(/\\r\\n/g, '\\n')\n}\n","import { format as prettierFormat } from 'prettier'\nimport pluginTypescript from 'prettier/plugins/typescript'\n\nimport type { Options } from 'prettier'\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}\n\n/**\n * Format the generated code based on Prettier\n */\nexport async function format(source: string) {\n // do some basic linting with the ts compiler\n if (!source) {\n return ''\n }\n\n return prettierFormat(source, formatOptions)\n}\n"]}