rubocop-ast 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/rubocop/ast.rb +2 -1
- data/lib/rubocop/ast/builder.rb +3 -1
- data/lib/rubocop/ast/ext/range.rb +28 -0
- data/lib/rubocop/ast/node/break_node.rb +1 -6
- data/lib/rubocop/ast/node/case_match_node.rb +2 -4
- data/lib/rubocop/ast/node/case_node.rb +12 -4
- data/lib/rubocop/ast/node/defined_node.rb +2 -0
- data/lib/rubocop/ast/node/float_node.rb +1 -0
- data/lib/rubocop/ast/node/hash_node.rb +4 -8
- data/lib/rubocop/ast/node/if_node.rb +3 -5
- data/lib/rubocop/ast/node/index_node.rb +5 -3
- data/lib/rubocop/ast/node/indexasgn_node.rb +5 -3
- data/lib/rubocop/ast/node/int_node.rb +1 -0
- data/lib/rubocop/ast/node/lambda_node.rb +10 -3
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -7
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +55 -0
- data/lib/rubocop/ast/node/next_node.rb +12 -0
- data/lib/rubocop/ast/node/pair_node.rb +2 -2
- data/lib/rubocop/ast/node/return_node.rb +1 -13
- data/lib/rubocop/ast/node/send_node.rb +7 -1
- data/lib/rubocop/ast/node/super_node.rb +2 -0
- data/lib/rubocop/ast/node/when_node.rb +2 -4
- data/lib/rubocop/ast/node/yield_node.rb +2 -0
- data/lib/rubocop/ast/node_pattern.rb +4 -3
- data/lib/rubocop/ast/processed_source.rb +43 -19
- data/lib/rubocop/ast/version.rb +1 -1
- metadata +6 -5
- data/lib/rubocop/ast/node/retry_node.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 918c6017183db74497fc68166973904312bac7583f7752c2e55d1829574cb248
|
4
|
+
data.tar.gz: 1d04eb24fcbaec96b4bf0f17694018c31034fce4b3d81a9446ca98b3085e05a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54f6882ebf057c76a9c97212a62b295d03baea85ec7c44e606b412e74edce01b6c4bba93dfafa341756009a8bfa2352c4b2b3b22bdbf689dac08ddd9395a9aff
|
7
|
+
data.tar.gz: '08e60b802e83dba688bf1a31be417f9880b640194cf3b966849dd2c211de5a1f3797c8a37be3f2d3aa466d6c994b10c31277b0ed3dc708b12fe1c2f7a0f15552'
|
data/README.md
CHANGED
@@ -35,8 +35,9 @@ See the [docs site](https://docs.rubocop.org/rubocop-ast) for more details.
|
|
35
35
|
|
36
36
|
### Parser compatibility switches
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
This gem, by default, uses most [legacy AST output from parser](https://github.com/whitequark/parser/#usage), except for `emit_forward_arg` which is set to `true`.
|
39
|
+
|
40
|
+
The main `RuboCop` gem uses these defaults (and is currently only compatible with these), but this gem can be used separately from `RuboCop` and is meant to be compatible with all settings. For example, to have `-> { ... }` emitted
|
40
41
|
as `LambdaNode` instead of `SendNode`:
|
41
42
|
|
42
43
|
```ruby
|
data/lib/rubocop/ast.rb
CHANGED
@@ -4,6 +4,7 @@ require 'parser'
|
|
4
4
|
require 'forwardable'
|
5
5
|
require 'set'
|
6
6
|
|
7
|
+
require_relative 'ast/ext/range'
|
7
8
|
require_relative 'ast/node_pattern'
|
8
9
|
require_relative 'ast/sexp'
|
9
10
|
require_relative 'ast/node'
|
@@ -41,12 +42,12 @@ require_relative 'ast/node/int_node'
|
|
41
42
|
require_relative 'ast/node/keyword_splat_node'
|
42
43
|
require_relative 'ast/node/lambda_node'
|
43
44
|
require_relative 'ast/node/module_node'
|
45
|
+
require_relative 'ast/node/next_node'
|
44
46
|
require_relative 'ast/node/or_node'
|
45
47
|
require_relative 'ast/node/pair_node'
|
46
48
|
require_relative 'ast/node/range_node'
|
47
49
|
require_relative 'ast/node/regexp_node'
|
48
50
|
require_relative 'ast/node/resbody_node'
|
49
|
-
require_relative 'ast/node/retry_node'
|
50
51
|
require_relative 'ast/node/return_node'
|
51
52
|
require_relative 'ast/node/self_class_node'
|
52
53
|
require_relative 'ast/node/send_node'
|
data/lib/rubocop/ast/builder.rb
CHANGED
@@ -14,6 +14,8 @@ module RuboCop
|
|
14
14
|
# parser = Parser::Ruby25.new(builder)
|
15
15
|
# root_node = parser.parse(buffer)
|
16
16
|
class Builder < Parser::Builders::Default
|
17
|
+
self.emit_forward_arg = true
|
18
|
+
|
17
19
|
NODE_MAP = {
|
18
20
|
and: AndNode,
|
19
21
|
alias: AliasNode,
|
@@ -42,11 +44,11 @@ module RuboCop
|
|
42
44
|
kwsplat: KeywordSplatNode,
|
43
45
|
lambda: LambdaNode,
|
44
46
|
module: ModuleNode,
|
47
|
+
next: NextNode,
|
45
48
|
or: OrNode,
|
46
49
|
pair: PairNode,
|
47
50
|
regexp: RegexpNode,
|
48
51
|
resbody: ResbodyNode,
|
49
|
-
retry: RetryNode,
|
50
52
|
return: ReturnNode,
|
51
53
|
csend: SendNode,
|
52
54
|
send: SendNode,
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
module Ext
|
6
|
+
# Extensions to Parser::AST::Range
|
7
|
+
module Range
|
8
|
+
# @return [Range] the range of line numbers for the node
|
9
|
+
# If `exclude_end` is `true`, then the range will be exclusive.
|
10
|
+
#
|
11
|
+
# Assume that `node` corresponds to the following array literal:
|
12
|
+
#
|
13
|
+
# [
|
14
|
+
# :foo,
|
15
|
+
# :bar
|
16
|
+
# ]
|
17
|
+
#
|
18
|
+
# node.loc.begin.line_span # => 1..1
|
19
|
+
# node.loc.expression.line_span(exclude_end: true) # => 1...4
|
20
|
+
def line_span(exclude_end: false)
|
21
|
+
::Range.new(first_line, last_line, exclude_end)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
::Parser::Source::Range.include ::RuboCop::AST::Ext::Range
|
@@ -6,12 +6,7 @@ module RuboCop
|
|
6
6
|
# plain node when the builder constructs the AST, making its methods
|
7
7
|
# available to all `break` nodes within RuboCop.
|
8
8
|
class BreakNode < Node
|
9
|
-
include
|
10
|
-
include ParameterizedNode
|
11
|
-
|
12
|
-
def arguments
|
13
|
-
[]
|
14
|
-
end
|
9
|
+
include ParameterizedNode::WrappedArguments
|
15
10
|
end
|
16
11
|
end
|
17
12
|
end
|
@@ -16,12 +16,10 @@ module RuboCop
|
|
16
16
|
end
|
17
17
|
|
18
18
|
# @deprecated Use `in_pattern_branches.each`
|
19
|
-
def each_in_pattern
|
19
|
+
def each_in_pattern(&block)
|
20
20
|
return in_pattern_branches.to_enum(__method__) unless block_given?
|
21
21
|
|
22
|
-
in_pattern_branches.each
|
23
|
-
yield condition
|
24
|
-
end
|
22
|
+
in_pattern_branches.each(&block)
|
25
23
|
|
26
24
|
self
|
27
25
|
end
|
@@ -16,12 +16,10 @@ module RuboCop
|
|
16
16
|
end
|
17
17
|
|
18
18
|
# @deprecated Use `when_branches.each`
|
19
|
-
def each_when
|
19
|
+
def each_when(&block)
|
20
20
|
return when_branches.to_enum(__method__) unless block_given?
|
21
21
|
|
22
|
-
when_branches.each
|
23
|
-
yield condition
|
24
|
-
end
|
22
|
+
when_branches.each(&block)
|
25
23
|
|
26
24
|
self
|
27
25
|
end
|
@@ -33,6 +31,16 @@ module RuboCop
|
|
33
31
|
node_parts[1...-1]
|
34
32
|
end
|
35
33
|
|
34
|
+
# Returns an array of all the when branches in the `case` statement.
|
35
|
+
#
|
36
|
+
# @return [Array<Node, nil>] an array of the bodies of the when branches
|
37
|
+
# and the else (if any). Note that these bodies could be nil.
|
38
|
+
def branches
|
39
|
+
bodies = when_branches.map(&:body)
|
40
|
+
bodies.push(else_branch) if else?
|
41
|
+
bodies
|
42
|
+
end
|
43
|
+
|
36
44
|
# Returns the else branch of the `case` statement, if any.
|
37
45
|
#
|
38
46
|
# @return [Node] the else branch node of the `case` statement
|
@@ -56,12 +56,10 @@ module RuboCop
|
|
56
56
|
#
|
57
57
|
# @return [self] if a block is given
|
58
58
|
# @return [Enumerator] if no block is given
|
59
|
-
def each_key
|
59
|
+
def each_key(&block)
|
60
60
|
return pairs.map(&:key).to_enum unless block_given?
|
61
61
|
|
62
|
-
pairs.map(&:key).each
|
63
|
-
yield key
|
64
|
-
end
|
62
|
+
pairs.map(&:key).each(&block)
|
65
63
|
|
66
64
|
self
|
67
65
|
end
|
@@ -82,12 +80,10 @@ module RuboCop
|
|
82
80
|
#
|
83
81
|
# @return [self] if a block is given
|
84
82
|
# @return [Enumerator] if no block is given
|
85
|
-
def each_value
|
83
|
+
def each_value(&block)
|
86
84
|
return pairs.map(&:value).to_enum unless block_given?
|
87
85
|
|
88
|
-
pairs.map(&:value).each
|
89
|
-
yield value
|
90
|
-
end
|
86
|
+
pairs.map(&:value).each(&block)
|
91
87
|
|
92
88
|
self
|
93
89
|
end
|
@@ -147,7 +147,7 @@ module RuboCop
|
|
147
147
|
def branches
|
148
148
|
branches = [if_branch]
|
149
149
|
|
150
|
-
return branches unless
|
150
|
+
return branches unless else?
|
151
151
|
|
152
152
|
other_branches = if elsif_conditional?
|
153
153
|
else_branch.branches
|
@@ -158,12 +158,10 @@ module RuboCop
|
|
158
158
|
end
|
159
159
|
|
160
160
|
# @deprecated Use `branches.each`
|
161
|
-
def each_branch
|
161
|
+
def each_branch(&block)
|
162
162
|
return branches.to_enum(__method__) unless block_given?
|
163
163
|
|
164
|
-
branches.each
|
165
|
-
yield branch
|
166
|
-
end
|
164
|
+
branches.each(&block)
|
167
165
|
end
|
168
166
|
end
|
169
167
|
end
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
# The main RuboCop runs in legacy mode; this node is only used
|
18
18
|
# if user `AST::Builder.modernize` or `AST::Builder.emit_index=true`
|
19
19
|
class IndexNode < Node
|
20
|
-
include ParameterizedNode
|
20
|
+
include ParameterizedNode::RestArguments
|
21
21
|
include MethodDispatchNode
|
22
22
|
|
23
23
|
# For similarity with legacy mode
|
@@ -35,11 +35,13 @@ module RuboCop
|
|
35
35
|
:[]
|
36
36
|
end
|
37
37
|
|
38
|
+
private
|
39
|
+
|
38
40
|
# An array containing the arguments of the dispatched method.
|
39
41
|
#
|
40
42
|
# @return [Array<Node>] the arguments of the dispatched method
|
41
|
-
def
|
42
|
-
|
43
|
+
def first_argument_index
|
44
|
+
1
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# The main RuboCop runs in legacy mode; this node is only used
|
20
20
|
# if user `AST::Builder.modernize` or `AST::Builder.emit_index=true`
|
21
21
|
class IndexasgnNode < Node
|
22
|
-
include ParameterizedNode
|
22
|
+
include ParameterizedNode::RestArguments
|
23
23
|
include MethodDispatchNode
|
24
24
|
|
25
25
|
# For similarity with legacy mode
|
@@ -37,11 +37,13 @@ module RuboCop
|
|
37
37
|
:[]=
|
38
38
|
end
|
39
39
|
|
40
|
+
private
|
41
|
+
|
40
42
|
# An array containing the arguments of the dispatched method.
|
41
43
|
#
|
42
44
|
# @return [Array<Node>] the arguments of the dispatched method
|
43
|
-
def
|
44
|
-
|
45
|
+
def first_argument_index
|
46
|
+
1
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
# The main RuboCop runs in legacy mode; this node is only used
|
22
22
|
# if user `AST::Builder.modernize` or `AST::Builder.emit_lambda=true`
|
23
23
|
class LambdaNode < Node
|
24
|
-
include ParameterizedNode
|
24
|
+
include ParameterizedNode::RestArguments
|
25
25
|
include MethodDispatchNode
|
26
26
|
|
27
27
|
# For similarity with legacy mode
|
@@ -44,14 +44,21 @@ module RuboCop
|
|
44
44
|
false
|
45
45
|
end
|
46
46
|
|
47
|
+
# For similarity with legacy mode
|
48
|
+
def receiver
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
47
52
|
# For similarity with legacy mode
|
48
53
|
def method_name
|
49
54
|
:lambda
|
50
55
|
end
|
51
56
|
|
57
|
+
private
|
58
|
+
|
52
59
|
# For similarity with legacy mode
|
53
|
-
def
|
54
|
-
|
60
|
+
def first_argument_index
|
61
|
+
2
|
55
62
|
end
|
56
63
|
end
|
57
64
|
end
|
@@ -26,13 +26,6 @@ module RuboCop
|
|
26
26
|
node_parts[1]
|
27
27
|
end
|
28
28
|
|
29
|
-
# An array containing the arguments of the dispatched method.
|
30
|
-
#
|
31
|
-
# @return [Array<Node>] the arguments of the dispatched method
|
32
|
-
def arguments
|
33
|
-
node_parts[2..-1]
|
34
|
-
end
|
35
|
-
|
36
29
|
# The `block` node associated with this method dispatch, if any.
|
37
30
|
#
|
38
31
|
# @return [BlockNode, nil] the `block` node associated with this method
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module RuboCop
|
4
4
|
module AST
|
5
|
+
# Requires implementing `arguments`.
|
6
|
+
#
|
5
7
|
# Common functionality for nodes that are parameterized:
|
6
8
|
# `send`, `super`, `zsuper`, `def`, `defs`
|
7
9
|
# and (modern only): `index`, `indexasgn`, `lambda`
|
@@ -57,6 +59,59 @@ module RuboCop
|
|
57
59
|
arguments? &&
|
58
60
|
(last_argument.block_pass_type? || last_argument.blockarg_type?)
|
59
61
|
end
|
62
|
+
|
63
|
+
# A specialized `ParameterizedNode` for node that have a single child
|
64
|
+
# containing either `nil`, an argument, or a `begin` node with all the
|
65
|
+
# arguments
|
66
|
+
module WrappedArguments
|
67
|
+
include ParameterizedNode
|
68
|
+
# @return [Array] The arguments of the node.
|
69
|
+
def arguments
|
70
|
+
first = children.first
|
71
|
+
if first&.begin_type?
|
72
|
+
first.children
|
73
|
+
else
|
74
|
+
children
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# A specialized `ParameterizedNode`.
|
80
|
+
# Requires implementing `first_argument_index`
|
81
|
+
# Implements `arguments` as `children[first_argument_index..-1]`
|
82
|
+
# and optimizes other calls
|
83
|
+
module RestArguments
|
84
|
+
include ParameterizedNode
|
85
|
+
# @return [Array] arguments, if any
|
86
|
+
def arguments
|
87
|
+
children[first_argument_index..-1]
|
88
|
+
end
|
89
|
+
|
90
|
+
# A shorthand for getting the first argument of the node.
|
91
|
+
# Equivalent to `arguments.first`.
|
92
|
+
#
|
93
|
+
# @return [Node, nil] the first argument of the node,
|
94
|
+
# or `nil` if there are no arguments
|
95
|
+
def first_argument
|
96
|
+
children[first_argument_index]
|
97
|
+
end
|
98
|
+
|
99
|
+
# A shorthand for getting the last argument of the node.
|
100
|
+
# Equivalent to `arguments.last`.
|
101
|
+
#
|
102
|
+
# @return [Node, nil] the last argument of the node,
|
103
|
+
# or `nil` if there are no arguments
|
104
|
+
def last_argument
|
105
|
+
children[-1] if arguments?
|
106
|
+
end
|
107
|
+
|
108
|
+
# Checks whether this node has any arguments.
|
109
|
+
#
|
110
|
+
# @return [Boolean] whether this node has any arguments
|
111
|
+
def arguments?
|
112
|
+
children.size > first_argument_index
|
113
|
+
end
|
114
|
+
end
|
60
115
|
end
|
61
116
|
end
|
62
117
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# A node extension for `next` 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 `next` nodes within RuboCop.
|
8
|
+
class NextNode < Node
|
9
|
+
include ParameterizedNode::WrappedArguments
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
#
|
33
33
|
# @param [Boolean] with_spacing whether to include spacing
|
34
34
|
# @return [String] the delimiter of the `pair`
|
35
|
-
def delimiter(with_spacing
|
35
|
+
def delimiter(*deprecated, with_spacing: deprecated.first)
|
36
36
|
if with_spacing
|
37
37
|
hash_rocket? ? SPACED_HASH_ROCKET : SPACED_COLON
|
38
38
|
else
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
#
|
45
45
|
# @param [Boolean] with_spacing whether to include spacing
|
46
46
|
# @return [String] the inverse delimiter of the `pair`
|
47
|
-
def inverse_delimiter(with_spacing
|
47
|
+
def inverse_delimiter(*deprecated, with_spacing: deprecated.first)
|
48
48
|
if with_spacing
|
49
49
|
hash_rocket? ? SPACED_COLON : SPACED_HASH_ROCKET
|
50
50
|
else
|
@@ -6,19 +6,7 @@ module RuboCop
|
|
6
6
|
# plain node when the builder constructs the AST, making its methods
|
7
7
|
# available to all `return` nodes within RuboCop.
|
8
8
|
class ReturnNode < Node
|
9
|
-
include
|
10
|
-
include ParameterizedNode
|
11
|
-
|
12
|
-
# Returns the arguments of the `return`.
|
13
|
-
#
|
14
|
-
# @return [Array] The arguments of the `return`.
|
15
|
-
def arguments
|
16
|
-
if node_parts.one? && node_parts.first.begin_type?
|
17
|
-
node_parts.first.children
|
18
|
-
else
|
19
|
-
node_parts
|
20
|
-
end
|
21
|
-
end
|
9
|
+
include ParameterizedNode::WrappedArguments
|
22
10
|
end
|
23
11
|
end
|
24
12
|
end
|
@@ -6,13 +6,19 @@ module RuboCop
|
|
6
6
|
# node when the builder constructs the AST, making its methods available
|
7
7
|
# to all `send` nodes within RuboCop.
|
8
8
|
class SendNode < Node
|
9
|
-
include ParameterizedNode
|
9
|
+
include ParameterizedNode::RestArguments
|
10
10
|
include MethodDispatchNode
|
11
11
|
|
12
12
|
def_node_matcher :attribute_accessor?, <<~PATTERN
|
13
13
|
[(send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
|
14
14
|
(_ _ _ _ ...)]
|
15
15
|
PATTERN
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def first_argument_index
|
20
|
+
2
|
21
|
+
end
|
16
22
|
end
|
17
23
|
end
|
18
24
|
end
|
@@ -14,12 +14,10 @@ module RuboCop
|
|
14
14
|
end
|
15
15
|
|
16
16
|
# @deprecated Use `conditions.each`
|
17
|
-
def each_condition
|
17
|
+
def each_condition(&block)
|
18
18
|
return conditions.to_enum(__method__) unless block_given?
|
19
19
|
|
20
|
-
conditions.each
|
21
|
-
yield condition
|
22
|
-
end
|
20
|
+
conditions.each(&block)
|
23
21
|
|
24
22
|
self
|
25
23
|
end
|
@@ -99,6 +99,7 @@ module RuboCop
|
|
99
99
|
# # These arguments can be patterns themselves, in
|
100
100
|
# # which case a matcher responding to === will be
|
101
101
|
# # passed.
|
102
|
+
# '# comment' # comments are accepted at the end of lines
|
102
103
|
#
|
103
104
|
# You can nest arbitrarily deep:
|
104
105
|
#
|
@@ -122,6 +123,8 @@ module RuboCop
|
|
122
123
|
class Compiler
|
123
124
|
SYMBOL = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}.freeze
|
124
125
|
IDENTIFIER = /[a-zA-Z_][a-zA-Z0-9_-]*/.freeze
|
126
|
+
COMMENT = /#\s.*$/.freeze
|
127
|
+
|
125
128
|
META = Regexp.union(
|
126
129
|
%w"( ) { } [ ] $< < > $... $ ! ^ ` ... + * ?"
|
127
130
|
).freeze
|
@@ -455,7 +458,6 @@ module RuboCop
|
|
455
458
|
[0..Float::INFINITY, 'true']
|
456
459
|
end
|
457
460
|
|
458
|
-
# rubocop:disable Metrics/AbcSize
|
459
461
|
# rubocop:disable Metrics/MethodLength
|
460
462
|
def compile_any_order(capture_all = nil)
|
461
463
|
rest = capture_rest = nil
|
@@ -475,7 +477,6 @@ module RuboCop
|
|
475
477
|
end
|
476
478
|
end
|
477
479
|
# rubocop:enable Metrics/MethodLength
|
478
|
-
# rubocop:enable Metrics/AbcSize
|
479
480
|
|
480
481
|
def insure_same_captures(enum, what)
|
481
482
|
return to_enum __method__, enum, what unless block_given?
|
@@ -790,7 +791,7 @@ module RuboCop
|
|
790
791
|
end
|
791
792
|
|
792
793
|
def self.tokens(pattern)
|
793
|
-
pattern.scan(TOKEN).grep_v(ONLY_SEPARATOR)
|
794
|
+
pattern.gsub(COMMENT, '').scan(TOKEN).grep_v(ONLY_SEPARATOR)
|
794
795
|
end
|
795
796
|
|
796
797
|
# This method minimizes the closure for our method
|
@@ -72,23 +72,23 @@ module RuboCop
|
|
72
72
|
end
|
73
73
|
|
74
74
|
# @deprecated Use `comments.each`
|
75
|
-
def each_comment
|
76
|
-
comments.each
|
75
|
+
def each_comment(&block)
|
76
|
+
comments.each(&block)
|
77
77
|
end
|
78
78
|
|
79
|
-
# @deprecated Use `comments.find`
|
80
|
-
def find_comment
|
81
|
-
comments.find
|
79
|
+
# @deprecated Use `comment_at_line`, `each_comment_in_lines`, or `comments.find`
|
80
|
+
def find_comment(&block)
|
81
|
+
comments.find(&block)
|
82
82
|
end
|
83
83
|
|
84
84
|
# @deprecated Use `tokens.each`
|
85
|
-
def each_token
|
86
|
-
tokens.each
|
85
|
+
def each_token(&block)
|
86
|
+
tokens.each(&block)
|
87
87
|
end
|
88
88
|
|
89
89
|
# @deprecated Use `tokens.find`
|
90
|
-
def find_token
|
91
|
-
tokens.find
|
90
|
+
def find_token(&block)
|
91
|
+
tokens.find(&block)
|
92
92
|
end
|
93
93
|
|
94
94
|
def file_path
|
@@ -99,22 +99,39 @@ module RuboCop
|
|
99
99
|
ast.nil?
|
100
100
|
end
|
101
101
|
|
102
|
+
# @return [Comment, nil] the comment at that line, if any.
|
103
|
+
def comment_at_line(line)
|
104
|
+
comment_index[line]
|
105
|
+
end
|
106
|
+
|
102
107
|
# @return [Boolean] if the given line number has a comment.
|
103
108
|
def line_with_comment?(line)
|
104
|
-
|
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
|
105
121
|
end
|
106
122
|
|
107
123
|
# @return [Boolean] if any of the lines in the given `source_range` has a comment.
|
124
|
+
# Consider using `each_comment_in_lines` instead
|
108
125
|
def contains_comment?(source_range)
|
109
|
-
(source_range.line..source_range.last_line).any?
|
110
|
-
line_with_comment?(line)
|
111
|
-
end
|
126
|
+
each_comment_in_lines(source_range.line..source_range.last_line).any?
|
112
127
|
end
|
113
128
|
# @deprecated use contains_comment?
|
114
129
|
alias commented? contains_comment?
|
115
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.
|
116
133
|
def comments_before_line(line)
|
117
|
-
|
134
|
+
each_comment_in_lines(0..line).to_a
|
118
135
|
end
|
119
136
|
|
120
137
|
def start_with?(string)
|
@@ -144,8 +161,10 @@ module RuboCop
|
|
144
161
|
|
145
162
|
private
|
146
163
|
|
147
|
-
def
|
148
|
-
@
|
164
|
+
def comment_index
|
165
|
+
@comment_index ||= {}.tap do |hash|
|
166
|
+
comments.each { |c| hash[c.location.line] = c }
|
167
|
+
end
|
149
168
|
end
|
150
169
|
|
151
170
|
def parse(source, ruby_version)
|
@@ -156,6 +175,9 @@ module RuboCop
|
|
156
175
|
@buffer.source = source
|
157
176
|
rescue EncodingError => e
|
158
177
|
@parser_error = e
|
178
|
+
@ast = nil
|
179
|
+
@comments = []
|
180
|
+
@tokens = []
|
159
181
|
return
|
160
182
|
end
|
161
183
|
|
@@ -165,13 +187,15 @@ module RuboCop
|
|
165
187
|
def tokenize(parser)
|
166
188
|
begin
|
167
189
|
ast, comments, tokens = parser.tokenize(@buffer)
|
168
|
-
|
169
|
-
ast.respond_to?(:complete!) && ast.complete!
|
190
|
+
ast ||= nil # force `false` to `nil`, see https://github.com/whitequark/parser/pull/722
|
170
191
|
rescue Parser::SyntaxError
|
171
192
|
# All errors are in diagnostics. No need to handle exception.
|
193
|
+
comments = []
|
194
|
+
tokens = []
|
172
195
|
end
|
173
196
|
|
174
|
-
|
197
|
+
ast&.complete!
|
198
|
+
tokens.map! { |t| Token.from_parser_token(t) }
|
175
199
|
|
176
200
|
[ast, comments, tokens]
|
177
201
|
end
|
data/lib/rubocop/ast/version.rb
CHANGED
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
|
+
version: 0.3.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-
|
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.
|
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.
|
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
|
@@ -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
|