Skip to content

[WIP] feat(params): Support inline parameter documentation #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/extractors/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function walkComments(
);
}

(path.node[type] || []).filter(isJSDocComment).forEach(parseComment);
if (path.node[type]) {
path.node[type].filter(isJSDocComment).forEach(parseComment);
}
}
});

Expand Down
26 changes: 22 additions & 4 deletions lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ const findTarget = require('./finders').findTarget;
const flowDoctrine = require('../flow_doctrine');
const util = require('util');
const debuglog = util.debuglog('documentation');
const parseJSDoc = require('../parse');
const isJSDocComment = require('../is_jsdoc_comment');

function getInlineDescription(param) {
if (param.trailingComments && isJSDocComment(param.trailingComments[0])) {
var parsedInlineComment = parseJSDoc(param.trailingComments[0].value);
if (parsedInlineComment && parsedInlineComment.description) {
return {
description: parsedInlineComment.description
};
}
}
return {};
}

/**
* Infers param tags by reading function parameter names
Expand Down Expand Up @@ -182,9 +196,13 @@ function paramToDoc(
return paramToDoc(indexedElement, prefix);
});
case 'ObjectProperty':
return _.assign(paramToDoc(param.value, prefix + '.' + param.key.name), {
name: prefix + '.' + param.key.name
});
return _.assign(
paramToDoc(param.value, prefix + '.' + param.key.name),
{
name: prefix + '.' + param.key.name
},
getInlineDescription(param)
);
case 'RestProperty': // (a, ...b)
case 'RestElement':
let type /*: DoctrineType */ = {
Expand Down Expand Up @@ -213,7 +231,7 @@ function paramToDoc(
newParam.type = flowDoctrine(param.typeAnnotation.typeAnnotation);
}

return newParam;
return _.assign(newParam, getInlineDescription(param));
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/fixture/params.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ function fishesAndFoxes({ fishes, foxes }) {
return fishes + foxes;
}

/**
* This method has inline documentation for a param
* @param {Object} options
*/
function fishesAndFoxesInline(
{ fishes /** number of kinds of fish */, foxes }
) {
return fishes + foxes;
}

/**
* This method has a type in the description and a default in the code
* @param {number} x
Expand Down
Loading