uncomment 3.0.1

A CLI tool to remove comments from code using tree-sitter for accurate parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::fs;
use std::process::Command;
use tempfile::TempDir;

#[test]
fn test_gitignore_from_subdirectory() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let subfolder = root.join("subfolder");
    let next_folder = subfolder.join(".next");

    fs::create_dir(&subfolder).unwrap();
    fs::create_dir(&next_folder).unwrap();

    fs::write(root.join(".gitignore"), ".next\n").unwrap();

    fs::write(
        subfolder.join("main.js"),
        "// Main file comment\nconst x = 1;",
    )
    .unwrap();
    fs::write(
        next_folder.join("test.js"),
        "// Test file comment\nconst y = 2;",
    )
    .unwrap();

    Command::new("git")
        .current_dir(root)
        .args(["init"])
        .output()
        .unwrap();

    let uncomment_path = std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("uncomment");

    let output = Command::new(&uncomment_path)
        .current_dir(&subfolder)
        .args([".", "--dry-run"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    if !output.status.success() {
        panic!("Command failed with stderr: {}", stderr);
    }

    assert!(
        stdout.contains("1 files processed"),
        "Expected to process only 1 file, but got: {}",
        stdout
    );
    assert!(
        stdout.contains("main.js"),
        "Expected to process main.js, but got: {}",
        stdout
    );
    assert!(
        !stdout.contains("test.js"),
        "Should not process test.js in .next folder, but got: {}",
        stdout
    );
}

#[test]
fn test_gitignore_with_no_gitignore_flag() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let subfolder = root.join("subfolder");
    let next_folder = subfolder.join(".next");

    fs::create_dir(&subfolder).unwrap();
    fs::create_dir(&next_folder).unwrap();

    fs::write(root.join(".gitignore"), ".next\n").unwrap();

    fs::write(
        subfolder.join("main.js"),
        "// Main file comment\nconst x = 1;",
    )
    .unwrap();
    fs::write(
        next_folder.join("test.js"),
        "// Test file comment\nconst y = 2;",
    )
    .unwrap();

    Command::new("git")
        .current_dir(root)
        .args(["init"])
        .output()
        .unwrap();

    let uncomment_path = std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("uncomment");

    let output = Command::new(&uncomment_path)
        .current_dir(&subfolder)
        .args([".", "--dry-run", "--no-gitignore"])
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("2 files processed"),
        "Expected to process 2 files with --no-gitignore, but got: {}",
        stdout
    );
}

#[test]
fn test_config_init_command() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["init", "--output", "test_config.toml"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Init command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let config_path = root.join("test_config.toml");
    assert!(config_path.exists(), "Config file was not created");

    let config_content = fs::read_to_string(&config_path).unwrap();
    assert!(config_content.contains("[global]"));
    assert!(config_content.contains("remove_todos = false"));
    assert!(config_content.contains("[languages.python]"));
    assert!(config_content.contains("[patterns."));
}

#[test]
fn test_config_basic_functionality() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create a simple config that removes TODO comments
    let config_content = r#"
[global]
remove_todos = true
remove_fixme = false
remove_docs = false
preserve_patterns = ["KEEP"]
"#;

    fs::write(root.join(".uncommentrc.toml"), config_content).unwrap();

    let test_file = root.join("test.py");
    let test_content = r#"# Header comment
# TODO: should be removed
# FIXME: should be preserved
# KEEP: should be preserved
# Regular comment
def hello():
    pass"#;

    fs::write(&test_file, test_content).unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["test.py"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let result_content = fs::read_to_string(&test_file).unwrap();

    // TODO should be removed (due to config)
    assert!(!result_content.contains("TODO: should be removed"));

    // FIXME should be preserved (due to config)
    assert!(result_content.contains("FIXME: should be preserved"));

    assert!(result_content.contains("KEEP: should be preserved"));

    assert!(!result_content.contains("# Header comment"));
    assert!(!result_content.contains("# Regular comment"));
}

