Skip to content

Commit 6ab7ef5

Browse files
committed
Pick up name and parameters from exported functions
This makes sure that we get the name and params from exported functions/classes/vars. For export default the name is inferred from the file name. Towards #424
1 parent 907a7a8 commit 6ab7ef5

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

lib/infer/finders.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ function findTarget(path) {
1010
path = path.node;
1111
}
1212

13+
if (t.isExportNamedDeclaration(path) || t.isExportDefaultDeclaration(path)) {
14+
path = path.declaration;
15+
}
16+
1317
// var x = TARGET;
1418
if (t.isVariableDeclaration(path)) {
1519
return path.declarations[0].init;

lib/infer/name.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ module.exports = function () {
3333
}
3434
}
3535

36-
// The strategy here is to do a depth-first traversal of the AST,
37-
// looking for nodes with a "name" property, with exceptions as needed.
38-
// For example, name inference for a MemberExpression `foo.bar = baz` will
39-
// infer the named based on the `property` of the MemberExpression (`bar`)
40-
// rather than the `object` (`foo`).
4136
if (comment.context.ast) {
37+
if (comment.context.ast.type === 'ExportDefaultDeclaration') {
38+
comment.name = pathParse(comment.context.file).name;
39+
return comment;
40+
}
41+
42+
// The strategy here is to do a depth-first traversal of the AST,
43+
// looking for nodes with a "name" property, with exceptions as needed.
44+
// For example, name inference for a MemberExpression `foo.bar = baz` will
45+
// infer the named based on the `property` of the MemberExpression (`bar`)
46+
// rather than the `object` (`foo`).
4247
comment.context.ast.traverse({
4348
Identifier: function (path) {
4449
if (inferName(path, path.node)) {

test/lib/infer/name.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,25 @@ test('inferName', function (t) {
123123
/** @module */
124124
}, '/path/inferred-from-file.js').name, 'inferred-from-file');
125125

126+
t.equal(evaluate(`
127+
/** Test */
128+
export function exported() {}
129+
`).name, 'exported');
130+
131+
t.equal(evaluate(`
132+
/** Test */
133+
export default function exported() {}
134+
`, '/path/inferred-from-file.js').name, 'inferred-from-file');
135+
136+
t.equal(evaluate(`
137+
/** Test */
138+
export var life = 42;
139+
`).name, 'life');
140+
141+
t.equal(evaluate(`
142+
/** Test */
143+
export class Wizard {}
144+
`).name, 'Wizard');
145+
126146
t.end();
127147
});

test/lib/infer/params.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var test = require('tap').test,
4+
parse = require('../../../lib/parsers/javascript'),
5+
inferParams = require('../../../lib/infer/params')();
6+
7+
function toComment(fn, file) {
8+
return parse({
9+
file: file,
10+
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
11+
})[0];
12+
}
13+
14+
function evaluate(fn, file) {
15+
return inferParams(toComment(fn, file));
16+
}
17+
18+
test('inferParams', function (t) {
19+
t.deepEqual(evaluate(function () {
20+
/** Test */
21+
function f(x) {}
22+
}).params, [{lineNumber: 3, name: 'x', title: 'param'}]);
23+
24+
t.deepEqual(evaluate(`
25+
/** Test */
26+
export function f(x) {}
27+
`).params, [{lineNumber: 3, name: 'x', title: 'param'}]);
28+
29+
t.deepEqual(evaluate(`
30+
/** Test */
31+
export default function f(x) {}
32+
`).params, [{lineNumber: 3, name: 'x', title: 'param'}]);
33+
34+
t.end();
35+
});

0 commit comments

Comments
 (0)