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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/exe/rdoc-rubocop +1 -1
  4. data/lib/rdoc_rubocop.rb +1 -3
  5. data/lib/rdoc_rubocop/file_path.rb +0 -2
  6. data/lib/rdoc_rubocop/indent_util.rb +19 -0
  7. data/lib/rdoc_rubocop/lang.rb +7 -0
  8. data/lib/rdoc_rubocop/lang/base/comment.rb +36 -0
  9. data/lib/rdoc_rubocop/lang/base/source_file.rb +93 -0
  10. data/lib/rdoc_rubocop/lang/c.rb +8 -0
  11. data/lib/rdoc_rubocop/lang/c/comment.rb +58 -0
  12. data/lib/rdoc_rubocop/lang/c/comment/banner.rb +68 -0
  13. data/lib/rdoc_rubocop/lang/c/comment/normal.rb +62 -0
  14. data/lib/rdoc_rubocop/lang/c/comment/one_line.rb +22 -0
  15. data/lib/rdoc_rubocop/lang/c/comment_extractor.rb +34 -0
  16. data/lib/rdoc_rubocop/lang/c/corrector.rb +26 -0
  17. data/lib/rdoc_rubocop/lang/c/source_file.rb +18 -0
  18. data/lib/rdoc_rubocop/lang/ruby.rb +8 -0
  19. data/lib/rdoc_rubocop/lang/ruby/comment.rb +52 -0
  20. data/lib/rdoc_rubocop/lang/ruby/comment_extractor.rb +57 -0
  21. data/lib/rdoc_rubocop/lang/ruby/corrector.rb +30 -0
  22. data/lib/rdoc_rubocop/lang/ruby/source_file.rb +18 -0
  23. data/lib/rdoc_rubocop/lang/ruby/token.rb +43 -0
  24. data/lib/rdoc_rubocop/lang/ruby/token/comment_token.rb +13 -0
  25. data/lib/rdoc_rubocop/rdoc.rb +69 -0
  26. data/lib/rdoc_rubocop/rdoc/line.rb +24 -0
  27. data/lib/rdoc_rubocop/rdoc/ruby_snippet.rb +62 -0
  28. data/lib/rdoc_rubocop/rubocop_modifier.rb +0 -2
  29. data/lib/rdoc_rubocop/rubocop_runner.rb +2 -3
  30. data/lib/rdoc_rubocop/version.rb +1 -1
  31. metadata +24 -10
  32. data/lib/rdoc_rubocop/comment.rb +0 -45
  33. data/lib/rdoc_rubocop/comment/source_code.rb +0 -28
  34. data/lib/rdoc_rubocop/comment_extractor.rb +0 -53
  35. data/lib/rdoc_rubocop/comment_token_organizable.rb +0 -17
  36. data/lib/rdoc_rubocop/source_file.rb +0 -72
  37. data/lib/rdoc_rubocop/source_file/corrector.rb +0 -49
  38. data/lib/rdoc_rubocop/token.rb +0 -39
  39. data/lib/rdoc_rubocop/token/comment_token.rb +0 -34