#[test]
fn test_nested_configuration() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create root config - preserves TODOs
    let root_config = r#"
[global]
remove_todos = false
remove_fixme = false
"#;
    fs::write(root.join(".uncommentrc.toml"), root_config).unwrap();

    let subdir = root.join("subdir");
    fs::create_dir(&subdir).unwrap();

    // Create subdirectory config - removes TODOs
    let sub_config = r#"
[global]
remove_todos = true
remove_fixme = false
"#;
    fs::write(subdir.join(".uncommentrc.toml"), sub_config).unwrap();

    let root_file = root.join("root_test.py");
    let sub_file = subdir.join("sub_test.py");

    let test_content = "# TODO: test comment\n# FIXME: test comment\ndef hello(): pass";
    fs::write(&root_file, test_content).unwrap();
    fs::write(&sub_file, test_content).unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["root_test.py", "subdir/sub_test.py"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Check root file - TODO should be preserved
    let root_result = fs::read_to_string(&root_file).unwrap();
    assert!(
        root_result.contains("TODO: test comment"),
        "Root file should preserve TODO"
    );
    assert!(
        root_result.contains("FIXME: test comment"),
        "Root file should preserve FIXME"
    );

    // Check sub file - TODO should be removed, FIXME preserved
    let sub_result = fs::read_to_string(&sub_file).unwrap();
    assert!(
        !sub_result.contains("TODO: test comment"),
        "Sub file should remove TODO"
    );
    assert!(
        sub_result.contains("FIXME: test comment"),
        "Sub file should preserve FIXME"
    );
}

#[test]
fn test_language_specific_configuration() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let config_content = r#"
[global]
remove_todos = false
remove_docs = false

[languages.python]
name = "Python"
extensions = [".py"]
comment_nodes = ["comment"]
doc_comment_nodes = ["string"]
preserve_patterns = ["mypy:", "type:"]
remove_docs = true

[languages.javascript]
name = "JavaScript"
extensions = [".js"]
comment_nodes = ["comment"]
preserve_patterns = ["@ts-ignore"]
"#;

    fs::write(root.join(".uncommentrc.toml"), config_content).unwrap();

    let py_file = root.join("test.py");
    let py_content = r#""""This is a docstring"""
# TODO: regular todo
# mypy: ignore
def hello(): pass"#;
    fs::write(&py_file, py_content).unwrap();

    let js_file = root.join("test.js");
    let js_content = r#"/**
 * This is a JSDoc comment
 */
// TODO: regular todo
// @ts-ignore
const x = 1;"#;
    fs::write(&js_file, js_content).unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["test.py", "test.js"])
        .output()
        .unwrap();

    if !output.status.success() {
        eprintln!("Command failed with exit code: {:?}", output.status.code());
        eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
        eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
    }
    assert!(
        output.status.success(),
        "Command failed: stderr={}, stdout={}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout)
    );

    let py_result = fs::read_to_string(&py_file).unwrap();
    assert!(
        !py_result.contains("This is a docstring"),
        "Python docstring should be removed"
    );
    assert!(
        py_result.contains("TODO: regular todo"),
        "Python TODO should be preserved (global setting)"
    );
    assert!(
        py_result.contains("mypy: ignore"),
        "Python mypy comment should be preserved"
    );

    // Check JavaScript file - JSDoc should be preserved, @ts-ignore preserved
    let js_result = fs::read_to_string(&js_file).unwrap();
    assert!(
        js_result.contains("This is a JSDoc comment"),
        "JavaScript JSDoc should be preserved (global setting)"
    );
    assert!(
        js_result.contains("TODO: regular todo"),
        "JavaScript TODO should be preserved (global setting)"
    );
    assert!(
        js_result.contains("@ts-ignore"),
        "JavaScript @ts-ignore should be preserved"
    );
}

