Skip to content

Make findTarget return VariableDeclarator #539

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions lib/infer/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function findTarget(path) {
path = path.declaration;
}

// var x = TARGET;
// var x = init;
if (t.isVariableDeclaration(path)) {
return path.declarations[0].init;
return path.declarations[0];
}

// foo.x = TARGET
Expand Down
6 changes: 6 additions & 0 deletions lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ function inferParams() {
return shouldSkipInference(function inferParams(comment) {
var node = finders.findTarget(comment.context.ast);

// In case of `/** */ var x = function () {}` findTarget returns
// the declarator.
if (t.isVariableDeclarator(node)) {
node = node.init;
}

if (!t.isFunction(node)) {
return comment;
}
Expand Down
16 changes: 16 additions & 0 deletions test/lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ test('inferParams', function (t) {
function f(x) {}
}).params, [{lineNumber: 3, name: 'x', title: 'param'}]);

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

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

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


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

Expand Down