@@ -5,6 +5,7 @@ export type ParsedComments = {
5
5
line : Element
6
6
token : Element
7
7
info : [ prefix : string , content : string , suffix ?: string ]
8
+ isLineCommentOnly : boolean
8
9
isJsxStyle : boolean
9
10
} [ ]
10
11
@@ -35,6 +36,49 @@ export function parseComments(
35
36
const out : ParsedComments = [ ]
36
37
37
38
for ( const line of lines ) {
39
+ // We split nested comments
40
+ if ( matchAlgorithm === 'v3' ) {
41
+ const splittedElements = line . children . flatMap ( ( element , idx ) => {
42
+ if ( element . type !== 'element' )
43
+ return element
44
+
45
+ const token = element . children [ 0 ]
46
+ if ( token . type !== 'text' )
47
+ return element
48
+
49
+ const isLast = idx === line . children . length - 1
50
+ const isComment = matchToken ( token . value , isLast )
51
+ if ( ! isComment )
52
+ return element
53
+ const rawSplits = token . value . split ( / ( \s + \/ \/ ) / )
54
+ if ( rawSplits . length <= 1 )
55
+ return element
56
+
57
+ let splits : string [ ] = [ rawSplits [ 0 ] ]
58
+ for ( let i = 1 ; i < rawSplits . length ; i += 2 ) {
59
+ splits . push ( rawSplits [ i ] + ( rawSplits [ i + 1 ] || '' ) )
60
+ }
61
+ splits = splits . filter ( Boolean )
62
+ if ( splits . length <= 1 )
63
+ return element
64
+
65
+ return splits . map ( ( split ) => {
66
+ return < Element > {
67
+ ...element ,
68
+ children : [
69
+ {
70
+ type : 'text' ,
71
+ value : split ,
72
+ } ,
73
+ ] ,
74
+ }
75
+ } )
76
+ } )
77
+
78
+ if ( splittedElements . length !== line . children . length )
79
+ line . children = splittedElements
80
+ }
81
+
38
82
const elements = line . children
39
83
let start = elements . length - 1
40
84
if ( matchAlgorithm === 'v1' )
@@ -57,18 +101,21 @@ export function parseComments(
57
101
continue
58
102
59
103
if ( jsx && ! isLast && i !== 0 ) {
104
+ const isJsxStyle = isValue ( elements [ i - 1 ] , '{' ) && isValue ( elements [ i + 1 ] , '}' )
60
105
out . push ( {
61
106
info : match ,
62
107
line,
63
108
token,
64
- isJsxStyle : isValue ( elements [ i - 1 ] , '{' ) && isValue ( elements [ i + 1 ] , '}' ) ,
109
+ isLineCommentOnly : elements . length === 3 && token . children . length === 1 ,
110
+ isJsxStyle,
65
111
} )
66
112
}
67
113
else {
68
114
out . push ( {
69
115
info : match ,
70
116
line,
71
117
token,
118
+ isLineCommentOnly : elements . length === 1 && token . children . length === 1 ,
72
119
isJsxStyle : false ,
73
120
} )
74
121
}
@@ -123,11 +170,25 @@ function matchToken(text: string, isLast: boolean): [prefix: string, content: st
123
170
* For matchAlgorithm v1
124
171
*/
125
172
export function v1ClearEndCommentPrefix ( text : string ) : string {
126
- const regex = / (?: \/ \/ | [ " ' # ] | ; { 1 , 2 } | % { 1 , 2 } | - - ) ( .* ) $ /
127
- const result = regex . exec ( text )
173
+ const match = text . match ( / (?: \/ \/ | [ " ' # ] | ; { 1 , 2 } | % { 1 , 2 } | - - ) ( \s * ) $ / )
174
+
175
+ if ( match && match [ 1 ] . trim ( ) . length === 0 ) {
176
+ return text . slice ( 0 , match . index )
177
+ }
178
+
179
+ return text
180
+ }
181
+
182
+ /**
183
+ * Remove empty comment prefixes at line end, e.g. `// `
184
+ *
185
+ * For matchAlgorithm v3
186
+ */
187
+ export function v3ClearEndCommentPrefix ( text : string ) : string {
188
+ const match = text . match ( / (?: \/ \/ | # | ; { 1 , 2 } | % { 1 , 2 } | - - ) ( \s * ) $ / )
128
189
129
- if ( result && result [ 1 ] . trim ( ) . length === 0 ) {
130
- return text . slice ( 0 , result . index )
190
+ if ( match && match [ 1 ] . trim ( ) . length === 0 ) {
191
+ return text . slice ( 0 , match . index ) . trimEnd ( )
131
192
}
132
193
133
194
return text
0 commit comments