@@ -97,6 +97,7 @@ export function removeSourceMappingURL(code: string | Uint8Array): string | Uint
9797const REGEX_PRECEDING_KEYWORDS = new Set ( [
9898 'await' ,
9999 'case' ,
100+ 'default' ,
100101 'delete' ,
101102 'do' ,
102103 'else' ,
@@ -143,10 +144,71 @@ function isIdentifierChar(char: string): boolean {
143144 ( code >= 65 && code <= 90 ) || // A-Z
144145 ( code >= 48 && code <= 57 ) || // 0-9
145146 code === 95 || // _
146- code === 36 // $
147+ code === 36 || // $
148+ code > 127 // Unicode / non-ASCII
147149 ) ;
148150}
149151
152+ function consumeIdentifier (
153+ code : string ,
154+ start : number ,
155+ ) : { nextIndex : number ; regexAllowed : boolean } {
156+ const len = code . length ;
157+ let i = start + 1 ;
158+ while ( i < len && isIdentifierChar ( code [ i ] ) ) {
159+ i ++ ;
160+ }
161+ const word = code . slice ( start , i ) ;
162+ let prevIdx = start - 1 ;
163+ while (
164+ prevIdx >= 0 &&
165+ ( code [ prevIdx ] === ' ' ||
166+ code [ prevIdx ] === '\t' ||
167+ code [ prevIdx ] === '\n' ||
168+ code [ prevIdx ] === '\r' )
169+ ) {
170+ prevIdx -- ;
171+ }
172+ const regexAllowed =
173+ ( prevIdx < 0 || code [ prevIdx ] !== '.' ) && REGEX_PRECEDING_KEYWORDS . has ( word ) ;
174+
175+ return { nextIndex : i , regexAllowed } ;
176+ }
177+
178+ function consumeRegexLiteral (
179+ code : string ,
180+ start : number ,
181+ ) : { nextIndex : number ; regexAllowed : boolean } {
182+ const len = code . length ;
183+ let i = start ;
184+ let inCharClass = false ;
185+
186+ while ( i < len ) {
187+ const char = code [ i ] ;
188+ if ( char === '\\' ) {
189+ const nextChar = code [ i + 1 ] ;
190+ if ( nextChar === '\n' || nextChar === '\r' ) {
191+ return { nextIndex : i + 2 , regexAllowed : true } ;
192+ }
193+ i += 2 ;
194+ continue ;
195+ }
196+ if ( char === '\n' || char === '\r' ) {
197+ return { nextIndex : i + 1 , regexAllowed : true } ;
198+ }
199+ if ( char === '[' ) {
200+ inCharClass = true ;
201+ } else if ( char === ']' && inCharClass ) {
202+ inCharClass = false ;
203+ } else if ( char === '/' && ! inCharClass ) {
204+ return { nextIndex : i + 1 , regexAllowed : false } ;
205+ }
206+ i ++ ;
207+ }
208+
209+ return { nextIndex : len , regexAllowed : false } ;
210+ }
211+
150212function removeSourceMappingURLFromString ( code : string ) : string {
151213 if ( ! code . includes ( SOURCEMAP_COMMENT_PREFIX ) ) {
152214 return code ;
@@ -164,11 +226,9 @@ function removeSourceMappingURLFromString(code: string): string {
164226 | 'string_double'
165227 | 'string_single'
166228 | 'template'
167- | 'regex'
168229 | 'comment_block'
169230 | 'comment_line' = 'normal' ;
170231 let regexAllowed = true ;
171- let inRegexCharClass = false ;
172232
173233 while ( i < len ) {
174234 const char = code [ i ] ;
@@ -181,24 +241,7 @@ function removeSourceMappingURLFromString(code: string): string {
181241 continue ;
182242 }
183243 if ( char === '/' && nextChar === '/' ) {
184- // Detect if the comment is escaped (e.g. inside a regex literal like `/\/\/#/`).
185- let isEscaped = false ;
186- let prevIdx = i - 1 ;
187- while ( prevIdx >= 0 && / \s / . test ( code [ prevIdx ] ) ) {
188- prevIdx -- ;
189- }
190- if ( prevIdx >= 0 && code [ prevIdx ] === '\\' ) {
191- let bsCount = 0 ;
192- while ( prevIdx >= 0 && code [ prevIdx ] === '\\' ) {
193- bsCount ++ ;
194- prevIdx -- ;
195- }
196- if ( bsCount % 2 === 1 ) {
197- isEscaped = true ;
198- }
199- }
200-
201- if ( ! isEscaped && code . startsWith ( SOURCEMAP_COMMENT_PREFIX , i ) ) {
244+ if ( code . startsWith ( SOURCEMAP_COMMENT_PREFIX , i ) ) {
202245 if ( i > lastCopiedIndex ) {
203246 result . push ( code . slice ( lastCopiedIndex , i ) ) ;
204247 }
@@ -260,40 +303,26 @@ function removeSourceMappingURLFromString(code: string): string {
260303 i ++ ;
261304 continue ;
262305 }
306+ if ( ( char === '+' && nextChar === '+' ) || ( char === '-' && nextChar === '-' ) ) {
307+ regexAllowed = false ;
308+ i += 2 ;
309+ continue ;
310+ }
263311 if ( char === '/' ) {
264312 if ( regexAllowed ) {
265- currentState = 'regex' ;
266- inRegexCharClass = false ;
267- regexAllowed = false ;
313+ ( { nextIndex : i , regexAllowed } = consumeRegexLiteral ( code , i + 1 ) ) ;
268314 } else {
269315 regexAllowed = true ;
316+ i ++ ;
270317 }
271- i ++ ;
272318 continue ;
273319 }
274320 if ( char === ' ' || char === '\t' || char === '\n' || char === '\r' ) {
275321 i ++ ;
276322 continue ;
277323 }
278324 if ( isIdentifierChar ( char ) ) {
279- const start = i ;
280- 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 ) ;
325+ ( { nextIndex : i , regexAllowed } = consumeIdentifier ( code , i ) ) ;
297326 continue ;
298327 }
299328
@@ -338,32 +367,6 @@ function removeSourceMappingURLFromString(code: string): string {
338367 regexAllowed = false ;
339368 }
340369 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 ;
365- }
366- i ++ ;
367370 } else if ( currentState === 'comment_block' ) {
368371 if ( char === '*' && nextChar === '/' ) {
369372 currentState = 'normal' ;
0 commit comments