rubocop-schema 0.2.5 → 0.2.6
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.
- checksums.yaml +4 -4
- data/lib/rubocop/ast/builder.rb +68 -0
- data/lib/rubocop/ast/node/def_node.rb +71 -0
- data/lib/rubocop-schema.rb +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5605a5aa19b68890f91c69788d78b5a675e86f67
|
4
|
+
data.tar.gz: 5cf07e32d62124447777ed27c49b02122e922372
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48264645ec6bbed4ddcb05b845fbdcc9d7d5cbc512f26fa2e75e30a6d0ea881824e127dd7f41912231d55a070973a11140e8b03453e27b710441649d02c3e736
|
7
|
+
data.tar.gz: dd864cc9159875b3695a2a8fb7bb7bba06458169a4f215df1d25b1f1d90812776320cd434d2e842f13e7b613979c710eb86b58f120d74fb5ea4b6eefbe2db73e
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# `RuboCop::Builder` is an AST builder that is utilized to let `Parser`
|
6
|
+
# generate ASTs with {RuboCop::AST::Node}.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# buffer = Parser::Source::Buffer.new('(string)')
|
10
|
+
# buffer.source = 'puts :foo'
|
11
|
+
#
|
12
|
+
# builder = RuboCop::Builder.new
|
13
|
+
# parser = Parser::CurrentRuby.new(builder)
|
14
|
+
# root_node = parser.parse(buffer)
|
15
|
+
class Builder < Parser::Builders::Default
|
16
|
+
NODE_MAP = {
|
17
|
+
AndNode => [:and],
|
18
|
+
ArgsNode => [:args],
|
19
|
+
ArrayNode => [:array],
|
20
|
+
DefNode => %i[def defs],
|
21
|
+
BlockNode => [:block],
|
22
|
+
CaseNode => [:case],
|
23
|
+
EnsureNode => [:ensure],
|
24
|
+
ForNode => [:for],
|
25
|
+
HashNode => [:hash],
|
26
|
+
IfNode => [:if],
|
27
|
+
KeywordSplatNode => [:kwsplat],
|
28
|
+
OrNode => [:or],
|
29
|
+
PairNode => [:pair],
|
30
|
+
ResbodyNode => [:resbody],
|
31
|
+
SendNode => %i[csend send],
|
32
|
+
SuperNode => %i[super zsuper],
|
33
|
+
UntilNode => %i[until until_post],
|
34
|
+
WhenNode => [:when],
|
35
|
+
WhileNode => %i[while while_post]
|
36
|
+
}.freeze
|
37
|
+
|
38
|
+
# Generates {Node} from the given information.
|
39
|
+
#
|
40
|
+
# @return [Node] the generated node
|
41
|
+
def n(type, children, source_map)
|
42
|
+
node_klass(type).new(type, children, location: source_map)
|
43
|
+
end
|
44
|
+
|
45
|
+
# TODO: Figure out what to do about literal encoding handling...
|
46
|
+
# More details here https://github.com/whitequark/parser/issues/283
|
47
|
+
def string_value(token)
|
48
|
+
value(token)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def node_klass(type)
|
54
|
+
node_map[type] || Node
|
55
|
+
end
|
56
|
+
|
57
|
+
# Take the human readable constant and generate a hash map where each
|
58
|
+
# (mapped) node type is a key with its constant as the value.
|
59
|
+
def node_map
|
60
|
+
@node_map ||= begin
|
61
|
+
NODE_MAP.each_pair.each_with_object({}) do |(klass, types), map|
|
62
|
+
types.each { |type| map[type] = klass }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module AST
|
5
|
+
# A node extension for `def` nodes. This will be used in place of a plain
|
6
|
+
# node when the builder constructs the AST, making its methods available
|
7
|
+
# to all `def` nodes within RuboCop.
|
8
|
+
class DefNode < Node
|
9
|
+
include ParameterizedNode
|
10
|
+
include MethodIdentifierPredicates
|
11
|
+
|
12
|
+
# Checks whether this node body is a void context.
|
13
|
+
#
|
14
|
+
# @return [Boolean] whether the `def` node body is a void context
|
15
|
+
def void_context?
|
16
|
+
method?(:initialize) || assignment_method?
|
17
|
+
end
|
18
|
+
|
19
|
+
# The name of the defined method as a symbol.
|
20
|
+
#
|
21
|
+
# @return [Symbol] the name of the defined method
|
22
|
+
def method_name
|
23
|
+
node_parts[2]
|
24
|
+
end
|
25
|
+
|
26
|
+
# An array containing the arguments of the method definition.
|
27
|
+
#
|
28
|
+
# @return [Array<Node>] the arguments of the method definition
|
29
|
+
def arguments
|
30
|
+
node_parts[1]
|
31
|
+
end
|
32
|
+
|
33
|
+
# The body of the method definition.
|
34
|
+
#
|
35
|
+
# @note this can be either a `begin` node, if the method body contains
|
36
|
+
# multiple expressions, or any other node, if it contains a single
|
37
|
+
# expression.
|
38
|
+
#
|
39
|
+
# @return [Node] the body of the method definition
|
40
|
+
def body
|
41
|
+
node_parts[0]
|
42
|
+
end
|
43
|
+
|
44
|
+
# The receiver of the method definition, if any.
|
45
|
+
#
|
46
|
+
# @return [Node, nil] the receiver of the method definition, or `nil`.
|
47
|
+
def receiver
|
48
|
+
node_parts[3]
|
49
|
+
end
|
50
|
+
|
51
|
+
# Custom destructuring method. This can be used to normalize
|
52
|
+
# destructuring for different variations of the node.
|
53
|
+
#
|
54
|
+
# In this case, the `def` node destructures into:
|
55
|
+
#
|
56
|
+
# `method_name, arguments, body`
|
57
|
+
#
|
58
|
+
# while the `defs` node destructures into:
|
59
|
+
#
|
60
|
+
# `receiver, method_name, arguments, body`
|
61
|
+
#
|
62
|
+
# so we reverse the destructured array to get the optional receiver
|
63
|
+
# at the end, where it can be discarded.
|
64
|
+
#
|
65
|
+
# @return [Array] the different parts of the `def` or `defs` node
|
66
|
+
def node_parts
|
67
|
+
to_a.reverse
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/rubocop-schema.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ceclinux
|
@@ -32,6 +32,8 @@ extra_rdoc_files: []
|
|
32
32
|
files:
|
33
33
|
- config/default.yml
|
34
34
|
- lib/rubocop-schema.rb
|
35
|
+
- lib/rubocop/ast/builder.rb
|
36
|
+
- lib/rubocop/ast/node/def_node.rb
|
35
37
|
- lib/rubocop/cop/style/migration_comments.rb
|
36
38
|
- lib/rubocop/cop/style/schema_comments.rb
|
37
39
|
- lib/rubocop/style/inject.rb
|