@@ -230,24 +230,26 @@ func ToJavaScriptWithSecrets(value string) string {
230230 buf := []byte (value )
231231
232232 // First handle secret variables
233+ p := handleSecretVariables (& s , buf , 0 )
234+
235+ // Then handle regular variables in the remaining text
236+ handleRegularVariables (& s , buf [p :])
237+
238+ return s .String ()
239+ }
240+
241+ // handleSecretVariables processes secret variables in the buffer and returns the position after the last secret
242+ func handleSecretVariables (s * strings.Builder , buf []byte , startPos int ) int {
233243 locs := SecretRegex .FindAllSubmatchIndex (buf , - 1 )
234- p := 0
244+ p := startPos
235245
236246 for _ , loc := range locs {
237247 if len (loc ) < 4 {
238248 panic ("unexpected result while building JavaScript" )
239249 }
240250
241- if s .Len () > 0 {
242- s .WriteRune ('+' )
243- }
244-
245- if pre := buf [p :loc [0 ]]; len (pre ) > 0 {
246- s .WriteRune ('\'' )
247- escapeJavaScript (& s , pre )
248- s .WriteRune ('\'' )
249- s .WriteRune ('+' )
250- }
251+ writePlusIfNeeded (s )
252+ writeTextBeforeMatch (s , buf , p , loc [0 ])
251253
252254 // Generate async secret lookup
253255 s .WriteString (`await secrets.get('` )
@@ -257,60 +259,79 @@ func ToJavaScriptWithSecrets(value string) string {
257259 p = loc [1 ]
258260 }
259261
260- // Then handle regular variables in the remaining text
261- remainingText := buf [p :]
262- if len (remainingText ) > 0 {
263- regularLocs := VariableRegex .FindAllSubmatchIndex (remainingText , - 1 )
264-
265- if len (regularLocs ) > 0 {
266- if s .Len () > 0 {
267- s .WriteRune ('+' )
268- }
262+ // Write any remaining text after secrets
263+ if len (buf [p :]) > 0 {
264+ writePlusIfNeeded (s )
265+ writeQuotedText (s , buf [p :])
266+ }
269267
270- p2 := 0
271- for _ , loc := range regularLocs {
272- if len (loc ) < 4 {
273- panic ("unexpected result while building JavaScript" )
274- }
268+ return p
269+ }
275270
276- if s .Len () > 0 {
277- s .WriteRune ('+' )
278- }
271+ // handleRegularVariables processes regular variables in the remaining text
272+ func handleRegularVariables (s * strings.Builder , remainingText []byte ) {
273+ if len (remainingText ) == 0 {
274+ return
275+ }
279276
280- if pre := remainingText [p2 :loc [0 ]]; len (pre ) > 0 {
281- s .WriteRune ('\'' )
282- escapeJavaScript (& s , pre )
283- s .WriteRune ('\'' )
284- s .WriteRune ('+' )
285- }
277+ regularLocs := VariableRegex .FindAllSubmatchIndex (remainingText , - 1 )
286278
287- s .WriteString (`vars['` )
288- s .Write (remainingText [loc [2 ]:loc [3 ]])
289- s .WriteString (`']` )
279+ if len (regularLocs ) > 0 {
280+ processRegularVariableMatches (s , remainingText , regularLocs )
281+ } else {
282+ // No regular variables, just append the remaining text
283+ writePlusIfNeeded (s )
284+ writeQuotedText (s , remainingText )
285+ }
286+ }
290287
291- p2 = loc [1 ]
292- }
288+ // processRegularVariableMatches processes the matches found by VariableRegex
289+ func processRegularVariableMatches (s * strings.Builder , remainingText []byte , regularLocs [][]int ) {
290+ writePlusIfNeeded (s )
293291
294- if len (remainingText [p2 :]) > 0 {
295- if s .Len () > 0 {
296- s .WriteRune ('+' )
297- }
298- s .WriteRune ('\'' )
299- escapeJavaScript (& s , remainingText [p2 :])
300- s .WriteRune ('\'' )
301- }
302- } else {
303- // No regular variables, just append the remaining text
304- if s .Len () > 0 {
305- s .WriteRune ('+' )
306- }
307- s .WriteRune ('\'' )
308- escapeJavaScript (& s , remainingText )
309- s .WriteRune ('\'' )
292+ p2 := 0
293+ for _ , loc := range regularLocs {
294+ if len (loc ) < 4 {
295+ panic ("unexpected result while building JavaScript" )
310296 }
297+
298+ writePlusIfNeeded (s )
299+ writeTextBeforeMatch (s , remainingText , p2 , loc [0 ])
300+
301+ s .WriteString (`vars['` )
302+ s .Write (remainingText [loc [2 ]:loc [3 ]])
303+ s .WriteString (`']` )
304+
305+ p2 = loc [1 ]
311306 }
312307
313- return s .String ()
308+ // Write any remaining text after the last variable
309+ if len (remainingText [p2 :]) > 0 {
310+ writePlusIfNeeded (s )
311+ writeQuotedText (s , remainingText [p2 :])
312+ }
313+ }
314+
315+ // writePlusIfNeeded writes a plus sign if the builder already has content
316+ func writePlusIfNeeded (s * strings.Builder ) {
317+ if s .Len () > 0 {
318+ s .WriteRune ('+' )
319+ }
320+ }
321+
322+ // writeTextBeforeMatch writes the text before a regex match, quoted and escaped
323+ func writeTextBeforeMatch (s * strings.Builder , buf []byte , start , matchStart int ) {
324+ if pre := buf [start :matchStart ]; len (pre ) > 0 {
325+ writeQuotedText (s , pre )
326+ s .WriteRune ('+' )
327+ }
328+ }
329+
330+ // writeQuotedText writes text as a quoted JavaScript string with proper escaping
331+ func writeQuotedText (s * strings.Builder , text []byte ) {
332+ s .WriteRune ('\'' )
333+ escapeJavaScript (s , text )
334+ s .WriteRune ('\'' )
314335}
315336
316337// escapeJavaScript escapes a byte slice for use in JavaScript strings
0 commit comments