Skip to content

Commit 3da1ff6

Browse files
committed
Source-sorting by default (#410)
* Source-sorting by default Many changes * Uses a new version of module-deps that exposes a sort key we use to sort documentation. * Adds inference for methods in objects * Groups events into a separate comment property * Add jsdoc property. Refs #388 (comment) * Namespaces and paths * More powerful paths: paths now include name, kind, and scope * comments now include a 'namespace' property with a formatted namespace * Fix tests * Re-ignore fonts * Nix sourceKey from output * Pin dependency versions * Fix module-deps-sortable ref * Update tests for right deps
1 parent e7a8c23 commit 3da1ff6

File tree

80 files changed

+5275
-5924
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5275
-5924
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = function (indexes, options, callback) {
118118
options.github && github
119119
))
120120
.filter(Boolean)
121-
.sort(sort.bind(undefined, options.order)))));
121+
.sort(sort))));
122122
} catch (e) {
123123
callback(e);
124124
}

lib/hierarchy.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
var hasOwnProperty = Object.prototype.hasOwnProperty;
44

5+
function pick(n) {
6+
var item = {
7+
name: n.name,
8+
kind: n.kind
9+
};
10+
if (n.scope) {
11+
item.scope = n.scope;
12+
}
13+
return item;
14+
}
15+
516
/**
617
* @param {Array<Object>} comments an array of parsed comments
718
* @returns {Array<Object>} nested comments, with only root comments
@@ -101,9 +112,30 @@ module.exports = function (comments) {
101112
comment.members[scope] = node.members[scope];
102113
}
103114

104-
comment.path = path.map(function (n) {
105-
return n.name;
106-
}).concat(comment.name);
115+
if (comment.members.instance.length) {
116+
comment.members.events = comment.members.instance.filter(function (member) {
117+
return member.kind === 'event';
118+
});
119+
120+
comment.members.instance = comment.members.instance.filter(function (member) {
121+
return member.kind !== 'event';
122+
});
123+
}
124+
125+
126+
comment.path = path.map(pick).concat(pick(comment));
127+
128+
var scopeChars = {
129+
instance: '#',
130+
static: '.'
131+
};
132+
133+
comment.namespace = comment.path.reduce(function (memo, part) {
134+
if (part.kind === 'event') {
135+
return memo + '.event:' + part.name;
136+
}
137+
return memo + (scopeChars[part.scope] || '') + part.name;
138+
}, '');
107139

108140
if (hasUndefinedParent) {
109141
var memberOfTag = comment.tags.filter(function (tag) {
@@ -121,6 +153,7 @@ module.exports = function (comments) {
121153
result.push(comment);
122154
}
123155
}
156+
124157
}
125158

126159
return result;

lib/infer/kind.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ module.exports = function () {
4848
} else if (t.isExpressionStatement(path)) {
4949
// module.exports = function() {}
5050
findKind(path.node.expression.right);
51+
} else if (t.isProperty(path)) {
52+
// { foo: function() {} }
53+
findKind(path.node.value);
5154
}
5255
}
5356

lib/inline_tokenizer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ function makeTokenizer(type, regex) {
1313
}
1414

1515
return eat(match[0])({
16-
'type': type,
17-
'url': match[1],
18-
'title': null,
19-
'children': [{
20-
'type': 'text',
21-
'value': match[2] || match[1]
16+
type: type,
17+
url: match[1],
18+
title: null,
19+
jsdoc: true,
20+
children: [{
21+
type: 'text',
22+
value: match[2] || match[1]
2223
}]
2324
});
2425
};

lib/input/dependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var mdeps = require('module-deps'),
3+
var mdeps = require('module-deps-sortable'),
44
fs = require('fs'),
55
path = require('path'),
66
babelify = require('babelify'),

lib/output/json.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function (comments, opts, callback) {
1515

1616
walk(comments, function (comment) {
1717
delete comment.errors;
18+
delete comment.context.sortKey;
1819
});
1920

2021
return callback(null, JSON.stringify(comments, null, 2));

lib/parsers/javascript.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ var babylon = require('babylon'),
66
isJSDocComment = require('../../lib/is_jsdoc_comment'),
77
parse = require('../../lib/parse');
88

9+
function leftPad(str, width) {
10+
str = str.toString();
11+
while (str.length < width) {
12+
str = '0' + str;
13+
}
14+
return str;
15+
}
16+
917
/**
1018
* Receives a module-dep item,
1119
* reads the file, parses the JavaScript, and parses the JSDoc.
@@ -50,7 +58,8 @@ function parseJavaScript(data) {
5058
function parseComment(comment) {
5159
var context = {
5260
loc: extend({}, JSON.parse(JSON.stringify(path.node.loc))),
53-
file: data.file
61+
file: data.file,
62+
sortKey: data.sortKey + ' ' + leftPad(path.node.loc.start.line, 8)
5463
};
5564
// Avoid visiting the same comment twice as a leading
5665
// and trailing node

lib/parsers/polyglot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function parsePolyglot(data) {
1818
.map(function (comment) {
1919
var context = {
2020
loc: extend({}, comment.loc),
21-
file: data.file
21+
file: data.file,
22+
sortKey: data.file + ' ' + comment.loc.start.line
2223
};
2324
return parse(comment.value, comment.loc, context);
2425
});

lib/sort.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,14 @@
11
'use strict';
22

3-
/**
4-
* Given a comment, get its sorting key: this is either the comment's
5-
* name tag, or a hardcoded sorting index given by a user-provided
6-
* `order` array.
7-
*
8-
* @param {Object} comment parsed documentation object
9-
* @param {Array<string>} [order=[]] an optional list of namepaths
10-
* @returns {string} sortable key
11-
* @private
12-
*/
13-
function getSortKey(comment, order) {
14-
var key = comment.context.file + ':' + comment.context.loc.start.line || comment.name;
15-
16-
if (order && order.indexOf(key) !== -1) {
17-
return order.indexOf(key);
18-
}
19-
20-
return key;
21-
}
22-
233
/**
244
* Sort two documentation objects, given an optional order object. Returns
255
* a numeric sorting value that is compatible with stream-sort.
266
*
27-
* @param {Array<string>} order an array of namepaths that will be sorted
28-
* in the order given.
297
* @param {Object} a documentation object
308
* @param {Object} b documentation object
319
* @return {number} sorting value
3210
* @private
3311
*/
34-
module.exports = function sortDocs(order, a, b) {
35-
a = getSortKey(a, order);
36-
b = getSortKey(b, order);
37-
38-
if (typeof a === 'number' && typeof b === 'number') {
39-
return a - b;
40-
}
41-
if (typeof a === 'number') {
42-
return -1;
43-
}
44-
if (typeof b === 'number') {
45-
return 1;
46-
}
47-
48-
return a.toLowerCase().localeCompare(b.toLowerCase());
12+
module.exports = function sortDocs(a, b) {
13+
return a.context.sortKey.localeCompare(b.context.sortKey);
4914
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"debounce": "^1.0.0",
2323
"disparity": "^2.0.0",
2424
"doctrine": "^1.1.0",
25-
"documentation-theme-default": "3.0.0-beta1",
26-
"documentation-theme-utils": "^2.0.2",
25+
"documentation-theme-default": "3.0.0",
26+
"documentation-theme-utils": "^3.0.0",
2727
"events": "^1.1.0",
2828
"extend": "^3.0.0",
2929
"get-comments": "^1.0.1",
@@ -33,7 +33,7 @@
3333
"mdast-util-inject": "^1.1.0",
3434
"micromatch": "^2.1.6",
3535
"mime": "^1.3.4",
36-
"module-deps": "^4.0.2",
36+
"module-deps-sortable": "4.0.6",
3737
"parse-filepath": "^0.6.3",
3838
"remark": "^4.1.2",
3939
"remark-toc": "^3.0.0",

0 commit comments

Comments
 (0)