@@ -211,13 +211,18 @@ const getMetadataFromComments = (contents: string): Record<string, string> => {
211
211
const lines = contents . split ( '\n' )
212
212
const metadata = { }
213
213
let commentStyle = null
214
- let spaceRegex = null
215
214
let inMultilineComment = false
216
215
let multilineCommentEnd = null
217
216
218
- const setCommentStyle = ( style : string ) => {
219
- commentStyle = style
220
- spaceRegex = new RegExp ( `^${ commentStyle } ?[^ ]` )
217
+ // Valid metadata key pattern: starts with a letter, can contain letters, numbers, and underscores
218
+ const validKeyPattern = / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ ] * $ /
219
+ // Common prefixes to ignore
220
+ const ignoreKeyPrefixes = [ 'TODO' , 'FIXME' , 'NOTE' , 'HACK' , 'XXX' , 'BUG' ]
221
+
222
+ // Regex to match comment lines with metadata
223
+ const commentRegex = {
224
+ '//' : / ^ \/ \/ \s * ( [ ^ : ] + ) : ( .* ) $ / ,
225
+ '#' : / ^ # \s * ( [ ^ : ] + ) : ( .* ) $ /
221
226
}
222
227
223
228
for ( const line of lines ) {
@@ -249,40 +254,42 @@ const getMetadataFromComments = (contents: string): Record<string, string> => {
249
254
// Skip lines that are part of a multiline comment block
250
255
if ( inMultilineComment ) continue
251
256
252
- // Determine the comment style based on the first encountered comment line
253
- if ( commentStyle === null ) {
254
- if ( line . startsWith ( '//' ) && ( line [ 2 ] === ' ' || / [ a - z A - Z ] / . test ( line [ 2 ] ) ) ) {
255
- setCommentStyle ( '//' )
256
- } else if ( line . startsWith ( '#' ) && ( line [ 1 ] === ' ' || / [ a - z A - Z ] / . test ( line [ 1 ] ) ) ) {
257
- setCommentStyle ( '#' )
258
- }
257
+ // Determine comment style and try to match metadata
258
+ let match = null
259
+ if ( line . startsWith ( '//' ) ) {
260
+ match = line . match ( commentRegex [ '//' ] )
261
+ commentStyle = '//'
262
+ } else if ( line . startsWith ( '#' ) ) {
263
+ match = line . match ( commentRegex [ '#' ] )
264
+ commentStyle = '#'
259
265
}
260
266
261
- // Skip lines that don't start with the determined comment style
262
- if ( commentStyle === null || ( commentStyle && ! line . startsWith ( commentStyle ) ) ) continue
267
+ if ( ! match ) continue
263
268
264
- // Check for 0 or 1 space after the comment style
265
- if ( ! line . match ( spaceRegex ) ) continue
269
+ // Extract and trim the key and value
270
+ const [ , rawKey , value ] = match
271
+ const trimmedKey = rawKey . trim ( )
272
+ const trimmedValue = value . trim ( )
266
273
267
- // Find the index of the first colon
268
- const colonIndex = line . indexOf ( ':' )
269
- if ( colonIndex === - 1 ) continue
274
+ // Skip if key starts with common prefixes to ignore
275
+ if ( ignoreKeyPrefixes . some ( prefix => trimmedKey . toUpperCase ( ) . startsWith ( prefix ) ) ) continue
270
276
271
- // Extract key and value based on the colon index
272
- let key = line . substring ( commentStyle . length , colonIndex ) . trim ( )
277
+ // Skip if key doesn't match valid pattern
278
+ if ( ! validKeyPattern . test ( trimmedKey ) ) continue
273
279
280
+ // Transform the key case
281
+ let key = trimmedKey
274
282
if ( key ?. length > 0 ) {
275
283
key = key [ 0 ] . toLowerCase ( ) + key . slice ( 1 )
276
284
}
277
- const value = line . substring ( colonIndex + 1 ) . trim ( )
278
285
279
286
// Skip empty keys or values
280
- if ( ! key || ! value ) {
287
+ if ( ! key || ! trimmedValue ) {
281
288
continue
282
289
}
283
290
284
291
let parsedValue : string | boolean | number
285
- let lowerValue = value . toLowerCase ( )
292
+ let lowerValue = trimmedValue . toLowerCase ( )
286
293
let lowerKey = key . toLowerCase ( )
287
294
switch ( true ) {
288
295
case lowerValue === 'true' :
@@ -292,10 +299,10 @@ const getMetadataFromComments = (contents: string): Record<string, string> => {
292
299
parsedValue = false
293
300
break
294
301
case lowerKey === 'timeout' :
295
- parsedValue = parseInt ( value , 10 )
302
+ parsedValue = Number . parseInt ( trimmedValue , 10 )
296
303
break
297
304
default :
298
- parsedValue = value
305
+ parsedValue = trimmedValue
299
306
}
300
307
301
308
// Only assign if the key hasn't been assigned before
0 commit comments