tz-clean 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +62 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -185,6 +185,66 @@ async function run() {
185
185
  }
186
186
  }
187
187
 
188
+ /**
189
+ * Strips all comments from a JS/TS source string without touching string literals or regex.
190
+ * Handles single-line (//), block (/* *\/), and preserves URLs inside strings like 'http://...'.
191
+ * @param {string} source - The source code string.
192
+ * @returns {string} The source with all comments removed.
193
+ */
194
+ function stripCommentsFromSource(source) {
195
+ let result = '';
196
+ let i = 0;
197
+ const len = source.length;
198
+
199
+ while (i < len) {
200
+ const ch = source[i];
201
+
202
+ // String literals: single, double, or template
203
+ if (ch === '"' || ch === "'" || ch === '`') {
204
+ const quote = ch;
205
+ result += ch;
206
+ i++;
207
+ while (i < len) {
208
+ const sc = source[i];
209
+ if (sc === '\\') {
210
+ result += sc + (source[i + 1] || '');
211
+ i += 2;
212
+ continue;
213
+ }
214
+ result += sc;
215
+ i++;
216
+ if (sc === quote) break;
217
+ }
218
+ continue;
219
+ }
220
+
221
+ // Block comment: /* ... */
222
+ if (ch === '/' && source[i + 1] === '*') {
223
+ i += 2;
224
+ while (i < len) {
225
+ if (source[i] === '*' && source[i + 1] === '/') {
226
+ i += 2;
227
+ break;
228
+ }
229
+ i++;
230
+ }
231
+ continue;
232
+ }
233
+
234
+ // Single-line comment: // ...
235
+ if (ch === '/' && source[i + 1] === '/') {
236
+ i += 2;
237
+ while (i < len && source[i] !== '\n') i++;
238
+ continue;
239
+ }
240
+
241
+ result += ch;
242
+ i++;
243
+ }
244
+
245
+ return result;
246
+ }
247
+
188
248
  /**
189
249
  * Strips all comments and excess blank lines from JS/TS files in the target paths.
190
250
  * Removes single-line comments (//), block comments (/* *\/), eslint-disable directives,
@@ -223,11 +283,8 @@ function stripCommentsAndEmptyLines(includePaths = [], excludePaths = []) {
223
283
  const original = fs.readFileSync(fullPath, 'utf-8');
224
284
  let cleaned = original;
225
285
 
226
- // Remove block comments /** */ and /* */ (but not shebang)
227
- cleaned = cleaned.replace(/\/\*[\s\S]*?\*\//g, '');
228
-
229
- // Remove single-line comments // (including eslint-disable)
230
- cleaned = cleaned.replace(/[ \t]*\/\/.*$/gm, '');
286
+ // Use parser-aware comment stripper (safe for URLs in strings)
287
+ cleaned = stripCommentsFromSource(cleaned);
231
288
 
232
289
  // Collapse multiple blank lines into one
233
290
  cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tz-clean",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {