rubocop-ast 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a691b22abba3de79be3aca5f66247b20b5bdd8fe0bcbe1403d5455c4da25fae9
4
- data.tar.gz: d0984b47a3d0f683447361683acb58afa4fcfffb1597bed575cce1d34fafd90e
3
+ metadata.gz: 35f0c94d6021f2223c413d1751c4e816346b740cf01663e5444325256ba987aa
4
+ data.tar.gz: 010a37da8da2e08bea4785821709b9e10cdedb02e133f33b1b8a12051939517e
5
5
  SHA512:
6
- metadata.gz: 165cfc53926eecd29099f29554dae5e751bb1bee7c4fc42f49abe93db18ea3dbf6589b7b57e6e16aaabf7e842ea253dd41609ab2af343c44db4181af4c815c79
7
- data.tar.gz: f150ffa3ae23ae80f4e3572a529fd19d42509e025e013c02c52eb408c60f3feaf55ef1d53304c19586ad2bf3b3b0ca7aac18503bc45b56ed3ad534dc875d894f
6
+ metadata.gz: ac8387283324c91278d848e1411cdede002cda15d00e20d2a889a5f7b4b37eed217e05e5538cfc99dfd2022ea68afae87eb4698db9d58aef2e08f15f3e5930e2
7
+ data.tar.gz: 3505c65f6921744d681f226f9f2e476a662c2da564db8f2534dd53a5cd17d15a4eadf839e7bf8d9e4bd8d769f0351527ad1a53c7da5a11cc3dc6f4e760a7c955
@@ -63,6 +63,7 @@ require_relative 'ast/node/while_node'
63
63
  require_relative 'ast/node/yield_node'
64
64
  require_relative 'ast/builder'
65
65
  require_relative 'ast/processed_source'
66
+ require_relative 'ast/rubocop_compatibility'
66
67
  require_relative 'ast/token'
67
68
  require_relative 'ast/traversal'
68
69
  require_relative 'ast/version'
@@ -92,6 +92,16 @@ module RuboCop
92
92
  @mutable_attributes[:parent] = node
93
93
  end
94
94
 
95
+ # @return [Boolean]
96
+ def parent?
97
+ !!parent
98
+ end
99
+
100
+ # @return [Boolean]
101
+ def root?
102
+ !parent
103
+ end
104
+
95
105
  def complete!
96
106
  @mutable_attributes.freeze
97
107
  each_child_node(&:complete!)
@@ -299,8 +309,10 @@ module RuboCop
299
309
  self
300
310
  end
301
311
 
312
+ # Note: Some rare nodes may have no source, like `s(:args)` in `foo {}`
313
+ # @return [String, nil]
302
314
  def source
303
- loc.expression.source
315
+ loc.expression&.source
304
316
  end
305
317
 
306
318
  def source_range
@@ -28,6 +28,8 @@ module RuboCop
28
28
 
29
29
  # @return [Boolean] if the constant starts with `::` (aka s(:cbase))
30
30
  def absolute?
31
+ return false unless namespace
32
+
31
33
  each_path.first.cbase_type?
32
34
  end
33
35
 
@@ -42,7 +42,7 @@ module RuboCop
42
42
  #
43
43
  # @return [Boolean] whether the dispatched method is a macro method
44
44
  def macro?
45
- !receiver && macro_scope?
45
+ !receiver && in_macro_scope?
46
46
  end
47
47
 
48
48
  # Checks whether the dispatched method is an access modifier.
@@ -222,31 +222,21 @@ module RuboCop
222
222
 
223
223
  private
224
224
 
225
- def_node_matcher :macro_scope?, <<~PATTERN
226
- {^{({sclass class module block} ...) class_constructor?}
227
- ^^{({sclass class module block} ... ({begin if} ...)) class_constructor?}
228
- ^#macro_kwbegin_wrapper?
229
- #root_node?}
225
+ def_node_matcher :in_macro_scope?, <<~PATTERN
226
+ {
227
+ root? # Either a root node,
228
+ ^{ # or the parent is...
229
+ sclass class module class_constructor? # a class-like node
230
+ [ { # or some "wrapper"
231
+ kwbegin begin block
232
+ (if _condition <%0 _>) # note: we're excluding the condition of `if` nodes
233
+ }
234
+ #in_macro_scope? # that is itself in a macro scope
235
+ ]
236
+ }
237
+ }
230
238
  PATTERN
231
239
 
232
- # Check if a node's parent is a kwbegin wrapper within a macro scope
233
- #
234
- # @param parent [Node] parent of the node being checked
235
- #
236
- # @return [Boolean] true if the parent is a kwbegin in a macro scope
237
- def macro_kwbegin_wrapper?(parent)
238
- parent.kwbegin_type? && macro_scope?(parent)
239
- end
240
-
241
- # Check if a node does not have a parent
242
- #
243
- # @param node [Node]
244
- #
245
- # @return [Boolean] if the parent is nil
246
- def root_node?(node)
247
- node.parent.nil?
248
- end
249
-
250
240
  def_node_matcher :adjacent_def_modifier?, <<~PATTERN
251
241
  (send nil? _ ({def defs} ...))
252
242
  PATTERN
@@ -14,12 +14,9 @@ module RuboCop
14
14
  o: 0
15
15
  }.freeze
