| 16 | } |
| 17 | |
| 18 | export async function parseAndFormat(text: string, options?: ParseAndFormatOptions): Promise<ParsedTree> { |
| 19 | // 5MB costs 240ms |
| 20 | const tree = parseJSON(text, options); |
| 21 | |
| 22 | if (options?.kind === "main") { |
| 23 | getViewState().setTree(tree); |
| 24 | } |
| 25 | |
| 26 | if (!tree.valid()) { |
| 27 | if (options?.format) { |
| 28 | tree.text = prettyFormat(text, options); |
| 29 | } |
| 30 | return { treeObject: tree.toObject() }; |
| 31 | } |
| 32 | |
| 33 | if (options?.format) { |
| 34 | // 5MB costs 69ms |
| 35 | tree.stringify(options); |
| 36 | } else if (!isEmpty(tree.nestNodeMap)) { |
| 37 | tree.stringifyNestNodes(); |
| 38 | } |
| 39 | |
| 40 | return { treeObject: tree.toObject() }; |
| 41 | } |