diff --git a/packages/action/dist/index.js b/packages/action/dist/index.js index 5b6a255..0e64167 100644 --- a/packages/action/dist/index.js +++ b/packages/action/dist/index.js @@ -203644,6 +203644,8 @@ var JS_FAMILY_QUERY = ` (function_declaration) @chunk (class_declaration) @chunk (method_definition) @chunk +(variable_declarator value: (arrow_function)) @chunk +(variable_declarator value: (function_expression)) @chunk `; var PYTHON_QUERY = ` (function_definition) @chunk @@ -203686,11 +203688,16 @@ function findEnclosingClassName(node, classNodeType) { return null; } function getChunkContainer(declNode) { - const parent = declNode.parent; - if (parent && parent.type === "export_statement") { - return parent; + let container = declNode; + const parent = container.parent; + if (parent && (parent.type === "lexical_declaration" || parent.type === "variable_declaration") && parent.namedChildCount === 1) { + container = parent; } - return declNode; + const grandparent = container.parent; + if (grandparent && grandparent.type === "export_statement") { + container = grandparent; + } + return container; } function extractSignature(container) { const text = container.text; @@ -203734,7 +203741,7 @@ async function extractChunksFromSource(source, filePath, grammar) { continue; const declNode = capture.node; const nameNode = declNode.childForFieldName("name"); - if (!nameNode) + if (!nameNode || nameNode.type !== "identifier") continue; const kind = resolveKind(declNode, grammar); const className = kind === "method" ? findEnclosingClassName(declNode, classNodeType) : null; diff --git a/packages/core/src/parsing/extract.ts b/packages/core/src/parsing/extract.ts index 94decb7..ea66fb7 100644 --- a/packages/core/src/parsing/extract.ts +++ b/packages/core/src/parsing/extract.ts @@ -40,11 +40,29 @@ function findEnclosingClassName(node: Node, classNodeType: string): string | nul } function getChunkContainer(declNode: Node): Node { - const parent = declNode.parent; - if (parent && parent.type === 'export_statement') { - return parent; + let container = declNode; + + // `const foo = () => {}` captures the `variable_declarator`, but the + // declaration keyword (and any `export`) lives one or two levels up. + // Skip the widen when siblings share the statement (`const a = 1, b = () => {}`) + // - otherwise every declarator on the line would get an identical + // body/signature, causing false-positive staleness on unrelated siblings + // and indistinguishable embedding text. + const parent = container.parent; + if ( + parent && + (parent.type === 'lexical_declaration' || parent.type === 'variable_declaration') && + parent.namedChildCount === 1 + ) { + container = parent; + } + + const grandparent = container.parent; + if (grandparent && grandparent.type === 'export_statement') { + container = grandparent; } - return declNode; + + return container; } function extractSignature(container: Node): string { @@ -97,7 +115,9 @@ export async function extractChunksFromSource( if (capture.name !== 'chunk') continue; const declNode = capture.node; const nameNode = declNode.childForFieldName('name'); - if (!nameNode) continue; + // `variable_declarator`'s name field can be a destructuring pattern + // (`const { a, b } = () => {}`) - not a real declaration name, skip it. + if (!nameNode || nameNode.type !== 'identifier') continue; const kind = resolveKind(declNode, grammar); const className = kind === 'method' ? findEnclosingClassName(declNode, classNodeType) : null; diff --git a/packages/core/src/parsing/queries.ts b/packages/core/src/parsing/queries.ts index 9da64bc..22ec102 100644 --- a/packages/core/src/parsing/queries.ts +++ b/packages/core/src/parsing/queries.ts @@ -2,6 +2,8 @@ export const JS_FAMILY_QUERY = ` (function_declaration) @chunk (class_declaration) @chunk (method_definition) @chunk +(variable_declarator value: (arrow_function)) @chunk +(variable_declarator value: (function_expression)) @chunk `; export const PYTHON_QUERY = `