Skip to content
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

Implement Resolves comment #80

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to
- Support adding namespace prefix to the entities names.
- Support for `BigInt64Array` and `BigUint64Array` types.
- Support ordering documentation entries.
- Implement `Resolves` comment. It can be used to specify what async function or
Promise will resolve.

### Changed

Expand Down
23 changes: 17 additions & 6 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ const getNativeArgs = str =>

const getSignatures = (name, sig) => {
const defaultArgs = sig.parameters
.filter(p => p.offset === 2 && p.name !== 'Returns:')
.filter(
p => p.offset === 2 && p.name !== 'Returns:' && p.name !== 'Resolves:'
)
.map(p => p.name);
let signatures = [defaultArgs];

Expand Down Expand Up @@ -259,6 +261,9 @@ const getSignatures = (name, sig) => {
return signatures;
};

const findComment = (comments, name) =>
comments.find(comment => comment.name === name);

const getPropsParams = comment =>
comment.parameters.reduce((props, prop) => {
if (prop.offset === CODE_PARAMS_OFFSET) {
Expand Down Expand Up @@ -323,7 +328,7 @@ const generateParameters = (parameters, opts) => {
}

let line;
if (parameter.name === 'Returns:') {
if (parameter.name === 'Returns:' || parameter.name === 'Resolves:') {
line = '- _' + parameter.name + (types ? '_ ' : '_');
} else {
line = '- `' + parameter.name + (types ? '`: ' : '`');
Expand All @@ -336,8 +341,7 @@ const generateParameters = (parameters, opts) => {
return buf;
};

const generateReturns = (comments, opts) => {
const comment = comments.find(comment => comment.name === 'Returns');
const genCommentWithParams = (comment, opts) => {
if (!comment) return [];

const typeLine = genTypeLine(comment, opts);
Expand All @@ -346,6 +350,12 @@ const generateReturns = (comments, opts) => {
return [line, '', ...res];
};

const generateReturns = (comments, opts) =>
genCommentWithParams(findComment(comments, 'Returns'), opts);

const generateResolves = (comments, opts) =>
genCommentWithParams(findComment(comments, 'Resolves'), opts);

const generateTitle = (sig, opts) => {
if (!sig.title) return [];
const title = wrapComment(sig.title, opts);
Expand Down Expand Up @@ -378,7 +388,7 @@ const generateRestComments = (comments, opts) => {

const generateStaticProperties = (method, comments, opts) => {
const buf = [];
const comment = comments.find(comm => comm.name === 'Static properties');
const comment = findComment(comments, 'Static properties');
if (!comment) return buf;

const params = getPropsParams(comment);
Expand All @@ -405,7 +415,7 @@ const generateStaticProperties = (method, comments, opts) => {

const generateProperties = (method, comments, opts) => {
const buf = [];
const comment = comments.find(comm => comm.name === 'Properties');
const comment = findComment(comments, 'Properties');
if (!comment) return buf;

const params = getPropsParams(comment);
Expand Down Expand Up @@ -436,6 +446,7 @@ const generateFunction = (name, sig, opts, methods) => {
...generateSignatures(name, sig, opts, methods),
...generateParameters(sig.parameters, opts),
...generateReturns(sig.comments, opts),
...generateResolves(sig.comments, opts),
...generateTitle(sig, opts),
...generateDescription(sig, opts),
...generateRestComments(sig.comments, opts),
Expand Down
3 changes: 2 additions & 1 deletion lib/introspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ALL_TYPES = new Set(types.ALL_TYPES);

const NAMED_LINES = [
'Returns:', // returned value
'Resolves:', // resolved value
'Properties:', // class instance/function properties
'Static properties:', // class instance/function properties
'Example:', // example of usage
Expand Down Expand Up @@ -196,7 +197,7 @@ const parseRest = lines => {
let nonStandardTypes = [];
let parameters = null;

if (name === 'Returns') {
if (name === 'Returns' || name === 'Resolves') {
[types, nonStandardTypes, comment] = parseTypes(comment.trimLeft());
const parameterLines = [comment, ...commentLines];
const start = parameterLines.findIndex(
Expand Down
10 changes: 9 additions & 1 deletion test/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ const asyncLambda = async () => {};

// Async functions are supported
// Returns: <Promise>
const asyncFunc = async function() {};
// Resolves: <Object>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid adding keywords for such cases. I think it'll be better to allow to customize types:

// Returns: <Promise<{ a, b }>>
//   a <number>
//   b <string>

This would also allow to specify Map, Set types etc. (i.e. Map<string, number>, Set<string>).

// a <number>
// b <string>
const asyncFunc = async function() {
return {
a: 3,
b: 'abc',
};
};

const asyncFunc2 = async function(a) {
console.log(a);
Expand Down
5 changes: 5 additions & 0 deletions test/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ Function with no arguments

_Returns:_ [`<Promise>`][promise]

_Resolves:_ [`<Object>`][object]

- `a`: [`<number>`][number]
- `b`: [`<string>`][string]

Async functions are supported

## async lambdas.asyncNestedFunctions.asyncFunc2(a)
Expand Down