Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 061d332

Browse files
committed
Simplify name inference
1 parent 6fdbe97 commit 061d332

14 files changed

+105
-237
lines changed

streams/infer_name.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,42 @@
33
var through = require('through'),
44
types = require('ast-types');
55

6+
/**
7+
* Create a transform stream that attempts to infer a `name` tag from the context.
8+
*
9+
* @name inferName
10+
* @return {stream.Transform}
11+
*/
612
module.exports = function () {
713
return through(function (comment) {
814
if (comment.tags.some(function (tag) { return tag.title === 'name'; })) {
915
this.push(comment);
1016
return;
1117
}
1218

13-
/**
14-
* Infer the function's name from the context, if possible.
15-
* If `inferredName` is present and `comment` does not already
16-
* have a `name` tag, `inferredName` is tagged as the name.
17-
* @param {Object} comment the current state of the parsed JSDoc comment
18-
* @param {string} inferredName a name inferred by the nearest function
19-
* or variable in the AST
20-
* @return {boolean} `false`: to satisfy an ast-types requirement for visitor methods
21-
*/
22-
function inferName(inferredName) {
23-
if (inferredName) {
24-
comment.tags.push({
25-
title: 'name',
26-
name: inferredName
27-
});
28-
}
29-
return false;
30-
}
31-
19+
// The strategy here is to do a depth-first traversal of the AST,
20+
// looking for nodes with a "name" property, with exceptions as needed.
21+
// For example, name inference for a MemberExpression `foo.bar = baz` will
22+
// infer the named based on the `property` of the MemberExpression (`bar`)
23+
// rather than the `object` (`foo`).
3224
types.visit(comment.context.ast, {
33-
visitExpressionStatement: function (path) {
34-
return inferName(path.value.expression.left.name);
25+
inferName: function(path, value) {
26+
if (value && value.name) {
27+
comment.tags.push({
28+
title: 'name',
29+
name: value.name
30+
});
31+
return false;
32+
}
33+
this.traverse(path);
3534
},
3635

37-
visitMemberExpression: function (path) {
38-
return inferName(path.value.property.name);
36+
visitNode: function (path) {
37+
return this.inferName(path, path.value);
3938
},
4039

41-
visitIdentifier: function (path) {
42-
return inferName(path.value.name);
40+
visitMemberExpression: function (path) {
41+
return this.inferName(path, path.value.property);
4342
}
4443
});
4544

test/fixture/comment-only.input.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/fixture/comment-only.output.json

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/fixture/deduce-function-declaration-override.input.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/fixture/deduce-function-declaration-override.output.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/fixture/deduce-function-declaration.input.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/fixture/deduce-function-declaration.output.json

Lines changed: 0 additions & 33 deletions
This file was deleted.

test/fixture/expression-statement.input.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/fixture/expression-statement.output.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/fixture/member-expression.input.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)