Skip to content

Handle assignments in document exported #544

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 1 commit into from
Sep 19, 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
20 changes: 12 additions & 8 deletions lib/extractors/exported.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var traverse = require('babel-traverse').default,
t = require('babel-types'),
nodePath = require('path'),
fs = require('fs'),
parseToAst = require('../parsers/parse_to_ast');
parseToAst = require('../parsers/parse_to_ast'),
findTarget = require('../infer/finders').findTarget;

/**
* Iterate through the abstract syntax tree, finding ES6-style exports,
Expand Down Expand Up @@ -52,14 +53,18 @@ function walkExported(ast, data, addComment) {
var declaration = path.get('declaration');
if (t.isDeclaration(declaration)) {
traverseExportedSubtree(declaration, data, addComments);
return path.skip();
}

if (path.isExportDefaultDeclaration()) {
if (declaration.isDeclaration()) {
traverseExportedSubtree(declaration, data, addComments);
} else if (declaration.isIdentifier()) {
return path.skip();
}
if (declaration.isIdentifier()) {
var binding = declaration.scope.getBinding(declaration.node.name);
traverseExportedSubtree(binding.path, data, addComments);
return path.skip();
}
}

Expand Down Expand Up @@ -92,6 +97,7 @@ function walkExported(ast, data, addComment) {

traverseExportedSubtree(bindingPath, specData, addComments, exported);
});
return path.skip();
}
}
});
Expand All @@ -110,12 +116,10 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
}
addComments(data, attachCommentPath, overrideName);

if (path.isVariableDeclaration()) {
// TODO: How does JSDoc handle multiple declarations?
path = path.get('declarations')[0].get('init');
if (!path) {
return;
}
path = findTarget(path);

if (t.isVariableDeclarator(path) && path.has('init')) {
path = path.get('init');
}

if (path.isClass() || path.isObjectExpression()) {
Expand Down
8 changes: 4 additions & 4 deletions lib/infer/augments.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var shouldSkipInference = require('./should_skip_inference'),
finders = require('./finders');
findClass = require('./finders').findClass;

/**
* Infers an `augments` tag from an ES6 class declaration
Expand All @@ -15,12 +15,12 @@ function inferAugments() {
return comment;
}

var node = finders.findClass(comment.context.ast);
var path = findClass(comment.context.ast);

if (node && node.superClass) {
if (path && path.node.superClass) {
comment.augments = [{
title: 'augments',
name: node.superClass.name
name: path.node.superClass.name
}];
}

Expand Down
35 changes: 17 additions & 18 deletions lib/infer/finders.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
'use strict';

var t = require('babel-types');

/**
* Try to find the part of JavaScript a comment is referring to, by
* looking at the syntax tree closest to that comment.
*
* @param {Object} path abstract syntax tree path
* @returns {?Object} ast node, if one is found.
* @returns {?Object} ast path, if one is found.
* @private
*/
function findTarget(path) {

if (!path) {
return path;
}

if (path.node) {
path = path.node;
}

if (t.isExportNamedDeclaration(path) || t.isExportDefaultDeclaration(path)) {
path = path.declaration;
if (t.isExportDefaultDeclaration(path) ||
t.isExportNamedDeclaration(path) && path.has('declaration')) {
path = path.get('declaration');
}

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

// foo.x = TARGET
if (t.isExpressionStatement(path)) {
return path.expression.right;
} else if (t.isExpressionStatement(path)) {
path = path.get('expression').get('right');
}

return path;
return path.node && path;
}

/**
* Try to find a JavaScript class that this comment refers to,
* whether an expression in an assignment, or a declaration.
*
* @param {Object} node abstract syntax tree node
* @returns {?Object} ast node, if one is found.
* @param {Object} path abstract syntax tree path
* @returns {?Object} ast path, if one is found.
* @private
*/
function findClass(node) {
var target = findTarget(node);
return (t.isClassDeclaration(target) || t.isClassExpression(target)) && target;
function findClass(path) {
var target = findTarget(path);
if (target && (target.isClassDeclaration() || target.isClassExpression())) {
return target;
}
}

module.exports.findTarget = findTarget;
Expand Down
12 changes: 6 additions & 6 deletions lib/infer/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var shouldSkipInference = require('./should_skip_inference'),
t = require('babel-types'),
finders = require('./finders'),
findTarget = require('./finders').findTarget,
flowDoctrine = require('../flow_doctrine');

/**
Expand Down Expand Up @@ -160,15 +160,15 @@ function paramToDoc(param, comment, i, prefix) {
*/
function inferParams() {
return shouldSkipInference(function inferParams(comment) {
var node = finders.findTarget(comment.context.ast);
var path = findTarget(comment.context.ast);

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

if (!t.isFunction(node)) {
if (!t.isFunction(path)) {
return comment;
}

Expand All @@ -182,7 +182,7 @@ function inferParams() {
var paramOrder = {};
var i = 0;

node.params
path.node.params
.reduce(function (params, param, i) {
return params.concat(paramToDoc(param, comment, i));
}, [])
Expand Down
3 changes: 2 additions & 1 deletion lib/infer/return.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = function () {
if (comment.returns) {
return comment;
}
var fn = finders.findTarget(comment.context.ast);
var path = finders.findTarget(comment.context.ast);
var fn = path && path.node;
if (t.isFunction(fn) &&
fn.returnType &&
fn.returnType.typeAnnotation) {
Expand Down
7 changes: 4 additions & 3 deletions lib/infer/type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var finders = require('./finders'),
var findTarget = require('./finders').findTarget,
shouldSkipInference = require('./should_skip_inference'),
flowDoctrine = require('../flow_doctrine'),
t = require('babel-types');
Expand All @@ -24,11 +24,12 @@ module.exports = function () {
return comment;
}

var n = finders.findTarget(comment.context.ast);
if (!n) {
var path = findTarget(comment.context.ast);
if (!path) {
return comment;
}

var n = path.node;
var type;
switch (n.type) {
case 'VariableDeclarator':
Expand Down
14 changes: 14 additions & 0 deletions test/fixture/document-exported.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ type T3 = string;
export type {T2, T3 as T4};

export type {T5} from './document-exported/x.js';

export var f4 = function(x: X) {}

var f5 = function(y: Y) {}
export {f5};

export var o1 = {
om1() {}
}

var o2 = {
om2() {}
}
export {o2};
Loading