#[test]
fn test_custom_config_file_path() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let custom_config = r#"
[global]
remove_todos = true
preserve_patterns = ["CUSTOM"]
"#;

    let custom_config_path = root.join("my_custom_config.toml");
    fs::write(&custom_config_path, custom_config).unwrap();

    let test_file = root.join("test.py");
    let test_content =
        "# TODO: should be removed\n# CUSTOM: should be preserved\ndef hello(): pass";
    fs::write(&test_file, test_content).unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["--config", "my_custom_config.toml", "test.py"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let result = fs::read_to_string(&test_file).unwrap();
    assert!(
        !result.contains("TODO: should be removed"),
        "TODO should be removed"
    );
    assert!(
        result.contains("CUSTOM: should be preserved"),
        "CUSTOM pattern should be preserved"
    );
}

#[test]
fn test_pattern_based_configuration() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let config_content = r#"
[global]
remove_todos = false
remove_docs = false

[patterns."test_*.py"]
remove_todos = true
remove_docs = true

[patterns."src/*.py"]
preserve_patterns = ["PRODUCTION"]
"#;

    fs::write(root.join(".uncommentrc.toml"), config_content).unwrap();

    fs::create_dir(root.join("src")).unwrap();

    let test_file = root.join("test_example.py");
    let src_file = root.join("src").join("main.py");
    let regular_file = root.join("regular.py");

    let file_content = r#"""
Docstring
"""
# TODO: todo comment
# PRODUCTION: prod comment
def hello(): pass"#;

    fs::write(&test_file, file_content).unwrap();
    fs::write(&src_file, file_content).unwrap();
    fs::write(&regular_file, file_content).unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["test_example.py", "src/main.py", "regular.py"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let test_result = fs::read_to_string(&test_file).unwrap();
    let src_result = fs::read_to_string(&src_file).unwrap();
    let regular_result = fs::read_to_string(&regular_file).unwrap();

    // All should preserve TODO and docs due to global config
    assert!(test_result.contains("TODO: todo comment"));
    assert!(test_result.contains("Docstring"));
    assert!(src_result.contains("TODO: todo comment"));
    assert!(src_result.contains("Docstring"));
    assert!(regular_result.contains("TODO: todo comment"));
    assert!(regular_result.contains("Docstring"));
}

#[test]
fn test_config_validation_errors() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let invalid_config = r#"
[global]
remove_todos = "invalid_boolean_value"  # Should fail TOML parsing
invalid_syntax_here
"#;

    fs::write(root.join(".uncommentrc.toml"), invalid_config).unwrap();

    fs::write(root.join("test.py"), "# comment\ndef hello(): pass").unwrap();

    let uncomment_path = get_binary_path();

    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["--config", ".uncommentrc.toml", "test.py"])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "Command should fail with invalid config"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Failed to parse config")
            || stderr.contains("TOML parse error")
            || stderr.contains("invalid type"),
        "Should show validation error, got: {}",
        stderr
    );
}

#[test]
fn test_config_override_cli_options() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create config that preserves TODOs
    let config_content = r#"
[global]
remove_todos = false
"#;

    fs::write(root.join(".uncommentrc.toml"), config_content).unwrap();

    let test_file = root.join("test.py");
    fs::write(&test_file, "# TODO: test comment\ndef hello(): pass").unwrap();

    let uncomment_path = get_binary_path();

    // Run with CLI flag that would remove TODOs (CLI should override config in future)
    let output = Command::new(&uncomment_path)
        .current_dir(root)
        .args(["--remove-todo", "test.py"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // CLI --remove-todo overrides config, so TODO should be removed
    let result = fs::read_to_string(&test_file).unwrap();
    assert!(
        !result.contains("# TODO: test comment"),
        "CLI --remove-todo should override config and remove TODO comments"
    );
    assert!(
        result.contains("def hello(): pass"),
        "Code should be preserved"
    );
}

fn get_binary_path() -> std::path::PathBuf {
    std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("uncomment")
}