@@ -0,0 +1,26 @@
1
+ module RDocRuboCop
2
+ module Lang
3
+ module C
4
+ class Corrector
5
+ attr_reader :corrected_source
6
+
7
+ def initialize(source_file)
8
+ @source_file = source_file
9
+ @corrected_source = nil
10
+ end
11
+
12
+ def correct
13
+ @corrected_source = @source_file.source.dup
14
+
15
+ @source_file.comments.reverse_each do |comment|
16
+ next if comment.source_codes.empty?
17
+
18
+ index = comment.offset_begin
19
+ length = comment.length
20
+ @corrected_source[index, length] = comment.corrected_text
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require "rdoc_rubocop/lang/c/comment_extractor"
2
+ require "rdoc_rubocop/lang/c/corrector"
3
+
4
+ module RDocRuboCop
5
+ module Lang
6
+ module C
7
+ class SourceFile < Lang::Base::SourceFile
8
+ def comment_extractor_class
9
+ C::CommentExtractor
10
+ end
11
+
12
+ def corrector_class
13
+ C::Corrector
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module RDocRuboCop
2
+ module Lang
3
+ module Ruby
4
+ end
5
+ end
6
+ end
7
+
8
+ require "rdoc_rubocop/lang/ruby/source_file"
@@ -0,0 +1,52 @@
1
+ require "rdoc_rubocop/lang/base/comment"
2
+ require "rdoc_rubocop/indent_util"
3
+
4
+ using RDocRuboCop::IndentUtil
5
+
6
+ module RDocRuboCop
7
+ module Lang
8
+ module Ruby
9
+ class Comment < Lang::Base::Comment
10
+ attr_reader :comment_tokens
11
+ attr_reader :source_file
12
+
13
+ def initialize(comment_tokens, source_file = nil)
14
+ @comment_tokens = comment_tokens
15
+ @source_file = source_file
16
+ end
17
+
18
+ def corrected_text
19
+ rdoc.
20
+ apply.
21
+ gsub(/^/, indent_and_commentchar).
22
+ gsub(/ *$/, "")
23
+ end
24
+
25
+ def lineno
26
+ @lineno ||= @comment_tokens.map(&:lineno).minmax
27
+ end
28
+
29
+ def number_of_lines
30
+ lineno[1] - lineno[0] + 1
31
+ end
32
+
33
+ private
34
+
35
+ def text_without_commentchar
36
+ text.gsub(/^ *#/, "").strip_indent
37
+ end
38
+
39
+ def indent_and_commentchar
40
+ indent = " " * @comment_tokens.map(&:column).min
41
+ commentchar_and_indent = text.scan(/^# *(?=\S)/).min
42
+
43
+ "#{indent}#{commentchar_and_indent}"
44
+ end
45
+
46
+ def text
47
+ @text ||= @comment_tokens.map(&:token).join
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,57 @@
1
+ require 'ripper'
2
+ require 'rdoc_rubocop/lang/ruby/token'
3
+ require 'rdoc_rubocop/lang/ruby/comment'
4
+
5
+ module RDocRuboCop
6
+ module Lang
7
+ module Ruby
8
+ class CommentExtractor
9
+ attr_reader :comments
10
+
11
+ def initialize(source_file)
12
+ @source_file = source_file
13
+ @comments = []
14
+ end
15
+
16
+ def extract
17
+ @comments = extract_comments
18
+ end
19
+
20
+ private
21
+
22
+ def extract_comments
23
+ chunk = []
24
+ comments = []
25
+
26
+ tokens.each do |tokens_in_line|
27
+ token = tokens_in_line.pop
28
+
29
+ if token.comment?
30
+ if tokens_in_line.all?(&:sp?)
31
+ chunk << token
32
+ else
33
+ comments << Comment.new(chunk, @source_file) if chunk.any?
34
+ chunk = []
35
+ end
36
+ else
37
+ if chunk.any?
38
+ comments << Comment.new(chunk, @source_file)
39
+ chunk = []
40
+ end
41
+ end
42
+ end
43
+ comments << Comment.new(chunk, @source_file) if chunk.any?
44
+
45
+ comments
46
+ end
47
+
48
+ def tokens
49
+ Ripper.
50
+ lex(@source_file.source).
51
+ map { |token| Token.build(*token) }.
52
+ slice_when { |token_before, token_after| token_before.lineno != token_after.lineno }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ require "rdoc_rubocop/lang/ruby/token/comment_token"
2
+
3
+ module RDocRuboCop
4
+ module Lang
5
+ module Ruby
6
+ class Corrector
7
+ attr_reader :corrected_source
8
+
9
+ def initialize(source_file)
10
+ @source_file = source_file
11
+ @corrected_source = nil
12
+ end
13
+
14
+ def correct
15
+ source_lines = @source_file.source.lines
16
+
17
+ @source_file.comments.reverse_each do |comment|
18
+ next if comment.source_codes.empty?
19
+
20
+ index = comment.lineno[0] - 1
21
+ number_of_lines = comment.number_of_lines
22
+ source_lines[index, number_of_lines] = comment.corrected_text
23
+ end
24
+
25
+ @corrected_source = source_lines.join
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ require "rdoc_rubocop/lang/ruby/comment_extractor"
2
+ require "rdoc_rubocop/lang/ruby/corrector"
3
+
4
+ module RDocRuboCop
5
+ module Lang
6
+ module Ruby
7
+ class SourceFile < Lang::Base::SourceFile
8
+ def comment_extractor_class
9
+ CommentExtractor
10
+ end
11
+
12
+ def corrector_class
13
+ Corrector
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ module RDocRuboCop
2
+ module Lang
3
+ module Ruby
4
+ class Token
5
+ attr_reader :locate, :type, :token, :state
6
+
7
+ def self.build(locate, type, token, state)
8
+ case type
9
+ when :on_comment
10
+ CommentToken.new(locate, type, token, state)
11
+ else
12
+ new(locate, type, token, state)
13
+ end
14
+ end
15
+
16
+ def initialize(locate, type, token, state)
17
+ @locate = locate
18
+ @type = type
19
+ @token = token
20
+ @state = state
21
+ end
22
+
23
+ def lineno
24
+ @locate[0]
25
+ end
26
+
27
+ def column
28
+ @locate[1]
29
+ end
30
+
31
+ %i(on_sp on_comment).each do |type|
32
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
33
+ def #{type.to_s.sub(/^on_/, "")}? # def sp?
34
+ type == :#{type} # type == :on_sp
35
+ end # end
36
+ RUBY
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ require "rdoc_rubocop/lang/ruby/token/comment_token"
@@ -0,0 +1,13 @@
1
+ module RDocRuboCop
2
+ module Lang
3
+ module Ruby
4
+ class Token
5
+ class CommentToken < self
6
+ def comment?
7
+ true
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,69 @@
1
+ require "rdoc_rubocop/rdoc/line"
2
+ require "rdoc_rubocop/rdoc/ruby_snippet"
3
+
4
+ module RDocRuboCop
5
+ class RDoc
6
+ attr_reader :text
7
+ attr_reader :ruby_snippets
8
+
9
+ def initialize(text)
10
+ @text = text
11
+
12
+ @ruby_snippets = []
13
+ @parsed = false
14
+ end
15
+
16
+ def ruby_snippets
17
+ parse unless @parsed
18
+ @ruby_snippets
19
+ end
20
+
21
+ def apply
22
+ lines = text_lines
23
+
24
+ @ruby_snippets.reverse_each do |ruby_snippet|
25
+ next unless ruby_snippet.corrected_text_with_indent
26
+
27
+ index = ruby_snippet.lineno[0] - 1
28
+ number_of_lines = ruby_snippet.number_of_lines
29
+ lines[index, number_of_lines] = ruby_snippet.corrected_text_with_indent
30
+ end
31
+
32
+ lines.join
33
+ end
34
+
35
+ private
36
+
37
+ def parse
38
+ @ruby_snippets = []
39
+ ruby_snippet = RubySnippet.new
40
+ is_in_call_seq = false
41
+
42
+ lines = text_lines.map.with_index { |line, i| Line.new(1 + i, line) }
43
+ lines.each do |line|
44
+ if line.blank?
45
+ ruby_snippet.append(line) if !ruby_snippet.empty?
46
+ elsif line.indent.length > 0
47
+ ruby_snippet.append(line) if !is_in_call_seq
48
+ elsif line.str.match?(/^call-seq:/)
49
+ is_in_call_seq = true
50
+ ruby_snippet = RubySnippet.new
51
+ else
52
+ is_in_call_seq = false
53
+
54
+ if !ruby_snippet.empty?
55
+ @ruby_snippets << ruby_snippet.tap(&:trim!)
56
+ ruby_snippet = RubySnippet.new
57
+ end
58
+ end
59
+ end
60
+ @ruby_snippets << ruby_snippet.tap(&:trim!) if !ruby_snippet.empty?
61
+
62
+ @parsed = true
63
+ end
64
+
65
+ def text_lines
66
+ @text_lines ||= @text.lines
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,24 @@
1
+ require "rdoc_rubocop/indent_util"
2
+
3
+ using RDocRuboCop::IndentUtil
4
+
5
+ module RDocRuboCop
6
+ class RDoc
7
+ class Line
8
+ attr_reader :lineno, :str
9
+
10
+ def initialize(lineno, str)
11
+ @lineno = lineno
12
+ @str = str
13
+ end
14
+
15
+ def blank?
16
+ /^ *$/.match?(@str)
17
+ end
18
+
19
+ def indent
20
+ @str.expand_tab.indent
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ require "rdoc_rubocop/indent_util"
2
+ require "rdoc_rubocop/rdoc/line"
3
+ require "rdoc_rubocop/file_path"
4
+
5
+ using RDocRuboCop::IndentUtil
6
+
7
+ module RDocRuboCop
8
+ class RDoc
9
+ class RubySnippet
10
+ attr_reader :file_path
11
+
12
+ def initialize
13
+ @lines = []
14
+ end
15
+
16
+ def append(line)
17
+ @lines << line
18
+ end
19
+
20
+ def empty?
21
+ @lines.empty?
22
+ end
23
+
24
+ def trim!
25
+ i = @lines.size - 1
26
+ while i >= 0 && @lines[i].blank? do
27
+ @lines.delete_at(i)
28
+ i -= 1
29
+ end
30
+ end
31
+
32
+ def text
33
+ text_with_indent.strip_indent
34
+ end
35
+
36
+ def lineno
37
+ @lineno ||= @lines.map(&:lineno).minmax
38
+ end
39
+
40
+ def number_of_lines
41
+ lineno[1] - lineno[0] + 1
42
+ end
43
+
44
+ def build_file_path(filename)
45
+ @file_path = FilePath.new(filename, self)
46
+ end
47
+
48
+ def corrected_text_with_indent
49
+ return unless @file_path
50
+
51
+ indent = text_with_indent.indent
52
+ @file_path.source.gsub(/^( *)(?=\S)/, "#{indent}\\1")
53
+ end
54
+
55
+ private
56
+
57
+ def text_with_indent
58
+ @lines.map(&:str).join
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,3 @@
1
- require "rdoc_rubocop/file_path"
2
-
3
1
  module RDocRuboCop
4
2
  module RuboCopModifier
5
3
  module HijackSTDIN