Class: Steep::Drivers::Scaffold::Generator
- Inherits:
-
Object
- Object
- Steep::Drivers::Scaffold::Generator
- Defined in:
- lib/steep/drivers/scaffold.rb
Defined Under Namespace
Classes: Module
Instance Attribute Summary collapse
-
#constants ⇒ Object
readonly
Returns the value of attribute constants.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
Instance Method Summary collapse
- #arg_types(args) ⇒ Object
- #each_child_node(node, &block) ⇒ Object
- #full_name(current_path, name) ⇒ Object
- #generate(node, current_path:, current_module: nil, is_instance_method: false) ⇒ Object
- #guess_type(node) ⇒ Object
-
#initialize(source:, stderr:) ⇒ Generator
constructor
A new instance of Generator.
- #module_name(name) ⇒ Object
- #write(io:) ⇒ Object
Constructor Details
#initialize(source:, stderr:) ⇒ Generator
Returns a new instance of Generator.
61 62 63 64 65 66 |
# File 'lib/steep/drivers/scaffold.rb', line 61 def initialize(source:, stderr:) @source = source @stderr = stderr @modules = [] @constants = {} end |
Instance Attribute Details
#constants ⇒ Object (readonly)
Returns the value of attribute constants.
58 59 60 |
# File 'lib/steep/drivers/scaffold.rb', line 58 def constants @constants end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
57 58 59 |
# File 'lib/steep/drivers/scaffold.rb', line 57 def modules @modules end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
56 57 58 |
# File 'lib/steep/drivers/scaffold.rb', line 56 def source @source end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
59 60 61 |
# File 'lib/steep/drivers/scaffold.rb', line 59 def stderr @stderr end |
Instance Method Details
#arg_types(args) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/steep/drivers/scaffold.rb', line 296 def arg_types(args) args.children.map do |arg| case arg.type when :arg "any" when :optarg "?#{guess_type(arg.children[1])}" when :restarg "*any" when :kwarg "#{arg.children.first.name}: any" when :kwoptarg "?#{arg.children.first.name}: #{guess_type(arg.children[1])}" when :kwrestarg "**any" end end.compact.join(", ") end |
#each_child_node(node, &block) ⇒ Object
288 289 290 291 292 293 294 |
# File 'lib/steep/drivers/scaffold.rb', line 288 def each_child_node(node, &block) node.children.each do |child| if child.is_a?(::AST::Node) yield child end end end |
#full_name(current_path, name) ⇒ Object
111 112 113 |
# File 'lib/steep/drivers/scaffold.rb', line 111 def full_name(current_path, name) (current_path + [name]).join("::") end |
#generate(node, current_path:, current_module: nil, is_instance_method: false) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/steep/drivers/scaffold.rb', line 115 def generate(node, current_path:, current_module: nil, is_instance_method: false) case node.type when :module name = module_name(node.children[0]) mod = Module.new(name: full_name(current_path, name), kind: :module) modules << mod if node.children[1] generate(node.children[1], current_path: current_path + [name], current_module: mod) end if current_module current_module.has_subclass = true end when :class name = module_name(node.children[0]) klass = Module.new(name: full_name(current_path, name), kind: :class) modules << klass if node.children[2] generate(node.children[2], current_path: current_path + [name], current_module: klass) end if current_module current_module.has_subclass = true end when :def name, args, body = node.children if current_module current_module.methods[name] = "(#{arg_types(args)}) -> #{guess_type(body)}" end if body generate(body, current_path: current_path, current_module: current_module, is_instance_method: true) end when :ivar, :ivasgn name = node.children[0] if current_module && is_instance_method current_module.ivars[name] = guess_type(node.children[1]) end each_child_node(node) do |child| generate(child, current_path: current_path, current_module: current_module, is_instance_method: is_instance_method) end when :defs if node.children[0].type == :self _, name, args, body = node.children if current_module current_module.singleton_methods[name] = "(#{arg_types(args)}) -> #{guess_type(body)}" end if body generate(body, current_path: current_path, current_module: current_module, is_instance_method: false) end end when :casgn if node.children[0] stderr.puts "Unexpected casgn: #{node}, #{node.loc.line}" end constants[full_name(current_path, node.children[1])] = guess_type(node.children[2]) if node.children[2] generate(node.children[2], current_path: current_path, current_module: current_module, is_instance_method: is_instance_method) end else each_child_node(node) do |child| generate(child, current_path: current_path, current_module: current_module, is_instance_method: is_instance_method) end end end |
#guess_type(node) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/steep/drivers/scaffold.rb', line 212 def guess_type(node) return "any" unless node case node.type when :false, :true "_Boolean" when :int "Integer" when :float "Float" when :complex "Complex" when :rational "Rational" when :str, :dstr, :xstr "String" when :sym, :dsym "Symbol" when :regexp "Regexp" when :array "Array<any>" when :hash "Hash<any, any>" when :irange, :erange "Range<any>" when :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn guess_type(node.children.last) when :send if node.children[1] == :[]= guess_type(node.children.last) else "any" end when :begin # should support shortcut return? guess_type(node.children.last) when :return children = node.children if children.size == 1 guess_type(node.children.last) else "Array<any>" # or Tuple or any? end when :if children = node.children if children[2] ty1 = guess_type(children[1]) ty2 = guess_type(children[2]) if ty1 == ty2 ty1 else "any" end else "void" # assuming no-else if statement implies void end when :while, :until, :while_post, :until_post, :for "void" when :case children = node.children if children.last ty = guess_type(children.last) children[1..-2].each do |child| return "any" if ty != guess_type(child.children.last) end ty else "any" end when :masgn "void" # assuming masgn implies void else "any" end end |
#module_name(name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/steep/drivers/scaffold.rb', line 97 def module_name(name) if name.type == :const prefix = name.children[0] if prefix "#{module_name(prefix)}::#{name.children[1]}" else name.children[1].to_s end else stderr.puts "Unexpected node for class name: #{name}" return "____" end end |
#write(io:) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/steep/drivers/scaffold.rb', line 68 def write(io:) generate(source.node, current_path: []) modules.each do |mod| unless mod.namespace_class? io.puts "#{mod.kind} #{mod.name}" mod.ivars.each do |name, type| io.puts " #{name}: #{type}" end mod.methods.each do |name, type| io.puts " def #{name}: #{type}" end mod.singleton_methods.each do |name, type| io.puts " def self.#{name}: #{type}" end io.puts "end" io.puts end end constants.each do |name, ty| io.puts "#{name}: #{ty}" end end |