@@ -20,10 +20,62 @@ import { mustGetDefaultFileSystem } from '../persist';
2020const escapeAssertionReg = new RegExp ( / ( ^ | [ ^ A - Z a - z 0 - 9 _ ] ) ( [ r p ] ) [ 0 - 9 ] * \. / g) ;
2121
2222function escapeAssertion ( s : string ) : string {
23- s = s . replace ( escapeAssertionReg , ( match , p1 , p2 ) => {
24- return p1 + p2 + match . substring ( p1 . length + p2 . length ) . replace ( '.' , '_' ) ;
25- } ) ;
26- return s ;
23+ // Track whether we're inside a string literal
24+ let result = '' ;
25+ let inSingleQuote = false ;
26+ let inDoubleQuote = false ;
27+ let i = 0 ;
28+
29+ while ( i < s . length ) {
30+ const char = s [ i ] ;
31+
32+ // Toggle quote tracking
33+ if ( char === "'" && ! inDoubleQuote ) {
34+ inSingleQuote = ! inSingleQuote ;
35+ result += char ;
36+ i ++ ;
37+ continue ;
38+ }
39+ if ( char === '"' && ! inSingleQuote ) {
40+ inDoubleQuote = ! inDoubleQuote ;
41+ result += char ;
42+ i ++ ;
43+ continue ;
44+ }
45+
46+ // If inside a string literal, just append the character
47+ if ( inSingleQuote || inDoubleQuote ) {
48+ result += char ;
49+ i ++ ;
50+ continue ;
51+ }
52+
53+ // Check if current position is the start of an r/p pattern that should be escaped
54+ // Pattern: (^|[^A-Za-z0-9_])([rp])[0-9]*\.
55+ const remaining = s . substring ( i ) ;
56+ const match = remaining . match ( / ^ ( [ r p ] ) ( [ 0 - 9 ] * ) \. / ) ;
57+
58+ if ( match ) {
59+ // Check if this is a valid boundary (either start of string or preceded by non-alphanumeric)
60+ const prevChar = i > 0 ? s [ i - 1 ] : '' ;
61+ const isValidBoundary = i === 0 || / [ ^ A - Z a - z 0 - 9 _ ] / . test ( prevChar ) ;
62+
63+ if ( isValidBoundary ) {
64+ // Replace the dot with underscore
65+ result += match [ 1 ] + match [ 2 ] + '_' ;
66+ i += match [ 0 ] . length ;
67+ } else {
68+ // Not a valid boundary, just add the character
69+ result += char ;
70+ i ++ ;
71+ }
72+ } else {
73+ result += char ;
74+ i ++ ;
75+ }
76+ }
77+
78+ return result ;
2779}
2880
2981// removeComments removes the comments starting with # in the text.
0 commit comments