@@ -217,7 +217,9 @@ const stringifyJSON = (data) => {
217217 */
218218function stripComments ( code ) {
219219 const len = code . length ;
220- let inStr = null ; // "'", '"', or '`'
220+ let inStr = null ; // "'", '"', or '`'
221+ let inRegex = false ;
222+ let regexClass = false ; // inside [...]
221223 let inBlockComment = false ;
222224 let inLineComment = false ;
223225 let out = '' ;
@@ -226,6 +228,7 @@ function stripComments(code) {
226228 while ( i < len ) {
227229 const char = code [ i ] ;
228230 const next = code [ i + 1 ] ;
231+ const prev = code [ i - 1 ] ;
229232
230233 // end of line comment
231234 if ( inLineComment && ( char === '\n' || char === '\r' ) ) {
@@ -247,24 +250,30 @@ function stripComments(code) {
247250 continue ;
248251 }
249252
250- // string start
251- if ( ! inStr && ( char === '"' || char === "'" || char === '`' ) ) {
252- inStr = char ;
253- out += char ;
254- i ++ ;
255- continue ;
256- }
257-
258- // string end (skip escaped)
253+ // inside string
259254 if ( inStr ) {
260255 out += char ;
261256 if ( char === '\\' ) {
262257 out += code [ i + 1 ] ;
263258 i += 2 ;
264259 continue ;
265260 }
266- if ( char === inStr ) {
267- inStr = null ;
261+ if ( char === inStr ) inStr = null ;
262+ i ++ ;
263+ continue ;
264+ }
265+
266+ // inside RegExp
267+ if ( inRegex ) {
268+ out += char ;
269+ if ( regexClass ) {
270+ if ( char === ']' && prev !== '\\' ) regexClass = false ;
271+ } else {
272+ if ( char === '[' && prev !== '\\' ) {
273+ regexClass = true ;
274+ } else if ( char === '/' && prev !== '\\' ) {
275+ inRegex = false ; // flags follow
276+ }
268277 }
269278 i ++ ;
270279 continue ;
@@ -284,6 +293,28 @@ function stripComments(code) {
284293 continue ;
285294 }
286295
296+ // string start
297+ if ( char === '"' || char === "'" || char === '`' ) {
298+ inStr = char ;
299+ out += char ;
300+ i ++ ;
301+ continue ;
302+ }
303+
304+ // RegExp start (простая, но практичная эвристика)
305+ if ( char === '/' && next !== '/' && next !== '*' ) {
306+ let j = i - 1 ;
307+ while ( j >= 0 && / \s / . test ( code [ j ] ) ) j -- ;
308+ const prevSym = j >= 0 ? code [ j ] : '' ;
309+ if ( j < 0 || / [ ( { \[ = : + \- * , ! & | ? ; < > ] / . test ( prevSym ) ) {
310+ inRegex = true ;
311+ regexClass = false ;
312+ out += char ;
313+ i ++ ;
314+ continue ;
315+ }
316+ }
317+
287318 out += char ;
288319 i ++ ;
289320 }
0 commit comments