Skip to content

Commit 341ab4a

Browse files
committed
Use module pattern for infers
Some inferencing will require state.
1 parent 452ffc0 commit 341ab4a

File tree

9 files changed

+231
-224
lines changed

9 files changed

+231
-224
lines changed

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ var sort = require('./lib/sort'),
1010
polyglot = require('./lib/parsers/polyglot'),
1111
github = require('./lib/github'),
1212
hierarchy = require('./lib/hierarchy'),
13-
inferName = require('./lib/infer/name'),
14-
inferKind = require('./lib/infer/kind'),
15-
inferParams = require('./lib/infer/params'),
16-
inferMembership = require('./lib/infer/membership'),
17-
inferReturn = require('./lib/infer/return'),
1813
lint = require('./lib/lint');
1914

2015
/**
@@ -61,6 +56,12 @@ module.exports = function (indexes, options, callback) {
6156
return memo.concat(parseFn(file));
6257
}, [])
6358
.map(function (comment) {
59+
var inferName = require('./lib/infer/name')();
60+
var inferKind = require('./lib/infer/kind')();
61+
var inferParams = require('./lib/infer/params')();
62+
var inferMembership = require('./lib/infer/membership')();
63+
var inferReturn = require('./lib/infer/return')();
64+
6465
// compose nesting & membership to avoid intermediate arrays
6566
comment = nestParams(
6667
inferMembership(

lib/infer/kind.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,42 @@ var kindShorthands = ['class', 'constant', 'event', 'external', 'file',
1212
* @param {Object} comment parsed comment
1313
* @returns {Object} comment with kind inferred
1414
*/
15-
module.exports = function inferKind(comment) {
16-
if (comment.kind) {
17-
return comment;
18-
}
19-
20-
for (var i = 0; i < kindShorthands.length; i++) {
21-
var kind = kindShorthands[i];
22-
if (kind in comment) {
23-
comment.kind = kind;
24-
// only allow a comment to have one kind
15+
module.exports = function () {
16+
return function inferKind(comment) {
17+
if (comment.kind) {
2518
return comment;
2619
}
27-
}
2820

29-
types.visit(comment.context.ast, {
30-
visitFunction: function (path) {
31-
if (path.value && path.value.id && path.value.id.name && !!/^[A-Z]/.exec(path.value.id.name)) {
32-
comment.kind = 'class';
33-
this.abort();
34-
} else {
35-
comment.kind = 'function';
36-
this.abort();
21+
for (var i = 0; i < kindShorthands.length; i++) {
22+
var kind = kindShorthands[i];
23+
if (kind in comment) {
24+
comment.kind = kind;
25+
// only allow a comment to have one kind
26+
return comment;
3727
}
38-
},
28+
}
3929

40-
visitVariableDeclaration: function (path) {
41-
if (path.value.kind === 'const') {
42-
comment.kind = 'constant';
43-
this.abort();
44-
} else {
45-
this.traverse(path);
30+
types.visit(comment.context.ast, {
31+
visitFunction: function (path) {
32+
if (path.value && path.value.id && path.value.id.name && !!/^[A-Z]/.exec(path.value.id.name)) {
33+
comment.kind = 'class';
34+
this.abort();
35+
} else {
36+
comment.kind = 'function';
37+
this.abort();
38+
}
39+
},
40+
41+
visitVariableDeclaration: function (path) {
42+
if (path.value.kind === 'const') {
43+
comment.kind = 'constant';
44+
this.abort();
45+
} else {
46+
this.traverse(path);
47+
}
4648
}
47-
}
48-
});
49+
});
4950

50-
return comment;
51+
return comment;
52+
};
5153
};

lib/infer/membership.js

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -83,82 +83,82 @@ function inferMembershipFromIdentifiers(comment, identifiers) {
8383
* @param {Object} comment parsed comment
8484
* @returns {Object} comment with membership inferred
8585
*/
86-
function inferMembership(comment) {
87-
if (comment.memberof) {
88-
return comment;
89-
}
90-
91-
if (comment.lends) {
92-
return comment;
93-
}
86+
module.exports = function () {
87+
return function inferMembership(comment) {
88+
if (comment.memberof) {
89+
return comment;
90+
}
9491

95-
var path = comment.context.ast;
96-
var identifiers;
97-
98-
/*
99-
* Deal with an oddity of espree: the jsdoc comment is attached to a different
100-
* node in the two expressions `a.b = c` vs `a.b = function () {}`.
101-
*/
102-
if (n.ExpressionStatement.check(path.node) &&
103-
n.AssignmentExpression.check(path.node.expression) &&
104-
n.MemberExpression.check(path.node.expression.left)) {
105-
path = path.get('expression').get('left');
106-
}
92+
if (comment.lends) {
93+
return comment;
94+
}
10795

108-
/*
109-
* Same as above but for `b: c` vs `b: function () {}`.
110-
*/
111-
if (n.Property.check(path.node) &&
112-
n.Identifier.check(path.node.key)) {
113-
path = path.get('key');
114-
}
96+
var path = comment.context.ast;
97+
var identifiers;
98+
99+
/*
100+
* Deal with an oddity of espree: the jsdoc comment is attached to a different
101+
* node in the two expressions `a.b = c` vs `a.b = function () {}`.
102+
*/
103+
if (n.ExpressionStatement.check(path.node) &&
104+
n.AssignmentExpression.check(path.node.expression) &&
105+
n.MemberExpression.check(path.node.expression.left)) {
106+
path = path.get('expression').get('left');
107+
}
115108

116-
// Foo.bar = ...;
117-
// Foo.prototype.bar = ...;
118-
// Foo.bar.baz = ...;
119-
if (n.MemberExpression.check(path.node)) {
120-
identifiers = extractIdentifiers(path);
121-
if (identifiers.length >= 2) {
122-
inferMembershipFromIdentifiers(comment, identifiers.slice(0, -1));
109+
/*
110+
* Same as above but for `b: c` vs `b: function () {}`.
111+
*/
112+
if (n.Property.check(path.node) &&
113+
n.Identifier.check(path.node.key)) {
114+
path = path.get('key');
123115
}
124-
}
125116

126-
// /** @lends Foo */{ bar: ... }
127-
if (n.Identifier.check(path.node) &&
128-
n.Property.check(path.parent.node) &&
129-
n.ObjectExpression.check(path.parent.parent.node)) {
130-
// The @lends comment is sometimes attached to the first property rather than
131-
// the object expression itself.
132-
identifiers = findLendsIdentifiers(path.parent.parent.node) ||
133-
findLendsIdentifiers(path.parent.parent.node.properties[0]);
134-
if (identifiers) {
135-
inferMembershipFromIdentifiers(comment, identifiers);
117+
// Foo.bar = ...;
118+
// Foo.prototype.bar = ...;
119+
// Foo.bar.baz = ...;
120+
if (n.MemberExpression.check(path.node)) {
121+
identifiers = extractIdentifiers(path);
122+
if (identifiers.length >= 2) {
123+
inferMembershipFromIdentifiers(comment, identifiers.slice(0, -1));
124+
}
136125
}
137-
}
138126

139-
// Foo = { bar: ... };
140-
// Foo.prototype = { bar: ... };
141-
// Foo.bar = { baz: ... };
142-
if (n.Identifier.check(path.node) &&
127+
// /** @lends Foo */{ bar: ... }
128+
if (n.Identifier.check(path.node) &&
143129
n.Property.check(path.parent.node) &&
144-
n.ObjectExpression.check(path.parent.parent.node) &&
145-
n.AssignmentExpression.check(path.parent.parent.parent.node)) {
146-
identifiers = extractIdentifiers(path.parent.parent.parent);
147-
if (identifiers.length >= 1) {
148-
inferMembershipFromIdentifiers(comment, identifiers);
130+
n.ObjectExpression.check(path.parent.parent.node)) {
131+
// The @lends comment is sometimes attached to the first property rather than
132+
// the object expression itself.
133+
identifiers = findLendsIdentifiers(path.parent.parent.node) ||
134+
findLendsIdentifiers(path.parent.parent.node.properties[0]);
135+
if (identifiers) {
136+
inferMembershipFromIdentifiers(comment, identifiers);
137+
}
149138
}
150-
}
151139

152-
// var Foo = { bar: ... }
153-
if (n.Identifier.check(path.node) &&
154-
n.Property.check(path.parent.node) &&
155-
n.ObjectExpression.check(path.parent.parent.node) &&
156-
n.VariableDeclarator.check(path.parent.parent.parent.node)) {
157-
identifiers = [path.parent.parent.parent.node.id.name];
158-
inferMembershipFromIdentifiers(comment, identifiers);
159-
}
140+
// Foo = { bar: ... };
141+
// Foo.prototype = { bar: ... };
142+
// Foo.bar = { baz: ... };
143+
if (n.Identifier.check(path.node) &&
144+
n.Property.check(path.parent.node) &&
145+
n.ObjectExpression.check(path.parent.parent.node) &&
146+
n.AssignmentExpression.check(path.parent.parent.parent.node)) {
147+
identifiers = extractIdentifiers(path.parent.parent.parent);
148+
if (identifiers.length >= 1) {
149+
inferMembershipFromIdentifiers(comment, identifiers);
150+
}
151+
}
160152

161-
return comment;
162-
}
153+
// var Foo = { bar: ... }
154+
if (n.Identifier.check(path.node) &&
155+
n.Property.check(path.parent.node) &&
156+
n.ObjectExpression.check(path.parent.parent.node) &&
157+
n.VariableDeclarator.check(path.parent.parent.parent.node)) {
158+
identifiers = [path.parent.parent.parent.node.id.name];
159+
inferMembershipFromIdentifiers(comment, identifiers);
160+
}
163161

164-
module.exports = inferMembership;
162+
return comment;
163+
};
164+
}

lib/infer/name.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,58 @@ var types = require('ast-types');
1010
* @param {Object} comment parsed comment
1111
* @returns {Object} comment with name inferred
1212
*/
13-
module.exports = function inferName(comment) {
14-
// If this comment is already explicitly named, simply pass it
15-
// through without doing any inference.
16-
if (comment.name) {
17-
return comment;
18-
}
13+
module.exports = function () {
14+
return function inferName(comment) {
15+
// If this comment is already explicitly named, simply pass it
16+
// through without doing any inference.
17+
if (comment.name) {
18+
return comment;
19+
}
1920

20-
if (comment.event) {
21-
comment.name = comment.event;
22-
return comment;
23-
}
21+
if (comment.event) {
22+
comment.name = comment.event;
23+
return comment;
24+
}
2425

25-
if (comment.callback) {
26-
comment.name = comment.callback;
27-
return comment;
28-
}
26+
if (comment.callback) {
27+
comment.name = comment.callback;
28+
return comment;
29+
}
2930

30-
if (comment.class && comment.class.name) {
31-
comment.name = comment.class.name;
32-
return comment;
33-
}
31+
if (comment.class && comment.class.name) {
32+
comment.name = comment.class.name;
33+
return comment;
34+
}
3435

35-
if (comment.typedef) {
36-
comment.name = comment.typedef.name;
37-
return comment;
38-
}
36+
if (comment.typedef) {
37+
comment.name = comment.typedef.name;
38+
return comment;
39+
}
3940

40-
// The strategy here is to do a depth-first traversal of the AST,
41-
// looking for nodes with a "name" property, with exceptions as needed.
42-
// For example, name inference for a MemberExpression `foo.bar = baz` will
43-
// infer the named based on the `property` of the MemberExpression (`bar`)
44-
// rather than the `object` (`foo`).
45-
types.visit(comment.context.ast, {
46-
inferName: function (path, value) {
47-
if (value && value.name) {
48-
comment.name = value.name;
49-
this.abort();
50-
} else {
51-
this.traverse(path);
52-
}
53-
},
41+
// The strategy here is to do a depth-first traversal of the AST,
42+
// looking for nodes with a "name" property, with exceptions as needed.
43+
// For example, name inference for a MemberExpression `foo.bar = baz` will
44+
// infer the named based on the `property` of the MemberExpression (`bar`)
45+
// rather than the `object` (`foo`).
46+
types.visit(comment.context.ast, {
47+
inferName: function (path, value) {
48+
if (value && value.name) {
49+
comment.name = value.name;
50+
this.abort();
51+
} else {
52+
this.traverse(path);
53+
}
54+
},
5455

55-
visitNode: function (path) {
56-
this.inferName(path, path.value);
57-
},
56+
visitNode: function (path) {
57+
this.inferName(path, path.value);
58+
},
5859

59-
visitMemberExpression: function (path) {
60-
this.inferName(path, path.value.property);
61-
}
62-
});
60+
visitMemberExpression: function (path) {
61+
this.inferName(path, path.value.property);
62+
}
63+
});
6364

64-
return comment;
65+
return comment;
66+
};
6567
};

0 commit comments

Comments
 (0)