Skip to content

Commit

Permalink
Infer module names
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 12, 2015
1 parent 5449757 commit 4c2cc4d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/infer/name.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var types = require('ast-types');
var types = require('ast-types'),
pathParse = require('parse-filepath');

/**
* Infers a `name` tag from the context,
Expand Down Expand Up @@ -33,6 +34,11 @@ module.exports = function () {
return comment;
}

if (comment.module) {
comment.name = comment.module.name || pathParse(comment.context.file).name;
return comment;
}

if (comment.typedef) {
comment.name = comment.typedef.name;
return comment;
Expand Down
41 changes: 37 additions & 4 deletions test/lib/infer/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ var test = require('tap').test,
parse = require('../../../lib/parsers/javascript'),
inferName = require('../../../lib/infer/name')();

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

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

test('inferName', function (t) {
Expand Down Expand Up @@ -136,5 +136,38 @@ test('inferName', function (t) {
return implicitName;
}).name, 'explicitCallback', 'explicitCallback');

t.equal(evaluate(function () {
/**
* @module explicitModule
* @returns {undefined} baz
*/
function implicitName() {}
return implicitName;
}).name, 'explicitModule');

t.equal(evaluate(function () {
/**
* @module {Function} explicitModule
* @returns {undefined} baz
*/
function implicitName() {}
return implicitName;
}).name, 'explicitModule');

t.equal(evaluate(function () {
/**
* @module
* @returns {undefined} baz
*/
function implicitName() {}
return implicitName;
}, '/path/inferred-from-file.js').name, 'inferred-from-file');

t.equal(evaluate(function () {
/**
* @module
*/
}, '/path/inferred-from-file.js').name, 'inferred-from-file');

t.end();
});

0 comments on commit 4c2cc4d

Please sign in to comment.