UNPKG

apisurf

Version:

Analyze API surface changes between npm package versions to catch breaking changes

45 lines (44 loc) 1.92 kB
import { parseParameters } from './parseParameters.js'; /** * Parse function signature from const/let/var declarations */ export function parseFunctionSignature(lines, startIndex, name) { const fullLine = lines[startIndex]; // Check if this is an arrow function const arrowFunctionMatch = fullLine.match(/export\s+(?:const|let|var)\s+\w+\s*=\s*(?:async\s+)?(?:<[^>]+>)?\s*\(/); if (arrowFunctionMatch) { // Extract the full function signature let fullSignature = fullLine.trim(); // Handle multi-line signatures if (!fullSignature.includes(';') && !fullSignature.includes('{')) { for (let i = startIndex + 1; i < lines.length; i++) { const nextLine = lines[i].trim(); fullSignature += ' ' + nextLine; if (nextLine.includes(';') || nextLine.includes('{')) { break; } } } // Extract parameters and return type const paramMatch = fullSignature.match(/\(([^)]*)\)/); const returnTypeMatch = fullSignature.match(/\)\s*:\s*([^=]+)\s*=>/); let parameters = []; if (paramMatch && paramMatch[1]) { // Use the robust parameter parser parameters = parseParameters(paramMatch[1]); } const returnType = returnTypeMatch ? returnTypeMatch[1].trim() : undefined; // Extract just the signature part up to the arrow or opening brace const signatureMatch = fullSignature.match(/(export\s+(?:const|let|var)\s+\w+\s*=\s*(?:async\s+)?(?:<[^>]+>)?\s*\([^)]*\)(?:\s*:\s*[^=]+)?)\s*=>/); if (signatureMatch) { return { name, kind: 'function', signature: signatureMatch[1].replace(/\s+/g, ' ').trim() + ' => ...', parameters, returnType }; } } return null; }