@@ -59,7 +59,8 @@ export function findTrailingSourceMapComment(
5959
6060/**
6161 * Removes `//# sourceMappingURL=` comments safely from the given JavaScript code,
62- * ignoring any occurrences that are inside string literals, template literals, or block comments.
62+ * ignoring any occurrences that are inside string literals, template literals, regex literals,
63+ * or block comments.
6364 *
6465 * For raw Uint8Array / Buffer inputs, it optimizes performance by inspecting trailing byte sequences
6566 * to slice the buffer directly without full string decoding.
@@ -93,6 +94,59 @@ export function removeSourceMappingURL(code: string | Uint8Array): string | Uint
9394 return stripped === text ? code : Buffer . from ( stripped , 'utf-8' ) ;
9495}
9596
97+ const REGEX_PRECEDING_KEYWORDS = new Set ( [
98+ 'await' ,
99+ 'case' ,
100+ 'delete' ,
101+ 'do' ,
102+ 'else' ,
103+ 'in' ,
104+ 'instanceof' ,
105+ 'new' ,
106+ 'of' ,
107+ 'return' ,
108+ 'throw' ,
109+ 'typeof' ,
110+ 'void' ,
111+ 'yield' ,
112+ ] ) ;
113+
114+ const REGEX_PRECEDING_PUNCTUATION = new Set ( [
115+ '(' ,
116+ ',' ,
117+ '=' ,
118+ ':' ,
119+ '[' ,
120+ '!' ,
121+ '&' ,
122+ '|' ,
123+ '?' ,
124+ '{' ,
125+ '}' ,
126+ ';' ,
127+ '+' ,
128+ '-' ,
129+ '*' ,
130+ '%' ,
131+ '~' ,
132+ '^' ,
133+ '<' ,
134+ '>' ,
135+ '/' ,
136+ ] ) ;
137+
138+ function isIdentifierChar ( char : string ) : boolean {
139+ const code = char . charCodeAt ( 0 ) ;
140+
141+ return (
142+ ( code >= 97 && code <= 122 ) || // a-z
143+ ( code >= 65 && code <= 90 ) || // A-Z
144+ ( code >= 48 && code <= 57 ) || // 0-9
145+ code === 95 || // _
146+ code === 36 // $
147+ ) ;
148+ }
149+
96150function removeSourceMappingURLFromString ( code : string ) : string {
97151 if ( ! code . includes ( SOURCEMAP_COMMENT_PREFIX ) ) {
98152 return code ;
@@ -106,8 +160,15 @@ function removeSourceMappingURLFromString(code: string): string {
106160 // Stack to track template literal state and curly brace depth for nested interpolations.
107161 const stack : { type : 'template' ; braceDepth : number } [ ] = [ ] ;
108162 let currentState :
109- 'normal' | 'string_double' | 'string_single' | 'template' | 'comment_block' | 'comment_line' =
110- 'normal' ;
163+ | 'normal'
164+ | 'string_double'
165+ | 'string_single'
166+ | 'template'
167+ | 'regex'
168+ | 'comment_block'
169+ | 'comment_line' = 'normal' ;
170+ let regexAllowed = true ;
171+ let inRegexCharClass = false ;
111172
112173 while ( i < len ) {
113174 const char = code [ i ] ;
@@ -156,17 +217,20 @@ function removeSourceMappingURLFromString(code: string): string {
156217 }
157218 if ( char === '"' ) {
158219 currentState = 'string_double' ;
220+ regexAllowed = false ;
159221 i ++ ;
160222 continue ;
161223 }
162224 if ( char === "'" ) {
163225 currentState = 'string_single' ;
226+ regexAllowed = false ;
164227 i ++ ;
165228 continue ;
166229 }
167230 if ( char === '`' ) {
168231 currentState = 'template' ;
169232 stack . push ( { type : 'template' , braceDepth : 0 } ) ;
233+ regexAllowed = false ;
170234 i ++ ;
171235 continue ;
172236 }
@@ -175,6 +239,7 @@ function removeSourceMappingURLFromString(code: string): string {
175239 if ( top ) {
176240 top . braceDepth ++ ;
177241 }
242+ regexAllowed = true ;
178243 i ++ ;
179244 continue ;
180245 }
@@ -186,14 +251,53 @@ function removeSourceMappingURLFromString(code: string): string {
186251 // Exiting a template literal interpolation ${ ... }
187252 stack . pop ( ) ;
188253 currentState = 'template' ;
254+ regexAllowed = false ;
189255 i ++ ;
190256 continue ;
191257 }
192258 }
259+ regexAllowed = true ;
260+ i ++ ;
261+ continue ;
262+ }
263+ if ( char === '/' ) {
264+ if ( regexAllowed ) {
265+ currentState = 'regex' ;
266+ inRegexCharClass = false ;
267+ regexAllowed = false ;
268+ } else {
269+ regexAllowed = true ;
270+ }
271+ i ++ ;
272+ continue ;
273+ }
274+ if ( char === ' ' || char === '\t' || char === '\n' || char === '\r' ) {
275+ i ++ ;
276+ continue ;
277+ }
278+ if ( isIdentifierChar ( char ) ) {
279+ const start = i ;
193280 i ++ ;
281+ while ( i < len && isIdentifierChar ( code [ i ] ) ) {
282+ i ++ ;
283+ }
284+ const word = code . slice ( start , i ) ;
285+ let prevIdx = start - 1 ;
286+ while (
287+ prevIdx >= 0 &&
288+ ( code [ prevIdx ] === ' ' ||
289+ code [ prevIdx ] === '\t' ||
290+ code [ prevIdx ] === '\n' ||
291+ code [ prevIdx ] === '\r' )
292+ ) {
293+ prevIdx -- ;
294+ }
295+ regexAllowed =
296+ ( prevIdx < 0 || code [ prevIdx ] !== '.' ) && REGEX_PRECEDING_KEYWORDS . has ( word ) ;
194297 continue ;
195298 }
196299
300+ regexAllowed = REGEX_PRECEDING_PUNCTUATION . has ( char ) ;
197301 i ++ ;
198302 } else if ( currentState === 'string_double' ) {
199303 if ( char === '\\' ) {
@@ -202,6 +306,7 @@ function removeSourceMappingURLFromString(code: string): string {
202306 }
203307 if ( char === '"' ) {
204308 currentState = 'normal' ;
309+ regexAllowed = false ;
205310 }
206311 i ++ ;
207312 } else if ( currentState === 'string_single' ) {
@@ -211,6 +316,7 @@ function removeSourceMappingURLFromString(code: string): string {
211316 }
212317 if ( char === "'" ) {
213318 currentState = 'normal' ;
319+ regexAllowed = false ;
214320 }
215321 i ++ ;
216322 } else if ( currentState === 'template' ) {
@@ -222,12 +328,40 @@ function removeSourceMappingURLFromString(code: string): string {
222328 // Entering template literal interpolation context
223329 currentState = 'normal' ;
224330 stack . push ( { type : 'template' , braceDepth : 0 } ) ;
331+ regexAllowed = true ;
225332 i += 2 ;
226333 continue ;
227334 }
228335 if ( char === '`' ) {
229336 stack . pop ( ) ;
230337 currentState = 'normal' ;
338+ regexAllowed = false ;
339+ }
340+ i ++ ;
341+ } else if ( currentState === 'regex' ) {
342+ if ( char === '\\' ) {
343+ if ( nextChar === '\n' || nextChar === '\r' ) {
344+ currentState = 'normal' ;
345+ inRegexCharClass = false ;
346+ regexAllowed = true ;
347+ }
348+ i += 2 ;
349+ continue ;
350+ }
351+ if ( char === '\n' || char === '\r' ) {
352+ currentState = 'normal' ;
353+ inRegexCharClass = false ;
354+ regexAllowed = true ;
355+ i ++ ;
356+ continue ;
357+ }
358+ if ( char === '[' ) {
359+ inRegexCharClass = true ;
360+ } else if ( char === ']' && inRegexCharClass ) {
361+ inRegexCharClass = false ;
362+ } else if ( char === '/' && ! inRegexCharClass ) {
363+ currentState = 'normal' ;
364+ regexAllowed = false ;
231365 }
232366 i ++ ;
233367 } else if ( currentState === 'comment_block' ) {
0 commit comments