-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrbs.rb
70 lines (58 loc) · 1.65 KB
/
rbs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# frozen_string_literal: true
require "prettier_print"
require "rbs"
require "syntax_tree"
module SyntaxTree
module RBS
# This is the parent class of any of the visitors that we define in this
# module. It is used to walk through the tree.
class Visitor
def visit(node)
node&.accept(self)
end
end
# A slight extension to the default PrettierPrint formatter that keeps track
# of the source (so that it can be referenced by annotations if they need
# it) and keeps track of the level of intersections and unions so that
# parentheses can be forced if necessary.
class Formatter < PrettierPrint
attr_reader :source
def initialize(source, *rest)
super(*rest)
@source = source
@force_parens = false
end
def force_parens
old_force_parens = @force_parens
@force_parens = true
yield
ensure
@force_parens = old_force_parens
end
def force_parens?
@force_parens
end
end
class << self
def format(source, maxwidth = 80, _options = {})
formatter = Formatter.new(source, [], maxwidth)
parse(source).format(formatter)
formatter.flush
formatter.output.join
end
def parse(source)
_, _, declarations = ::RBS::Parser.parse_signature(source)
Root.new(declarations)
end
def read(filepath)
File.read(filepath)
end
end
end
register_handler(".rbs", RBS)
end
require_relative "rbs/shims"
require_relative "rbs/entrypoints"
require_relative "rbs/format"
require_relative "rbs/pretty_print"
require_relative "rbs/version"