documentation/lib/nest.js
Tom MacWright 631c6925d4 feat(core): Switch to Promises everywhere. Adopt Node v4 ES6 (#648)
* feat(core): Switch to Promises everywhere. Adopt Node v4 ES6

Big changes:

* Uses template strings where appropriate
* Config and argument parsing is unified and there is no such thing
  as formatterOptions anymore. All user-passed options go through
  mergeConfig.
* The node API surface changed (again): `buildSync` is removed,
  building operations return Promises.
* Now using Flow for internal type annotations.

More changes:

* Remove buildSync command
* feat(inference): Partially implement object shorthand support
* Refs #649
* Use Flow annotations to  enforce types
* Keep flow but switch to comment syntax
* Clarify types
* More flow improvements
* Turn server into class
* LinkerStack becomes class too
* Fix comment description type
* Run flow on lint
* Many more flow fixes
* More intense flow refactoring
* Simplify inference steps
* Update inference tests, flow errors down to 1
* Continue refining types
* Fix more flow issues
* Use 'use strict' everywhere
* Make 'ast' property configurable
* Fix many tests
* Fix more tests
* Fix more tests
* Fix augments
* Test Markdown meta support
* Improve test coverage
* Switch back from for of to for for speed
2017-01-27 16:14:19 -05:00

73 lines
1.8 KiB
JavaScript

/* @flow */
'use strict';
/**
* Nest nestable tags, like param and property, into nested
* arrays that are suitable for output.
*
* @private
* @param {Object} comment a comment with tags
* @param {string} tagTitle the tag to nest
* @param {string} target the tag to nest into
* @returns {Object} nested comment
*/
function nestTag(
comment/*: Comment */,
tagTitle/*: string */,
target/*: string */) {
if (!comment[target]) {
return comment;
}
var result = [],
index = {};
comment[target].forEach(tag => {
var tagName = tag.name;
if (tagName) {
index[tagName] = tag;
var parts = tagName.split(/(\[\])?\./)
.filter(part => part && part !== '[]');
if (parts.length > 1) {
var parent = index[parts.slice(0, -1).join('.')];
if (parent === undefined) {
comment.errors.push({
message: '@' + tagTitle + ' ' + tag.name + '\'s parent ' + parts[0] + ' not found',
commentLineNumber: tag.lineNumber
});
result.push(tag);
return;
}
parent.properties = parent.properties || [];
parent.properties.push(tag);
} else {
result.push(tag);
}
}
});
comment[target] = result;
return comment;
}
/**
* Nests
* [parameters with properties](http://usejsdoc.org/tags-param.html#parameters-with-properties).
*
* A parameter `employee.name` will be attached to the parent parameter `employee` in
* a `properties` array.
*
* This assumes that incoming comments have been flattened.
*
* @param {Object} comment input comment
* @return {Object} nested comment
*/
function nest(comment/*: Comment*/) {
return nestTag(nestTag(comment, 'param', 'params'),
'property', 'properties');
}
module.exports = nest;