JSDoc

ESLint utility to find a JSDoc comment associated with a given AST node.

Usage

var findJSDoc = require( '@stdlib/_tools/eslint/utils/find-jsdoc' );

findJSDoc( source, node )

Retrieves a JSDoc comment associated with a given AST node, provided a source code tree.

Examples

var Linter = require( 'eslint' ).Linter;
var findJSDoc = require( '@stdlib/_tools/eslint/utils/find-jsdoc' );

var linter = new Linter();
var code;

function rule( context ) {
    var source = context.getSourceCode();

    return {
        'FunctionExpression:exit': log,
        'FunctionDeclaration:exit': log,
        'VariableDeclaration:exit': log,
        'ExpressionStatement:exit': log
    };

    function log( node ) {
        var jsdoc = findJSDoc( source, node );
        if ( jsdoc ) {
            console.log( jsdoc );
        }
    }
}

code = [
    '/**',
    '* Squares a number.',
    '* ',
    '* @params {number} x - input number',
    '* @returns {number} x squared',
    '*',
    '* @examples',
    '* var y = square( 2.0 );',
    '* // returns 4.0',
    '*/',
    'function square( x ) {',
    '  return x*x;',
    '}'
].join( '\n' );

linter.defineRule( 'my-rule', rule );

linter.verify( code, {
    'rules': {
        'my-rule': 'error'
    }
});