@@ -31,6 +31,45 @@ function findLendsIdentifiers(node) {
31
31
}
32
32
}
33
33
34
+ /**
35
+ * Extract and return the identifiers for expressions of type this.foo
36
+ *
37
+ * @param {NodePath } path AssignmentExpression, MemberExpression, or Identifier
38
+ * @returns {Array<string> } identifiers
39
+ * @private
40
+ */
41
+ function extractThis ( path ) {
42
+ var identifiers = [ ] ;
43
+
44
+ path . traverse ( {
45
+ /**
46
+ * Add the resolved identifier of this in a path to the identifiers array
47
+ * @param {Object } path ast path
48
+ * @returns {undefined } has side-effects
49
+ * @private
50
+ */
51
+ ThisExpression : function ( path ) {
52
+ var scope = path . scope ;
53
+
54
+ while ( n . isBlockStatement ( scope . block ) ) {
55
+ scope = scope . parent ;
56
+ }
57
+
58
+ if ( n . isClassMethod ( scope . block ) ) {
59
+ identifiers . push ( scope . path . parentPath . parentPath . node . id . name , 'prototype' ) ;
60
+ }
61
+
62
+ if ( n . isFunctionDeclaration ( scope . block ) ||
63
+ n . isFunctionExpression ( scope . block ) ) {
64
+ identifiers . push ( scope . block . id . name , 'prototype' ) ;
65
+ }
66
+ }
67
+ } ) ;
68
+
69
+ return identifiers ;
70
+ }
71
+
72
+
34
73
/**
35
74
* Extract and return the chain of identifiers from the left hand side of expressions
36
75
* of the forms `Foo = ...`, `Foo.bar = ...`, `Foo.bar.baz = ...`, etc.
@@ -76,6 +115,33 @@ function countModuleIdentifiers(comment, identifiers) {
76
115
return 0 ;
77
116
}
78
117
118
+ /**
119
+ * Returns the comment object after normalizing Foo.prototype and Foo# expressions
120
+ * @param {Object } comment parsed comment
121
+ * @returns {Object } the normalized comment
122
+ */
123
+ function normalizeMemberof ( comment ) {
124
+ var memberof = comment . memberof ;
125
+
126
+ var isPrototype = / .p r o t o t y p e $ / ;
127
+
128
+ if ( memberof . match ( isPrototype ) !== null ) {
129
+ comment . memberof = memberof . replace ( isPrototype , '' ) ;
130
+ comment . scope = 'instance' ;
131
+
132
+ return comment ;
133
+ }
134
+
135
+ var isInstanceMember = / # $ / ;
136
+
137
+ if ( memberof . match ( isInstanceMember ) !== null ) {
138
+ comment . memberof = memberof . replace ( isInstanceMember , '' ) ;
139
+ comment . scope = 'instance' ;
140
+ }
141
+
142
+ return comment ;
143
+ }
144
+
79
145
/**
80
146
* Uses code structure to infer `memberof`, `instance`, and `static`
81
147
* tags from the placement of JSDoc
@@ -147,7 +213,7 @@ module.exports = function () {
147
213
}
148
214
149
215
if ( comment . memberof ) {
150
- return comment ;
216
+ return normalizeMemberof ( comment ) ;
151
217
}
152
218
153
219
if ( ! comment . context . ast ) {
@@ -176,7 +242,10 @@ module.exports = function () {
176
242
// Foo.prototype.bar = ...;
177
243
// Foo.bar.baz = ...;
178
244
if ( n . isMemberExpression ( path . node ) ) {
179
- identifiers = extractIdentifiers ( path ) ;
245
+ identifiers = [ ] . concat (
246
+ extractThis ( path ) ,
247
+ extractIdentifiers ( path )
248
+ ) ;
180
249
if ( identifiers . length >= 2 ) {
181
250
inferMembershipFromIdentifiers ( comment , identifiers . slice ( 0 , - 1 ) ) ;
182
251
}
0 commit comments