Skip to content

Commit efd5b4c

Browse files
committed
Correctly name comments attached to module.exports (fixes #178)
1 parent 4c2cc4d commit efd5b4c

25 files changed

+157
-214
lines changed

lib/infer/membership.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ function extractIdentifiers(path) {
6363
* @returns {number} number of identifiers referring to a module export (0, 1 or 2)
6464
*/
6565
function countModuleIdentifiers(comment, identifiers) {
66-
if (identifiers.length >= 1 &&
67-
(identifiers[0] === 'exports' || (identifiers[0] === 'module' && comment.name === 'exports'))) {
66+
if (identifiers.length >= 1 && identifiers[0] === 'exports') {
6867
return 1;
6968
}
7069

71-
if (identifiers.length >= 2 &&
72-
(identifiers[0] === 'module' && identifiers[1] === 'exports')) {
70+
if (identifiers.length >= 2 && identifiers[0] === 'module' && identifiers[1] === 'exports') {
7371
return 2;
7472
}
7573

@@ -105,6 +103,11 @@ module.exports = function () {
105103
* @private
106104
*/
107105
function inferMembershipFromIdentifiers(comment, identifiers) {
106+
if (identifiers.length === 1 && identifiers[0] === 'module' && comment.name === 'exports') {
107+
comment.name = inferModuleName(currentModule || comment);
108+
return;
109+
}
110+
108111
/*
109112
* Test whether identifiers start with a module export (`exports` or `module.exports`),
110113
* and if so replace those identifiers with the name of the current module.

test/fixture/_external-deps-included.json

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
11
[
2+
{
3+
"description": "I am in `external.input.js`.",
4+
"tags": [],
5+
"loc": {
6+
"start": {
7+
"line": 5,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 7,
12+
"column": 3
13+
}
14+
},
15+
"context": {
16+
"loc": {
17+
"start": {
18+
"line": 8,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 11,
23+
"column": 1
24+
}
25+
},
26+
"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"
27+
},
28+
"errors": [],
29+
"name": "foo",
30+
"kind": "function",
31+
"members": {
32+
"instance": [],
33+
"static": []
34+
},
35+
"events": [],
36+
"path": [
37+
"foo"
38+
]
39+
},
240
{
341
"description": "This function returns the number one.",
442
"tags": [
@@ -35,12 +73,7 @@
3573
},
3674
"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"
3775
},
38-
"errors": [
39-
{
40-
"message": "memberof reference to index not found",
41-
"commentLineNumber": 0
42-
}
43-
],
76+
"errors": [],
4477
"returns": [
4578
{
4679
"title": "returns",
@@ -52,17 +85,15 @@
5285
}
5386
}
5487
],
55-
"name": "exports",
88+
"name": "index",
5689
"kind": "function",
57-
"memberof": "index",
58-
"scope": "static",
5990
"members": {
6091
"instance": [],
6192
"static": []
6293
},
6394
"events": [],
6495
"path": [
65-
"exports"
96+
"index"
6697
]
6798
},
6899
{
@@ -101,12 +132,7 @@
101132
},
102133
"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"
103134
},
104-
"errors": [
105-
{
106-
"message": "memberof reference to main not found",
107-
"commentLineNumber": 0
108-
}
109-
],
135+
"errors": [],
110136
"returns": [
111137
{
112138
"title": "returns",
@@ -118,55 +144,15 @@
118144
}
119145
}
120146
],
121-
"name": "exports",
122-
"kind": "function",
123-
"memberof": "main",
124-
"scope": "static",
125-
"members": {
126-
"instance": [],
127-
"static": []
128-
},
129-
"events": [],
130-
"path": [
131-
"exports"
132-
]
133-
},
134-
{
135-
"description": "I am in `external.input.js`.",
136-
"tags": [],
137-
"loc": {
138-
"start": {
139-
"line": 5,
140-
"column": 0
141-
},
142-
"end": {
143-
"line": 7,
144-
"column": 3
145-
}
146-
},
147-
"context": {
148-
"loc": {
149-
"start": {
150-
"line": 8,
151-
"column": 0
152-
},
153-
"end": {
154-
"line": 11,
155-
"column": 1
156-
}
157-
},
158-
"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"
159-
},
160-
"errors": [],
161-
"name": "foo",
147+
"name": "main",
162148
"kind": "function",
163149
"members": {
164150
"instance": [],
165151
"static": []
166152
},
167153
"events": [],
168154
"path": [
169-
"foo"
155+
"main"
170156
]
171157
}
172158
]

test/fixture/_multi-file-input.json

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
[
22
{
3-
"description": "This function returns the number one.",
3+
"description": "This function returns the number plus two.",
44
"tags": [
5+
{
6+
"title": "param",
7+
"description": "the number",
8+
"lineNumber": 3,
9+
"type": {
10+
"type": "NameExpression",
11+
"name": "Number"
12+
},
13+
"name": "a"
14+
},
515
{
616
"title": "returns",
7-
"description": "numberone",
8-
"lineNumber": 2,
17+
"description": "numbertwo",
18+
"lineNumber": 4,
919
"type": {
1020
"type": "NameExpression",
1121
"name": "Number"
1222
}
23+
},
24+
{
25+
"title": "example",
26+
"description": "var result = returnTwo(4);\n// result is 6",
27+
"lineNumber": 5
1328
}
1429
],
1530
"loc": {
@@ -18,79 +33,77 @@
1833
"column": 0
1934
},
2035
"end": {
21-
"line": 4,
36+
"line": 9,
2237
"column": 3
2338
}
2439
},
2540
"context": {
2641
"loc": {
2742
"start": {
28-
"line": 5,
43+
"line": 10,
2944
"column": 0
3045
},
3146
"end": {
32-
"line": 8,
33-
"column": 2
47+
"line": 13,
48+
"column": 1
3449
}
3550
},
36-
"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"
51+
"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"
3752
},
3853
"errors": [
3954
{
40-
"message": "memberof reference to simple.input not found",
41-
"commentLineNumber": 0
55+
"message": "type Number found, number is standard",
56+
"commentLineNumber": 3
57+
}
58+
],
59+
"params": [
60+
{
61+
"title": "param",
62+
"description": "the number",
63+
"lineNumber": 3,
64+
"type": {
65+
"type": "NameExpression",
66+
"name": "Number"
67+
},
68+
"name": "a"
4269
}
4370
],
4471
"returns": [
4572
{
4673
"title": "returns",
47-
"description": "numberone",
48-
"lineNumber": 2,
74+
"description": "numbertwo",
75+
"lineNumber": 4,
4976
"type": {
5077
"type": "NameExpression",
5178
"name": "Number"
5279
}
5380
}
5481
],
55-
"name": "exports",
82+
"examples": [
83+
"var result = returnTwo(4);\n// result is 6"
84+
],
85+
"name": "returnTwo",
5686
"kind": "function",
57-
"memberof": "simple.input",
58-
"scope": "static",
5987
"members": {
6088
"instance": [],
6189
"static": []
6290
},
6391
"events": [],
6492
"path": [
65-
"exports"
93+
"returnTwo"
6694
]
6795
},
6896
{
69-
"description": "This function returns the number plus two.",
97+
"description": "This function returns the number one.",
7098
"tags": [
71-
{
72-
"title": "param",
73-
"description": "the number",
74-
"lineNumber": 3,
75-
"type": {
76-
"type": "NameExpression",
77-
"name": "Number"
78-
},
79-
"name": "a"
80-
},
8199
{
82100
"title": "returns",
83-
"description": "numbertwo",
84-
"lineNumber": 4,
101+
"description": "numberone",
102+
"lineNumber": 2,
85103
"type": {
86104
"type": "NameExpression",
87105
"name": "Number"
88106
}
89-
},
90-
{
91-
"title": "example",
92-
"description": "var result = returnTwo(4);\n// result is 6",
93-
"lineNumber": 5
94107
}
95108
],
96109
"loc": {
@@ -99,64 +112,44 @@
99112
"column": 0
100113
},
101114
"end": {
102-
"line": 9,
115+
"line": 4,
103116
"column": 3
104117
}
105118
},
106119
"context": {
107120
"loc": {
108121
"start": {
109-
"line": 10,
122+
"line": 5,
110123
"column": 0
111124
},
112125
"end": {
113-
"line": 13,
114-
"column": 1
126+
"line": 8,
127+
"column": 2
115128
}
116129
},
117-
"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"
130+
"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"
118131
},
119-
"errors": [
120-
{
121-
"message": "type Number found, number is standard",
122-
"commentLineNumber": 3
123-
}
124-
],
125-
"params": [
126-
{
127-
"title": "param",
128-
"description": "the number",
129-
"lineNumber": 3,
130-
"type": {
131-
"type": "NameExpression",
132-
"name": "Number"
133-
},
134-
"name": "a"
135-
}
136-
],
132+
"errors": [],
137133
"returns": [
138134
{
139135
"title": "returns",
140-
"description": "numbertwo",
141-
"lineNumber": 4,
136+
"description": "numberone",
137+
"lineNumber": 2,
142138
"type": {
143139
"type": "NameExpression",
144140
"name": "Number"
145141
}
146142
}
147143
],
148-
"examples": [
149-
"var result = returnTwo(4);\n// result is 6"
150-
],
151-
"name": "returnTwo",
144+
"name": "simple.input",
152145
"kind": "function",
153146
"members": {
154147
"instance": [],
155148
"static": []
156149
},
157150
"events": [],
158151
"path": [
159-
"returnTwo"
152+
"simple.input"
160153
]
161154
}
162155
]

0 commit comments

Comments
 (0)