rubocop-ast 0.1.0 → 0.3.0

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -5
  3. data/lib/rubocop/ast.rb +2 -1
  4. data/lib/rubocop/ast/builder.rb +3 -1
  5. data/lib/rubocop/ast/ext/range.rb +28 -0
  6. data/lib/rubocop/ast/node.rb +23 -6
  7. data/lib/rubocop/ast/node/array_node.rb +2 -8
  8. data/lib/rubocop/ast/node/break_node.rb +1 -6
  9. data/lib/rubocop/ast/node/case_match_node.rb +3 -9
  10. data/lib/rubocop/ast/node/case_node.rb +13 -9
  11. data/lib/rubocop/ast/node/def_node.rb +4 -23
  12. data/lib/rubocop/ast/node/defined_node.rb +2 -0
  13. data/lib/rubocop/ast/node/float_node.rb +1 -0
  14. data/lib/rubocop/ast/node/hash_node.rb +21 -8
  15. data/lib/rubocop/ast/node/if_node.rb +7 -14
  16. data/lib/rubocop/ast/node/index_node.rb +5 -3
  17. data/lib/rubocop/ast/node/indexasgn_node.rb +5 -3
  18. data/lib/rubocop/ast/node/int_node.rb +1 -0
  19. data/lib/rubocop/ast/node/lambda_node.rb +10 -3
  20. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -7
  21. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +55 -0
  22. data/lib/rubocop/ast/node/next_node.rb +12 -0
  23. data/lib/rubocop/ast/node/pair_node.rb +2 -2
  24. data/lib/rubocop/ast/node/return_node.rb +1 -13
  25. data/lib/rubocop/ast/node/send_node.rb +7 -1
  26. data/lib/rubocop/ast/node/super_node.rb +2 -0
  27. data/lib/rubocop/ast/node/when_node.rb +3 -9
  28. data/lib/rubocop/ast/node/yield_node.rb +2 -0
  29. data/lib/rubocop/ast/node_pattern.rb +99 -86
  30. data/lib/rubocop/ast/processed_source.rb +54 -16
  31. data/lib/rubocop/ast/traversal.rb +1 -1
  32. data/lib/rubocop/ast/version.rb +1 -1
  33. metadata +9 -8
  34. data/lib/rubocop/ast/node/retry_node.rb +0 -17
@@ -71,20 +71,24 @@ module RuboCop
71
71
  Digest::SHA1.hexdigest(@raw_source)
72
72
  end
73
73
 
74
- def each_comment
75
- comments.each { |comment| yield comment }
74
+ # @deprecated Use `comments.each`
75
+ def each_comment(&block)
76
+ comments.each(&block)
76
77
  end
77
78
 
78
- def find_comment
79
- comments.find { |comment| yield comment }
79
+ # @deprecated Use `comment_at_line`, `each_comment_in_lines`, or `comments.find`
80
+ def find_comment(&block)
81
+ comments.find(&block)
80
82
  end
81
83
 
82
- def each_token
83
- tokens.each { |token| yield token }
84
+ # @deprecated Use `tokens.each`
85
+ def each_token(&block)
86
+ tokens.each(&block)
84
87
  end
85
88
 
86
- def find_token
87
- tokens.find { |token| yield token }
89
+ # @deprecated Use `tokens.find`
90
+ def find_token(&block)
91
+ tokens.find(&block)
88
92
  end
89
93
 
90
94
  def file_path
@@ -95,12 +99,39 @@ module RuboCop
95
99
  ast.nil?
96
100
  end
97
101
 
98
- def commented?(source_range)
99
- comment_lines.include?(source_range.line)
102
+ # @return [Comment, nil] the comment at that line, if any.
103
+ def comment_at_line(line)
104
+ comment_index[line]
100
105
  end
101
106
 
107
+ # @return [Boolean] if the given line number has a comment.
108
+ def line_with_comment?(line)
109
+ comment_index.include?(line)
110
+ end
111
+
112
+ # Enumerates on the comments contained with the given `line_range`
113
+ def each_comment_in_lines(line_range)
114
+ return to_enum(:each_comment_in_lines, line_range) unless block_given?
115
+
116
+ line_range.each do |line|
117
+ if (comment = comment_index[line])
118
+ yield comment
119
+ end
120
+ end
121
+ end
122
+
123
+ # @return [Boolean] if any of the lines in the given `source_range` has a comment.
124
+ # Consider using `each_comment_in_lines` instead
125
+ def contains_comment?(source_range)
126
+ each_comment_in_lines(source_range.line..source_range.last_line).any?
127
+ end
128
+ # @deprecated use contains_comment?
129
+ alias commented? contains_comment?
130
+
131
+ # @deprecated Use `each_comment_in_lines`
132
+ # Should have been called `comments_before_or_at_line`. Doubtful it has of any valid use.
102
133
  def comments_before_line(line)
