|
1 | 1 | import pluginTypescript from "prettier/plugins/typescript"; |
| 2 | +import pluginEstree from "prettier/plugins/estree"; |
2 | 3 | import { magic, preProcess } from "./replace.js"; |
3 | 4 | import { builders } from "prettier/doc"; |
4 | 5 |
|
5 | | -let as_estree = {}; |
| 6 | +// Get the estree printer from the estree plugin |
| 7 | +const baseEstreePrinter = pluginEstree.printers.estree; |
6 | 8 |
|
7 | | -async function initPrinter(jsPlugin) { |
8 | | - let estree = jsPlugin.printers.estree; |
9 | | - estree = typeof estree == "function" ? await estree() : estree; |
10 | | - Object.assign(as_estree, { |
11 | | - ...estree, |
12 | | - printComment(commentPath, options) { |
13 | | - let comment = commentPath.getValue().value; |
14 | | - if (comment.startsWith(magic) && comment.endsWith(magic)) { |
15 | | - let doc = []; |
16 | | - if (commentPath.stack[commentPath.stack.length - 2] == 0) { |
17 | | - doc.push(builders.hardline); |
18 | | - } |
19 | | - doc.push(comment.slice(magic.length, -magic.length)); |
20 | | - return doc; |
21 | | - } else { |
22 | | - return jsPlugin.printers.estree.printComment(commentPath, options); |
| 9 | +// Initialize the AssemblyScript estree printer at module load time |
| 10 | +const as_estree = { |
| 11 | + ...baseEstreePrinter, |
| 12 | + printComment(commentPath, options) { |
| 13 | + let comment = commentPath.getValue().value; |
| 14 | + if (comment.startsWith(magic) && comment.endsWith(magic)) { |
| 15 | + let doc = []; |
| 16 | + if (commentPath.stack[commentPath.stack.length - 2] == 0) { |
| 17 | + doc.push(builders.hardline); |
23 | 18 | } |
24 | | - }, |
25 | | - }); |
26 | | -} |
| 19 | + doc.push(comment.slice(magic.length, -magic.length)); |
| 20 | + return doc; |
| 21 | + } else { |
| 22 | + return baseEstreePrinter.printComment(commentPath, options); |
| 23 | + } |
| 24 | + }, |
| 25 | +}; |
27 | 26 |
|
28 | | -async function parse(text, options) { |
29 | | - await initPrinter(options.plugins.find((plugin) => plugin.printers && plugin.printers.estree)); |
| 27 | +function parse(text, options) { |
30 | 28 | let ast = pluginTypescript.parsers.typescript.parse(text, options); |
31 | 29 | return ast; |
32 | 30 | } |
|
0 commit comments