no_comments 0.1.9 → 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/.rubocop.yml +1 -1
- 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 +42 -16
- data/lib/no_comments/remover.rb +5 -5
- data/lib/no_comments/version.rb +1 -1
- metadata +4 -8
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/.rubocop.yml
CHANGED
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)
|
@@ -93,17 +100,36 @@ module NoComments
|
|
93
100
|
|
94
101
|
def process_code_line(line)
|
95
102
|
code_part, comment_part = split_line(line)
|
103
|
+
return handle_empty_line(line) if code_part.strip.empty? && comment_part.nil?
|
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)
|
96
106
|
|
97
|
-
|
98
|
-
|
99
|
-
elsif comment_part && tool_comment?(comment_part.strip)
|
100
|
-
@result_lines << ("#{code_part.rstrip} #{comment_part.strip}")
|
101
|
-
else
|
102
|
-
@comments << [@line_number, comment_part.strip] if comment_part
|
103
|
-
return if code_part.strip.empty?
|
107
|
+
handle_regular_code_line(code_part, comment_part)
|
108
|
+
end
|
104
109
|
|
105
|
-
|
106
|
-
|
110
|
+
def keep_doc_comment?(comment_part)
|
111
|
+
comment_part && @keep_doc_comments && documentation_comment?(comment_part.strip)
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def handle_empty_line(line)
|
117
|
+
@result_lines << line.rstrip
|
118
|
+
end
|
119
|
+
|
120
|
+
def handle_tool_comment_line(code_part, comment_part)
|
121
|
+
@result_lines << ("#{code_part.rstrip} #{comment_part.strip}")
|
122
|
+
end
|
123
|
+
|
124
|
+
def handle_doc_comment_line(code_part, comment_part)
|
125
|
+
@result_lines << ("#{code_part.rstrip} #{comment_part.strip}")
|
126
|
+
end
|
127
|
+
|
128
|
+
def handle_regular_code_line(code_part, comment_part)
|
129
|
+
@comments << [@line_number, comment_part.strip] if comment_part
|
130
|
+
return if code_part.strip.empty?
|
131
|
+
|
132
|
+
@result_lines << code_part.rstrip
|
107
133
|
end
|
108
134
|
end
|
109
135
|
end
|
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
metadata
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: no_comments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justyna
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
|
-
description:
|
14
12
|
email:
|
15
13
|
- justine84@gmail.com
|
16
14
|
executables:
|
@@ -38,7 +36,6 @@ metadata:
|
|
38
36
|
source_code_uri: https://github.com/justi/no_comments
|
39
37
|
homepage_uri: https://github.com/justi/no_comments
|
40
38
|
rubygems_mfa_required: 'true'
|
41
|
-
post_install_message:
|
42
39
|
rdoc_options: []
|
43
40
|
require_paths:
|
44
41
|
- lib
|
@@ -46,15 +43,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
43
|
requirements:
|
47
44
|
- - ">="
|
48
45
|
- !ruby/object:Gem::Version
|
49
|
-
version: 3.
|
46
|
+
version: 3.4.4
|
50
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
48
|
requirements:
|
52
49
|
- - ">="
|
53
50
|
- !ruby/object:Gem::Version
|
54
51
|
version: '0'
|
55
52
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
57
|
-
signing_key:
|
53
|
+
rubygems_version: 3.6.7
|
58
54
|
specification_version: 4
|
59
55
|
summary: NoComments is a Ruby gem designed to clean up .rb files by removing unnecessary
|
60
56
|
comments, leaving your code clean and ready for deployment.
|