Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/tc39-try. Enable support for try expressions in JavaScript tools
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243import { generateTransform, MagicStringAST } from 'magic-string-ast'import { createUnplugin, type UnpluginInstance } from 'unplugin'import { untsx } from '.'import type { Codes } from 'ts-macro'
export const FN_NAME = '__v_try'const HELPER_IMPORT = `import { t as ${FN_NAME} } from 'tc39-try/runtime';\n`
export function transformTryOperator( code: string, id: string, s: MagicStringAST | Codes,): boolean { const changed = untsx.transform(code, id, s) if (changed) { s.replaceRange(0, 0, HELPER_IMPORT) } return changed}
const unplugin: UnpluginInstance<{} | undefined, false> = createUnplugin(() => { return { name: 'tc39-try', enforce: 'pre', transform: { filter: { code: /\btry\b/, id: { include: /\.[cm]?[jt]sx?$/, exclude: /node_modules/, }, }, handler(code, id) { const s = new MagicStringAST(code) transformTryOperator(code, id, s) return generateTransform(s, id) }, }, }})
export default unplugin