@@ -2,7 +2,7 @@ import path from 'node:path';
22import ts from 'typescript' ;
33import { createFSBackedSystem , createVirtualTypeScriptEnvironment } from '@typescript/vfs' ;
44
5- const CODEBLOCK_REGEX = / (?< openingFence > ` ` ` (?: t s | t y p e s c r i p t ) ? \n ) (?< code > [ \s \S ] * ?) ` ` ` / g ;
5+ const CODEBLOCK_REGEX = / (?< openingFence > ` ` ` (?: t s | t y p e s c r i p t ) ? \n ) (?< code > [ \s \S ] * ?) ` ` ` / gv ;
66const FILENAME = 'example-codeblock.ts' ;
77const TWOSLASH_COMMENT = '//=>' ;
88
@@ -24,8 +24,7 @@ const compilerOptions = {
2424 exactOptionalPropertyTypes : true ,
2525} ;
2626
27- const virtualFsMap = new Map ( ) ;
28- virtualFsMap . set ( FILENAME , '// Can\'t be empty' ) ;
27+ const virtualFsMap = new Map ( [ [ FILENAME , '// Can\'t be empty' ] ] ) ;
2928
3029const rootDir = path . join ( import . meta. dirname , '..' ) ;
3130const system = createFSBackedSystem ( virtualFsMap , rootDir , ts ) ;
@@ -41,7 +40,7 @@ function parseCompilerOptions(code) {
4140 continue ;
4241 }
4342
44- const match = line . match ( / ^ \s * \/ \/ @ ( \w + ) : ( .* ) $ / ) ;
43+ const match = line . match ( / ^ \s * \/ \/ @ ( \w + ) : ( .* ) $ / v ) ;
4544 if ( ! match ) {
4645 // Stop parsing at the first non-matching line
4746 return options ;
@@ -265,12 +264,12 @@ function normalizeType(type, onlySortNumbers = false) {
265264
266265 if ( onlySortNumbers ) {
267266 // Sort only numeric members while keeping non-numeric members at their original positions
268- const sortedNumericTypes = types . filter ( ( [ a ] ) => isNumeric ( a ) ) . sort ( ( [ a ] , [ b ] ) => Number ( a ) - Number ( b ) ) ;
267+ const sortedNumericTypes = types . filter ( ( [ a ] ) => isNumeric ( a ) ) . toSorted ( ( [ a ] , [ b ] ) => Number ( a ) - Number ( b ) ) ;
269268 let numericIndex = 0 ;
270269 types = types . map ( t => isNumeric ( t [ 0 ] ) ? sortedNumericTypes [ numericIndex ++ ] [ 1 ] : t [ 1 ] ) ;
271270 } else {
272271 types = types
273- . sort ( ( [ a ] , [ b ] ) => a < b ? - 1 : ( a > b ? 1 : 0 ) )
272+ . toSorted ( ( [ a ] , [ b ] ) => a < b ? - 1 : ( a > b ? 1 : 0 ) )
274273 . map ( t => t [ 1 ] ) ;
275274 }
276275
@@ -298,7 +297,7 @@ function normalizeType(type, onlySortNumbers = false) {
298297 return node ;
299298 } ;
300299
301- return print ( visit ( typeNode ) ) . replaceAll ( / ^ ( + ) / gm , indentation => {
300+ return print ( visit ( typeNode ) ) . replaceAll ( / ^ ( + ) / gmv , indentation => {
302301 // Replace spaces used for indentation with tabs
303302 const spacesPerTab = 4 ;
304303 const tabCount = Math . floor ( indentation . length / spacesPerTab ) ;
@@ -312,10 +311,10 @@ function getCommentForType(type) {
312311
313312 if ( type . length < 80 ) {
314313 comment = type
315- . replaceAll ( / \r ? \n \s * / g , ' ' ) // Collapse into single line
316- . replaceAll ( / { \s + / g , '{' ) // Remove spaces after `{`
317- . replaceAll ( / \s + } / g , '}' ) // Remove spaces before ` }`
318- . replaceAll ( / ; (? = } ) / g , '' ) ; // Remove semicolons before ` }`
314+ . replaceAll ( / \r ? \n \s * / gv , ' ' ) // Collapse into single line
315+ . replaceAll ( / \ {\s + / gv , '{' ) // Remove spaces after `{`
316+ . replaceAll ( / \s + \} / gv , '}' ) // Remove spaces before ` }`
317+ . replaceAll ( / ; (? = \ }) / gv , '' ) ; // Remove semicolons before ` }`
319318 }
320319
321320 return `${ TWOSLASH_COMMENT } ${ comment . replaceAll ( '\n' , '\n// ' ) } ` ;
0 commit comments