Skip to content

Commit

Permalink
Add Scaffold::RBI
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Sep 10, 2019
1 parent 834b369 commit 38567a8
Show file tree
Hide file tree
Showing 9 changed files with 1,026 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/ruby/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "set"
require "json"
require "pathname"
require "pp"
require "ripper"

require "ruby/signature/errors"
require "ruby/signature/buffer"
Expand All @@ -24,5 +26,6 @@
require "ruby/signature/constant_table"
require "ruby/signature/ast/comment"
require "ruby/signature/writer"
require "ruby/signature/scaffold/rbi"

require "ruby/signature/parser"
5 changes: 5 additions & 0 deletions lib/ruby/signature/builtin_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def self.define(name, namespace: Namespace.root)
Enumerable = Name.define(:Enumerable)
Class = Name.define(:Class)
Module = Name.define(:Module)
Array = Name.define(:Array)
Hash = Name.define(:Hash)
Range = Name.define(:Range)
Enumerator = Name.define(:Enumerator)
Set = Name.define(:Set)
end
end
end
18 changes: 17 additions & 1 deletion lib/ruby/signature/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def initialize(stdout:, stderr:)
@stderr = stderr
end

COMMANDS = [:ast, :list, :ancestors, :methods, :method, :validate, :constant, :paths, :version]
COMMANDS = [:ast, :list, :ancestors, :methods, :method, :validate, :constant, :paths, :scaffold, :version]

def library_parse(opts, options:)
opts.on("-r LIBRARY") do |lib|
Expand Down Expand Up @@ -392,6 +392,22 @@ def run_paths(args, options)
end
end

def run_scaffold(args, options)
format = args.shift

parser = case format
when "rbi"
Scaffold::RBI.new()
end

args.each do |file|
parser.parse Pathname(file).read
end

writer = Writer.new(out: stdout)
writer.write parser.decls
end

def parse_type_name(string)
Namespace.parse(string).yield_self do |namespace|
last = namespace.path.last
Expand Down
9 changes: 9 additions & 0 deletions lib/ruby/signature/method_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def sub(s)
end
end

def update(type_params: self.type_params, type: self.type, block: self.block, location: self.location)
self.class.new(
type_params: type_params,
type: type,
block: block,
location: location
)
end

def free_variables(set = Set.new)
type.free_variables(set)
block&.type&.free_variables(set)
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby/signature/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def absolute!
self.class.new(path: path, absolute: true)
end

def relative!
self.class.new(path: path, absolute: false)
end

def empty?
path.empty?
end
Expand Down
Loading

0 comments on commit 38567a8

Please sign in to comment.