@@ -263,6 +263,79 @@ export class SourceCode {
263
263
return ancestors ;
264
264
}
265
265
266
+ /**
267
+ * @returns {Array<Deno.lint.LineComment | Deno.lint.BlockComment> }
268
+ */
269
+ getAllComments ( ) {
270
+ materializeComments ( this . #ctx) ;
271
+ return this . #ctx. comments ;
272
+ }
273
+
274
+ /**
275
+ * @param {Deno.lint.Node } node
276
+ * @returns {Array<Deno.lint.LineComment | Deno.lint.BlockComment> }
277
+ */
278
+ getCommentsBefore ( node ) {
279
+ materializeComments ( this . #ctx) ;
280
+
281
+ /** @type {Array<Deno.lint.LineComment | Deno.lint.BlockComment> } */
282
+ const before = [ ] ;
283
+
284
+ const { comments } = this . #ctx;
285
+ for ( let i = 0 ; i < comments . length ; i ++ ) {
286
+ const comment = comments [ i ] ;
287
+ if ( comment . range [ 0 ] <= node . range [ 0 ] ) {
288
+ before . push ( comment ) ;
289
+ }
290
+ }
291
+
292
+ return before ;
293
+ }
294
+
295
+ /**
296
+ * @param {Deno.lint.Node } node
297
+ * @returns {Array<Deno.lint.LineComment | Deno.lint.BlockComment> }
298
+ */
299
+ getCommentsAfter ( node ) {
300
+ materializeComments ( this . #ctx) ;
301
+
302
+ /** @type {Array<Deno.lint.LineComment | Deno.lint.BlockComment> } */
303
+ const after = [ ] ;
304
+
305
+ const { comments } = this . #ctx;
306
+ for ( let i = 0 ; i < comments . length ; i ++ ) {
307
+ const comment = comments [ i ] ;
308
+ if ( comment . range [ 0 ] >= node . range [ 1 ] ) {
309
+ after . push ( comment ) ;
310
+ }
311
+ }
312
+
313
+ return after ;
314
+ }
315
+
316
+ /**
317
+ * @param {Deno.lint.Node } node
318
+ * @returns {Array<Deno.lint.LineComment | Deno.lint.BlockComment> }
319
+ */
320
+ getCommentsInside ( node ) {
321
+ materializeComments ( this . #ctx) ;
322
+
323
+ /** @type {Array<Deno.lint.LineComment | Deno.lint.BlockComment> } */
324
+ const inside = [ ] ;
325
+
326
+ const { comments } = this . #ctx;
327
+ for ( let i = 0 ; i < comments . length ; i ++ ) {
328
+ const comment = comments [ i ] ;
329
+ if (
330
+ comment . range [ 0 ] >= node . range [ 0 ] && comment . range [ 1 ] <= node . range [ 1 ]
331
+ ) {
332
+ inside . push ( comment ) ;
333
+ }
334
+ }
335
+
336
+ return inside ;
337
+ }
338
+
266
339
/**
267
340
* @returns {string }
268
341
*/
@@ -345,6 +418,35 @@ export class Context {
345
418
}
346
419
}
347
420
421
+ /**
422
+ * TODO(@marvinhagemeister): Is it worth it to do this lazily?
423
+ * @param {AstContext } ctx
424
+ */
425
+ function materializeComments ( ctx ) {
426
+ const { buf, commentsOffset, comments, strTable } = ctx ;
427
+
428
+ let offset = commentsOffset ;
429
+ const count = readU32 ( buf , offset ) ;
430
+ offset += 4 ;
431
+
432
+ if ( comments . length === count ) return ;
433
+
434
+ while ( offset < buf . length && comments . length < count ) {
435
+ const kind = buf [ offset ] ;
436
+ offset ++ ;
437
+ const spanId = readU32 ( buf , offset ) ;
438
+ offset += 4 ;
439
+ const strId = readU32 ( buf , offset ) ;
440
+ offset += 4 ;
441
+
442
+ comments . push ( {
443
+ type : kind === 0 ? "Line" : "Block" ,
444
+ range : readSpan ( ctx , spanId ) ,
445
+ value : getString ( strTable , strId ) ,
446
+ } ) ;
447
+ }
448
+ }
449
+
348
450
/**
349
451
* @param {Deno.lint.Plugin[] } plugins
350
452
* @param {string[] } exclude
@@ -489,6 +591,7 @@ class FacadeNode {
489
591
490
592
/** @type {Set<number> } */
491
593
const appliedGetters = new Set ( ) ;
594
+ let hasCommenstGetter = false ;
492
595
493
596
/**
494
597
* Add getters for all potential properties found in the message.
@@ -515,6 +618,16 @@ function setNodeGetters(ctx) {
515
618
} ,
516
619
} ) ;
517
620
}
621
+
622
+ if ( ! hasCommenstGetter ) {
623
+ hasCommenstGetter = true ;
624
+ Object . defineProperty ( FacadeNode . prototype , "comments" , {
625
+ get ( ) {
626
+ materializeComments ( ctx ) ;
627
+ return ctx . comments ;
628
+ } ,
629
+ } ) ;
630
+ }
518
631
}
519
632
520
633
/**
@@ -994,6 +1107,7 @@ function createAstContext(buf, token) {
994
1107
995
1108
// The buffer has a few offsets at the end which allows us to easily
996
1109
// jump to the relevant sections of the message.
1110
+ const commentsOffset = readU32 ( buf , buf . length - 28 ) ;
997
1111
const propsOffset = readU32 ( buf , buf . length - 24 ) ;
998
1112
const spansOffset = readU32 ( buf , buf . length - 20 ) ;
999
1113
const typeMapOffset = readU32 ( buf , buf . length - 16 ) ;
@@ -1060,7 +1174,9 @@ function createAstContext(buf, token) {
1060
1174
rootOffset,
1061
1175
spansOffset,
1062
1176
propsOffset,
1177
+ commentsOffset,
1063
1178
nodes : new Map ( ) ,
1179
+ comments : [ ] ,
1064
1180
strTableOffset,
1065
1181
strByProp,
1066
1182
strByType,
0 commit comments