lowkey 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a9a4fe0baffd60656da5b3ccd8047b72b9c655a9e789797e01bdaf8950cd7a2
4
- data.tar.gz: 24f2f98ec543e28105e0494605f550e37d2437829fe1c29018f7b659c55128f2
3
+ metadata.gz: 3f105e73eb19ec3b29ba8a8e2452071c1bf9f693fdd5dc5449374bf31315566b
4
+ data.tar.gz: 95680c064c4020c42a473cef33e83d9277177ab22eae60ef8180cd79928fa5db
5
5
  SHA512:
6
- metadata.gz: 9949841c337d4f2bbe88f0bfaabadac171d1fb1db5a34a0ee1d8a090ac295c6f91ad87fcb8819c035e4be2bdd2d14d5eb296fb44bd5b24cf3396e0f21dcadbdd
7
- data.tar.gz: 4e96b53e4a83a9f53055e9d8041b54942453506b87a5cad97b494f1a091aa4b64fa8fef4d768fe5d8429199a98e04155ed6e09f6b2cdab31840f891a825d7a55
6
+ metadata.gz: 6427844a8671c39e1a6e3ff7298b74d89b6b128d1243c745e65a568498efe72bfc70f5fbe2ee277e5b4b632e18056bf6301f85418fe7dd201dbbd84d5258b9d5
7
+ data.tar.gz: fbb67e2e9a8d69848658b6bf51d4afaa1d6c96a46b630b85b74309e20791c0e0745680090d4cb56dd25a113f1e8957db159f2eb3704f29968a5093b08c8688a0
data/lib/lowkey.rb CHANGED
@@ -37,7 +37,7 @@ module Lowkey
37
37
 
38
38
  def config
39
39
  config = Struct.new(:cache)
40
- @config ||= config.new(false)
40
+ @config ||= config.new(true)
41
41
  end
42
42
 
43
43
  def configure
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
-
5
3
  module Lowkey
6
4
  class ClassProxy
7
- attr_reader :namespace, :file_proxy, :start_line, :end_line
8
- attr_accessor :private_start_line, :class_methods, :instance_methods, :method_calls
5
+ attr_reader :namespace, :start_line, :end_line
6
+ attr_writer :method_calls
7
+ attr_accessor :private_start_line, :class_methods, :instance_methods
9
8
 
10
9
  def initialize(node:, namespace:, file_proxy:)
11
10
  @namespace = namespace
12
11
  @file_proxy = file_proxy
13
12
 
14
- @start_line = node.class_keyword_loc.start_line
15
- @end_line = node.end_keyword_loc.end_line # class_keyword_loc ?
13
+ @start_line = node.respond_to?(:class_keyword_loc) ? node.class_keyword_loc.start_line : 0
14
+ @end_line = node.respond_to?(:end_keyword_loc) ? node.end_keyword_loc.end_line : @start_line
15
+ @end_line = file_proxy.end_line if namespace == 'Object'
16
16
  @private_start_line = nil
17
17
 
18
18
  @class_methods = {}
@@ -20,17 +20,46 @@ module Lowkey
20
20
  @method_calls = []
21
21
  end
22
22
 
23
- private
23
+ def method_calls(method_names = nil)
24
+ return @method_calls if method_names.nil?
25
+
26
+ @method_calls.filter { |method_call| method_names.include?(method_call.name) }
27
+ end
28
+
29
+ def file_path
30
+ @file_proxy.path
31
+ end
32
+
33
+ class << self
34
+ # Only a lambda defined immediately after a method's parameters/block is considered a return type expression.
35
+ def return_type(method_node:)
36
+ # Method statements.
37
+ statements_node = method_node.compact_child_nodes.find { |node| node.is_a?(Prism::StatementsNode) }
24
38
 
25
- def class_method?(node)
26
- return true if node.is_a?(::Prism::DefNode) && node.receiver.instance_of?(Prism::SelfNode) # self.method_name
27
- return true if node.is_a?(::Prism::SingletonClassNode) # class << self
39
+ # Block statements.
40
+ if statements_node.nil?
41
+ block_node = method_node.compact_child_nodes.find { |node| node.is_a?(Prism::BlockNode) }
42
+ statements_node = block_node.compact_child_nodes.find { |node| node.is_a?(Prism::StatementsNode) } if block_node
43
+ end
28
44
 
29
- if (parent_node = parent_map.parent(node:))
30
- return class_method?(parent_node)
45
+ return nil if statements_node.nil? # Sometimes developers define methods without code inside them.
46
+
47
+ node = statements_node.body.first
48
+ return node if node.is_a?(Prism::LambdaNode)
49
+
50
+ nil
31
51
  end
