Skip to content

Commit

Permalink
refactor(nest): Better nesting implementation
Browse files Browse the repository at this point in the history
This nesting implementation uses a proper recursive tree algorithm

Fixes #554

BREAKING CHANGE: referencing inferred destructure params without
renaming them, like $0.x, from JSDoc comments will no longer
work. To reference them, instead add a param tag to name the
destructuring param, and then refer to members of that name.

Before:

```js
/**
 * @param {number} $0.x a member of x
 */
function a({ x }) {}
```

After:

```js
/**
 * @param {Object} options
 * @param {number} options.x a member of x
 */
function a({ x }) {}
```
  • Loading branch information
tmcw committed Apr 15, 2017
1 parent d8ec5da commit 7156122
Show file tree
Hide file tree
Showing 29 changed files with 1,054 additions and 517 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
machine:
node:
version: 4
version: 7
43 changes: 29 additions & 14 deletions declarations/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ declare type CommentContextGitHub = {
url: string
};

declare type CommentTag = {
declare type CommentTagBase = {
title: string
};

declare type CommentTag = CommentTagBase & {
name?: string,
title: string,
description?: Object,
default?: any,
lineNumber?: number,
type?: DoctrineType,
properties?: Array<CommentTag>
};

declare type CommentTagNamed = CommentTag & {
name?: string,
title: string,
description?: Object,
Expand Down Expand Up @@ -88,18 +102,19 @@ declare type Remark = {

declare type Access = 'private' | 'public' | 'protected';
declare type Scope = 'instance' | 'static' | 'inner' | 'global';
declare type Kind = 'class' |
'constant' |
'event' |
'external' |
'file' |
'function' |
'member' |
'mixin' |
'module' |
'namespace' |
'typedef' |
'interface';
declare type Kind =
| 'class'
| 'constant'
| 'event'
| 'external'
| 'file'
| 'function'
| 'member'
| 'mixin'
| 'module'
| 'namespace'
| 'typedef'
| 'interface';

declare type Comment = {
errors: Array<CommentError>,
Expand Down Expand Up @@ -152,4 +167,4 @@ declare type ReducedComment = {
name: string,
kind: ?Kind,
scope?: ?Scope
}
};
4 changes: 2 additions & 2 deletions default_theme/section._
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
= <code><%- param.default %></code><% } %>)</code>
<%= md(param.description, true) %>
</div>
<% if (param.properties) { %>
<% if (param.properties && param.properties.length) { %>
<table class='mt1 mb2 fixed-table h5 col-12'>
<colgroup>
<col width='30%' />
Expand Down Expand Up @@ -93,7 +93,7 @@
<% } %><% if (property.description) {
%>: <%= md(property.description, true) %><%
} %>
<% if (property.properties) { %>
<% if (property.properties && property.properties.length) { %>
<ul>
<% property.properties.forEach(function(property) { %>
<li><code><%- property.name %></code> <%= formatType(property.type) %>
Expand Down
43 changes: 21 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ function pipeline() {
};
}



function configure(indexes, args)/*: Promise<InputsConfig> */ {
function configure(indexes, args) /*: Promise<InputsConfig> */ {
let mergedConfig = mergeConfig(args);

return mergedConfig.then(config => {

let expandedInputs = expandInputs(indexes, config);

return expandedInputs.then(inputs => {
Expand All @@ -71,8 +68,10 @@ function configure(indexes, args)/*: Promise<InputsConfig> */ {
* @param {Object} config options
* @returns {Promise<Array<string>>} promise with results
*/
function expandInputs(indexes/*: string|Array<string> */,
config /*: DocumentationConfig */) {
function expandInputs(
indexes /*: string|Array<string> */,
config /*: DocumentationConfig */
) {
// Ensure that indexes is an array of strings
indexes = [].concat(indexes);

Expand All @@ -91,40 +90,42 @@ function buildInternal(inputsAndConfig) {
config.access = ['public', 'undefined', 'protected'];
}

var parseFn = (config.polyglot) ? polyglot : parseJavaScript;
var parseFn = config.polyglot ? polyglot : parseJavaScript;

var buildPipeline = pipeline(
inferName,
inferAccess(config.inferPrivate),
inferAugments,
inferKind,
nest,
inferParams,
inferProperties,
inferReturn,
inferMembership(),
inferType,
nest,
config.github && github,
garbageCollect);
garbageCollect
);

let extractedComments = _.flatMap(inputs, function (sourceFile) {
let extractedComments = _.flatMap(inputs, function(sourceFile) {
if (!sourceFile.source) {
sourceFile.source = fs.readFileSync(sourceFile.file, 'utf8');
}

return parseFn(sourceFile, config).map(buildPipeline);
}).filter(Boolean);

return filterAccess(config.access,
hierarchy(
sort(extractedComments, config)));
return filterAccess(
config.access,
hierarchy(sort(extractedComments, config))
);
}

function lintInternal(inputsAndConfig) {
let inputs = inputsAndConfig.inputs;
let config = inputsAndConfig.config;

let parseFn = (config.polyglot) ? polyglot : parseJavaScript;
let parseFn = config.polyglot ? polyglot : parseJavaScript;

let lintPipeline = pipeline(
lintComments,
Expand All @@ -137,7 +138,8 @@ function lintInternal(inputsAndConfig) {
inferReturn,
inferMembership(),
inferType,
nest);
nest
);

let extractedComments = _.flatMap(inputs, sourceFile => {
if (!sourceFile.source) {
Expand Down Expand Up @@ -183,8 +185,7 @@ function lintInternal(inputsAndConfig) {
* }
* });
*/
let lint = (indexes, args) => configure(indexes, args)
.then(lintInternal);
let lint = (indexes, args) => configure(indexes, args).then(lintInternal);

/**
* Generate JavaScript documentation as a list of parsed JSDoc
Expand Down Expand Up @@ -227,8 +228,7 @@ let lint = (indexes, args) => configure(indexes, args)
* // any other kind of code data.
* });
*/
let build = (indexes, args) => configure(indexes, args)
.then(buildInternal);
let build = (indexes, args) => configure(indexes, args).then(buildInternal);

/**
* Documentation's formats are modular methods that take comments
Expand All @@ -240,9 +240,8 @@ let build = (indexes, args) => configure(indexes, args)
var formats = {
html: require('./lib/output/html'),
md: require('./lib/output/markdown'),
remark: (comments/*: Array<Comment> */, config/*: DocumentationConfig */) =>
markdownAST(comments, config)
.then(res => JSON.stringify(res, null, 2)),
remark: (comments /*: Array<Comment> */, config /*: DocumentationConfig */) =>
markdownAST(comments, config).then(res => JSON.stringify(res, null, 2)),
json: require('./lib/output/json')
};

Expand Down
Loading

0 comments on commit 7156122

Please sign in to comment.