Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts-snapshot. DTS snapshot plugin for Rolldown
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152import { readFile } from 'node:fs/promises'import { createFromBuffer, type Formatter, type GlobalConfiguration,} from '@dprint/formatter'import { getPath } from '@dprint/typescript'import { walk } from 'estree-walker'import MagicString from 'magic-string'import { parseSync } from 'rolldown/utils'import { sortObjectKeys } from './utils.ts'import type { Node, Span, TSTypeAnnotation } from '@oxc-project/types'
const multilineCommentsRE = /\/\*.*?\*\//gsconst singlelineCommentsRE = /\/\/.*$/gm
export async function snapshot( code: string, fileName: string = 'dummy.d.ts', { applyExportRename = true }: { applyExportRename?: boolean } = {},): Promise<Record<string, string>> { code = code .replaceAll(multilineCommentsRE, '') .replaceAll(singlelineCommentsRE, '') const s = new MagicString(code) const slice = (node: Span) => s.slice(node.start, node.end)
const { program } = parseSync(fileName, code)
;(walk as any)(program, { enter(node: Node, parent: Node, key: string) { if ( key === 'params' && (node.type === 'Identifier' || node.type === 'ObjectPattern' || node.type === 'ArrayPattern') ) { const end = node.typeAnnotation?.start ?? node.end s.overwrite(node.start, end, '_') } }, })
const result: Record<string, string> = Object.create(null)
if (!tsFormatter) { await initFormatter() }
for (const stmt of program.body) { let decl: Node if ( (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') && stmt.declaration ) { decl = stmt.declaration } else { decl = stmt }
const register = (symbol: string, node: Node) => { let code: string | undefined if (node.type === 'VariableDeclarator') { const typeAnnotation = ( node.id.typeAnnotation as TSTypeAnnotation | null | undefined )?.typeAnnotation if (typeAnnotation) { code = s.slice(typeAnnotation.start, node.end) } else if (node.init) { code = slice(node.init) } } code ||= slice(node)
const snapshot = format(code) result[symbol] = snapshot }
if (decl.type === 'VariableDeclaration') { for (const node of decl.declarations) { register(nodeToString(node.id), node) } } else if ('id' in decl && decl.id) { register(nodeToString(decl.id), decl) } else if ( // default export decl.type === 'ExportDefaultDeclaration' && 'id' in decl.declaration && decl.declaration.id ) { register('default', decl.declaration) } }
if (applyExportRename) for (const stmt of program.body) { if ( stmt.type === 'ExportNamedDeclaration' && stmt.declaration === null && stmt.specifiers.length ) { for (const specifier of stmt.specifiers) { const exported = nodeToString(specifier.exported) const local = nodeToString(specifier.local) if (local !== exported) { result[exported] = result[local] } } } }
return sortObjectKeys(result)
function nodeToString(node: Node) { return node.type === 'Identifier' ? node.name : node.type === 'Literal' ? (node.value as string) : slice(node) }}let tsFormatter: Formatterasync function initFormatter() { const globalConfig: GlobalConfiguration = { indentWidth: 1, lineWidth: 100000000, } tsFormatter = createFromBuffer(await readFile(getPath())) tsFormatter.setConfig(globalConfig, { semiColons: 'asi', preferSingleLine: true, 'arrowFunction.useParentheses': 'force', quoteStyle: 'alwaysSingle',
singleBodyPosition: 'sameLine', bracePosition: 'sameLine', operatorPosition: 'sameLine', preferHanging: true, })}
function format(code: string) { return tsFormatter .formatText({ filePath: 'dummy.d.ts', fileText: code, }) .trim() .replaceAll('\n\n', '\n')}