no_comments 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +34 -0
- data/LICENSE +21 -0
- data/README.md +121 -0
- data/Rakefile +12 -0
- data/exe/no_comments +33 -0
- data/lib/no_comments/version.rb +5 -0
- data/lib/no_comments.rb +76 -0
- data/sig/no_comments.rbs +4 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8f4d0bbabf8d2083a22cb850026add69671c3af79b1d0cb75a0a891e5187aea4
|
4
|
+
data.tar.gz: a8a312eea4658d6da9ac64007d9da4ea94efc642fc0381406e4fbc82b0b84726
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa15e36876ee00b72f23b2043eb661a736df5de5876b14e5e587cb04c99e7af9858d8f030520efac7af0ff8939901571a12f67f72182f87e6e94ae9abc1eb779
|
7
|
+
data.tar.gz: a3aa64ab64f4c7ba06da376b7fe16e027d363b8a22626b5b23e7a5c6d99051939dd23039751ac14ed3f27258abdcf7c5869516c5fdcfc772c0f81c67e1d23478
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
- rubocop-rake
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.2
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Style/StringLiterals:
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/StringLiteralsInInterpolation:
|
13
|
+
EnforcedStyle: double_quotes
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Exclude:
|
17
|
+
- 'spec/no_comments/**/*'
|
18
|
+
|
19
|
+
Style/Documentation:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
RSpec/ExampleLength:
|
23
|
+
Exclude:
|
24
|
+
- 'spec/no_comments/**/*'
|
25
|
+
|
26
|
+
RSpec/MultipleExpectations:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
RSpec/DescribeClass:
|
30
|
+
Exclude:
|
31
|
+
- 'spec/no_comments/no_comments_cli_spec.rb'
|
32
|
+
|
33
|
+
RSpec/NestedGroups:
|
34
|
+
Enabled: false
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Justyna Wojtczak
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# no_comments
|
2
|
+
|
3
|
+
`no_comments` is a simple Ruby gem that removes comments from `.rb` files. It can handle single-line comments and inline comments, leaving your code clean and readable.
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
## Table of Contents
|
8
|
+
|
9
|
+
1. [Installation](#installation)
|
10
|
+
2. [Usage](#usage)
|
11
|
+
3. [Testing](#testing)
|
12
|
+
4. [Contribution](#contribution)
|
13
|
+
5. [License](#license)
|
14
|
+
6. [TODO](#todo)
|
15
|
+
|
16
|
+
---
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
To install `no_comments`, add it to your Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'no_comments'
|
24
|
+
```
|
25
|
+
Then execute:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
bundle install
|
29
|
+
```
|
30
|
+
Or install it yourself using the following command:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
gem install no_comments
|
34
|
+
```
|
35
|
+
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
To clean up comments from a .rb file or directory, use the `NoComments::Remover.clean` method. This will remove all single-line comments and inline comments from the file or directory.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'no_comments'
|
42
|
+
|
43
|
+
NoComments::Remover.clean('path/to/your_file.rb')
|
44
|
+
NoComments::Remover.clean('path/to/your_directory')
|
45
|
+
|
46
|
+
```
|
47
|
+
### Audit Mode
|
48
|
+
|
49
|
+
To use the audit mode, pass the `audit: true` flag to the `clean` method. This will output the file paths and lines containing comments without modifying the files.
|
50
|
+
|
51
|
+
#### Example
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
NoComments::Remover.clean('example.rb', audit: true)
|
55
|
+
```
|
56
|
+
#### Output
|
57
|
+
|
58
|
+
```bash
|
59
|
+
File: example.rb
|
60
|
+
Line 1: # This is a comment
|
61
|
+
Line 3: # Another comment
|
62
|
+
```
|
63
|
+
|
64
|
+
### Command-Line Interface (CLI)
|
65
|
+
|
66
|
+
You can use `no_comments` directly from the command line to clean or audit `.rb` files.
|
67
|
+
|
68
|
+
#### Clean Comments
|
69
|
+
|
70
|
+
To remove comments from a file, use:
|
71
|
+
|
72
|
+
```bash
|
73
|
+
no_comments -p path/to/file.rb
|
74
|
+
no_comments -p path/to/directory
|
75
|
+
```
|
76
|
+
#### Audit mode
|
77
|
+
|
78
|
+
To run `no_comments` in audit mode without modifying files, use the `--audit` flag:
|
79
|
+
|
80
|
+
```bash
|
81
|
+
no_comments -p path/to/file.rb --audit
|
82
|
+
no_comments -p path/to/directory --audit
|
83
|
+
```
|
84
|
+
|
85
|
+
## Testing
|
86
|
+
`no_comments` uses RSpec for testing. To run the tests, first make sure all dependencies are installed:
|
87
|
+
|
88
|
+
```bash
|
89
|
+
bundle install
|
90
|
+
```
|
91
|
+
Then, run the tests with:
|
92
|
+
|
93
|
+
```bash
|
94
|
+
bundle exec rspec
|
95
|
+
```
|
96
|
+
All tests are located in the spec directory and cover the main functionality of the gem, ensuring it properly removes comments from Ruby files.
|
97
|
+
|
98
|
+
## Contribution
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/justi/no_comments.
|
101
|
+
|
102
|
+
To contribute:
|
103
|
+
|
104
|
+
Fork the repository.
|
105
|
+
Create a new branch (git checkout -b feature-branch).
|
106
|
+
Make your changes.
|
107
|
+
Commit your changes (git commit -m 'Add new feature').
|
108
|
+
Push to the branch (git push origin feature-branch).
|
109
|
+
Open a pull request.
|
110
|
+
|
111
|
+
Please ensure that your code follows the existing style and that all tests pass.
|
112
|
+
|
113
|
+
## License
|
114
|
+
The gem is available as open source under the terms of the MIT License.
|
115
|
+
|
116
|
+
|
117
|
+
## TODO
|
118
|
+
- Add support multi-line comments (`=begin`...`=end`)
|
119
|
+
- Add support to magic comments (e.g. `# frozen_string_literal: true`) https://docs.ruby-lang.org/en/3.2/syntax/comments_rdoc.html - thanks [Chris](https://github.com/khasinski)!
|
120
|
+
- Option to keep documentation comments (e.g. `# @param`) https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Documentation
|
121
|
+
- Option to clean all files in a directory except for a specified file
|
data/Rakefile
ADDED
data/exe/no_comments
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "optparse"
|
5
|
+
require "no_comments"
|
6
|
+
|
7
|
+
options = {}
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: no_comments [options]"
|
10
|
+
|
11
|
+
opts.on("-p", "--path PATH", "Path to a .rb file or directory") do |path|
|
12
|
+
options[:path] = path
|
13
|
+
end
|
14
|
+
|
15
|
+
opts.on("--audit", "Run in audit mode to display comments without modifying files") do
|
16
|
+
options[:audit] = true
|
17
|
+
end
|
18
|
+
end.parse!
|
19
|
+
|
20
|
+
if options[:path]
|
21
|
+
begin
|
22
|
+
NoComments::Remover.clean(options[:path], audit: options[:audit])
|
23
|
+
if options[:audit]
|
24
|
+
puts "Audit completed successfully."
|
25
|
+
else
|
26
|
+
puts "Cleaning completed successfully."
|
27
|
+
end
|
28
|
+
rescue StandardError => e
|
29
|
+
puts "Error: #{e.message}"
|
30
|
+
end
|
31
|
+
else
|
32
|
+
puts "Please provide a file or directory with -p."
|
33
|
+
end
|
data/lib/no_comments.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "no_comments/version"
|
4
|
+
require "ripper"
|
5
|
+
|
6
|
+
module NoComments
|
7
|
+
class Remover
|
8
|
+
def self.clean(file_path, audit: false)
|
9
|
+
if File.directory?(file_path)
|
10
|
+
Dir.glob("#{file_path}/**/*.rb").each do |file|
|
11
|
+
process_file(file, audit: audit)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
process_file(file_path, audit: audit)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.process_file(file_path, audit: false)
|
19
|
+
validate_file_extension(file_path)
|
20
|
+
file_content = File.read(file_path)
|
21
|
+
|
22
|
+
if audit
|
23
|
+
audit_comments(file_path, file_content)
|
24
|
+
else
|
25
|
+
content_cleaned = remove_comments(file_content)
|
26
|
+
File.write(file_path, content_cleaned)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.validate_file_extension(file_path)
|
31
|
+
raise "Only Ruby files are supported" unless file_path.end_with?(".rb")
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.audit_comments(file_path, content)
|
35
|
+
comments = extract_comments(content)
|
36
|
+
puts "File: #{file_path}" unless comments.empty?
|
37
|
+
|
38
|
+
comments.each do |(pos, _, tok, _)|
|
39
|
+
line, = pos
|
40
|
+
puts " Line #{line}: #{tok.strip}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.remove_comments(content)
|
45
|
+
comments = extract_comments(content)
|
46
|
+
content = process_comments(content, comments)
|
47
|
+
remove_empty_lines(content)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.extract_comments(content)
|
51
|
+
Ripper.lex(content).select { |_pos, type, _tok, _| type == :on_comment }
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.process_comments(content, comments)
|
55
|
+
comments.sort_by { |(pos, _, _, _)| [pos[0], pos[1]] }.reverse.each do |(pos, _, _, _)|
|
56
|
+
line, col = pos
|
57
|
+
lines = content.lines
|
58
|
+
lines[line - 1] = process_comment_line(lines[line - 1], col)
|
59
|
+
content = lines.join
|
60
|
+
end
|
61
|
+
content
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.process_comment_line(line, col)
|
65
|
+
if line[col..].strip.start_with?("#")
|
66
|
+
"#{line[0...col].rstrip}\n"
|
67
|
+
else
|
68
|
+
"\n"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.remove_empty_lines(content)
|
73
|
+
content.gsub(/^\s*$\n/, "")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/sig/no_comments.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: no_comments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justyna
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- justine84@gmail.com
|
16
|
+
executables:
|
17
|
+
- no_comments
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- exe/no_comments
|
27
|
+
- lib/no_comments.rb
|
28
|
+
- lib/no_comments/version.rb
|
29
|
+
- sig/no_comments.rbs
|
30
|
+
homepage:
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
metadata:
|
34
|
+
rubygems_mfa_required: 'true'
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.2.4
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.4.19
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Remove comments from Ruby files
|
54
|
+
test_files: []
|