@@ -4,6 +4,8 @@ import doctrine from 'doctrine'
4
4
5
5
import debug from 'debug'
6
6
7
+ import SourceCode from 'eslint/lib/util/source-code'
8
+
7
9
import parse from 'eslint-module-utils/parse'
8
10
import resolve from 'eslint-module-utils/resolve'
9
11
import isIgnored , { hasValidExtension } from 'eslint-module-utils/ignore'
@@ -193,22 +195,28 @@ export default class ExportMap {
193
195
* @param {...[type] } nodes [description]
194
196
* @return {{doc: object} }
195
197
*/
196
- function captureDoc ( docStyleParsers ) {
198
+ function captureDoc ( source , docStyleParsers ) {
197
199
const metadata = { }
198
200
, nodes = Array . prototype . slice . call ( arguments , 1 )
199
201
200
202
// 'some' short-circuits on first 'true'
201
203
nodes . some ( n => {
202
- if ( ! n . leadingComments ) return false
203
-
204
- for ( let name in docStyleParsers ) {
205
- const doc = docStyleParsers [ name ] ( n . leadingComments )
206
- if ( doc ) {
207
- metadata . doc = doc
204
+ try {
205
+ // n.leadingComments is legacy `attachComments` behavior
206
+ let leadingComments = n . leadingComments || source . getCommentsBefore ( n )
207
+ if ( leadingComments . length === 0 ) return false
208
+
209
+ for ( let name in docStyleParsers ) {
210
+ const doc = docStyleParsers [ name ] ( leadingComments )
211
+ if ( doc ) {
212
+ metadata . doc = doc
213
+ }
208
214
}
209
- }
210
215
211
- return true
216
+ return true
217
+ } catch ( err ) {
218
+ return false
219
+ }
212
220
} )
213
221
214
222
return metadata
@@ -338,6 +346,8 @@ ExportMap.parse = function (path, content, context) {
338
346
docStyleParsers [ style ] = availableDocStyleParsers [ style ]
339
347
} )
340
348
349
+ const source = makeSourceCode ( content , ast )
350
+
341
351
// attempt to collect module doc
342
352
if ( ast . comments ) {
343
353
ast . comments . some ( c => {
@@ -405,7 +415,7 @@ ExportMap.parse = function (path, content, context) {
405
415
ast . body . forEach ( function ( n ) {
406
416
407
417
if ( n . type === 'ExportDefaultDeclaration' ) {
408
- const exportMeta = captureDoc ( docStyleParsers , n )
418
+ const exportMeta = captureDoc ( source , docStyleParsers , n )
409
419
if ( n . declaration . type === 'Identifier' ) {
410
420
addNamespace ( exportMeta , n . declaration )
411
421
}
@@ -441,12 +451,12 @@ ExportMap.parse = function (path, content, context) {
441
451
case 'TSInterfaceDeclaration' :
442
452
case 'TSAbstractClassDeclaration' :
443
453
case 'TSModuleDeclaration' :
444
- m . namespace . set ( n . declaration . id . name , captureDoc ( docStyleParsers , n ) )
454
+ m . namespace . set ( n . declaration . id . name , captureDoc ( source , docStyleParsers , n ) )
445
455
break
446
456
case 'VariableDeclaration' :
447
457
n . declaration . declarations . forEach ( ( d ) =>
448
458
recursivePatternCapture ( d . id ,
449
- id => m . namespace . set ( id . name , captureDoc ( docStyleParsers , d , n ) ) ) )
459
+ id => m . namespace . set ( id . name , captureDoc ( source , docStyleParsers , d , n ) ) ) )
450
460
break
451
461
}
452
462
}
@@ -531,3 +541,17 @@ function childContext(path, context) {
531
541
path,
532
542
}
533
543
}
544
+
545
+
546
+ /**
547
+ * sometimes legacy support isn't _that_ hard... right?
548
+ */
549
+ function makeSourceCode ( text , ast ) {
550
+ if ( SourceCode . length > 1 ) {
551
+ // ESLint 3
552
+ return new SourceCode ( text , ast )
553
+ } else {
554
+ // ESLint 4, 5
555
+ return new SourceCode ( { text, ast } )
556
+ }
557
+ }
0 commit comments