16
16
 
17
- # Note: The 'o' option is ignored.
18
- #
19
17
  # @return [Regexp] a regexp of this node
20
18
  def to_regexp
21
- option = regopt.children.map { |opt| OPTIONS.fetch(opt) }.inject(:|)
22
- Regexp.new(content, option)
19
+ Regexp.new(content, options)
23
20
  end
24
21
 
25
22
  # @return [RuboCop::AST::Node] a regopt node
@@ -27,6 +24,13 @@ module RuboCop
27
24
  children.last
28
25
  end
29
26
 
27
+ # Note: The 'o' option is ignored.
28
+ #
29
+ # @return [Integer] the Regexp option bits as returned by Regexp#options
30
+ def options
31
+ regopt.children.map { |opt| OPTIONS.fetch(opt) }.inject(0, :|)
32
+ end
33
+
30
34
  # @return [String] a string of regexp content
31
35
  def content
32
36
  children.select(&:str_type?).map(&:str_content).join
@@ -229,9 +229,9 @@ module RuboCop
229
229
  when 2.7
230
230
  require 'parser/ruby27'
231
231
  Parser::Ruby27
232
- when 2.8
233
- require 'parser/ruby28'
234
- Parser::Ruby28
232
+ when 2.8, 3.0
233
+ require 'parser/ruby30'
234
+ Parser::Ruby30
235
235
  else
236
236
  raise ArgumentError,
237
237
  "RuboCop found unknown Ruby version: #{ruby_version.inspect}"
@@ -269,7 +269,8 @@ module RuboCop
269
269
  # is passed as a method argument. In this case tokens are interleaved by
270
270
  # heredoc contents' tokens.
271
271
  def sorted_tokens
272
- @sorted_tokens ||= tokens.sort_by(&:begin_pos)
272
+ # Use stable sort.
273
+ @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] }
273
274
  end
274
275
 
275
276
  def source_range(range_or_node)
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # ...
5
+ module AST
6
+ # Responsible for compatibility with main gem
7
+ # @api private
8
+ module RuboCopCompatibility
9
+ INCOMPATIBLE_COPS = {
10
+ '0.89.0' => 'Layout/LineLength',
11
+ '0.92.0' => 'Style/MixinUsage'
12
+ }.freeze
13
+ def rubocop_loaded
14
+ loaded = Gem::Version.new(RuboCop::Version::STRING)
15
+ incompatible = INCOMPATIBLE_COPS.select do |k, _v|
16
+ loaded < Gem::Version.new(k)
17
+ end.values
18
+ return if incompatible.empty?
19
+
20
+ warn <<~WARNING
21
+ *** WARNING – Incompatible versions of `rubocop` and `rubocop-ast`
22
+ You may encounter issues with the following \
23
+ Cop#{'s' if incompatible.size > 1}: #{incompatible.join(', ')}
24
+ Please upgrade rubocop to at least v#{INCOMPATIBLE_COPS.keys.last}
25
+ WARNING
26
+ end
27
+ end
28
+
29
+ extend RuboCopCompatibility
30
+ end
31
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module AST
5
5
  module Version
6
- STRING = '0.4.0'
6
+ STRING = '0.6.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-ast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-09-11 00:00:00.000000000 Z
13
+ date: 2020-09-26 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.1.4
21
+ version: 2.7.1.5
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.1.4
28
+ version: 2.7.1.5
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,7 @@ files:
116
116
  - lib/rubocop/ast/node/yield_node.rb
117
117
  - lib/rubocop/ast/node_pattern.rb
118
118
  - lib/rubocop/ast/processed_source.rb
119
+ - lib/rubocop/ast/rubocop_compatibility.rb
119
120
  - lib/rubocop/ast/sexp.rb
120
121
  - lib/rubocop/ast/token.rb
121
122
  - lib/rubocop/ast/traversal.rb