Skip to content

Commit

Permalink
Correctly name comments attached to module.exports (fixes #178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Oct 12, 2015
1 parent 4c2cc4d commit efd5b4c
Show file tree
Hide file tree
Showing 25 changed files with 157 additions and 214 deletions.
11 changes: 7 additions & 4 deletions lib/infer/membership.js
Expand Up @@ -63,13 +63,11 @@ function extractIdentifiers(path) {
* @returns {number} number of identifiers referring to a module export (0, 1 or 2)
*/
function countModuleIdentifiers(comment, identifiers) {
if (identifiers.length >= 1 &&
(identifiers[0] === 'exports' || (identifiers[0] === 'module' && comment.name === 'exports'))) {
if (identifiers.length >= 1 && identifiers[0] === 'exports') {
return 1;
}

if (identifiers.length >= 2 &&
(identifiers[0] === 'module' && identifiers[1] === 'exports')) {
if (identifiers.length >= 2 && identifiers[0] === 'module' && identifiers[1] === 'exports') {
return 2;
}

Expand Down Expand Up @@ -105,6 +103,11 @@ module.exports = function () {
* @private
*/
function inferMembershipFromIdentifiers(comment, identifiers) {
if (identifiers.length === 1 && identifiers[0] === 'module' && comment.name === 'exports') {
comment.name = inferModuleName(currentModule || comment);
return;
}

/*
* Test whether identifiers start with a module export (`exports` or `module.exports`),
* and if so replace those identifiers with the name of the current module.
Expand Down
102 changes: 44 additions & 58 deletions test/fixture/_external-deps-included.json
@@ -1,4 +1,42 @@
[
{
"description": "I am in `external.input.js`.",
"tags": [],
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 7,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 8,
"column": 0
},
"end": {
"line": 11,
"column": 1
}
},
"code": "require('external');\nrequire('external2');\nrequire('module-not-found');\n\n/**\n * I am in `external.input.js`.\n */\nfunction foo() {\n 'use strict';\n return 'bar';\n}\n\nmodule.exports = foo;\n"
},
"errors": [],
"name": "foo",
"kind": "function",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"foo"
]
},
{
"description": "This function returns the number one.",
"tags": [
Expand Down Expand Up @@ -35,12 +73,7 @@
},
"code": "/**\n * This function returns the number one.\n * @return {Number} numberone\n */\nmodule.exports = function () {\n // this returns 1\n return 1;\n};\n"
},
"errors": [
{
"message": "memberof reference to index not found",
"commentLineNumber": 0
}
],
"errors": [],
"returns": [
{
"title": "returns",
Expand All @@ -52,17 +85,15 @@
}
}
],
"name": "exports",
"name": "index",
"kind": "function",
"memberof": "index",
"scope": "static",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"exports"
"index"
]
},
{
Expand Down Expand Up @@ -101,12 +132,7 @@
},
"code": "'use strict';\n\nvar otherDep = require('external2');\n\n/**\n * This function returns the number one.\n * @return {Number} numberone\n */\nmodule.exports = function () {\n // this returns 1\n return otherDep() - 1;\n};\n"
},
"errors": [
{
"message": "memberof reference to main not found",
"commentLineNumber": 0
}
],
"errors": [],
"returns": [
{
"title": "returns",
Expand All @@ -118,55 +144,15 @@
}
}
],
"name": "exports",
"kind": "function",
"memberof": "main",
"scope": "static",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"exports"
]
},
{
"description": "I am in `external.input.js`.",
"tags": [],
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 7,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 8,
"column": 0
},
"end": {
"line": 11,
"column": 1
}
},
"code": "require('external');\nrequire('external2');\nrequire('module-not-found');\n\n/**\n * I am in `external.input.js`.\n */\nfunction foo() {\n 'use strict';\n return 'bar';\n}\n\nmodule.exports = foo;\n"
},
"errors": [],
"name": "foo",
"name": "main",
"kind": "function",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"foo"
"main"
]
}
]
121 changes: 57 additions & 64 deletions test/fixture/_multi-file-input.json
@@ -1,15 +1,30 @@
[
{
"description": "This function returns the number one.",
"description": "This function returns the number plus two.",
"tags": [
{
"title": "param",
"description": "the number",
"lineNumber": 3,
"type": {
"type": "NameExpression",
"name": "Number"
},
"name": "a"
},
{
"title": "returns",
"description": "numberone",
"lineNumber": 2,
"description": "numbertwo",
"lineNumber": 4,
"type": {
"type": "NameExpression",
"name": "Number"
}
},
{
"title": "example",
"description": "var result = returnTwo(4);\n// result is 6",
"lineNumber": 5
}
],
"loc": {
Expand All @@ -18,79 +33,77 @@
"column": 0
},
"end": {
"line": 4,
"line": 9,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 5,
"line": 10,
"column": 0
},
"end": {
"line": 8,
"column": 2
"line": 13,
"column": 1
}
},
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nmodule.exports = function () {\n // this returns 1\n return 1;\n};\n"
"code": "/**\n * This function returns the number plus two.\n *\n * @param {Number} a the number\n * @returns {Number} numbertwo\n * @example\n * var result = returnTwo(4);\n * // result is 6\n */\nfunction returnTwo(a) {\n // this returns a + 2\n return a + 2;\n}\n"
},
"errors": [
{
"message": "memberof reference to simple.input not found",
"commentLineNumber": 0
"message": "type Number found, number is standard",
"commentLineNumber": 3
}
],
"params": [
{
"title": "param",
"description": "the number",
"lineNumber": 3,
"type": {
"type": "NameExpression",
"name": "Number"
},
"name": "a"
}
],
"returns": [
{
"title": "returns",
"description": "numberone",
"lineNumber": 2,
"description": "numbertwo",
"lineNumber": 4,
"type": {
"type": "NameExpression",
"name": "Number"
}
}
],
"name": "exports",
"examples": [
"var result = returnTwo(4);\n// result is 6"
],
"name": "returnTwo",
"kind": "function",
"memberof": "simple.input",
"scope": "static",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"exports"
"returnTwo"
]
},
{
"description": "This function returns the number plus two.",
"description": "This function returns the number one.",
"tags": [
{
"title": "param",
"description": "the number",
"lineNumber": 3,
"type": {
"type": "NameExpression",
"name": "Number"
},
"name": "a"
},
{
"title": "returns",
"description": "numbertwo",
"lineNumber": 4,
"description": "numberone",
"lineNumber": 2,
"type": {
"type": "NameExpression",
"name": "Number"
}
},
{
"title": "example",
"description": "var result = returnTwo(4);\n// result is 6",
"lineNumber": 5
}
],
"loc": {
Expand All @@ -99,64 +112,44 @@
"column": 0
},
"end": {
"line": 9,
"line": 4,
"column": 3
}
},
"context": {
"loc": {
"start": {
"line": 10,
"line": 5,
"column": 0
},
"end": {
"line": 13,
"column": 1
"line": 8,
"column": 2
}
},
"code": "/**\n * This function returns the number plus two.\n *\n * @param {Number} a the number\n * @returns {Number} numbertwo\n * @example\n * var result = returnTwo(4);\n * // result is 6\n */\nfunction returnTwo(a) {\n // this returns a + 2\n return a + 2;\n}\n"
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nmodule.exports = function () {\n // this returns 1\n return 1;\n};\n"
},
"errors": [
{
"message": "type Number found, number is standard",
"commentLineNumber": 3
}
],
"params": [
{
"title": "param",
"description": "the number",
"lineNumber": 3,
"type": {
"type": "NameExpression",
"name": "Number"
},
"name": "a"
}
],
"errors": [],
"returns": [
{
"title": "returns",
"description": "numbertwo",
"lineNumber": 4,
"description": "numberone",
"lineNumber": 2,
"type": {
"type": "NameExpression",
"name": "Number"
}
}
],
"examples": [
"var result = returnTwo(4);\n// result is 6"
],
"name": "returnTwo",
"name": "simple.input",
"kind": "function",
"members": {
"instance": [],
"static": []
},
"events": [],
"path": [
"returnTwo"
"simple.input"
]
}
]

0 comments on commit efd5b4c

Please sign in to comment.