Skip to content

Pick up comments on VariableDeclarations #571

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 2 commits into from
Oct 21, 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
16 changes: 10 additions & 6 deletions lib/extractors/exported.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ function walkExported(ast, data, addComment) {
}

function getComments(data, path) {
if (!hasJSDocComment(path)) {
var comments = (path.node.leadingComments || []).filter(isJSDocComment);

if (!comments.length) {
// If this is the first declarator we check for comments on the VariableDeclaration.
if (t.isVariableDeclarator(path) && path.parentPath.get('declarations')[0] === path) {
return getComments(data, path.parentPath);
}

var added = addBlankComment(data, path, path.node);
return added ? [added] : [];
}
return path.node.leadingComments.filter(isJSDocComment).map(function (comment) {

return comments.map(function (comment) {
return addComment(data, comment.value, comment.loc, path, path.node.loc, true);
}).filter(Boolean);
}
Expand Down Expand Up @@ -106,10 +114,6 @@ function walkExported(ast, data, addComment) {
return newResults;
}

function hasJSDocComment(path) {
return path.node.leadingComments && path.node.leadingComments.some(isJSDocComment);
}

function traverseExportedSubtree(path, data, addComments, overrideName) {
var attachCommentPath = path;
if (path.parentPath && path.parentPath.isExportDeclaration()) {
Expand Down
10 changes: 6 additions & 4 deletions test/fixture/document-exported.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ 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() {}
}
/** f5 comment */
var f5 = function(y: Y) {},
o2 = {
om2() {}
};
export {o2};
175 changes: 114 additions & 61 deletions test/fixture/document-exported.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -1189,150 +1189,203 @@
"tags": [],
"loc": {
"start": {
"line": 69,
"column": 4
"line": 72,
"column": 0
},
"end": {
"line": 69,
"column": 26
"line": 74,
"column": 1
}
},
"context": {
"loc": {
"start": {
"line": 69,
"column": 4
"line": 72,
"column": 0
},
"end": {
"line": 69,
"column": 26
"line": 74,
"column": 1
}
}
},
"errors": [],
"name": "f5",
"params": [
{
"title": "param",
"name": "y",
"lineNumber": 69,
"type": {
"type": "NameExpression",
"name": "Y"
}
}
],
"name": "o1",
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "f5"
"name": "o1"
}
],
"namespace": "f5"
"namespace": "o1"
},
{
"description": "",
"tags": [],
"loc": {
"start": {
"line": 72,
"column": 0
"line": 73,
"column": 2
},
"end": {
"line": 74,
"column": 1
"line": 73,
"column": 10
}
},
"context": {
"loc": {
"start": {
"line": 72,
"column": 0
"line": 73,
"column": 2
},
"end": {
"line": 74,
"column": 1
"line": 73,
"column": 10
}
}
},
"errors": [],
"name": "o1",
"name": "om1",
"kind": "function",
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "o1"
"name": "om1",
"kind": "function"
}
],
"namespace": "o1"
"namespace": "om1"
},
{
"description": "",
"description": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "f5 comment",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 11,
"offset": 10
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 11,
"offset": 10
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 11,
"offset": 10
}
}
},
"tags": [],
"loc": {
"start": {
"line": 73,
"column": 2
"line": 76,
"column": 0
},
"end": {
"line": 73,
"column": 10
"line": 76,
"column": 17
}
},
"context": {
"loc": {
"start": {
"line": 73,
"column": 2
"line": 77,
"column": 0
},
"end": {
"line": 73,
"column": 10
"line": 80,
"column": 4
}
}
},
"errors": [],
"name": "om1",
"name": "f5",
"kind": "function",
"params": [
{
"title": "param",
"name": "y",
"lineNumber": 77,
"type": {
"type": "NameExpression",
"name": "Y"
}
}
],
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "om1",
"name": "f5",
"kind": "function"
}
],
"namespace": "om1"
"namespace": "f5"
},
{
"description": "",
"tags": [],
"loc": {
"start": {
"line": 76,
"column": 4
"line": 78,
"column": 2
},
"end": {
"line": 78,
"column": 1
"line": 80,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 76,
"column": 4
"line": 78,
"column": 2
},
"end": {
"line": 78,
"column": 1
"line": 80,
"column": 3
}
}
},
Expand All @@ -1354,23 +1407,23 @@
"tags": [],
"loc": {
"start": {
"line": 77,
"column": 2
"line": 79,
"column": 4
},
"end": {
"line": 77,
"column": 10
"line": 79,
"column": 12
}
},
"context": {
"loc": {
"start": {
"line": 77,
"column": 2
"line": 79,
"column": 4
},
"end": {
"line": 77,
"column": 10
"line": 79,
"column": 12
}
}
},
Expand Down
10 changes: 6 additions & 4 deletions test/fixture/document-exported.output.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,18 @@ Returns **void**

- `x` **X**

# o1

# om1

# f5

f5 comment

**Parameters**

- `y` **Y**

# o1

# om1

# o2

# om2
Loading