rubocop-schema 0.2.5 → 0.2.6

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
  SHA1:
3
- metadata.gz: 74ebb239864efb97d282fd4b9e8064ad9fafafb9
4
- data.tar.gz: 1b30fa6f53e8e3f2c20015d32cb6e8f41b28887e
3
+ metadata.gz: 5605a5aa19b68890f91c69788d78b5a675e86f67
4
+ data.tar.gz: 5cf07e32d62124447777ed27c49b02122e922372
5
5
  SHA512:
6
- metadata.gz: e0cee0a3383c134ef4f7241b936d3afc9cbf7669e43df9e235a26f42aa8c36e4a28625864f9d740620487641adfe0dfe3586deb7c25ce420df4ccc5615296982
7
- data.tar.gz: e3aae90d5df023bb9c4279de2a414aaade64d1d70f497e73ba50a581aa369486748980cbb900afddf8c253531c0292b1fc3a1d212348c9fe0b402d7ff6b54348
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
@@ -1,5 +1,7 @@
1
1
  require 'rubocop'
2
2
  require 'rubocop/style/inject'
3
+ require 'rubocop/ast/builder'
4
+ require 'rubocop/ast/node/def_node'
3
5
  RuboCop::Style::Inject.defaults!
4
6
 
5
7
  require 'rubocop/cop/style/schema_comments'
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.5
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