@@ -191,20 +191,6 @@ function getLocFor(start, end, ast) {
191
191
*/
192
192
function getLoc ( nodeOrToken , ast ) {
193
193
return getLocFor ( nodeOrToken . getStart ( ) , nodeOrToken . end , ast ) ;
194
- // var start = nodeOrToken.getStart(),
195
- // startLoc = ast.getLineAndCharacterOfPosition(start),
196
- // endLoc = ast.getLineAndCharacterOfPosition(nodeOrToken.end);
197
-
198
- // return {
199
- // start: {
200
- // line: startLoc.line + 1,
201
- // column: startLoc.character
202
- // },
203
- // end: {
204
- // line: endLoc.line + 1,
205
- // column: endLoc.character
206
- // }
207
- // };
208
194
}
209
195
210
196
/**
@@ -451,6 +437,157 @@ function convertTokens(ast) {
451
437
return result ;
452
438
}
453
439
440
+
441
+ /**
442
+ * Converts a TypeScript comment to an Esprima comment.
443
+ * @param {boolean } block True if it's a block comment, false if not.
444
+ * @param {string } text The text of the comment.
445
+ * @param {int } start The index at which the comment starts.
446
+ * @param {int } end The index at which the comment ends.
447
+ * @param {Location } startLoc The location at which the comment starts.
448
+ * @param {Location } endLoc The location at which the comment ends.
449
+ * @returns {Object } The comment object.
450
+ * @private
451
+ */
452
+ function convertTypeScriptCommentToEsprimaComment ( block , text , start , end , startLoc , endLoc ) {
453
+ var comment = {
454
+ type : block ? "Block" : "Line" ,
455
+ value : text
456
+ } ;
457
+
458
+ if ( typeof start === "number" ) {
459
+ comment . range = [ start , end ] ;
460
+ }
461
+
462
+ if ( typeof startLoc === "object" ) {
463
+ comment . loc = {
464
+ start : startLoc ,
465
+ end : endLoc
466
+ } ;
467
+ }
468
+
469
+ return comment ;
470
+ }
471
+
472
+ /**
473
+ * Convert comment from TypeScript Triva Scanner.
474
+ * @param {Object } triviaScanner TS Scanner
475
+ * @param {Object } ast the AST object
476
+ * @param {string } code TypeScript code
477
+ * @returns {ESTreeComment } the converted ESTreeComment
478
+ * @private
479
+ */
480
+ function getCommentFromTriviaScanner ( triviaScanner , ast , code ) {
481
+ var kind = triviaScanner . getToken ( ) ;
482
+ var isBlock = ( kind === ts . SyntaxKind . MultiLineCommentTrivia ) ;
483
+ var range = {
484
+ pos : triviaScanner . getTokenPos ( ) ,
485
+ end : triviaScanner . getTextPos ( ) ,
486
+ kind : triviaScanner . getToken ( )
487
+ } ;
488
+
489
+ var comment = code . substring ( range . pos , range . end ) ;
490
+ var text = comment . replace ( "//" , "" ) . replace ( "/*" , "" ) . replace ( "*/" , "" ) ;
491
+ var loc = getLocFor ( range . pos , range . end , ast ) ;
492
+
493
+ var esprimaComment = convertTypeScriptCommentToEsprimaComment ( isBlock , text , range . pos , range . end , loc . start , loc . end ) ;
494
+
495
+ return esprimaComment ;
496
+ }
497
+
498
+
499
+ /**
500
+ * Get container token node between range
501
+ * @param {Object } ast the AST object
502
+ * @param {int } start The index at which the comment starts.
503
+ * @param {int } end The index at which the comment ends.
504
+ * @returns {TSToken } typescript container token
505
+ * @private
506
+ */
507
+ function getNodeContainer ( ast , start , end ) {
508
+ var container = null ;
509
+
510
+ /**
511
+ * @param {TSNode } node the TSNode
512
+ * @returns {undefined }
513
+ */
514
+ function walk ( node ) {
515
+ var nodeStart = node . pos ;
516
+ var nodeEnd = node . end ;
517
+
518
+ if ( start >= nodeStart && end <= nodeEnd ) {
519
+ if ( isToken ( node ) ) {
520
+ container = node ;
521
+ } else {
522
+ node . getChildren ( ) . forEach ( walk ) ;
523
+ }
524
+ }
525
+ }
526
+ walk ( ast ) ;
527
+
528
+ return container ;
529
+ }
530
+
531
+ /**
532
+ * Convert all comments for the given AST.
533
+ * @param {Object } ast the AST object
534
+ * @param {string } code the TypeScript code
535
+ * @returns {ESTreeComment[] } the converted ESTreeComment
536
+ * @private
537
+ */
538
+ function convertComments ( ast , code ) {
539
+ var comments = [ ] ;
540
+
541
+ /**
542
+ * Create a TypeScript Scanner, with skipTrivia set to false so that
543
+ * we can parse the comments
544
+ */
545
+ var triviaScanner = ts . createScanner ( ast . languageVersion , false , 0 , code ) ;
546
+
547
+ var kind = triviaScanner . scan ( ) ;
548
+ while ( kind !== ts . SyntaxKind . EndOfFileToken ) {
549
+ var start = triviaScanner . getTokenPos ( ) ;
550
+ var end = triviaScanner . getTextPos ( ) ;
551
+
552
+ var container = null ;
553
+ switch ( kind ) {
554
+ case ts . SyntaxKind . SingleLineCommentTrivia :
555
+ case ts . SyntaxKind . MultiLineCommentTrivia :
556
+ var comment = getCommentFromTriviaScanner ( triviaScanner , ast , code ) ;
557
+
558
+ comments . push ( comment ) ;
559
+ break ;
560
+ case ts . SyntaxKind . CloseBraceToken :
561
+ container = getNodeContainer ( ast , start , end ) ;
562
+
563
+ if (
564
+ container . kind === ts . SyntaxKind . TemplateMiddle
565
+ || container . kind === ts . SyntaxKind . TemplateTail
566
+ ) {
567
+ kind = triviaScanner . reScanTemplateToken ( ) ;
568
+ continue ;
569
+ }
570
+ break ;
571
+ case ts . SyntaxKind . SlashToken :
572
+ case ts . SyntaxKind . SlashEqualsToken :
573
+ container = getNodeContainer ( ast , start , end ) ;
574
+
575
+ if (
576
+ container . kind === ts . SyntaxKind . RegularExpressionLiteral
577
+ ) {
578
+ kind = triviaScanner . reScanSlashToken ( ) ;
579
+ continue ;
580
+ }
581
+ break ;
582
+ default :
583
+ break ;
584
+ }
585
+ kind = triviaScanner . scan ( ) ;
586
+ }
587
+
588
+ return comments ;
589
+ }
590
+
454
591
//------------------------------------------------------------------------------
455
592
// Public
456
593
//------------------------------------------------------------------------------
@@ -2088,13 +2225,8 @@ module.exports = function(ast, extra) {
2088
2225
estree . tokens = convertTokens ( ast ) ;
2089
2226
}
2090
2227
2091
- /**
2092
- * Add the comment nodes to the AST (that were parsed separately in parser.js)
2093
- * TODO: Track the progress of https://github.com/eslint/eslint/issues/6724
2094
- * regarding ESLint itself becoming responsible for attributing comment nodes
2095
- */
2096
- if ( extra . comment || extra . attachComment ) {
2097
- estree . comments = extra . comments || [ ] ;
2228
+ if ( extra . comment ) {
2229
+ estree . comments = convertComments ( ast , extra . code ) ;
2098
2230
}
2099
2231
2100
2232
return estree ;
0 commit comments