Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from documentationjs/infer-params
Infer parameters. Fixes #51
- Loading branch information
Showing
21 changed files
with
780 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
var types = require('ast-types'); | ||
|
||
/** | ||
* Infers param tags by reading function parameter names | ||
* | ||
* @name inferParams | ||
* @param {Object} comment parsed comment | ||
* @returns {Object} comment with parameters | ||
*/ | ||
module.exports = function inferParams(comment) { | ||
|
||
types.visit(comment.context.ast, { | ||
visitFunction: function (path) { | ||
|
||
// Ensure that explicitly specified parameters are not overridden | ||
// by inferred parameters | ||
var existingParams = (comment.params || []).reduce(function (memo, param) { | ||
memo[param.name] = param; | ||
return memo; | ||
}, {}); | ||
|
||
var paramOrder = []; | ||
|
||
path.value.params.forEach(function (param) { | ||
if (existingParams[param.name] === undefined) { | ||
if (!comment.params) { | ||
comment.params = []; | ||
} | ||
comment.params.push({ | ||
title: 'param', | ||
name: param.name, | ||
lineNumber: param.loc.start.line | ||
}); | ||
} | ||
paramOrder.push(param.name); | ||
}); | ||
|
||
// Ensure that if params are specified partially or in | ||
// the wrong order, they'll be output in the order | ||
// they actually appear in code | ||
if (comment.params) { | ||
comment.params.sort(function (a, b) { | ||
return paramOrder.indexOf(a.name) - paramOrder.indexOf(b.name); | ||
}); | ||
} | ||
|
||
this.abort(); | ||
} | ||
}); | ||
|
||
return comment; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,14 @@ | |
This function returns the number one. | ||
|
||
|
||
**Parameters** | ||
|
||
- `a` | ||
|
||
- `b` | ||
|
||
|
||
|
||
Returns **Number** numberone | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,14 @@ | |
This function returns the number one. | ||
|
||
|
||
**Parameters** | ||
|
||
- `a` | ||
|
||
- `b` | ||
|
||
|
||
|
||
Returns **Number** numberone | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,21 @@ Returns **area** chart | |
|
||
|
||
|
||
**Parameters** | ||
|
||
- `selection` | ||
|
||
|
||
|
||
|
||
# data | ||
|
||
Sets the chart data. | ||
|
||
|
||
**Parameters** | ||
|
||
- `_` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,21 @@ Returns **area** chart | |
|
||
|
||
|
||
**Parameters** | ||
|
||
- `selection` | ||
|
||
|
||
|
||
|
||
# data | ||
|
||
Sets the chart data. | ||
|
||
|
||
**Parameters** | ||
|
||
- `_` | ||
|
||
|
||
|
Oops, something went wrong.