Skip to content

Commit ec7b1ca

Browse files
committed
[rail_inspector] Use Prism for Visitor::Attribute
1 parent 9f39304 commit ec7b1ca

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# frozen_string_literal: true
22

3-
require "syntax_tree"
3+
require "prism"
44

55
module RailInspector
66
module Visitor
7-
class Attribute < SyntaxTree::Visitor
7+
class Attribute < Prism::Visitor
88
attr_reader :attribute_map
99

1010
def initialize
@@ -13,31 +13,30 @@ def initialize
1313
end
1414

1515
def with_namespace(node)
16-
@namespace_stack << node.constant.constant.value
16+
@namespace_stack << node.constant_path.name
1717
visit_child_nodes(node)
1818
@namespace_stack.pop
1919
end
2020

21-
visit_method alias_method :visit_module, :with_namespace
21+
alias visit_class_node with_namespace
22+
alias visit_module_node with_namespace
2223

23-
visit_method alias_method :visit_class, :with_namespace
24-
25-
visit_method def visit_command(node)
26-
attr_access = node.message.value
24+
def visit_call_node(node)
25+
attr_access = node.name
2726
return unless ATTRIBUTE_METHODS.include?(attr_access)
2827

2928
full_namespace = @namespace_stack.join("::")
3029

3130
@attribute_map[full_namespace] ||= {}
3231
@attribute_map[full_namespace][attr_access] ||= Set.new
3332

34-
attributes = node.arguments.parts.map { |p| p.value.value }
33+
attributes = node.arguments.arguments.map { |p| p.value }
3534

3635
@attribute_map[full_namespace][attr_access].merge(attributes)
3736
end
3837

3938
private
40-
ATTRIBUTE_METHODS = %w[attr_accessor attr_reader attr_writer]
39+
ATTRIBUTE_METHODS = %i[attr_accessor attr_reader attr_writer]
4140
end
4241
end
4342
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "rail_inspector/visitor/attribute"
5+
6+
class AttributeTest < Minitest::Test
7+
def test_parses_attributes
8+
source = <<~FILE
9+
module Rails
10+
attr_accessor :logger
11+
12+
class Application
13+
class Configuration
14+
attr_accessor :yjit
15+
16+
attr_reader :log_level
17+
end
18+
end
19+
end
20+
FILE
21+
22+
visitor = RailInspector::Visitor::Attribute.new
23+
Prism.parse(source).value.accept(visitor)
24+
25+
assert_equal %w[logger], visitor.attribute_map["Rails"][:attr_accessor].to_a
26+
27+
config_map = visitor.attribute_map["Rails::Application::Configuration"]
28+
29+
assert_equal %w[yjit], config_map[:attr_accessor].to_a
30+
assert_equal %w[log_level], config_map[:attr_reader].to_a
31+
end
32+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# frozen_string_literal: true
22

33
require "active_support"
4+
require "minitest/autorun"

0 commit comments

Comments
 (0)