JSDoc for Arrow Functions and Const Declarations
Generate JSDoc for arrow functions declared with const, let, or var. Handle the difference between function declarations and arrow expression documentation.
Detailed Explanation
Documenting Arrow Functions
Arrow functions are common in modern JavaScript, especially for callbacks, React components, and module exports. The JSDoc comment goes above the variable declaration, not above the arrow.
Example Signature
const formatPrice = (amount: number, currency: string = "USD"): string => {
// ...
}
Generated JSDoc
/**
* Formats a numeric amount into a localized currency string.
*
* @param {number} amount - The monetary amount to format.
* @param {string} [currency="USD"] - The ISO 4217 currency code.
* Defaults to "USD".
* @returns {string} The formatted price string (e.g., "$1,234.56").
*
* @example
* ```typescript
* formatPrice(1234.56); // "$1,234.56"
* formatPrice(1234.56, "EUR"); // "€1,234.56"
* ```
*/
const formatPrice = (amount: number, currency: string = "USD"): string => {
// ...
}
Placement Rules
For arrow functions, place the comment directly above the const/let/var keyword:
/** This is correct */
const fn = () => {};
const fn = /** This is wrong */ () => {};
Named vs Anonymous
Arrow functions assigned to variables get the variable name as their documented name. For anonymous arrow functions passed directly to a higher-order function, document the parameter in the calling function instead.
Exported Arrow Functions
/**
* Validates an email address format.
* @param {string} email - The email to validate.
* @returns {boolean} True if the email format is valid.
*/
export const isValidEmail = (email: string): boolean => { ... }
Implicit Returns
For arrow functions with implicit returns, the @returns description should be especially clear since there is no return keyword visible in the code.
Use Case
Documenting utility modules exported as arrow functions, React event handlers, array method callbacks, Express route handlers, and functional programming utilities like pipe or compose.