Skip to content

fix: Infer parameters for classes from constructors #753

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

Merged
merged 2 commits into from
Apr 24, 2017
Merged
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
39 changes: 39 additions & 0 deletions docs/RECIPES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
## Classes

ES6 has a nice, formal way of declaring classes. documentation.js handles it well:
here are tips for dealing with them.

**Document constructor parameters with the class, not the constructor method.**

Do:

```js
/**
* A table object
* @param {number} width
* @param {number} height
*/
class Table {
constructor(width, height) {
this.width = width;
this.height = height;
}
}
```

Don't:

```js
/** A table object */
class Table {
/*
* @param {number} width
* @param {number} height
*/
constructor(width, height) {
this.width = width;
this.height = height;
}
}
```

## Destructuring Parameters

In ES6, you can use [destructuring assignment in functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment):
Expand Down
6 changes: 5 additions & 1 deletion lib/extractors/exported.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
path.skip();
},
Method(path) {
addComments(data, path);
// Don't explicitly document constructor methods: their
// parameters are output as part of the class itself.
if (path.node.kind !== 'constructor') {
addComments(data, path);
}
path.skip();
}
});
Expand Down
20 changes: 17 additions & 3 deletions lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _ = require('lodash');
const findTarget = require('./finders').findTarget;
const flowDoctrine = require('../flow_doctrine');
const util = require('util');
const debuglog = util.debuglog('infer');
const debuglog = util.debuglog('documentation');

/**
* Infers param tags by reading function parameter names
Expand All @@ -24,13 +24,27 @@ function inferParams(comment /*: Comment */) {
path = path.get('init');
}

// If this is an ES6 class with a constructor method, infer
// parameters from that constructor method.
if (t.isClassDeclaration(path)) {
let constructor = path.node.body.body.find(item => {
// https://github.com/babel/babylon/blob/master/ast/spec.md#classbody
return t.isClassMethod(item) && item.kind === 'constructor';
});
if (constructor) {
return inferAndCombineParams(constructor.params, comment);
}
}

if (!t.isFunction(path)) {
return comment;
}

var inferredParams = path.node.params.map((param, i) =>
paramToDoc(param, '', i));
return inferAndCombineParams(path.node.params, comment);
}

function inferAndCombineParams(params, comment) {
var inferredParams = params.map((param, i) => paramToDoc(param, '', i));
var mergedParams = mergeTrees(inferredParams, comment.params);

// Then merge the trees. This is the hard part.
Expand Down
10 changes: 10 additions & 0 deletions lib/parsers/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
/* @flow */

var _ = require('lodash'),
t = require('babel-types'),
parse = require('../../lib/parse'),
walkComments = require('../extractors/comments'),
walkExported = require('../extractors/exported'),
util = require('util'),
debuglog = util.debuglog('documentation'),
parseToAst = require('./parse_to_ast');

/**
Expand Down Expand Up @@ -89,6 +92,13 @@ function _addComment(
value: path
});

// #689
if (t.isClassMethod(path) && path.node.kind === 'constructor') {
debuglog(
'A constructor was documented explicitly: document along with the class instead'
);
}

if (path.parentPath && path.parentPath.node) {
var parentNode = path.parentPath.node;
context.code = data.source.substring(parentNode.start, parentNode.end);
Expand Down
1 change: 1 addition & 0 deletions test/fixture/document-exported.input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Options: {"documentExported": true}

export class Class {
constructor(a: string) {}
classMethod() {}
get classGetter() {}
set classSetter(v) {}
Expand Down
Loading