rdoc_rubocop 0.1.3 → 0.1.4

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.
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
@@ -1,5 +1,4 @@
1
- require "rdoc_rubocop/source_file"
2
- require "rdoc_rubocop/comment_extractor"
1
+ require "rdoc_rubocop/lang/base/source_file"
3
2
 
4
3
  module RDocRuboCop
5
4
  class RuboCopRunner
@@ -54,7 +53,7 @@ module RDocRuboCop
54
53
  end
55
54
 
56
55
  def source_files
57
- @paths.map(&SourceFile.method(:build))
56
+ @paths.map(&Lang::Base::SourceFile.method(:build))
58
57
  end
59
58
 
60
59
  # Report with a message: "Inspecting 0 files"
@@ -1,3 +1,3 @@
1
1
  module RDocRuboCop
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdoc_rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loose Coupling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-25 00:00:00.000000000 Z
11
+ date: 2019-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -113,18 +113,32 @@ files:
113
113
  - bin/setup
114
114
  - exe/rdoc-rubocop
115
115
  - lib/rdoc_rubocop.rb
116
- - lib/rdoc_rubocop/comment.rb
117
- - lib/rdoc_rubocop/comment/source_code.rb
118
- - lib/rdoc_rubocop/comment_extractor.rb
119
- - lib/rdoc_rubocop/comment_token_organizable.rb
120
116
  - lib/rdoc_rubocop/file_path.rb
117
+ - lib/rdoc_rubocop/indent_util.rb
118
+ - lib/rdoc_rubocop/lang.rb
119
+ - lib/rdoc_rubocop/lang/base/comment.rb
120
+ - lib/rdoc_rubocop/lang/base/source_file.rb
121
+ - lib/rdoc_rubocop/lang/c.rb
122
+ - lib/rdoc_rubocop/lang/c/comment.rb
123
+ - lib/rdoc_rubocop/lang/c/comment/banner.rb
124
+ - lib/rdoc_rubocop/lang/c/comment/normal.rb
125
+ - lib/rdoc_rubocop/lang/c/comment/one_line.rb
126
+ - lib/rdoc_rubocop/lang/c/comment_extractor.rb
127
+ - lib/rdoc_rubocop/lang/c/corrector.rb
128
+ - lib/rdoc_rubocop/lang/c/source_file.rb
129
+ - lib/rdoc_rubocop/lang/ruby.rb
130
+ - lib/rdoc_rubocop/lang/ruby/comment.rb
131
+ - lib/rdoc_rubocop/lang/ruby/comment_extractor.rb
132
+ - lib/rdoc_rubocop/lang/ruby/corrector.rb
133
+ - lib/rdoc_rubocop/lang/ruby/source_file.rb
134
+ - lib/rdoc_rubocop/lang/ruby/token.rb
135
+ - lib/rdoc_rubocop/lang/ruby/token/comment_token.rb
121
136
  - lib/rdoc_rubocop/options.rb
137
+ - lib/rdoc_rubocop/rdoc.rb
138
+ - lib/rdoc_rubocop/rdoc/line.rb
139
+ - lib/rdoc_rubocop/rdoc/ruby_snippet.rb
122
140
  - lib/rdoc_rubocop/rubocop_modifier.rb
123
141
  - lib/rdoc_rubocop/rubocop_runner.rb
124
- - lib/rdoc_rubocop/source_file.rb
125
- - lib/rdoc_rubocop/source_file/corrector.rb
126
- - lib/rdoc_rubocop/token.rb
127
- - lib/rdoc_rubocop/token/comment_token.rb
128
142
  - lib/rdoc_rubocop/version.rb
129
143
  - rdoc_rubocop.gemspec
130
144
  homepage: https://github.com/nowlinuxing/rdoc_rubocop/
