* An arrow function's or a `function`'s parameter list and body, its * `async` and return type skipped. Null for any other expression.
(text: string)
| 1647 | * `async` and return type skipped. Null for any other expression. |
| 1648 | */ |
| 1649 | function functionParts(text: string): { params: string; body: string; block: boolean } | null { |
| 1650 | let s = text.trim().replace(/^async\b\s*/, ''); |
| 1651 | const keyword = /^function\b\s*\*?\s*(?:[A-Za-z_$][\w$]*\s*)?/.exec(s); |
| 1652 | if (keyword) s = s.slice(keyword[0].length); |
| 1653 | let params: string; |
| 1654 | let rest: string; |
| 1655 | if (s.startsWith('(')) { |
| 1656 | const close = matchBracket(s, 0); |
| 1657 | if (close < 0) return null; |
| 1658 | params = s.slice(1, close); |
| 1659 | rest = s.slice(close + 1).trimStart(); |
| 1660 | } else { |
| 1661 | const id = keyword ? null : /^[A-Za-z_$][\w$]*/.exec(s); |
| 1662 | if (!id) return null; |
| 1663 | params = id[0]; |
| 1664 | rest = s.slice(id[0].length).trimStart(); |
| 1665 | } |
| 1666 | // `async (): Promise<RouteProps> => …` |
| 1667 | if (!keyword && rest.startsWith(':')) { |
| 1668 | const arrow = arrowAt(rest); |
| 1669 | if (arrow < 0) return null; |
| 1670 | rest = rest.slice(arrow); |
| 1671 | } |
| 1672 | if (keyword) { |
| 1673 | return rest.startsWith('{') && matchBracket(rest, 0) === rest.length - 1 ? { params, body: rest.slice(1, -1), block: true } : null; |
| 1674 | } |
| 1675 | if (!rest.startsWith('=>')) return null; |
| 1676 | const body = rest.slice(2).trim(); |
| 1677 | if (!body.startsWith('{')) return { params, body, block: false }; |
| 1678 | return matchBracket(body, 0) === body.length - 1 ? { params, body: body.slice(1, -1), block: true } : null; |
| 1679 | } |
| 1680 | |
| 1681 | /** Where the first `=>` outside brackets and strings is in `text`, or -1. */ |
| 1682 | function arrowAt(text: string): number { |
no test coverage detected