32
52
 
33
- false
53
+ def class_method?(node:, parent_map:)
54
+ return true if node.is_a?(::Prism::DefNode) && node.receiver.instance_of?(Prism::SelfNode) # self.method_name
55
+ return true if node.is_a?(::Prism::SingletonClassNode) # class << self
56
+
57
+ if (parent_node = parent_map[node])
58
+ return class_method?(node: parent_node, parent_map:)
59
+ end
60
+
61
+ false
62
+ end
34
63
  end
35
64
  end
36
65
  end
@@ -17,8 +17,9 @@ module Lowkey
17
17
  @dependencies = []
18
18
  end
19
19
 
20
- def class_proxy(node:, parent_map:)
20
+ def upsert_class_proxy(node:, parent_map:)
21
21
  namespace = namespace(node:, parent_map:).reverse.join('::')
22
+ namespace = 'Object' if namespace.empty?
22
23
  @definitions[namespace] ||= ClassProxy.new(node:, namespace:, file_proxy: self)
23
24
  end
24
25
 
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lowkey
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class MethodCallVisitor
4
- attr_reader :parent_map
3
+ module Lowkey
4
+ class MethodCallVisitor
5
+ attr_reader :parent_map
5
6
 
6
- def initialize(file_proxy:, parent_map:)
7
- @file_proxy = file_proxy
8
- @parent_map = parent_map
9
- end
7
+ def initialize(file_proxy:, parent_map:)
8
+ @file_proxy = file_proxy
9
+ @parent_map = parent_map
10
+ end
10
11
 
11
- def visit(node)
12
- class_proxy = @file_proxy.class_proxy(node:, parent_map:)
13
- class_proxy.method_calls << node
12
+ def visit(node)
13
+ class_proxy = @file_proxy.upsert_class_proxy(node:, parent_map:)
14
+ class_proxy.method_calls << node
14
15
 
15
- return unless node.name == :private && node.respond_to?(:start_line) && class_proxy.start_line && class_proxy.end_line
16
- return unless node.start_line > class_proxy.start_line && node.start_line < class_proxy.end_line
16
+ return unless node.name == :private && node.respond_to?(:start_line) && class_proxy.start_line && class_proxy.end_line
17
+ return unless node.start_line > class_proxy.start_line && node.start_line < class_proxy.end_line
17
18
 
18
- class_proxy.private_start_line = node.start_line
19
+ class_proxy.private_start_line = node.start_line
20
+ end
19
21
  end
20
22
  end
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class MethodClassVisitor
4
- attr_reader :parent_map
3
+ module Lowkey
4
+ class MethodClassVisitor
5
+ attr_reader :parent_map
5
6
 
6
- def initialize(file_proxy:, parent_map:)
7
- @file_proxy = file_proxy
8
- @parent_map = parent_map
9
- end
7
+ def initialize(file_proxy:, parent_map:)
8
+ @file_proxy = file_proxy
9
+ @parent_map = parent_map
10
+ end
10
11
 
11
- def visit(node)
12
- @file_proxy.class_proxy(node:, parent_map:)
12
+ def visit(node)
13
+ @file_proxy.upsert_class_proxy(node:, parent_map:)
14
+ end
13
15
  end
14
16
  end
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class MethodDefVisitor
4
- attr_reader :parent_map
3
+ module Lowkey
4
+ class MethodDefVisitor
5
+ attr_reader :parent_map
5
6
 
6
- def initialize(file_proxy:, parent_map:)
7
- @file_proxy = file_proxy
8
- @parent_map = parent_map
9
- end
7
+ def initialize(file_proxy:, parent_map:)
8
+ @file_proxy = file_proxy
9
+ @parent_map = parent_map
10
+ end
10
11
 
11
- def visit(node)
12
- class_proxy = @file_proxy.class_proxy(node:, parent_map:)
12
+ def visit(node)
13
+ class_proxy = @file_proxy.upsert_class_proxy(node:, parent_map:)
13
14
 
14
- if class_proxy.class_method?(node:, parent_map:)
15
- class_proxy.class_methods[node.name] = node
16
- else
17
- class_proxy.instance_methods[node.name] = node
15
+ if ClassProxy.class_method?(node:, parent_map:)
16
+ class_proxy.class_methods[node.name] = node
17
+ else
18
+ class_proxy.instance_methods[node.name] = node
19
+ end
18
20
  end
19
21
  end
20
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi