* Extract a function
(node: SyntaxNode, nameOverride?: string)
| 2364 | * Extract a function |
| 2365 | */ |
| 2366 | private extractFunction(node: SyntaxNode, nameOverride?: string): void { |
| 2367 | if (!this.extractor) return; |
| 2368 | |
| 2369 | // If the language provides getReceiverType and this function has a receiver |
| 2370 | // (e.g., Rust function_item inside an impl block), extract as method instead |
| 2371 | if (this.extractor.getReceiverType?.(node, this.source)) { |
| 2372 | this.extractMethod(node); |
| 2373 | return; |
| 2374 | } |
| 2375 | |
| 2376 | // `Ctor.prototype.m = function () {…}` / `Ctor.m = function () {…}`: a |
| 2377 | // method of the constructor (or class) the scope declares (#2966). |
| 2378 | if ( |
| 2379 | !nameOverride && |
| 2380 | (node.type === 'function_expression' || node.type === 'arrow_function' || node.type === 'generator_function') && |
| 2381 | node.parent?.type === 'assignment_expression' |
| 2382 | ) { |
| 2383 | const member = this.constructorMember(node.parent, node); |
| 2384 | if (member) { |
| 2385 | this.extractConstructorMember(node, node.parent, member); |
| 2386 | return; |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | // nameOverride is supplied only for explicitly-named anonymous functions the |
| 2391 | // caller resolved itself (e.g. arrow values of exported-const object members |
| 2392 | // — SvelteKit actions). Inline-object arrows reached by the general walker |
| 2393 | // get no override, so they still fall through to the <anonymous> skip below. |
| 2394 | let name = nameOverride ?? extractName(node, this.source, this.extractor); |
| 2395 | // A CommonJS export assignment names the function it holds — see below. |
| 2396 | let commonJsExport = false; |
| 2397 | // For arrow functions and function expressions assigned to variables, |
| 2398 | // resolve the name from the parent variable_declarator. |
| 2399 | // e.g. `export const useAuth = () => { ... }` — the arrow_function node |
| 2400 | // has no `name` field; the name lives on the variable_declarator. |
| 2401 | if ( |
| 2402 | !nameOverride && |
| 2403 | name === '<anonymous>' && |
| 2404 | (node.type === 'arrow_function' || node.type === 'function_expression' || node.type === 'generator_function') |
| 2405 | ) { |
| 2406 | const parent = node.parent; |
| 2407 | if (parent?.type === 'variable_declarator') { |
| 2408 | const varName = getChildByField(parent, 'name'); |
| 2409 | if (varName) { |
| 2410 | name = getNodeText(varName, this.source); |
| 2411 | } |
| 2412 | } else if (parent?.type === 'assignment_expression') { |
| 2413 | // `exports.getItems = async (req, res) => {…}` / `module.exports.x = |
| 2414 | // function () {…}` — the CommonJS controller style. The function is |
| 2415 | // anonymous only syntactically: the export property is the name every |
| 2416 | // `router.get('/items', getItems)` resolves. Without a node the handler |
| 2417 | // is invisible to callers/impact and its calls attribute to the file |
| 2418 | // (#1675). Same treatment `const X = () => {}` already gets. |
| 2419 | const exportName = this.commonJsExportName(parent, node); |
| 2420 | if (exportName) { |
| 2421 | name = exportName; |
| 2422 | commonJsExport = true; |
| 2423 | } |
no test coverage detected