Class: Mutineer::Project
- Inherits:
-
Object
- Object
- Mutineer::Project
- Defined in:
- lib/mutineer/project.rb
Overview
Subject discovery: parse each path and walk its AST for method definitions, tracking the enclosing class/module namespace.
Defined Under Namespace
Classes: SubjectVisitor
Class Method Summary collapse
-
.created_method_locations(node) ⇒ Array?
private
The source line and names Ruby assigns to explicit method creators.
-
.discover(paths, only: nil) ⇒ Array<Mutineer::Subject>
Discovers subjects from source paths.
-
.exclude_ambiguous_hook_defs!(subjects, program) ⇒ void
private
Keeps hook defs unplaceable when the raw AST can define another method with the same name and source line.
-
.literal_method_name(node) ⇒ Symbol?
private
A literal method name, or nil when it depends on a runtime value.
-
.plain_path_calls(program) ⇒ Set<Prism::CallNode>
private
The call statements that run each time the file loads, in order: each one sits in the file's top-level statements or in a
moduleorclassbody reached only through such statements. -
.top_level_return?(node) ⇒ Boolean
private
True when the node holds a
returnthat is not inside a def, a block or a lambda, so loading the file can stop early.
Class Method Details
.created_method_locations(node) ⇒ Array? (private)
The source line and names Ruby assigns to explicit method creators. A block-defined method starts at the block opening and a literal lambda at its arrow. A positional callable takes precedence over a supplied block. Other positional bodies and block passes have an indirect source: the caller checks possible block/lambda lines instead of the call line. An attribute method starts at the message token, not its receiver line. Literal symbol and string names are exact. A dynamic name, splat, or attr writer flag may name any method on that line, represented by nil.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mutineer/project.rb', line 88 def self.created_method_locations(node) arguments = node.arguments&.arguments || [] case node.name when :define_method, :define_singleton_method body = arguments[1] || node.block body = body.expression if body.is_a?(Prism::BlockArgumentNode) line = body.location.start_line if body.is_a?(Prism::BlockNode) || body.is_a?(Prism::LambdaNode) return [line, [literal_method_name(arguments.first)]] when :attr_reader, :attr_writer, :attr_accessor, :attr mode = { attr_reader: :reader, attr_writer: :writer, attr_accessor: :both, attr: :reader }.fetch(node.name) if node.name == :attr && arguments.size == 2 && (arguments.last.is_a?(Prism::TrueNode) || arguments.last.is_a?(Prism::FalseNode)) mode = arguments.last.is_a?(Prism::TrueNode) ? :both : :reader arguments = arguments.first(1) end names = arguments.flat_map do |argument| name = literal_method_name(argument) next [nil] unless name case mode when :reader then [name] when :writer then [:"#{name}="] else [name, :"#{name}="] end end [node..start_line, names] end end |
.discover(paths, only: nil) ⇒ Array<Mutineer::Subject>
Discovers subjects from source paths.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mutineer/project.rb', line 17 def self.discover(paths, only: nil) subjects = Array(paths).flat_map do |path| result = Parser.parse_file(path) visitor = SubjectVisitor.new(path, plain_path_calls(result.value)) visitor.visit(result.value) visitor.promote_module_functions! exclude_ambiguous_hook_defs!(visitor.subjects, result.value) visitor.subjects end only ? subjects.select { |s| s.qualified_name == only } : subjects end |
.exclude_ambiguous_hook_defs!(subjects, program) ⇒ void (private)
This method returns an undefined value.
Keeps hook defs unplaceable when the raw AST can define another method
with the same name and source line. Count all defs, including bodies that
SubjectVisitor does not select, and Ruby's explicit method creators (see
created_method_locations). Indirect callable bodies may come from any
block or lambda in this file, including a stored hook block. An unknown
creator name blocks only those possible source lines. Instance and
singleton definitions can both reach singleton-class hosts, so their
shared name and line are ambiguous too. Run before only so selecting
one subject cannot hide a collision.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mutineer/project.rb', line 42 def self.exclude_ambiguous_hook_defs!(subjects, program) return unless subjects.any?(&:reload_hook_call) counts = Hash.new(0) body_lines = Set.new indirect_names = Set.new nodes = [program] while (node = nodes.pop) nodes.concat(node.compact_child_nodes) if node.is_a?(Prism::LambdaNode) || node.is_a?(Prism::BlockNode) body_lines << node.location.start_line end location = if node.is_a?(Prism::DefNode) [node.location.start_line, [node.name]] elsif node.is_a?(Prism::CallNode) created_method_locations(node) end next unless location line, names = location if line names.each { |name| counts[[line, name]] += 1 } else names.each { |name| indirect_names << name } end end indirect_names.each { |name| body_lines.each { |line| counts[[line, name]] += 1 } } subjects.each do |subject| line = subject.def_node.location.start_line subject.reload_hook_call = nil if counts[[line, subject.name]] > 1 || counts[[line, nil]].positive? end end |
.literal_method_name(node) ⇒ Symbol? (private)
A literal method name, or nil when it depends on a runtime value.
122 123 124 |
# File 'lib/mutineer/project.rb', line 122 def self.literal_method_name(node) node.unescaped.to_sym if node.is_a?(Prism::SymbolNode) || node.is_a?(Prism::StringNode) end |
.plain_path_calls(program) ⇒ Set<Prism::CallNode> (private)
The call statements that run each time the file loads, in order: each one
sits in the file's top-level statements or in a module or class body
reached only through such statements. A call under any if, unless,
case, &&, rescue or loop, an assignment, an argument, a def or a block
is not on this path. A file with a return outside any def or block may
stop before a call, so it has none (#297).
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mutineer/project.rb', line 136 def self.plain_path_calls(program) calls = Set.new.compare_by_identity return calls if top_level_return?(program) bodies = [program.statements] while (body = bodies.pop) body.body.each do |node| calls << node if node.is_a?(Prism::CallNode) bodies << node.body if (node.is_a?(Prism::ModuleNode) || node.is_a?(Prism::ClassNode)) && node.body.is_a?(Prism::StatementsNode) end end calls end |
.top_level_return?(node) ⇒ Boolean (private)
True when the node holds a return that is not inside a def, a block or a
lambda, so loading the file can stop early.
157 158 159 160 161 162 |
# File 'lib/mutineer/project.rb', line 157 def self.top_level_return?(node) return true if node.is_a?(Prism::ReturnNode) return false if node.is_a?(Prism::DefNode) || node.is_a?(Prism::BlockNode) || node.is_a?(Prism::LambdaNode) node.compact_child_nodes.any? { |child| top_level_return?(child) } end |