* Where the function written at `at` keeps its body: a block, from its `{` to * its `}`, or an arrow's expression, at `start`. Null for no function.
(safe: string, at: number)
| 2240 | * its `}`, or an arrow's expression, at `start`. Null for no function. |
| 2241 | */ |
| 2242 | function functionBody(safe: string, at: number): { block: boolean; start: number; end: number } | null { |
| 2243 | let i = skipSpace(safe, at); |
| 2244 | const keyword = (word: string): boolean => { |
| 2245 | const re = new RegExp(`${word}\\b`, 'y'); |
| 2246 | re.lastIndex = i; |
| 2247 | if (!re.test(safe)) return false; |
| 2248 | i = skipSpace(safe, re.lastIndex); |
| 2249 | return true; |
| 2250 | }; |
| 2251 | keyword('async'); |
| 2252 | const arrow = !keyword('function'); |
| 2253 | const name = /\*?\s*[A-Za-z_$][\w$]*/y; |
| 2254 | if (!arrow) { |
| 2255 | name.lastIndex = i; |
| 2256 | if (name.test(safe)) i = skipSpace(safe, name.lastIndex); |
| 2257 | } |
| 2258 | if (safe[i] === '(') { |
| 2259 | const close = matchBracket(safe, i); |
| 2260 | if (close < 0) return null; |
| 2261 | i = skipSpace(safe, close + 1); |
| 2262 | } else { |
| 2263 | // `user => [ … ]` |
| 2264 | const param = /[A-Za-z_$][\w$]*/y; |
| 2265 | param.lastIndex = i; |
| 2266 | if (!arrow || !param.test(safe)) return null; |
| 2267 | i = skipSpace(safe, param.lastIndex); |
| 2268 | } |
| 2269 | // A return type, up to the arrow or the body: `(): RouteObject[] => [ … ]`. |
| 2270 | if (safe[i] === ':') { |
| 2271 | const stop = safe.indexOf(arrow ? '=>' : '{', i); |
| 2272 | if (stop < 0) return null; |
| 2273 | i = stop; |
| 2274 | } |
| 2275 | if (arrow) { |
| 2276 | if (!safe.startsWith('=>', i)) return null; |
| 2277 | i = skipSpace(safe, i + 2); |
| 2278 | if (safe[i] !== '{') return { block: false, start: i, end: i }; |
| 2279 | } |
| 2280 | if (safe[i] !== '{') return null; |
| 2281 | const end = blockEnd(safe, i); |
| 2282 | return end < 0 ? null : { block: true, start: i, end }; |
| 2283 | } |
| 2284 | |
| 2285 | /** The statements that start a block of their own after a parenthesized head: never a method's. */ |
| 2286 | const BLOCK_HEADS = new Set(['if', 'for', 'while', 'switch', 'catch', 'with']); |
no test coverage detected