From 6ab7ef5b4398a6be33b7d091917a80215dd7cc26 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Wed, 25 May 2016 17:06:18 -0700 Subject: [PATCH 1/2] 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 --- lib/infer/finders.js | 4 ++++ lib/infer/name.js | 15 ++++++++++----- test/lib/infer/name.js | 20 ++++++++++++++++++++ test/lib/infer/params.js | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 test/lib/infer/params.js 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..d26c71a1a 100644 --- a/test/lib/infer/name.js +++ b/test/lib/infer/name.js @@ -123,5 +123,25 @@ 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..9d08ef35f --- /dev/null +++ b/test/lib/infer/params.js @@ -0,0 +1,35 @@ +'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: 3, name: 'x', title: 'param'}]); + + t.deepEqual(evaluate(` + /** Test */ + export default function f(x) {} + `).params, [{lineNumber: 3, name: 'x', title: 'param'}]); + + t.end(); +}); From af5862a29cf434cbbfe99a51252c8716a2b70212 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Wed, 25 May 2016 19:25:47 -0700 Subject: [PATCH 2/2] Change to string literls --- test/lib/infer/name.js | 21 +++++---------------- test/lib/infer/params.js | 12 ++++-------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/test/lib/infer/name.js b/test/lib/infer/name.js index d26c71a1a..54aad2ca3 100644 --- a/test/lib/infer/name.js +++ b/test/lib/infer/name.js @@ -123,25 +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 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 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 var life = 42;').name, 'life'); - t.equal(evaluate(` - /** Test */ - export class Wizard {} - `).name, 'Wizard'); + 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 index 9d08ef35f..4f48dae76 100644 --- a/test/lib/infer/params.js +++ b/test/lib/infer/params.js @@ -21,15 +21,11 @@ test('inferParams', function (t) { function f(x) {} }).params, [{lineNumber: 3, name: 'x', title: 'param'}]); - t.deepEqual(evaluate(` - /** Test */ - export 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: 3, name: 'x', title: 'param'}]); + t.deepEqual(evaluate('/** Test */ export default function f(x) {}').params, + [{lineNumber: 1, name: 'x', title: 'param'}]); t.end(); });