1
1
'use strict' ;
2
2
3
3
var types = require ( 'ast-types' ) ,
4
+ pathParse = require ( 'parse-filepath' ) ,
4
5
isJSDocComment = require ( '../../lib/is_jsdoc_comment' ) ,
5
6
parse = require ( '../../lib/parse' ) ;
6
7
@@ -56,22 +57,15 @@ function extractIdentifiers(path) {
56
57
}
57
58
58
59
/**
59
- * Set `memberof` and `instance`/`static` tags on `comment` based on the
60
- * array of `identifiers`. If the last element of the `identifiers` is
61
- * `"prototype"`, it is assumed to be an instance member; otherwise static.
62
- *
63
- * @param {Object } comment comment for which to infer memberships
60
+ * Test whether some identifiers refer to a module export (`exports` or `module.exports`).
64
61
* @param {Array<string> } identifiers array of identifier names
65
- * @returns {undefined } mutates `comment`
66
- * @private
62
+ * @returns {boolean } true if identifiers refer to a module export
67
63
*/
68
- function inferMembershipFromIdentifiers ( comment , identifiers ) {
69
- if ( identifiers [ identifiers . length - 1 ] === 'prototype' ) {
70
- comment . memberof = identifiers . slice ( 0 , - 1 ) . join ( '.' ) ;
71
- comment . scope = 'instance' ;
72
- } else {
73
- comment . memberof = identifiers . join ( '.' ) ;
74
- comment . scope = 'static' ;
64
+ function isModuleExport ( identifiers ) {
65
+ switch ( identifiers . length ) {
66
+ case 1 : return identifiers [ 0 ] === 'exports' ;
67
+ case 2 : return identifiers [ 0 ] === 'module' && identifiers [ 1 ] === 'exports' ;
68
+ default : return false ;
75
69
}
76
70
}
77
71
@@ -84,7 +78,41 @@ function inferMembershipFromIdentifiers(comment, identifiers) {
84
78
* @returns {Object } comment with membership inferred
85
79
*/
86
80
module . exports = function ( ) {
81
+ var currentModule ;
82
+
83
+ function inferModuleName ( comment ) {
84
+ return ( comment . module && comment . module . name ) ||
85
+ pathParse ( comment . context . file ) . name ;
86
+ }
87
+
88
+ /**
89
+ * Set `memberof` and `instance`/`static` tags on `comment` based on the
90
+ * array of `identifiers`. If the last element of the `identifiers` is
91
+ * `"prototype"`, it is assumed to be an instance member; otherwise static.
92
+ *
93
+ * @param {Object } comment comment for which to infer memberships
94
+ * @param {Array<string> } identifiers array of identifier names
95
+ * @returns {undefined } mutates `comment`
96
+ * @private
97
+ */
98
+ function inferMembershipFromIdentifiers ( comment , identifiers ) {
99
+ if ( isModuleExport ( identifiers ) ) {
100
+ comment . memberof = inferModuleName ( currentModule || comment ) ;
101
+ comment . scope = 'static' ;
102
+ } else if ( identifiers [ identifiers . length - 1 ] === 'prototype' ) {
103
+ comment . memberof = identifiers . slice ( 0 , - 1 ) . join ( '.' ) ;
104
+ comment . scope = 'instance' ;
105
+ } else {
106
+ comment . memberof = identifiers . join ( '.' ) ;
107
+ comment . scope = 'static' ;
108
+ }
109
+ }
110
+
87
111
return function inferMembership ( comment ) {
112
+ if ( comment . module ) {
113
+ currentModule = comment ;
114
+ }
115
+
88
116
if ( comment . memberof ) {
89
117
return comment ;
90
118
}
0 commit comments