1
1
'use strict' ;
2
2
3
- var types = require ( 'ast -types' ) ,
3
+ var n = require ( 'babel -types' ) ,
4
4
pathParse = require ( 'parse-filepath' ) ,
5
5
shouldSkipInference = require ( './should_skip_inference' ) ,
6
6
isJSDocComment = require ( '../../lib/is_jsdoc_comment' ) ,
7
7
parse = require ( '../../lib/parse' ) ;
8
8
9
- var n = types . namedTypes ;
10
-
11
9
function findLendsIdentifiers ( node ) {
12
10
if ( ! node || ! node . leadingComments ) {
13
11
return ;
@@ -35,22 +33,9 @@ function findLendsIdentifiers(node) {
35
33
function extractIdentifiers ( path ) {
36
34
var identifiers = [ ] ;
37
35
38
- types . visit ( path , {
39
- visitNode : function ( ) {
40
- return false ;
41
- } ,
42
-
43
- visitAssignmentExpression : function ( path ) {
44
- this . traverse ( path ) ;
45
- } ,
46
-
47
- visitMemberExpression : function ( path ) {
48
- this . traverse ( path ) ;
49
- } ,
50
-
51
- visitIdentifier : function ( path ) {
36
+ path . traverse ( {
37
+ Identifier : function ( path ) {
52
38
identifiers . push ( path . node . name ) ;
53
- return false ;
54
39
}
55
40
} ) ;
56
41
@@ -148,45 +133,49 @@ module.exports = function () {
148
133
return comment ;
149
134
}
150
135
136
+ if ( ! comment . context . ast ) {
137
+ return comment ;
138
+ }
139
+
151
140
var path = comment . context . ast ;
152
141
var identifiers ;
153
142
154
143
/*
155
144
* Deal with an oddity of espree: the jsdoc comment is attached to a different
156
145
* node in the two expressions `a.b = c` vs `a.b = function () {}`.
157
146
*/
158
- if ( n . ExpressionStatement . check ( path . node ) &&
159
- n . AssignmentExpression . check ( path . node . expression ) &&
160
- n . MemberExpression . check ( path . node . expression . left ) ) {
147
+ if ( n . isExpressionStatement ( path . node ) &&
148
+ n . isAssignmentExpression ( path . node . expression ) &&
149
+ n . isMemberExpression ( path . node . expression . left ) ) {
161
150
path = path . get ( 'expression' ) . get ( 'left' ) ;
162
151
}
163
152
164
153
/*
165
154
* Same as above but for `b: c` vs `b: function () {}`.
166
155
*/
167
- if ( n . Property . check ( path . node ) &&
168
- n . Identifier . check ( path . node . key ) ) {
156
+ if ( n . isProperty ( path . node ) &&
157
+ n . isIdentifier ( path . node . key ) ) {
169
158
path = path . get ( 'key' ) ;
170
159
}
171
160
172
161
// Foo.bar = ...;
173
162
// Foo.prototype.bar = ...;
174
163
// Foo.bar.baz = ...;
175
- if ( n . MemberExpression . check ( path . node ) ) {
164
+ if ( n . isMemberExpression ( path . node ) ) {
176
165
identifiers = extractIdentifiers ( path ) ;
177
166
if ( identifiers . length >= 2 ) {
178
167
inferMembershipFromIdentifiers ( comment , identifiers . slice ( 0 , - 1 ) ) ;
179
168
}
180
169
}
181
170
182
171
// /** @lends Foo */{ bar: ... }
183
- if ( n . Identifier . check ( path . node ) &&
184
- n . Property . check ( path . parent . node ) &&
185
- n . ObjectExpression . check ( path . parent . parent . node ) ) {
172
+ if ( n . isIdentifier ( path . node ) &&
173
+ n . isObjectProperty ( path . parentPath ) &&
174
+ n . isObjectExpression ( path . parentPath . parentPath ) ) {
186
175
// The @lends comment is sometimes attached to the first property rather than
187
176
// the object expression itself.
188
- identifiers = findLendsIdentifiers ( path . parent . parent . node ) ||
189
- findLendsIdentifiers ( path . parent . parent . node . properties [ 0 ] ) ;
177
+ identifiers = findLendsIdentifiers ( path . parentPath . parentPath . node ) ||
178
+ findLendsIdentifiers ( path . parentPath . parentPath . node . properties [ 0 ] ) ;
190
179
if ( identifiers ) {
191
180
inferMembershipFromIdentifiers ( comment , identifiers ) ;
192
181
}
@@ -195,30 +184,33 @@ module.exports = function () {
195
184
// Foo = { bar: ... };
196
185
// Foo.prototype = { bar: ... };
197
186
// Foo.bar = { baz: ... };
198
- if ( n . Identifier . check ( path . node ) &&
199
- n . Property . check ( path . parent . node ) &&
200
- n . ObjectExpression . check ( path . parent . parent . node ) &&
201
- n . AssignmentExpression . check ( path . parent . parent . parent . node ) ) {
202
- identifiers = extractIdentifiers ( path . parent . parent . parent ) ;
187
+ if ( n . isIdentifier ( path . node ) &&
188
+ n . isObjectProperty ( path . parentPath ) &&
189
+ n . isObjectExpression ( path . parentPath . parentPath ) &&
190
+ n . isAssignmentExpression ( path . parentPath . parentPath . parentPath ) ) {
191
+ identifiers = extractIdentifiers ( path . parentPath . parentPath . parentPath ) ;
192
+ // The last identifier is the thing itself, so throw it away
193
+ // TODO: is this safe?
194
+ identifiers . pop ( ) ;
203
195
if ( identifiers . length >= 1 ) {
204
196
inferMembershipFromIdentifiers ( comment , identifiers ) ;
205
197
}
206
198
}
207
199
208
200
// var Foo = { bar: ... }
209
- if ( n . Identifier . check ( path . node ) &&
210
- n . Property . check ( path . parent . node ) &&
211
- n . ObjectExpression . check ( path . parent . parent . node ) &&
212
- n . VariableDeclarator . check ( path . parent . parent . parent . node ) ) {
213
- identifiers = [ path . parent . parent . parent . node . id . name ] ;
201
+ if ( n . isIdentifier ( path ) &&
202
+ n . isObjectProperty ( path . parentPath ) &&
203
+ n . isObjectExpression ( path . parentPath . parentPath ) &&
204
+ n . isVariableDeclarator ( path . parentPath . parentPath . parentPath ) ) {
205
+ identifiers = [ path . parentPath . parentPath . parentPath . node . id . name ] ;
214
206
inferMembershipFromIdentifiers ( comment , identifiers ) ;
215
207
}
216
208
217
209
// class Foo { bar() { } }
218
- if ( n . MethodDefinition . check ( path . node ) &&
219
- n . ClassBody . check ( path . parent . node ) &&
220
- n . ClassDeclaration . check ( path . parent . parent . node ) ) {
221
- identifiers = [ path . parent . parent . node . id . name ] ;
210
+ if ( n . isClassMethod ( path ) &&
211
+ n . isClassBody ( path . parentPath ) &&
212
+ n . isClassDeclaration ( path . parentPath . parentPath ) ) {
213
+ identifiers = [ path . parentPath . parentPath . node . id . name ] ;
222
214
var scope = 'instance' ;
223
215
if ( path . node . static == true ) {
224
216
scope = 'static' ;
@@ -227,24 +219,28 @@ module.exports = function () {
227
219
}
228
220
229
221
// var Foo = class { bar() { } }
230
- if ( n . MethodDefinition . check ( path . node ) &&
231
- n . ClassBody . check ( path . parent . node ) &&
232
- n . ClassExpression . check ( path . parent . parent . node ) &&
233
- n . AssignmentExpression . check ( path . parent . parent . parent . node ) ) {
234
- identifiers = extractIdentifiers ( path . parent . parent . parent . node ) ;
222
+ if ( n . isClassMethod ( path ) &&
223
+ n . isClassBody ( path . parentPath ) &&
224
+ n . isClassExpression ( path . parentPath . parentPath ) ) {
225
+ identifiers = extractIdentifiers ( path . parentPath . parentPath . parentPath . get ( 'left' ) ) ;
235
226
scope = 'instance' ;
236
227
if ( path . node . static == true ) {
237
228
scope = 'static' ;
238
229
}
239
230
inferMembershipFromIdentifiers ( comment , identifiers , scope ) ;
240
231
}
241
232
242
- // var function Foo(){ function bar(){} return { bar: bar }; }
243
- if ( n . FunctionDeclaration . check ( path . node ) &&
244
- n . BlockStatement . check ( path . parent . node ) &&
245
- n . FunctionDeclaration . check ( path . parent . parent . node ) ) {
246
- inferMembershipFromIdentifiers ( comment , [ path . parent . parent . value . id . name ] ) ;
233
+ // var function Foo() {
234
+ // function bar() {}
235
+ // return { bar: bar };
236
+ // }
237
+ /*
238
+ if (n.isFunctionDeclaration(path) &&
239
+ n.isBlockStatement(path.parentPath) &&
240
+ n.isFunction(path.parentPath.parentPath)) {
241
+ inferMembershipFromIdentifiers(comment, [path.parentPath.parentPath.node.id.name]);
247
242
}
243
+ */
248
244
249
245
return comment ;
250
246
} ) ;
0 commit comments