File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed
Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change 1+ function generateRandomString ( length : number ) {
2+ return [ ...Array ( length ) ] . map ( ( ) => Math . random ( ) . toString ( 36 ) [ 2 ] ) . join ( "" ) ;
3+ }
4+
5+ function preserveStrings ( code : string ) {
6+ let replacements = { } as Record < string , string > ;
7+
8+ const output = code . replace ( / ( [ ' " ` ] ) ( [ ^ ' ` " ] + ) \1/ g, replacedString => {
9+ const replacementId = generateRandomString ( 32 ) ;
10+ replacements = {
11+ ...replacements ,
12+ [ replacementId ] : replacedString ,
13+ } ;
14+ return `<@~${ replacementId } ~@>` ;
15+ } ) ;
16+ return { output, replacements } ;
17+ }
18+
19+ function restoreStrings ( code : string , replacements : Record < string , string > ) {
20+ return code . replace ( / < @ ~ ( .* ?) ~ @ > / g, ( _ , replacementId ) => {
21+ return replacements [ replacementId ] ;
22+ } ) ;
23+ }
24+
125export default function injectSchemas ( code : string , refName : string ) {
2- return code
26+ const { output : preservedCode , replacements } = preserveStrings ( code ) ;
27+
28+ const preservedCodeWithSchemasInjected = preservedCode
329 . replace ( new RegExp ( `\\b${ refName } \\.` , "g" ) , `global.schemas[${ refName } ].` )
430 . replace ( new RegExp ( `\\b${ refName } \\b` , "g" ) , `"${ refName } "` ) ;
31+
32+ return restoreStrings ( preservedCodeWithSchemasInjected , replacements ) ;
533}
You can’t perform that action at this time.
0 commit comments