103
- comments.select { |c| c.location.line <= line }
134
+ each_comment_in_lines(0..line).to_a
104
135
  end
105
136
 
106
137
  def start_with?(string)
@@ -130,8 +161,10 @@ module RuboCop
130
161
 
131
162
  private
132
163
 
133
- def comment_lines
134
- @comment_lines ||= comments.map { |c| c.location.line }
164
+ def comment_index
165
+ @comment_index ||= {}.tap do |hash|
166
+ comments.each { |c| hash[c.location.line] = c }
167
+ end
135
168
  end
136
169
 
137
170
  def parse(source, ruby_version)
@@ -142,6 +175,9 @@ module RuboCop
142
175
  @buffer.source = source
143
176
  rescue EncodingError => e
144
177
  @parser_error = e
178
+ @ast = nil
179
+ @comments = []
180
+ @tokens = []
145
181
  return
146
182
  end
147
183
 
@@ -151,13 +187,15 @@ module RuboCop
151
187
  def tokenize(parser)
152
188
  begin
153
189
  ast, comments, tokens = parser.tokenize(@buffer)
154
-
155
- ast.respond_to?(:complete!) && ast.complete!
190
+ ast ||= nil # force `false` to `nil`, see https://github.com/whitequark/parser/pull/722
156
191
  rescue Parser::SyntaxError
157
192
  # All errors are in diagnostics. No need to handle exception.
193
+ comments = []
194
+ tokens = []
158
195
  end
159
196
 
160
- tokens = tokens.map { |t| Token.from_parser_token(t) } if tokens
197
+ ast&.complete!
198
+ tokens.map! { |t| Token.from_parser_token(t) }
161
199
 
162
200
  [ast, comments, tokens]
163
201
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  match_with_lvasgn begin kwbegin return
35
35
  in_match match_alt
36
36
  match_as array_pattern array_pattern_with_tail
37
- hash_pattern const_pattern
37
+ hash_pattern const_pattern find_pattern
38
38
  index indexasgn].freeze
39
39
  SECOND_CHILD_ONLY = %i[lvasgn ivasgn cvasgn gvasgn optarg kwarg
40
40
  kwoptarg].freeze
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '0.1.0'
6
+ STRING = '0.3.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-06-26 00:00:00.000000000 Z
13
+ date: 2020-08-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parser
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 2.7.0.1
21
+ version: 2.7.1.4
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 2.7.0.1
28
+ version: 2.7.1.4
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +59,7 @@ files:
59
59
  - lib/rubocop-ast.rb
60
60
  - lib/rubocop/ast.rb
61
61
  - lib/rubocop/ast/builder.rb
62
+ - lib/rubocop/ast/ext/range.rb
62
63
  - lib/rubocop/ast/node.rb
63
64
  - lib/rubocop/ast/node/alias_node.rb
64
65
  - lib/rubocop/ast/node/and_node.rb
@@ -94,12 +95,12 @@ files:
94
95
  - lib/rubocop/ast/node/mixin/parameterized_node.rb
95
96
  - lib/rubocop/ast/node/mixin/predicate_operator_node.rb
96
97
  - lib/rubocop/ast/node/module_node.rb
98
+ - lib/rubocop/ast/node/next_node.rb
97
99
  - lib/rubocop/ast/node/or_node.rb
98
100
  - lib/rubocop/ast/node/pair_node.rb
99
101
  - lib/rubocop/ast/node/range_node.rb
100
102
  - lib/rubocop/ast/node/regexp_node.rb
101
103
  - lib/rubocop/ast/node/resbody_node.rb
102
- - lib/rubocop/ast/node/retry_node.rb
103
104
  - lib/rubocop/ast/node/return_node.rb
104
105
  - lib/rubocop/ast/node/self_class_node.rb
105
106
  - lib/rubocop/ast/node/send_node.rb
@@ -125,7 +126,7 @@ metadata:
125
126
  source_code_uri: https://github.com/rubocop-hq/rubocop-ast/
126
127
  documentation_uri: https://docs.rubocop.org/rubocop-ast/
127
128
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop-ast/issues
128
- post_install_message:
129
+ post_install_message:
129
130
  rdoc_options: []
130
131
  require_paths:
131
132
  - lib
@@ -141,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
142
  version: '0'
142
143
  requirements: []
143
144
  rubygems_version: 3.1.2
144
- signing_key:
145
+ signing_key:
145
146
  specification_version: 4
146
147
  summary: RuboCop tools to deal with Ruby code AST.
147
148
  test_files: []
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module AST
5
- # A node extension for `retry` nodes. This will be used in place of a
6
- # plain node when the builder constructs the AST, making its methods
7
- # available to all `retry` nodes within RuboCop.
8
- class RetryNode < Node
9
- include MethodDispatchNode
10
- include ParameterizedNode
11
-
12
- def arguments
13
- []
14
- end
15
- end
16
- end
17
- end