UNPKG

@kubb/parser-ts

Version:

TypeScript parsing and manipulation utilities for Kubb, enabling code generation with proper TypeScript syntax and formatting.

40 lines (38 loc) 1.48 kB
export { factory_exports as factory } from './chunk-BEHWCDCH.js'; import ts from 'typescript'; import { format as format$1 } from 'prettier'; import pluginTypescript from 'prettier/plugins/typescript'; var { factory } = ts; var escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */"); var restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n"); function print(elements, { source = "", baseName = "print.ts", removeComments, noEmitHelpers, newLine = ts.NewLineKind.LineFeed, scriptKind = ts.ScriptKind.TS } = {}) { const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, false, scriptKind); const printer = ts.createPrinter({ omitTrailingSemicolon: true, newLine, removeComments, noEmitHelpers }); const nodes = (Array.isArray(elements) ? elements : [elements]).filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0)); const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile); return restoreNewLines(output).replace(/\r\n/g, "\n"); } var formatOptions = { tabWidth: 2, printWidth: 160, parser: "typescript", singleQuote: true, semi: false, bracketSameLine: false, endOfLine: "auto", plugins: [pluginTypescript] }; async function format(source) { if (!source) { return ""; } return format$1(source, formatOptions); } export { format, print }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map