1
1
import { FormatOptions } from '../FormatOptions.js' ;
2
- import { equalizeWhitespace , isMultiline } from '../utils.js' ;
2
+ import { equalizeWhitespace , isMultiline , last } from '../utils.js' ;
3
3
4
4
import Params from './Params.js' ;
5
5
import { isTabularStyle } from './config.js' ;
@@ -356,8 +356,20 @@ export default class ExpressionFormatter {
356
356
return isMultiline ( node . text ) || isMultiline ( node . precedingWhitespace || '' ) ;
357
357
}
358
358
359
+ private isDocComment ( comment : string ) : boolean {
360
+ const lines = comment . split ( / \n / ) ;
361
+ return (
362
+ // first line starts with /* or /**
363
+ / ^ \/ \* \* ? $ / . test ( lines [ 0 ] ) &&
364
+ // intermediate lines start with *
365
+ lines . slice ( 1 , lines . length - 1 ) . every ( line => / ^ \s * \* / . test ( line ) ) &&
366
+ // last line ends with */
367
+ / ^ \s * \* \/ $ / . test ( last ( lines ) as string )
368
+ ) ;
369
+ }
370
+
359
371
// Breaks up block comment to multiple lines.
360
- // For example this comment (dots representing leading whitespace):
372
+ // For example this doc- comment (dots representing leading whitespace):
361
373
//
362
374
// ..../**
363
375
// .....* Some description here
@@ -371,14 +383,30 @@ export default class ExpressionFormatter {
371
383
// '.* and here too',
372
384
// '.*/' ]
373
385
//
386
+ // However, a normal comment (non-doc-comment) like this:
387
+ //
388
+ // ..../*
389
+ // ....Some description here
390
+ // ....*/
391
+ //
392
+ // gets broken to this array (no leading spaces):
393
+ //
394
+ // [ '/*',
395
+ // 'Some description here',
396
+ // '*/' ]
397
+ //
374
398
private splitBlockComment ( comment : string ) : string [ ] {
375
- return comment . split ( / \n / ) . map ( line => {
376
- if ( / ^ \s * \* / . test ( line ) ) {
377
- return ' ' + line . replace ( / ^ \s * / , '' ) ;
378
- } else {
379
- return line . replace ( / ^ \s * / , '' ) ;
380
- }
381
- } ) ;
399
+ if ( this . isDocComment ( comment ) ) {
400
+ return comment . split ( / \n / ) . map ( line => {
401
+ if ( / ^ \s * \* / . test ( line ) ) {
402
+ return ' ' + line . replace ( / ^ \s * / , '' ) ;
403
+ } else {
404
+ return line ;
405
+ }
406
+ } ) ;
407
+ } else {
408
+ return comment . split ( / \n / ) . map ( line => line . replace ( / ^ \s * / , '' ) ) ;
409
+ }
382
410
}
383
411
384
412
private formatSubExpression ( nodes : AstNode [ ] ) : Layout {
0 commit comments