Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/untsx. Universal TypeScript extension for all popular toolchains.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163// @ts-expect-errorimport { parsers as prettierEspree } from 'prettier/plugins/acorn.mjs'// @ts-expect-errorimport { parsers as prettierBabel } from 'prettier/plugins/babel.mjs'// @ts-expect-errorimport { printers } from 'prettier/plugins/estree.mjs'// @ts-expect-errorimport { parsers as prettierTS } from 'prettier/plugins/typescript.mjs'import { REGEX_JSX, type UntsxFactory, type UntsxInstance } from '.'import type { ParserOptions } from '@babel/parser'import type { Comment } from 'acorn'import type { Parser, Plugin } from 'prettier'
const babelOptions: ParserOptions = { sourceType: 'module', allowImportExportEverywhere: true, allowReturnOutsideFunction: true, allowNewTargetOutsideFunction: true, allowSuperOutsideMethod: true, allowUndeclaredExports: true, errorRecovery: true, createParenthesizedExpressions: true, createImportExpressions: true, attachComment: false, plugins: [ 'doExpressions', 'exportDefaultFrom', 'functionBind', 'functionSent', 'throwExpressions', 'partialApplication', 'decorators', 'moduleBlocks', 'asyncDoExpressions', 'destructuringPrivate', 'decoratorAutoAccessors', 'sourcePhaseImports', 'deferredImportEvaluation', ['optionalChainingAssign', { version: '2023-07' }], ], tokens: false, ranges: false,}
export function createPrettierPlugin( { shouldTransform, isTarget, getVisitorKeys, format }: UntsxFactory, acorn: UntsxInstance['acorn'], babel: UntsxInstance['babel'], espree: UntsxInstance['espree'], // TODO eslintTypescript: UntsxInstance['eslintTypescript'],): Plugin { const acornParser: Parser = { ...prettierEspree.acorn, parse(text, options) { if (shouldTransform && !shouldTransform(text, options.filepath)) { return prettierEspree.acorn.parse(text, options) }
const comments: Comment[] = [] const ast = acorn(text, { ecmaVersion: 'latest', sourceType: 'module', onComment: comments, allowReserved: true, allowReturnOutsideFunction: true, allowSuperOutsideMethod: true, allowImportExportEverywhere: true, checkPrivateFields: false, locations: false, ranges: true, preserveParens: true, }) // @ts-expect-error -- expected ast.comments = comments return ast }, }
const babelParser: Parser = { ...prettierBabel.babel, parse(text, options) { if (shouldTransform && !shouldTransform(text, options.filepath)) { return prettierBabel.babel.parse(text, options) }
return babel(text, babelOptions) }, } const babelTsParser: Parser = { ...prettierBabel['babel-ts'], parse(text, options) { if (shouldTransform && !shouldTransform(text, options.filepath)) { return prettierBabel['babel-ts'].parse(text, options) }
return babel(text, { ...babelOptions, plugins: [ 'typescript', ...(REGEX_JSX.test(options.filepath) ? ['jsx' as const] : []), ...(babelOptions.plugins || []), ], }) }, }
const typescriptParser: Parser = { ...prettierTS.typescript, parse(text, options) { if (shouldTransform && !shouldTransform(text, options.filepath)) { return prettierTS.typescript.parse(text, options) }
return eslintTypescript(text, { sourceType: 'module', loc: true, range: true, comment: true, tokens: false, loggerFn: false, project: false, jsDocParsingMode: 'none', suppressDeprecatedPropertyWarnings: true, filePath: options.filepath, }) }, }
return { parsers: { acorn: acornParser, babel: babelParser, 'babel-ts': babelTsParser, typescript: typescriptParser, }, printers: format || getVisitorKeys ? { estree: { ...printers.estree, print: format ? (path, ...args) => { if (isTarget(path.node)) { return format(path, ...args) } return printers.estree.print(path, ...args) } : printers.estree.print,
getVisitorKeys: getVisitorKeys ? (node, ...args) => { if (isTarget(node)) { return getVisitorKeys(node, ...args) } return printers.estree.getVisitorKeys(node, ...args) } : printers.estree.getVisitorKeys, }, } : undefined, }}