(source: string)
| 6 | const parser = Parser.extend(jsx({ allowNamespaces: false })); |
| 7 | |
| 8 | export const parse = (source: string): SparseNode => { |
| 9 | const parsed = parser.parse(source, { |
| 10 | ecmaVersion: 2015, |
| 11 | }); |
| 12 | |
| 13 | if (parsed.body.length !== 1) { |
| 14 | throw new SyntaxError(`Expected a single root node, but got: ${parsed.body.length}`); |
| 15 | } |
| 16 | |
| 17 | if (parsed.body[0].type !== 'ExpressionStatement') { |
| 18 | throw new SyntaxError(`Expected an expression statement, but got: ${parsed.body[0].type}`); |
| 19 | } |
| 20 | |
| 21 | return parseNode(parsed.body[0].expression); |
| 22 | }; |
| 23 | |
| 24 | const parseNode = (node: any): SparseNode => { |
| 25 | const children = (node.children ?? []).map(parseNode).filter(Boolean); |
no test coverage detected