@@ -35,9 +35,15 @@ const enum COMMENT_TYPE {
35
35
36
36
const enum COMMENT_SKIP_NEWLINE {
37
37
DEFAULT ,
38
- SKIP_ALL ,
39
- SKIP_LEADING ,
40
- SKIP_TRAILING ,
38
+ ALL ,
39
+ LEADING ,
40
+ TRAILING ,
41
+ }
42
+
43
+ const enum PRINT_COMMENT_HINT {
44
+ SKIP ,
45
+ ALLOW ,
46
+ DEFER ,
41
47
}
42
48
43
49
export type Format = {
@@ -906,48 +912,50 @@ class Printer {
906
912
}
907
913
}
908
914
909
- // Returns `true ` if the comment cannot be printed in this position due to
915
+ // Returns `PRINT_COMMENT_HINT.DEFER ` if the comment cannot be printed in this position due to
910
916
// line terminators, signaling that the print comments loop can stop and
911
- // resume printing comments at the next posisble position. This happens when
917
+ // resume printing comments at the next possible position. This happens when
912
918
// printing inner comments, since if we have an inner comment with a multiline
913
919
// there is at least one inner position where line terminators are allowed.
914
- _printComment (
915
- comment : t . Comment ,
916
- skipNewLines : COMMENT_SKIP_NEWLINE ,
917
- ) : boolean {
920
+ _shouldPrintComment ( comment : t . Comment ) : PRINT_COMMENT_HINT {
918
921
// Some plugins (such as flow-strip-types) use this to mark comments as removed using the AST-root 'comments' property,
919
922
// where they can't manually mutate the AST node comment lists.
920
- if ( comment . ignore ) return false ;
921
-
922
- if ( this . _printedComments . has ( comment ) ) return false ;
923
+ if ( comment . ignore ) return PRINT_COMMENT_HINT . SKIP ;
923
924
924
- const noLineTerminator = this . _noLineTerminator ;
925
+ if ( this . _printedComments . has ( comment ) ) return PRINT_COMMENT_HINT . SKIP ;
925
926
926
927
if (
927
- noLineTerminator &&
928
+ this . _noLineTerminator &&
928
929
( HAS_NEWLINE . test ( comment . value ) ||
929
930
HAS_BlOCK_COMMENT_END . test ( comment . value ) )
930
931
) {
931
- return true ;
932
+ return PRINT_COMMENT_HINT . DEFER ;
932
933
}
933
934
934
- if ( ! this . format . shouldPrintComment ( comment . value ) ) return false ;
935
-
936
935
this . _printedComments . add ( comment ) ;
937
936
937
+ if ( ! this . format . shouldPrintComment ( comment . value ) ) {
938
+ return PRINT_COMMENT_HINT . SKIP ;
939
+ }
940
+
941
+ return PRINT_COMMENT_HINT . ALLOW ;
942
+ }
943
+
944
+ _printComment ( comment : t . Comment , skipNewLines : COMMENT_SKIP_NEWLINE ) {
945
+ const noLineTerminator = this . _noLineTerminator ;
938
946
const isBlockComment = comment . type === "CommentBlock" ;
939
947
940
948
// Add a newline before and after a block comment, unless explicitly
941
949
// disallowed
942
950
const printNewLines =
943
951
isBlockComment &&
944
- skipNewLines !== COMMENT_SKIP_NEWLINE . SKIP_ALL &&
952
+ skipNewLines !== COMMENT_SKIP_NEWLINE . ALL &&
945
953
! this . _noLineTerminator ;
946
954
947
955
if (
948
956
printNewLines &&
949
957
this . _buf . hasContent ( ) &&
950
- skipNewLines !== COMMENT_SKIP_NEWLINE . SKIP_LEADING
958
+ skipNewLines !== COMMENT_SKIP_NEWLINE . LEADING
951
959
) {
952
960
this . newline ( 1 ) ;
953
961
}
@@ -999,11 +1007,9 @@ class Printer {
999
1007
this . newline ( 1 , true ) ;
1000
1008
}
1001
1009
1002
- if ( printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE . SKIP_TRAILING ) {
1010
+ if ( printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE . TRAILING ) {
1003
1011
this . newline ( 1 ) ;
1004
1012
}
1005
-
1006
- return false ;
1007
1013
}
1008
1014
1009
1015
_printComments (
@@ -1028,8 +1034,12 @@ class Printer {
1028
1034
for ( let i = 0 ; i < len ; i ++ ) {
1029
1035
const comment = comments [ i ] ;
1030
1036
1031
- const printed = this . _printedComments . has ( comment ) ;
1032
- if ( hasLoc && comment . loc && ! printed ) {
1037
+ const shouldPrint = this . _shouldPrintComment ( comment ) ;
1038
+ if ( shouldPrint === PRINT_COMMENT_HINT . DEFER ) {
1039
+ hasLoc = false ;
1040
+ break ;
1041
+ }
1042
+ if ( hasLoc && comment . loc && shouldPrint === PRINT_COMMENT_HINT . ALLOW ) {
1033
1043
const commentStartLine = comment . loc . start . line ;
1034
1044
const commentEndLine = comment . loc . end . line ;
1035
1045
if ( type === COMMENT_TYPE . LEADING ) {
@@ -1050,7 +1060,7 @@ class Printer {
1050
1060
lastLine = commentEndLine ;
1051
1061
1052
1062
maybeNewline ( offset ) ;
1053
- this . _printComment ( comment , COMMENT_SKIP_NEWLINE . SKIP_ALL ) ;
1063
+ this . _printComment ( comment , COMMENT_SKIP_NEWLINE . ALL ) ;
1054
1064
1055
1065
if ( i + 1 === len ) {
1056
1066
maybeNewline (
@@ -1064,7 +1074,7 @@ class Printer {
1064
1074
lastLine = commentEndLine ;
1065
1075
1066
1076
maybeNewline ( offset ) ;
1067
- if ( this . _printComment ( comment , COMMENT_SKIP_NEWLINE . SKIP_ALL ) ) break ;
1077
+ this . _printComment ( comment , COMMENT_SKIP_NEWLINE . ALL ) ;
1068
1078
1069
1079
if ( i + 1 === len ) {
1070
1080
maybeNewline ( Math . min ( 1 , nodeEndLine - lastLine ) ) ; // TODO: Improve here when inner comments processing is stronger
@@ -1076,12 +1086,13 @@ class Printer {
1076
1086
lastLine = commentEndLine ;
1077
1087
1078
1088
maybeNewline ( offset ) ;
1079
- this . _printComment ( comment , COMMENT_SKIP_NEWLINE . SKIP_ALL ) ;
1089
+ this . _printComment ( comment , COMMENT_SKIP_NEWLINE . ALL ) ;
1080
1090
}
1081
1091
} else {
1082
1092
hasLoc = false ;
1083
-
1084
- if ( printed ) continue ;
1093
+ if ( shouldPrint !== PRINT_COMMENT_HINT . ALLOW ) {
1094
+ continue ;
1095
+ }
1085
1096
1086
1097
if ( len === 1 ) {
1087
1098
const singleLine = comment . loc
@@ -1099,13 +1110,11 @@ class Printer {
1099
1110
comment ,
1100
1111
( shouldSkipNewline && node . type !== "ObjectExpression" ) ||
1101
1112
( singleLine && isFunction ( parent , { body : node } ) )
1102
- ? COMMENT_SKIP_NEWLINE . SKIP_ALL
1113
+ ? COMMENT_SKIP_NEWLINE . ALL
1103
1114
: COMMENT_SKIP_NEWLINE . DEFAULT ,
1104
1115
) ;
1105
1116
} else if ( shouldSkipNewline && type === COMMENT_TYPE . TRAILING ) {
1106
- if ( this . _printComment ( comment , COMMENT_SKIP_NEWLINE . SKIP_ALL ) ) {
1107
- break ;
1108
- }
1117
+ this . _printComment ( comment , COMMENT_SKIP_NEWLINE . ALL ) ;
1109
1118
} else {
1110
1119
this . _printComment ( comment , COMMENT_SKIP_NEWLINE . DEFAULT ) ;
1111
1120
}
@@ -1120,15 +1129,14 @@ class Printer {
1120
1129
// /*:: b: ?string*/
1121
1130
// }
1122
1131
1123
- const skippedDueToNewline = this . _printComment (
1132
+ this . _printComment (
1124
1133
comment ,
1125
1134
i === 0
1126
- ? COMMENT_SKIP_NEWLINE . SKIP_LEADING
1135
+ ? COMMENT_SKIP_NEWLINE . LEADING
1127
1136
: i === len - 1
1128
- ? COMMENT_SKIP_NEWLINE . SKIP_TRAILING
1137
+ ? COMMENT_SKIP_NEWLINE . TRAILING
1129
1138
: COMMENT_SKIP_NEWLINE . DEFAULT ,
1130
1139
) ;
1131
- if ( skippedDueToNewline ) break ;
1132
1140
} else {
1133
1141
this . _printComment ( comment , COMMENT_SKIP_NEWLINE . DEFAULT ) ;
1134
1142
}
0 commit comments