| 8 | } = prettier; |
| 9 | |
| 10 | function makePlugin(withOutdatedApi = false) { |
| 11 | return { |
| 12 | parsers: { |
| 13 | // parsers leak across tests, so the name needs to be unique |
| 14 | [withOutdatedApi ? "baz-parser-outdated" : "baz-parser"]: { |
| 15 | parse: (text) => ({ |
| 16 | type: "root", |
| 17 | lines: text |
| 18 | .trim() |
| 19 | .split("\n") |
| 20 | .map((line) => ({ |
| 21 | type: line.startsWith("{") ? "json" : "plain", |
| 22 | text: line, |
| 23 | })), |
| 24 | }), |
| 25 | astFormat: "baz-ast", |
| 26 | }, |
| 27 | }, |
| 28 | printers: { |
| 29 | "baz-ast": { |
| 30 | print(path, options, print) { |
| 31 | const { type, text } = path.getValue(); |
| 32 | switch (type) { |
| 33 | case "root": |
| 34 | return path.map(print, "lines"); |
| 35 | case "json": |
| 36 | case "plain": |
| 37 | return [text, hardline]; |
| 38 | } |
| 39 | }, |
| 40 | embed: withOutdatedApi |
| 41 | ? (path, print, textToDoc) => { |
| 42 | const { type, text } = path.getValue(); |
| 43 | if (type === "json") { |
| 44 | return [ |
| 45 | textToDoc(text, { |
| 46 | parser: "json", |
| 47 | printWidth: Number.POSITIVE_INFINITY, |
| 48 | }), |
| 49 | hardline, |
| 50 | ]; |
| 51 | } |
| 52 | } |
| 53 | : (path) => { |
| 54 | const { type, text } = path.getValue(); |
| 55 | if (type === "json") { |
| 56 | return async (textToDoc) => [ |
| 57 | await textToDoc(text, { |
| 58 | parser: "json", |
| 59 | printWidth: Number.POSITIVE_INFINITY, |
| 60 | }), |
| 61 | hardline, |
| 62 | ]; |
| 63 | } |
| 64 | }, |
| 65 | }, |
| 66 | }, |
| 67 | }; |