no_comments 0.1.10 → 0.1.11
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.
- checksums.yaml +4 -4
- data/README.md +7 -4
- data/exe/no_comments +5 -1
- data/lib/no_comments/comment_detector.rb +5 -0
- data/lib/no_comments/content_processor.rb +23 -7
- data/lib/no_comments/remover.rb +5 -5
- data/lib/no_comments/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c98d97eb96221297ef0ef1b6140abd04ac9a54ce759fdfd46b0708497b223022
|
4
|
+
data.tar.gz: 42a109143ae3b1ab99a176e625a74dbe350d878838eb40b93c5f483f13428f11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82418111acf7d4de92b39c52cd6bafb32959b1ae024836d53a7be3a2b66257cd13299e4b179f84506012debc71db3c48bcbade705e6df0cb18b7a83fa09cc550
|
7
|
+
data.tar.gz: 01df35d37bc0e4200beb1c15c6b4f2846d2904faa681d20706e218681999e7f4e946ef7df0eab2ede035f415b095d784d06e37ffed15062dc5cbae206e52d510
|
data/README.md
CHANGED
@@ -13,6 +13,7 @@ It preserves:
|
|
13
13
|
- Shebangs (e.g., #!/usr/bin/env ruby)
|
14
14
|
- Magic comments (e.g., # frozen_string_literal: true)
|
15
15
|
- Tool-defined comments (e.g., # rubocop:disable all)
|
16
|
+
- Documentation comments (e.g., # @param id)
|
16
17
|
|
17
18
|
## Table of Contents
|
18
19
|
1. [When to Use This Gem](#When-to-Use-This-Gem)
|
@@ -112,6 +113,12 @@ no_comments -p path/to/file.rb --audit
|
|
112
113
|
no_comments -p path/to/directory --audit
|
113
114
|
```
|
114
115
|
|
116
|
+
#### Preserve Documentation Comments
|
117
|
+
```bash
|
118
|
+
no_comments -p path/to/file.rb --keep-doc-comments
|
119
|
+
no_comments -p path/to/directory --keep-doc-comments
|
120
|
+
```
|
121
|
+
|
115
122
|
## Testing
|
116
123
|
|
117
124
|
This gem uses RSpec for testing. To run tests:
|
@@ -152,10 +159,6 @@ Please ensure your code follows the existing style and that all tests pass befor
|
|
152
159
|
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
153
160
|
|
154
161
|
## TODO
|
155
|
-
Planned features for future updates:
|
156
|
-
- **Option to Keep Documentation Comments:**
|
157
|
-
- Preserve comments like # @param or # @return for tools like YARD.
|
158
|
-
Reference: [RuboCop documentation cop](https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Documentation).
|
159
162
|
- **Selective Cleaning:**
|
160
163
|
- Allow users to clean all files in a directory except for specified ones.
|
161
164
|
|
data/exe/no_comments
CHANGED
@@ -15,11 +15,15 @@ OptionParser.new do |opts|
|
|
15
15
|
opts.on("--audit", "Run in audit mode to display comments without modifying files") do
|
16
16
|
options[:audit] = true
|
17
17
|
end
|
18
|
+
|
19
|
+
opts.on("-d", "--keep-doc-comments", "Preserve documentation comments like @param") do
|
20
|
+
options[:keep_doc_comments] = true
|
21
|
+
end
|
18
22
|
end.parse!
|
19
23
|
|
20
24
|
if options[:path]
|
21
25
|
begin
|
22
|
-
NoComments::Remover.clean(options[:path], audit: options[:audit])
|
26
|
+
NoComments::Remover.clean(options[:path], audit: options[:audit], keep_doc_comments: options[:keep_doc_comments])
|
23
27
|
if options[:audit]
|
24
28
|
puts "Audit completed successfully."
|
25
29
|
else
|
@@ -4,6 +4,7 @@ module NoComments
|
|
4
4
|
module CommentDetector
|
5
5
|
MAGIC_COMMENT_REGEX = /\A#.*\b(?:frozen_string_literal|encoding|coding|warn_indent|fileencoding)\b.*\z/
|
6
6
|
TOOL_COMMENT_REGEX = /\A#\s*(?:rubocop|reek|simplecov|coveralls|pry|byebug|noinspection|sorbet|type)\b/
|
7
|
+
DOC_COMMENT_REGEX = /\A#\s*@\w+/
|
7
8
|
def magic_comment?(stripped_line)
|
8
9
|
stripped_line.match?(MAGIC_COMMENT_REGEX)
|
9
10
|
end
|
@@ -11,5 +12,9 @@ module NoComments
|
|
11
12
|
def tool_comment?(stripped_line)
|
12
13
|
stripped_line.match?(TOOL_COMMENT_REGEX)
|
13
14
|
end
|
15
|
+
|
16
|
+
def documentation_comment?(stripped_line)
|
17
|
+
stripped_line.match?(DOC_COMMENT_REGEX)
|
18
|
+
end
|
14
19
|
end
|
15
20
|
end
|
@@ -7,7 +7,7 @@ module NoComments
|
|
7
7
|
class ContentProcessor
|
8
8
|
include CommentDetector
|
9
9
|
include LineParser
|
10
|
-
def initialize
|
10
|
+
def initialize(keep_doc_comments: false)
|
11
11
|
@comments = []
|
12
12
|
@result_lines = []
|
13
13
|
@in_multiline_comment = false
|
@@ -15,6 +15,7 @@ module NoComments
|
|
15
15
|
@heredoc_delimiter = nil
|
16
16
|
@line_number = 0
|
17
17
|
@code_started = false
|
18
|
+
@keep_doc_comments = keep_doc_comments
|
18
19
|
end
|
19
20
|
|
20
21
|
def process(content)
|
@@ -45,17 +46,21 @@ module NoComments
|
|
45
46
|
if stripped_line.empty? || stripped_line.start_with?("#!") || magic_comment?(stripped_line)
|
46
47
|
@result_lines << line.rstrip
|
47
48
|
elsif stripped_line.start_with?("#")
|
48
|
-
|
49
|
-
@result_lines << line.rstrip
|
50
|
-
else
|
51
|
-
@comments << [@line_number, stripped_line]
|
52
|
-
end
|
49
|
+
handle_initial_comment_line(line, stripped_line)
|
53
50
|
else
|
54
51
|
@code_started = true
|
55
52
|
handle_regular_line(line, stripped_line)
|
56
53
|
end
|
57
54
|
end
|
58
55
|
|
56
|
+
def handle_initial_comment_line(line, stripped_line)
|
57
|
+
if tool_comment?(stripped_line) || (@keep_doc_comments && documentation_comment?(stripped_line))
|
58
|
+
@result_lines << line.rstrip
|
59
|
+
else
|
60
|
+
@comments << [@line_number, stripped_line]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
59
64
|
def handle_multiline_comment(stripped_line)
|
60
65
|
@comments << [@line_number, stripped_line]
|
61
66
|
@in_multiline_comment = false if stripped_line == "=end"
|
@@ -73,7 +78,9 @@ module NoComments
|
|
73
78
|
start_multiline_comment(stripped_line)
|
74
79
|
elsif (heredoc_start = detect_heredoc_start(line))
|
75
80
|
start_heredoc(line, heredoc_start)
|
76
|
-
elsif stripped_line.start_with?("#") &&
|
81
|
+
elsif stripped_line.start_with?("#") &&
|
82
|
+
(tool_comment?(stripped_line) ||
|
83
|
+
(@keep_doc_comments && documentation_comment?(stripped_line)))
|
77
84
|
@result_lines << line.rstrip
|
78
85
|
else
|
79
86
|
process_code_line(line)
|
@@ -95,10 +102,15 @@ module NoComments
|
|
95
102
|
code_part, comment_part = split_line(line)
|
96
103
|
return handle_empty_line(line) if code_part.strip.empty? && comment_part.nil?
|
97
104
|
return handle_tool_comment_line(code_part, comment_part) if comment_part && tool_comment?(comment_part.strip)
|
105
|
+
return handle_doc_comment_line(code_part, comment_part) if keep_doc_comment?(comment_part)
|
98
106
|
|
99
107
|
handle_regular_code_line(code_part, comment_part)
|
100
108
|
end
|
101
109
|
|
110
|
+
def keep_doc_comment?(comment_part)
|
111
|
+
comment_part && @keep_doc_comments && documentation_comment?(comment_part.strip)
|
112
|
+
end
|
113
|
+
|
102
114
|
private
|
103
115
|
|
104
116
|
def handle_empty_line(line)
|
@@ -109,6 +121,10 @@ module NoComments
|
|
109
121
|
@result_lines << ("#{code_part.rstrip} #{comment_part.strip}")
|
110
122
|
end
|
111
123
|
|
124
|
+
def handle_doc_comment_line(code_part, comment_part)
|
125
|
+
@result_lines << ("#{code_part.rstrip} #{comment_part.strip}")
|
126
|
+
end
|
127
|
+
|
112
128
|
def handle_regular_code_line(code_part, comment_part)
|
113
129
|
@comments << [@line_number, comment_part.strip] if comment_part
|
114
130
|
return if code_part.strip.empty?
|
data/lib/no_comments/remover.rb
CHANGED
@@ -4,20 +4,20 @@ require "no_comments/version"
|
|
4
4
|
require "no_comments/content_processor"
|
5
5
|
module NoComments
|
6
6
|
class Remover
|
7
|
-
def self.clean(file_path, audit: false)
|
7
|
+
def self.clean(file_path, audit: false, keep_doc_comments: false)
|
8
8
|
if File.directory?(file_path)
|
9
9
|
Dir.glob("#{file_path}/**/*.rb").each do |file|
|
10
|
-
process_file(file, audit: audit)
|
10
|
+
process_file(file, audit: audit, keep_doc_comments: keep_doc_comments)
|
11
11
|
end
|
12
12
|
else
|
13
|
-
process_file(file_path, audit: audit)
|
13
|
+
process_file(file_path, audit: audit, keep_doc_comments: keep_doc_comments)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def self.process_file(file_path, audit: false)
|
17
|
+
def self.process_file(file_path, audit: false, keep_doc_comments: false)
|
18
18
|
validate_file_extension(file_path)
|
19
19
|
content = File.read(file_path)
|
20
|
-
processor = ContentProcessor.new
|
20
|
+
processor = ContentProcessor.new(keep_doc_comments: keep_doc_comments)
|
21
21
|
cleaned_content, comments = processor.process(content)
|
22
22
|
if audit
|
23
23
|
print_audit(file_path, comments)
|
data/lib/no_comments/version.rb
CHANGED