* 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)
| 1669 | * `async` and return type skipped. Null for any other expression. |
| 1670 | */ |
| 1671 | function functionParts(text: string): { params: string; body: string; block: boolean } | null { |
| 1672 | let s = text.trim().replace(/^async\b\s*/, ''); |
| 1673 | const keyword = /^function\b\s*\*?\s*(?:[A-Za-z_$][\w$]*\s*)?/.exec(s); |
| 1674 | if (keyword) s = s.slice(keyword[0].length); |
| 1675 | let params: string; |
| 1676 | let rest: string; |
| 1677 | if (s.startsWith('(')) { |
| 1678 | const close = matchBracket(s, 0); |
| 1679 | if (close < 0) return null; |
| 1680 | params = s.slice(1, close); |
| 1681 | rest = s.slice(close + 1).trimStart(); |
| 1682 | } else { |
| 1683 | const id = keyword ? null : /^[A-Za-z_$][\w$]*/.exec(s); |
| 1684 | if (!id) return null; |
| 1685 | params = id[0]; |
| 1686 | rest = s.slice(id[0].length).trimStart(); |
| 1687 | } |
| 1688 | // `async (): Promise<RouteProps> => …` |
| 1689 | if (!keyword && rest.startsWith(':')) { |
| 1690 | const arrow = arrowAt(rest); |
| 1691 | if (arrow < 0) return null; |
| 1692 | rest = rest.slice(arrow); |
| 1693 | } |
| 1694 | if (keyword) { |
| 1695 | return rest.startsWith('{') && matchBracket(rest, 0) === rest.length - 1 ? { params, body: rest.slice(1, -1), block: true } : null; |
| 1696 | } |
| 1697 | if (!rest.startsWith('=>')) return null; |
| 1698 | const body = rest.slice(2).trim(); |
| 1699 | if (!body.startsWith('{')) return { params, body, block: false }; |
| 1700 | return matchBracket(body, 0) === body.length - 1 ? { params, body: body.slice(1, -1), block: true } : null; |
| 1701 | } |
| 1702 | |
| 1703 | /** Where the first `=>` outside brackets and strings is in `text`, or -1. */ |
| 1704 | function arrowAt(text: string): number { |
no test coverage detected