|
1 | | -import type { Printer } from 'prettier'; |
| 1 | +import { type AstPath, doc as AST, type Printer } from 'prettier'; |
| 2 | +import { printers as prettierPrinters } from 'prettier/plugins/estree.js'; |
2 | 3 |
|
3 | | -import { printer } from './printers/index.js'; |
| 4 | +import type { PluginOptions } from './options.js'; |
| 5 | +import { |
| 6 | + checkPrettierIgnore, |
| 7 | + docMatchesString, |
| 8 | + fixPreviousPrint, |
| 9 | + printRawText, |
| 10 | + printTemplateContent, |
| 11 | + printTemplateTag, |
| 12 | + saveCurrentPrintOnSiblingNode, |
| 13 | + trimPrinted, |
| 14 | +} from './printers/index.js'; |
| 15 | +import { isGlimmerTemplate, isGlimmerTemplateParent } from './types/glimmer.js'; |
| 16 | +import { assert } from './utils/assert.js'; |
4 | 17 | import { type NodeType, PRINTER_NAME } from './utils/index.js'; |
5 | 18 |
|
| 19 | +const printer = prettierPrinters['estree'] as Printer<NodeType>; |
| 20 | + |
| 21 | +function embed(path: AstPath<NodeType>, options: PluginOptions<NodeType>) { |
| 22 | + const { node } = path; |
| 23 | + |
| 24 | + return async ( |
| 25 | + textToDoc: ( |
| 26 | + text: string, |
| 27 | + options: PluginOptions<NodeType>, |
| 28 | + ) => Promise<AST.builders.Doc>, |
| 29 | + ) => { |
| 30 | + if (node && isGlimmerTemplate(node)) { |
| 31 | + if (checkPrettierIgnore(path)) { |
| 32 | + return printRawText(path, options); |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + const content = await printTemplateContent( |
| 37 | + node.extra.template.contents, |
| 38 | + textToDoc, |
| 39 | + options, |
| 40 | + ); |
| 41 | + |
| 42 | + const printed = printTemplateTag(content); |
| 43 | + saveCurrentPrintOnSiblingNode(path, printed); |
| 44 | + return printed; |
| 45 | + } catch (error) { |
| 46 | + console.error(error); |
| 47 | + const printed = [printRawText(path, options)]; |
| 48 | + saveCurrentPrintOnSiblingNode(path, printed); |
| 49 | + return printed; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Nothing to embed, so move on to the regular printer. |
| 54 | + return; |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function getVisitorKeys(node: NodeType, nonTraversableKeys: Set<string>) { |
| 59 | + if (node && isGlimmerTemplate(node)) { |
| 60 | + return []; |
| 61 | + } |
| 62 | + return printer.getVisitorKeys?.(node, nonTraversableKeys) || []; |
| 63 | +} |
| 64 | + |
| 65 | +function print( |
| 66 | + path: AstPath<NodeType>, |
| 67 | + options: PluginOptions<NodeType>, |
| 68 | + print: (path: AstPath<NodeType>) => AST.builders.Doc, |
| 69 | + args?: unknown, |
| 70 | +) { |
| 71 | + const { node } = path; |
| 72 | + |
| 73 | + if (isGlimmerTemplateParent(node)) { |
| 74 | + if (checkPrettierIgnore(path)) { |
| 75 | + return printRawText(path, options); |
| 76 | + } else { |
| 77 | + let printed = printer.print(path, options, print, args); |
| 78 | + |
| 79 | + assert('Expected Glimmer doc to be an array', Array.isArray(printed)); |
| 80 | + trimPrinted(printed); |
| 81 | + |
| 82 | + // Remove semicolons so we can manage them ourselves |
| 83 | + if (docMatchesString(printed[0], ';')) { |
| 84 | + printed.shift(); |
| 85 | + } |
| 86 | + if (docMatchesString(printed.at(-1), ';')) { |
| 87 | + printed.pop(); |
| 88 | + } |
| 89 | + |
| 90 | + trimPrinted(printed); |
| 91 | + |
| 92 | + // Always remove export default so we start with a blank slate |
| 93 | + if ( |
| 94 | + docMatchesString(printed[0], 'export') && |
| 95 | + docMatchesString(printed[1], 'default') |
| 96 | + ) { |
| 97 | + printed = printed.slice(2); |
| 98 | + trimPrinted(printed); |
| 99 | + } |
| 100 | + |
| 101 | + if (options.templateExportDefault) { |
| 102 | + printed.unshift('export ', 'default '); |
| 103 | + } |
| 104 | + |
| 105 | + saveCurrentPrintOnSiblingNode(path, printed); |
| 106 | + |
| 107 | + return printed; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (options.semi && node?.extra?.['prevTemplatePrinted']) { |
| 112 | + fixPreviousPrint( |
| 113 | + node.extra['prevTemplatePrinted'] as AST.builders.Doc[], |
| 114 | + path, |
| 115 | + options, |
| 116 | + print, |
| 117 | + args, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + return printer.print(path, options, print, args); |
| 122 | +} |
| 123 | + |
| 124 | +function printPrettierIgnored( |
| 125 | + path: AstPath<NodeType>, |
| 126 | + options: PluginOptions<NodeType>, |
| 127 | +) { |
| 128 | + return printRawText(path, options); |
| 129 | +} |
| 130 | + |
6 | 131 | export const printers: Record<string, Printer<NodeType>> = { |
7 | | - [PRINTER_NAME]: printer, |
| 132 | + [PRINTER_NAME]: { |
| 133 | + ...printer, |
| 134 | + // @ts-expect-error: Type <...> is not assignable to <...> |
| 135 | + embed, |
| 136 | + getVisitorKeys, |
| 137 | + print, |
| 138 | + printPrettierIgnored, |
| 139 | + }, |
8 | 140 | }; |
0 commit comments