diff --git a/lib/infer/finders.js b/lib/infer/finders.js index 8098ad933..b66848fd3 100644 --- a/lib/infer/finders.js +++ b/lib/infer/finders.js @@ -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; diff --git a/lib/infer/name.js b/lib/infer/name.js index b3db496c9..37a6d1b52 100644 --- a/lib/infer/name.js +++ b/lib/infer/name.js @@ -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)) { diff --git a/test/lib/infer/name.js b/test/lib/infer/name.js index 425ab0680..54aad2ca3 100644 --- a/test/lib/infer/name.js +++ b/test/lib/infer/name.js @@ -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(); }); diff --git a/test/lib/infer/params.js b/test/lib/infer/params.js new file mode 100644 index 000000000..4f48dae76 --- /dev/null +++ b/test/lib/infer/params.js @@ -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(); +});