JSDoc Parser
Doctrine is a specialized JavaScript parser focused solely on extracting and interpreting JSDoc comments from string inputs, rather than processing entire JavaScript files. Maintained by the ESLint team, its current stable version is 3.0.0. Releases appear irregularly, often aligned with maintenance or feature updates related to its integration within the broader ESLint ecosystem. Its primary differentiator is its granular approach to JSDoc parsing, offering fine-grained control over extraction options like unwrapping comment wrappers, filtering by specific tags, and handling parsing errors. It's designed for scenarios where only the documentation blocks need analysis, making it a foundational tool for linters, documentation generators, and code analysis tools that rely on JSDoc annotations.
Common errors
-
Error: Expected JSDoc comment string, received non-comment input or full file.
cause Attempting to pass an entire JavaScript file or an invalid string that is not a JSDoc comment to `doctrine.parse()`.fixEnsure you extract only the raw JSDoc comment block (e.g., `/** ... */`) as a string before invoking `doctrine.parse()`. -
JSDoc parsing error: `@param {string} [foo]` syntax errorcause Using optional parameter syntax with square brackets (e.g., `@param {string} [name]`) without enabling the `sloppy` parsing option.fixPass `{ sloppy: true }` in the options object to `doctrine.parse()` to allow this syntax.
Warnings
- breaking Version 3.0.0 dropped support for Node.js versions older than 6.0.0. Ensure your Node.js environment meets this minimum requirement.
- breaking Version 2.0.0 changed the project's license from MIT to Apache License 2.0. Review the new license terms for compatibility with your project.
- gotcha Doctrine is designed exclusively for parsing JSDoc comment strings, not entire JavaScript files. Passing a full script or arbitrary code will lead to parsing errors or incorrect AST output.
- gotcha By default, `doctrine.parse()` does not tolerate optional parameters defined with square brackets (e.g., `@param {string} [name]`). Parsing such comments requires enabling 'sloppy' mode.
- gotcha Without the `recoverable` option, `doctrine.parse()` will stop and potentially throw an error upon encountering syntax errors within the JSDoc comment. Errors will not be reported in the AST.
Install
-
npm install doctrine -
yarn add doctrine -
pnpm add doctrine
Imports
- doctrine
import doctrine from 'doctrine';
const doctrine = require('doctrine'); - parse
import { parse } from 'doctrine';const { parse } = require('doctrine'); - Tag, Type (TypeScript types)
import type { Tag, Type } from 'doctrine';import type { Tag, Type } from '@types/doctrine';
Quickstart
const doctrine = require('doctrine');
// Example 1: Basic JSDoc parsing with multiple options
const comment1 = [
'/**',
' * This is a test function.',
' * @param {string} name - The user\'s name.',
' * @param {number} [age=30] - The user\'s age (optional).',
' * @returns {boolean} True if successful, false otherwise.',
' * @deprecated Use `newName` instead.',
' */'
].join('\n');
const ast1 = doctrine.parse(comment1, { unwrap: true, recoverable: true, sloppy: true, lineNumbers: true });
console.log('AST 1 (parsed with full options):', JSON.stringify(ast1, null, 2));
// Example 2: Parsing with specific tags only
const comment2 = [
'/**',
' * Another function.',
' * @param {Object} config - Configuration object.',
' * @property {string} config.url - The URL to use.',
' * @todo Implement error handling.',
' */'
].join('\n');
const ast2 = doctrine.parse(comment2, { unwrap: true, tags: ['param', 'property'], recoverable: true });
console.log('\nAST 2 (filtered tags):', JSON.stringify(ast2, null, 2));