Skip to content

Pick up name and parameters from exported functions #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/infer/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function findTarget(path) {
path = path.node;
}

if (t.isExportNamedDeclaration(path) || t.isExportDefaultDeclaration(path)) {
path = path.declaration;
}

// var x = TARGET;
if (t.isVariableDeclaration(path)) {
return path.declarations[0].init;
Expand Down
15 changes: 10 additions & 5 deletions lib/infer/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ module.exports = function () {
}
}

// The strategy here is to do a depth-first traversal of the AST,
// looking for nodes with a "name" property, with exceptions as needed.
// For example, name inference for a MemberExpression `foo.bar = baz` will
// infer the named based on the `property` of the MemberExpression (`bar`)
// rather than the `object` (`foo`).
if (comment.context.ast) {
if (comment.context.ast.type === 'ExportDefaultDeclaration') {
comment.name = pathParse(comment.context.file).name;
return comment;
}

// The strategy here is to do a depth-first traversal of the AST,
// looking for nodes with a "name" property, with exceptions as needed.
// For example, name inference for a MemberExpression `foo.bar = baz` will
// infer the named based on the `property` of the MemberExpression (`bar`)
// rather than the `object` (`foo`).
comment.context.ast.traverse({
Identifier: function (path) {
if (inferName(path, path.node)) {
Expand Down
9 changes: 9 additions & 0 deletions test/lib/infer/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,14 @@ test('inferName', function (t) {
/** @module */
}, '/path/inferred-from-file.js').name, 'inferred-from-file');

t.equal(evaluate('/** Test */ export function exported() {}').name, 'exported');

t.equal(evaluate('/** Test */ export default function exported() {}',
'/path/inferred-from-file.js').name, 'inferred-from-file');

t.equal(evaluate('/** Test */ export var life = 42;').name, 'life');

t.equal(evaluate('/** Test */ export class Wizard {}').name, 'Wizard');

t.end();
});
31 changes: 31 additions & 0 deletions test/lib/infer/params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var test = require('tap').test,
parse = require('../../../lib/parsers/javascript'),
inferParams = require('../../../lib/infer/params')();

function toComment(fn, file) {
return parse({
file: file,
source: fn instanceof Function ? '(' + fn.toString() + ')' : fn
})[0];
}

function evaluate(fn, file) {
return inferParams(toComment(fn, file));
}

test('inferParams', function (t) {
t.deepEqual(evaluate(function () {
/** Test */
function f(x) {}
}).params, [{lineNumber: 3, name: 'x', title: 'param'}]);

t.deepEqual(evaluate('/** Test */ export function f(x) {}').params,
[{lineNumber: 1, name: 'x', title: 'param'}]);

t.deepEqual(evaluate('/** Test */ export default function f(x) {}').params,
[{lineNumber: 1, name: 'x', title: 'param'}]);

t.end();
});