rdoc_rubocop 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/exe/rdoc-rubocop +1 -1
- data/lib/rdoc_rubocop.rb +1 -3
- data/lib/rdoc_rubocop/file_path.rb +0 -2
- data/lib/rdoc_rubocop/indent_util.rb +19 -0
- data/lib/rdoc_rubocop/lang.rb +7 -0
- data/lib/rdoc_rubocop/lang/base/comment.rb +36 -0
- data/lib/rdoc_rubocop/lang/base/source_file.rb +93 -0
- data/lib/rdoc_rubocop/lang/c.rb +8 -0
- data/lib/rdoc_rubocop/lang/c/comment.rb +58 -0
- data/lib/rdoc_rubocop/lang/c/comment/banner.rb +68 -0
- data/lib/rdoc_rubocop/lang/c/comment/normal.rb +62 -0
- data/lib/rdoc_rubocop/lang/c/comment/one_line.rb +22 -0
- data/lib/rdoc_rubocop/lang/c/comment_extractor.rb +34 -0
- data/lib/rdoc_rubocop/lang/c/corrector.rb +26 -0
- data/lib/rdoc_rubocop/lang/c/source_file.rb +18 -0
- data/lib/rdoc_rubocop/lang/ruby.rb +8 -0
- data/lib/rdoc_rubocop/lang/ruby/comment.rb +52 -0
- data/lib/rdoc_rubocop/lang/ruby/comment_extractor.rb +57 -0
- data/lib/rdoc_rubocop/lang/ruby/corrector.rb +30 -0
- data/lib/rdoc_rubocop/lang/ruby/source_file.rb +18 -0
- data/lib/rdoc_rubocop/lang/ruby/token.rb +43 -0
- data/lib/rdoc_rubocop/lang/ruby/token/comment_token.rb +13 -0
- data/lib/rdoc_rubocop/rdoc.rb +69 -0
- data/lib/rdoc_rubocop/rdoc/line.rb +24 -0
- data/lib/rdoc_rubocop/rdoc/ruby_snippet.rb +62 -0
- data/lib/rdoc_rubocop/rubocop_modifier.rb +0 -2
- data/lib/rdoc_rubocop/rubocop_runner.rb +2 -3
- data/lib/rdoc_rubocop/version.rb +1 -1
- metadata +24 -10
- data/lib/rdoc_rubocop/comment.rb +0 -45
- data/lib/rdoc_rubocop/comment/source_code.rb +0 -28
- data/lib/rdoc_rubocop/comment_extractor.rb +0 -53
- data/lib/rdoc_rubocop/comment_token_organizable.rb +0 -17
- data/lib/rdoc_rubocop/source_file.rb +0 -72
- data/lib/rdoc_rubocop/source_file/corrector.rb +0 -49
- data/lib/rdoc_rubocop/token.rb +0 -39
- data/lib/rdoc_rubocop/token/comment_token.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7269874a91137d88ca85006a12c4648aa3a12c8720265a7cc583409668faa75
|
4
|
+
data.tar.gz: a91582e932953cbd855dfed031e97f25be3ee4d2d35cdc89cb39b941ecf8862f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e169d14f61ccd2dd385281924b23df9ada5307844689a16e2fc4935038933348f30f33724f04b0f01937043f56ad1ffbd377ee77608208c6b88203264fe0f62
|
7
|
+
data.tar.gz: 13e0d4521a81793b26622f2fad729c80fce61be9f0adff35864e025337e50757dc95b9e71a9d164df3a4c944258b4e700b9af9a2574bad30a3a8d96e6553fa23
|
data/README.md
CHANGED
@@ -65,6 +65,8 @@ Or install it yourself as:
|
|
65
65
|
# Correct the code
|
66
66
|
$ rdoc-rubocop -a
|
67
67
|
|
68
|
+
If you do not specify any file, rdoc_rubocop finds for files recursively from current directory. The extension of the target file is used to determine programming language. If it is .rb, it is parsed as Ruby, and if it is .c, it is parsed as C.
|
69
|
+
|
68
70
|
## Development
|
69
71
|
|
70
72
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/exe/rdoc-rubocop
CHANGED
data/lib/rdoc_rubocop.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require "rubocop"
|
2
2
|
require "rdoc_rubocop/version"
|
3
|
-
require "rdoc_rubocop/comment_extractor"
|
4
3
|
require "rdoc_rubocop/options"
|
5
|
-
require "rdoc_rubocop/source_file"
|
6
|
-
require "rdoc_rubocop/rubocop_modifier"
|
7
4
|
require "rdoc_rubocop/rubocop_runner"
|
5
|
+
require "rdoc_rubocop/rubocop_modifier"
|
8
6
|
|
9
7
|
module RDocRuboCop
|
10
8
|
class Error < StandardError; end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RDocRuboCop
|
2
|
+
module IndentUtil
|
3
|
+
refine String do
|
4
|
+
def indent
|
5
|
+
scan(/^ *(?=\S)/).min
|
6
|
+
end
|
7
|
+
|
8
|
+
def strip_indent
|
9
|
+
gsub(/^#{indent}/, "")
|
10
|
+
end
|
11
|
+
|
12
|
+
# Since space indent and tab indent are mixed in Ruby source code,
|
13
|
+
# it is unified with space indent.
|
14
|
+
def expand_tab
|
15
|
+
gsub(/\t/, " ")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "rdoc_rubocop/rdoc"
|
2
|
+
|
3
|
+
module RDocRuboCop
|
4
|
+
module Lang
|
5
|
+
module Base
|
6
|
+
class Comment
|
7
|
+
# def initialize(comment, source_file = nil)
|
8
|
+
# @comment = comment
|
9
|
+
# @source_file = source_file
|
10
|
+
# end
|
11
|
+
|
12
|
+
def rdoc
|
13
|
+
@rdoc ||= RDoc.new(text_without_commentchar)
|
14
|
+
end
|
15
|
+
|
16
|
+
def source_codes
|
17
|
+
@source_codes ||= extract_source_codes
|
18
|
+
end
|
19
|
+
|
20
|
+
# def corrected_text
|
21
|
+
# rdoc.apply
|
22
|
+
# end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def extract_source_codes
|
27
|
+
rdoc.ruby_snippets
|
28
|
+
end
|
29
|
+
|
30
|
+
# def text_without_commentchar
|
31
|
+
# @comment
|
32
|
+
# end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "digest"
|
2
|
+
|
3
|
+
module RDocRuboCop
|
4
|
+
module Lang
|
5
|
+
module Base
|
6
|
+
class SourceFile
|
7
|
+
attr_reader :source
|
8
|
+
attr_reader :filename
|
9
|
+
|
10
|
+
def self.build(filename)
|
11
|
+
klass =
|
12
|
+
case filename
|
13
|
+
when /\.c\z/ then Lang::C::SourceFile
|
14
|
+
when /\.rb\z/ then Lang::Ruby::SourceFile
|
15
|
+
end
|
16
|
+
|
17
|
+
return unless klass
|
18
|
+
|
19
|
+
source = File.open(filename, "r").read
|
20
|
+
klass.new(source, filename)
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(source, filename)
|
24
|
+
@source = source
|
25
|
+
@filename = filename
|
26
|
+
|
27
|
+
reset
|
28
|
+
end
|
29
|
+
|
30
|
+
def source_code_file_paths
|
31
|
+
@source_code_file_paths ||=
|
32
|
+
comments.
|
33
|
+
flat_map(&:source_codes).
|
34
|
+
map { |source_code| source_code.build_file_path(@filename) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def comments
|
38
|
+
@comments ||=
|
39
|
+
begin
|
40
|
+
comment_extractor = comment_extractor_class.new(self)
|
41
|
+
comment_extractor.extract
|
42
|
+
comment_extractor.comments
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# def comment_extractor_class
|
47
|
+
# CommentExtractor
|
48
|
+
# end
|
49
|
+
|
50
|
+
def correct!
|
51
|
+
correct
|
52
|
+
save if changed?
|
53
|
+
reset
|
54
|
+
end
|
55
|
+
|
56
|
+
def correct
|
57
|
+
corrector = corrector_class.new(self)
|
58
|
+
corrector.correct
|
59
|
+
|
60
|
+
@source = corrector.corrected_source
|
61
|
+
end
|
62
|
+
|
63
|
+
# def corrector_class
|
64
|
+
# Corrector
|
65
|
+
# end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def reset
|
70
|
+
@checksum = compute_digest(@source)
|
71
|
+
@source_code_file_paths = nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def compute_digest(str)
|
75
|
+
Digest::MD5.hexdigest(str)
|
76
|
+
end
|
77
|
+
|
78
|
+
def save
|
79
|
+
File.open(@filename, "w") do |f|
|
80
|
+
f.puts @source
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def changed?
|
85
|
+
compute_digest(@source) != @checksum
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
require "rdoc_rubocop/lang/c/source_file"
|
93
|
+
require "rdoc_rubocop/lang/ruby/source_file"
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "rdoc_rubocop/lang/base/comment"
|
2
|
+
|
3
|
+
module RDocRuboCop
|
4
|
+
module Lang
|
5
|
+
module C
|
6
|
+
class Comment < Lang::Base::Comment
|
7
|
+
attr_reader :comment_text
|
8
|
+
attr_reader :source_file
|
9
|
+
attr_reader :offset_begin
|
10
|
+
attr_reader :offset_end
|
11
|
+
|
12
|
+
def self.build(comment_text, source_file, offset_begin, offset_end)
|
13
|
+
body = comment_text.dup
|
14
|
+
first_line = body.slice!(/\A.*\R/)
|
15
|
+
|
16
|
+
klass =
|
17
|
+
if first_line.nil?
|
18
|
+
Comment::OneLine
|
19
|
+
elsif first_line.match?(%r(^/\*[\x21-\x2f\x3a-\x3f])) || !body.match?(/\A *\*/)
|
20
|
+
Comment::Banner
|
21
|
+
else
|
22
|
+
Comment::Normal
|
23
|
+
end
|
24
|
+
|
25
|
+
klass.new(comment_text, source_file, offset_begin, offset_end)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(comment_text, source_file, offset_begin, offset_end)
|
29
|
+
@comment_text = comment_text
|
30
|
+
@source_file = source_file
|
31
|
+
@offset_begin = offset_begin
|
32
|
+
@offset_end = offset_end
|
33
|
+
end
|
34
|
+
|
35
|
+
# def corrected_text
|
36
|
+
# end
|
37
|
+
|
38
|
+
def length
|
39
|
+
@offset_end - @offset_begin
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def text_without_commentchar
|
45
|
+
parse unless @body
|
46
|
+
@body
|
47
|
+
end
|
48
|
+
|
49
|
+
# def parse
|
50
|
+
# end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
require "rdoc_rubocop/lang/c/comment/one_line"
|
57
|
+
require "rdoc_rubocop/lang/c/comment/normal"
|
58
|
+
require "rdoc_rubocop/lang/c/comment/banner"
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "rdoc_rubocop/indent_util"
|
2
|
+
|
3
|
+
using RDocRuboCop::IndentUtil
|
4
|
+
|
5
|
+
module RDocRuboCop
|
6
|
+
module Lang
|
7
|
+
module C
|
8
|
+
class Comment < Lang::Base::Comment
|
9
|
+
#
|
10
|
+
# This class manages comments of the following form:
|
11
|
+
#
|
12
|
+
# /**********************
|
13
|
+
# * Document-class: Foo
|
14
|
+
# *
|
15
|
+
# * code1
|
16
|
+
# * code2
|
17
|
+
# **********************/
|
18
|
+
#
|
19
|
+
# Those whose body is before the column position of the comment
|
20
|
+
# start are also included.
|
21
|
+
#
|
22
|
+
# /*
|
23
|
+
# Document-class: Foo
|
24
|
+
#
|
25
|
+
# code1
|
26
|
+
# code2
|
27
|
+
# */
|
28
|
+
#
|
29
|
+
class Banner < self
|
30
|
+
def corrected_text
|
31
|
+
body = rdoc.apply
|
32
|
+
|
33
|
+
text = body.gsub(/^/, indent).gsub(/ *$/, "")
|
34
|
+
text = "#{@padding_begin_str}#{text}"
|
35
|
+
|
36
|
+
"#{text}#{@end_str}"
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def parse
|
42
|
+
body = @comment_text.expand_tab
|
43
|
+
|
44
|
+
#
|
45
|
+
# /********* <- @padding_begin_str
|
46
|
+
# * comment1 <- first_line
|
47
|
+
# *
|
48
|
+
# * comment2
|
49
|
+
# #********/ <- @end_str
|
50
|
+
#
|
51
|
+
@padding_begin_str = body.slice!(/\A.*\R/)
|
52
|
+
@end_str = body.slice!(%r(^.*\*/\z))
|
53
|
+
|
54
|
+
@indent_before_asterisk = body.scan(/^ *\*/).min || ""
|
55
|
+
@indent_after_asterisk = body.gsub(/^ *\*/, "").indent || ""
|
56
|
+
indent = @indent_before_asterisk + @indent_after_asterisk
|
57
|
+
|
58
|
+
@body = body.gsub(/^.{0,#{indent.length}}/, "")
|
59
|
+
end
|
60
|
+
|
61
|
+
def indent
|
62
|
+
@indent_before_asterisk + @indent_after_asterisk
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "rdoc_rubocop/indent_util"
|
2
|
+
|
3
|
+
using RDocRuboCop::IndentUtil
|
4
|
+
|
5
|
+
module RDocRuboCop
|
6
|
+
module Lang
|
7
|
+
module C
|
8
|
+
class Comment < Lang::Base::Comment
|
9
|
+
#
|
10
|
+
# This class manages comments of the following form:
|
11
|
+
#
|
12
|
+
# /*
|
13
|
+
# * Document-class: Foo
|
14
|
+
# *
|
15
|
+
# * code1
|
16
|
+
# * code2
|
17
|
+
# */
|
18
|
+
#
|
19
|
+
class Normal < self
|
20
|
+
def corrected_text
|
21
|
+
body = rdoc.apply
|
22
|
+
|
23
|
+
first_line = body.slice!(/\A.*\R/)
|
24
|
+
body.gsub!(/^/, indent)
|
25
|
+
|
26
|
+
text = "/*#{@indent_after_asterisk}#{first_line}#{body}".gsub(/ *$/, "")
|
27
|
+
"#{text}#{@end_str}"
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def parse
|
33
|
+
body = @comment_text.expand_tab
|
34
|
+
|
35
|
+
#
|
36
|
+
# /* <- first_line
|
37
|
+
# * comment1
|
38
|
+
# *
|
39
|
+
# * comment2
|
40
|
+
# */ <- @end_str
|
41
|
+
#
|
42
|
+
first_line = body.slice!(/\A.*\R/)
|
43
|
+
@end_str = body.slice!(%r(^.*\*/\z))
|
44
|
+
|
45
|
+
tmp_indent = @end_str[/^ */]
|
46
|
+
body = tmp_indent + "*" + first_line.sub(%r(/\*), "") + body
|
47
|
+
|
48
|
+
@indent_before_asterisk = body.scan(/^ *\*/).min || ""
|
49
|
+
@indent_after_asterisk = body.gsub(/^ *\*/, "").indent || ""
|
50
|
+
indent = @indent_before_asterisk + @indent_after_asterisk
|
51
|
+
|
52
|
+
@body = body.gsub(/^.{0,#{indent.length}}/, "")
|
53
|
+
end
|
54
|
+
|
55
|
+
def indent
|
56
|
+
@indent_before_asterisk + @indent_after_asterisk
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RDocRuboCop
|
2
|
+
module Lang
|
3
|
+
module C
|
4
|
+
class Comment < Lang::Base::Comment
|
5
|
+
class OneLine < self
|
6
|
+
def corrected_text
|
7
|
+
"#{@begin_str}#{rdoc.apply}#{@end_str}"
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def parse
|
13
|
+
@body = @comment_text.dup
|
14
|
+
|
15
|
+
@begin_str = @body.slice!(%r(^/\*\s*))
|
16
|
+
@end_str = @body.slice!(%r(\s*\*/$))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "rdoc_rubocop/lang/c/comment"
|
2
|
+
|
3
|
+
module RDocRuboCop
|
4
|
+
module Lang
|
5
|
+
module C
|
6
|
+
class CommentExtractor
|
7
|
+
attr_reader :comments
|
8
|
+
|
9
|
+
def initialize(source_file)
|
10
|
+
@source_file = source_file
|
11
|
+
@comments = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def extract
|
15
|
+
@comments = extract_comments
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def extract_comments
|
21
|
+
pos = 0
|
22
|
+
comments = []
|
23
|
+
|
24
|
+
while match_data = %r(/\*.*?\*/)m.match(@source_file.source, pos) do
|
25
|
+
comments << Comment.build(match_data[0], self, match_data.begin(0), match_data.end(0))
|
26
|
+
pos = match_data.end(0)
|
27
|
+
end
|
28
|
+
|
29
|
+
comments
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|