@@ -203,13 +203,10 @@ const stringifyJSON = (data) => {
203203} ;
204204
205205/**
206- * Removes all JavaScript comments (single-line and multi-line)
207- * from the given source code, while preserving string and template literals .
206+ * Removes all JavaScript comments (single-line and multi-line) from code,
207+ * while preserving strings containing comment-like patterns .
208208 *
209- * This function avoids removing comment-like patterns inside string literals
210- * (single quotes, double quotes, or backticks), and skips escaped characters.
211- *
212- * @param {string } code The JavaScript source code to clean.
209+ * @param {string } code The source code.
213210 * @returns {string } The code with all comments removed.
214211 *
215212 * @example
@@ -219,12 +216,12 @@ const stringifyJSON = (data) => {
219216 * // => "const x = 'text // not a comment'; "
220217 */
221218function stripComments ( code ) {
222- let out = '' ;
223- let i = 0 ;
224219 const len = code . length ;
225220 let inStr = null ; // "'", '"', or '`'
226221 let inBlockComment = false ;
227222 let inLineComment = false ;
223+ let out = '' ;
224+ let i = 0 ;
228225
229226 while ( i < len ) {
230227 const char = code [ i ] ;
@@ -250,15 +247,15 @@ function stripComments(code) {
250247 continue ;
251248 }
252249
253- // handle string start
250+ // string start
254251 if ( ! inStr && ( char === '"' || char === "'" || char === '`' ) ) {
255252 inStr = char ;
256253 out += char ;
257254 i ++ ;
258255 continue ;
259256 }
260257
261- // handle string end (skip escaped)
258+ // string end (skip escaped)
262259 if ( inStr ) {
263260 out += char ;
264261 if ( char === '\\' ) {
@@ -273,14 +270,14 @@ function stripComments(code) {
273270 continue ;
274271 }
275272
276- // handle line comment start
273+ // line comment start
277274 if ( char === '/' && next === '/' ) {
278275 inLineComment = true ;
279276 i += 2 ;
280277 continue ;
281278 }
282279
283- // handle block comment start
280+ // block comment start
284281 if ( char === '/' && next === '*' ) {
285282 inBlockComment = true ;
286283 i += 2 ;
@@ -295,7 +292,7 @@ function stripComments(code) {
295292}
296293
297294/**
298- * Stringify any JavaScript function and remove all comments .
295+ * Stringify any JavaScript function.
299296 *
300297 * @param {Function } fn - The function to stringify.
301298 * @returns {string|null } The cleaned stringified function or null if not a function or native.
0 commit comments