@@ -1,45 +0,0 @@
1
- require "rdoc_rubocop/comment_token_organizable"
2
- require "rdoc_rubocop/comment/source_code"
3
-
4
- module RDocRuboCop
5
- class Comment
6
- include CommentTokenOrganizable
7
-
8
- attr_reader :source_file
9
-
10
- def initialize(comment_tokens, source_file = nil)
11
- @comment_tokens = comment_tokens
12
- @source_file = source_file
13
- end
14
-
15
- def source_codes
16
- code_chunk = []
17
- codes = []
18
- @comment_tokens.each do |comment_token|
19
- if comment_token.comment_indent > comment_indent
20
- code_chunk << comment_token
21
- elsif comment_token.blank? && code_chunk.any?
22
- code_chunk << comment_token
23
- elsif code_chunk.any?
24
- codes << SourceCode.new(trim(code_chunk))
25
- code_chunk = []
26
- end
27
- end
28
- codes << SourceCode.new(trim(code_chunk), self) if code_chunk.any?
29
-
30
- codes
31
- end
32
-
33
- private
34
-
35
- def trim(code_chunk)
36
- i = code_chunk.size - 1
37
- while i >= 0 && code_chunk[i].blank? do
38
- code_chunk.delete_at(i)
39
- i -= 1
40
- end
41
-
42
- code_chunk
43
- end
44
- end
45
- end
@@ -1,28 +0,0 @@
1
- require "rdoc_rubocop/token/comment_token"
2
-
3
- module RDocRuboCop
4
- class Comment
5
- class SourceCode
6
- include CommentTokenOrganizable
7
-
8
- attr_reader :comment
9
-
10
- def initialize(comment_tokens, comment = nil)
11
- @comment_tokens = comment_tokens
12
- @comment = comment
13
- end
14
-
15
- def text
16
- @comment_tokens.map { |comment_token| comment_token.text_without_indent(comment_indent) }.join
17
- end
18
-
19
- def lineno
20
- @lineno ||= comment_tokens.map(&:lineno).minmax
21
- end
22
-
23
- def indent_and_commentchar
24
- Token::CommentToken.indent_and_commentchar(comment_tokens[0].column, comment_indent)
25
- end
26
- end
27
- end
28
- end
@@ -1,53 +0,0 @@
1
- require 'ripper'
2
- require 'rdoc_rubocop/token'
3
- require 'rdoc_rubocop/comment'
4
-
5
- module RDocRuboCop
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
- chunk = []
22
- comments = []
23
-
24
- tokens.each do |tokens_in_line|
25
- token = tokens_in_line.pop
26
-
27
- if token.comment?
28
- if tokens_in_line.all?(&:sp?)
29
- chunk << token
30
- else
31
- comments << Comment.new(chunk, @source_file) if chunk.any?
32
- chunk = [token]
33
- end
34
- else
35
- if chunk.any?
36
- comments << Comment.new(chunk, @source_file)
37
- chunk = []
38
- end
39
- end
40
- end
41
- comments << Comment.new(chunk, @source_file) if chunk.any?
42
-
43
- comments
44
- end
45
-
46
- def tokens
47
- Ripper.
48
- lex(@source_file.source).
49
- map { |token| Token.build(*token) }.
50
- slice_when { |token_before, token_after| token_before.lineno != token_after.lineno }
51
- end
52
- end
53
- end
@@ -1,17 +0,0 @@
1
- module RDocRuboCop
2
- module CommentTokenOrganizable
3
- def self.included(base)
4
- base.include InstanceMethods
5
-
6
- attr_reader :comment_tokens
7
- end
8
-
9
- module InstanceMethods
10
- private
11
-
12
- def comment_indent
13
- @comment_indent ||= @comment_tokens.reject(&:blank?).map(&:comment_indent).min
14
- end
15
- end
16
- end
17
- end
@@ -1,72 +0,0 @@
1
- require "digest"
2
- require "rdoc_rubocop/comment_extractor"
3
- require "rdoc_rubocop/file_path"
4
- require "rdoc_rubocop/source_file/corrector"
5
-
6
- module RDocRuboCop
7
- class SourceFile
8
- attr_reader :source
9
- attr_reader :filename
10
-
11
- def self.build(filename)
12
- source = File.open(filename, "r").read
13
- new(source, filename)
14
- end
15
-
16
- def initialize(source, filename)
17
- @source = source
18
- @filename = filename
19
-
20
- reset
21
- end
22
-
23
- def source_code_file_paths
24
- @source_code_file_paths ||=
25
- comments.flat_map do |comment|
26
- comment.source_codes.map do |source_code|
27
- FilePath.new(@filename, source_code)
28
- end
29
- end
30
- end
31
-
32
- def comments
33
- comment_extractor = CommentExtractor.new(self)
34
- comment_extractor.extract
35
- comment_extractor.comments
36
- end
37
-
38
- def correct!
39
- correct
40
- save if changed?
41
- reset
42
- end
43
-
44
- def correct
45
- corrector = Corrector.new(@source, @source_code_file_paths)
46
- corrector.correct
47
-
48
- @source = corrector.source
49
- end
50
-
51
- private
52
-
53
- def reset
54
- @checksum = compute_digest(@source)
55
- @source_code_file_paths = nil
56
- end
57
-
58
- def compute_digest(str)
59
- Digest::MD5.hexdigest(str)
60
- end
61
-
62
- def save
63
- File.open(@filename, "w") do |f|
64
- f.puts @source
65
- end
66
- end
67
-
68
- def changed?
69
- compute_digest(@source) != @checksum
70
- end
71
- end
72
- end
@@ -1,49 +0,0 @@
1
- require "rdoc_rubocop/token/comment_token"
2
-
3
- module RDocRuboCop
4
- class SourceFile
5
- class Corrector
6
- attr_reader :source
7
-
8
- def initialize(source, source_code_file_paths)
9
- @source = source
10
- @source_code_file_paths = source_code_file_paths
11
- end
12
-
13
- def correct
14
- source_lines = source.lines
15
-
16
- @source_code_file_paths.reverse_each do |file_path|
17
- apply(source_lines, file_path)
18
- end
19
-
20
- @source = source_lines.flatten.join
21
- end
22
-
23
- private
24
-
25
- def apply(source_lines, file_path)
26
- lineno = file_path.source_code.lineno
27
-
28
- delete_lines(source_lines, lineno[0] - 1, lineno[1] - 1)
29
- insert(source_lines, lineno[0] - 1, file_path)
30
- end
31
-
32
- def delete_lines(source_lines, lineno_from, lineno_to)
33
- (lineno_to).downto(lineno_from).each do |i|
34
- source_lines.delete_at(i)
35
- end
36
- end
37
-
38
- def insert(source_lines, index, file_path)
39
- indent_and_commentchar = file_path.source_code.indent_and_commentchar
40
- source_with_indent =
41
- file_path.source.
42
- gsub(/^/, indent_and_commentchar).
43
- gsub(/#{Token::CommentToken::COMMENT_CHAR}\s*$/, "#")
44
-
45
- source_lines.insert(index, source_with_indent)
46
- end
47
- end
48
- end
49
- end
@@ -1,39 +0,0 @@
1
- module RDocRuboCop
2
- class Token
3
- attr_reader :locate, :type, :token, :state
4
-
5
- def self.build(locate, type, token, state)
6
- case type
7
- when :on_comment
8
- CommentToken.new(locate, type, token, state)
9
- else
10
- new(locate, type, token, state)
11
- end
12
- end
13
-
14
- def initialize(locate, type, token, state)
15
- @locate = locate
16
- @type = type
17
- @token = token
18
- @state = state
19
- end
20
-
21
- def lineno
22
- @locate[0]
23
- end
24
-
25
- def column
26
- @locate[1]
27
- end
28
-
29
- %i(on_sp on_comment).each do |type|
30
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
31
- def #{type.to_s.sub(/^on_/, "")}? # def sp?
32
- type == :#{type} # type == :on_sp
33
- end # end
34
- RUBY
35
- end
36
- end
37
- end
38
-
39
- require "rdoc_rubocop/token/comment_token"
@@ -1,34 +0,0 @@
1
- module RDocRuboCop
2
- class Token
3
- class CommentToken < self
4
- COMMENT_CHAR = "#".freeze
5
- SPACE_CHAR = " ".freeze
6
-
7
- def self.indent_and_commentchar(indent_before_comment, indent_after_comment)
8
- SPACE_CHAR * indent_before_comment +
9
- COMMENT_CHAR +
10
- SPACE_CHAR * indent_after_comment
11
- end
12
-
13
- def comment?
14
- true
15
- end
16
-
17
- def comment_indent
18
- text_with_indent[/^#{SPACE_CHAR}*/].length
19
- end
20
-
21
- def text_with_indent
22
- token.sub(COMMENT_CHAR, "")
23
- end
24
-
25
- def text_without_indent(indent)
26
- text_with_indent.sub(/^#{SPACE_CHAR}{#{indent}}/, "")
27
- end
28
-
29
- def blank?
30
- text_with_indent.match?(/^\s*$/)
31
- end
32
- end
33
- end
34
- end