rbi 0.0.11 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 727196c54429957687b3624d4b5a895e526973b045689a7689edb9cd73c97ca9
4
- data.tar.gz: 115a6efabd78950aa980d0b03e3b9910f474ad03cdf04f7a87b56ed38bdc30b7
3
+ metadata.gz: 570f1035fa8bb56861eabd49590fbf2374f428c053f0fc189e27958feedf0c4b
4
+ data.tar.gz: 0dbb944e45b675851de18089c22a23c4c527f03dfca281426db8fb3cff94372e
5
5
  SHA512:
6
- metadata.gz: 7357ca0bb3e8700a95d1ea6e285e710f3545a9016e36bc0a64fe0682da80328d5c645b4d6cd3d14bba14e7ccd6b9134c225944ce27625de6a26a995f5283d173
7
- data.tar.gz: 71559ec6d855d46d3ac7bb074dbfd24fa024aaa6563dd241a7c286b66d138bf18f6c6e688ee33df99bdc240cbd6f683ebc068cb5c98b7e2b37dde6d4c9debca8
6
+ metadata.gz: 45e5f53144cc46f1cd2c86b55c1d490ab5bd4b2d3bf5e593b3f6405e6b4583645c13130cdc6762f0e5eb18f5f74bfa1dfe990d108f6db21da394ba4d30a7b810
7
+ data.tar.gz: 603617b73fc3e4e862f897bf0b10b165efd4ac608c07b71e3ef43e1190e4d1fff2f26484a65d8f4d9e5965eafa99435d02bf668fef756abe5a24f1fa3e41f52b
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  source "https://rubygems.org"
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/gem_tasks"
@@ -0,0 +1,66 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ class Formatter
6
+ extend T::Sig
7
+
8
+ sig { returns(T::Boolean) }
9
+ attr_accessor :add_sig_templates, :group_nodes, :nest_singleton_methods, :nest_non_public_methods, :sort_nodes
10
+
11
+ sig { returns(T.nilable(Integer)) }
12
+ attr_accessor :max_line_length
13
+
14
+ sig do
15
+ params(
16
+ add_sig_templates: T::Boolean,
17
+ group_nodes: T::Boolean,
18
+ max_line_length: T.nilable(Integer),
19
+ nest_singleton_methods: T::Boolean,
20
+ nest_non_public_methods: T::Boolean,
21
+ sort_nodes: T::Boolean
22
+ ).void
23
+ end
24
+ def initialize(
25
+ add_sig_templates: false,
26
+ group_nodes: false,
27
+ max_line_length: nil,
28
+ nest_singleton_methods: false,
29
+ nest_non_public_methods: false,
30
+ sort_nodes: false
31
+ )
32
+ @add_sig_templates = add_sig_templates
33
+ @group_nodes = group_nodes
34
+ @max_line_length = max_line_length
35
+ @nest_singleton_methods = nest_singleton_methods
36
+ @nest_non_public_methods = nest_non_public_methods
37
+ @sort_nodes = sort_nodes
38
+ end
39
+
40
+ sig { params(file: RBI::File).returns(String) }
41
+ def print_file(file)
42
+ format_file(file)
43
+ file.string(max_line_length: @max_line_length)
44
+ end
45
+
46
+ sig { params(tree: RBI::Tree).returns(String) }
47
+ def print_tree(tree)
48
+ format_tree(tree)
49
+ tree.string(max_line_length: @max_line_length)
50
+ end
51
+
52
+ sig { params(file: RBI::File).void }
53
+ def format_file(file)
54
+ format_tree(file.root)
55
+ end
56
+
57
+ sig { params(tree: RBI::Tree).void }
58
+ def format_tree(tree)
59
+ tree.add_sig_templates! if @add_sig_templates
60
+ tree.nest_singleton_methods! if @nest_singleton_methods
61
+ tree.nest_non_public_methods! if @nest_non_public_methods
62
+ tree.group_nodes! if @group_nodes
63
+ tree.sort_nodes! if @sort_nodes
64
+ end
65
+ end
66
+ end
data/lib/rbi/loc.rb CHANGED
@@ -32,5 +32,20 @@ module RBI
32
32
  def to_s
33
33
  "#{file}:#{begin_line}:#{begin_column}-#{end_line}:#{end_column}"
34
34
  end
35
+
36
+ sig { returns(T.nilable(String)) }
37
+ def source
38
+ file = self.file
39
+ return nil unless file
40
+ return nil unless ::File.file?(file)
41
+
42
+ return ::File.read(file) unless begin_line && end_line
43
+
44
+ string = String.new
45
+ ::File.foreach(file).with_index do |line, line_number|
46
+ string << line if line_number + 1 >= begin_line && line_number + 1 <= end_line
47
+ end
48
+ string
49
+ end
35
50
  end
36
51
  end
data/lib/rbi/model.rb CHANGED
@@ -1026,7 +1026,7 @@ module RBI
1026
1026
  attr_accessor :return_type
1027
1027
 
1028
1028
  sig { returns(T::Boolean) }
1029
- attr_accessor :is_abstract, :is_override, :is_overridable
1029
+ attr_accessor :is_abstract, :is_override, :is_overridable, :is_final
1030
1030
 
1031
1031
  sig { returns(T::Array[String]) }
1032
1032
  attr_reader :type_params
@@ -1041,6 +1041,7 @@ module RBI
1041
1041
  is_abstract: T::Boolean,
1042
1042
  is_override: T::Boolean,
1043
1043
  is_overridable: T::Boolean,
1044
+ is_final: T::Boolean,
1044
1045
  type_params: T::Array[String],
1045
1046
  checked: T.nilable(Symbol),
1046
1047
  loc: T.nilable(Loc),
@@ -1053,6 +1054,7 @@ module RBI
1053
1054
  is_abstract: false,
1054
1055
  is_override: false,
1055
1056
  is_overridable: false,
1057
+ is_final: false,
1056
1058
  type_params: [],
1057
1059
  checked: nil,
1058
1060
  loc: nil,
@@ -1064,6 +1066,7 @@ module RBI
1064
1066
  @is_abstract = is_abstract
1065
1067
  @is_override = is_override
1066
1068
  @is_overridable = is_overridable
1069
+ @is_final = is_final
1067
1070
  @type_params = type_params
1068
1071
  @checked = checked
1069
1072
  block&.call(self)
@@ -1078,7 +1081,7 @@ module RBI
1078
1081
  def ==(other)
1079
1082
  return false unless other.is_a?(Sig)
1080
1083
  params == other.params && return_type == other.return_type && is_abstract == other.is_abstract &&
1081
- is_override == other.is_override && is_overridable == other.is_overridable &&
1084
+ is_override == other.is_override && is_overridable == other.is_overridable && is_final == other.is_final &&
1082
1085
  type_params == other.type_params && checked == other.checked
1083
1086
  end
1084
1087
  end
data/lib/rbi/parser.rb CHANGED
@@ -17,6 +17,39 @@ module RBI
17
17
  end
18
18
  end
19
19
 
20
+ class UnexpectedParserError < StandardError
21
+ extend T::Sig
22
+
23
+ sig { returns(Loc) }
24
+ attr_reader :last_location
25
+
26
+ sig { params(parent_exception: Exception, last_location: Loc).void }
27
+ def initialize(parent_exception, last_location)
28
+ super(parent_exception)
29
+ set_backtrace(parent_exception.backtrace)
30
+ @last_location = last_location
31
+ end
32
+
33
+ sig { params(io: T.any(IO, StringIO)).void }
34
+ def print_debug(io: $stderr)
35
+ io.puts ""
36
+ io.puts "##################################"
37
+ io.puts "### RBI::Parser internal error ###"
38
+ io.puts "##################################"
39
+ io.puts ""
40
+ io.puts "There was an internal parser error while processing this source."
41
+ io.puts ""
42
+ io.puts "Error: #{message} while parsing #{last_location}:"
43
+ io.puts ""
44
+ io.puts last_location.source || "<no source>"
45
+ io.puts ""
46
+ io.puts "Please open an issue at https://github.com/Shopify/rbi/issues/new."
47
+ io.puts ""
48
+ io.puts "##################################"
49
+ io.puts ""
50
+ end
51
+ end
52
+
20
53
  class Parser
21
54
  extend T::Sig
22
55
 
@@ -77,6 +110,19 @@ module RBI
77
110
  builder.tree
78
111
  rescue ::Parser::SyntaxError => e
79
112
  raise ParseError.new(e.message, Loc.from_ast_loc(file, e.diagnostic.location))
113
+ rescue ParseError => e
114
+ raise e
115
+ rescue => e
116
+ last_node = builder&.last_node
117
+ last_location = if last_node
118
+ Loc.from_ast_loc(file, last_node.location)
119
+ else
120
+ Loc.new(file: file)
121
+ end
122
+
123
+ exception = UnexpectedParserError.new(e, last_location)
124
+ exception.print_debug
125
+ raise exception
80
126
  end
81
127
  end
82
128
 
@@ -113,6 +159,9 @@ module RBI
113
159
  sig { returns(Tree) }
114
160
  attr_reader :tree
115
161
 
162
+ sig { returns(T.nilable(::AST::Node)) }
163
+ attr_reader :last_node
164
+
116
165
  sig do
117
166
  params(
118
167
  file: String,
@@ -127,6 +176,7 @@ module RBI
127
176
  @nodes_comments_assoc = nodes_comments_assoc
128
177
  @tree = T.let(Tree.new, Tree)
129
178
  @scopes_stack = T.let([@tree], T::Array[Tree])
179
+ @last_node = T.let(nil, T.nilable(::AST::Node))
130
180
  @last_sigs = T.let([], T::Array[RBI::Sig])
131
181
 
132
182
  separate_header_comments
@@ -141,6 +191,8 @@ module RBI
141
191
  sig { override.params(node: T.nilable(Object)).void }
142
192
  def visit(node)
143
193
  return unless node.is_a?(AST::Node)
194
+ @last_node = node
195
+
144
196
  case node.type
145
197
  when :module, :class, :sclass
146
198
  scope = parse_scope(node)
@@ -165,6 +217,8 @@ module RBI
165
217
  else
166
218
  visit_all(node.children)
167
219
  end
220
+
221
+ @last_node = nil
168
222
  end
169
223
 
170
224
  private
@@ -289,7 +343,14 @@ module RBI
289
343
  names = node.children[2..-1].map { |child| parse_name(child) }
290
344
  MixesInClassMethods.new(*names, loc: loc, comments: comments)
291
345
  when :public, :protected, :private
292
- visibility = Visibility.new(method_name, loc: loc)
346
+ visibility = case method_name
347
+ when :protected
348
+ Protected.new(loc: loc)
349
+ when :private
350
+ Private.new(loc: loc)
351
+ else
352
+ Public.new(loc: loc)
353
+ end
293
354
  nested_node = node.children[2]
294
355
  case nested_node&.type
295
356
  when :def, :defs
@@ -422,7 +483,14 @@ module RBI
422
483
  sig { params(node: AST::Node).returns(TEnumBlock) }
423
484
  def parse_enum(node)
424
485
  enum = TEnumBlock.new
425
- node.children[2].children.each do |child|
486
+
487
+ body = if node.children[2].type == :begin
488
+ node.children[2].children
489
+ else
490
+ [node.children[2]]
491
+ end
492
+
493
+ body.each do |child|
426
494
  enum << parse_name(child)
427
495
  end
428
496
  enum.loc = node_loc(node)
@@ -561,7 +629,7 @@ module RBI
561
629
  sig { params(node: AST::Node).returns(Sig) }
562
630
  def self.build(node)
563
631
  v = SigBuilder.new
564
- v.visit_all(node.children[2..-1])
632
+ v.visit_all(node.children)
565
633
  v.current
566
634
  end
567
635
 
@@ -588,6 +656,9 @@ module RBI
588
656
  visit(node.children[0]) if node.children[0]
589
657
  name = node.children[1]
590
658
  case name
659
+ when :sig
660
+ arg = node.children[2]
661
+ @current.is_final = arg && arg.children[0] == :final
591
662
  when :abstract
592
663
  @current.is_abstract = true
593
664
  when :override
@@ -595,19 +666,25 @@ module RBI
595
666
  when :overridable
596
667
  @current.is_overridable = true
597
668
  when :checked
598
- @current.checked = node.children[2].children[0]
669
+ if node.children.length >= 3
670
+ @current.checked = node.children[2].children[0]
671
+ end
599
672
  when :type_parameters
600
673
  node.children[2..-1].each do |child|
601
674
  @current.type_params << child.children[0].to_s
602
675
  end
603
676
  when :params
604
- node.children[2].children.each do |child|
605
- name = child.children[0].children[0].to_s
606
- type = parse_expr(child.children[1])
607
- @current << SigParam.new(name, type)
677
+ if node.children.length >= 3
678
+ node.children[2].children.each do |child|
679
+ name = child.children[0].children[0].to_s
680
+ type = parse_expr(child.children[1])
681
+ @current << SigParam.new(name, type)
682
+ end
608
683
  end
609
684
  when :returns
610
- @current.return_type = parse_expr(node.children[2])
685
+ if node.children.length >= 3
686
+ @current.return_type = parse_expr(node.children[2])
687
+ end
611
688
  when :void
612
689
  @current.return_type = nil
613
690
  else
data/lib/rbi/printer.rb CHANGED
@@ -200,7 +200,7 @@ module RBI
200
200
  end
201
201
 
202
202
  lines.each do |line|
203
- text = line.strip
203
+ text = line.rstrip
204
204
  v.printt("#")
205
205
  v.print(" #{text}") unless text.empty?
206
206
  v.printn
@@ -455,7 +455,7 @@ module RBI
455
455
 
456
456
  sig { returns(T::Array[String]) }
457
457
  def comments_lines
458
- comments.flat_map { |comment| comment.text.lines.map(&:strip) }
458
+ comments.flat_map { |comment| comment.text.lines.map(&:rstrip) }
459
459
  end
460
460
  end
461
461
 
@@ -670,7 +670,9 @@ module RBI
670
670
 
671
671
  sig { params(v: Printer).void }
672
672
  def print_as_line(v)
673
- v.printt("sig { ")
673
+ v.printt("sig")
674
+ v.print("(:final)") if is_final
675
+ v.print(" { ")
674
676
  sig_modifiers.each do |modifier|
675
677
  v.print("#{modifier}.")
676
678
  end
@@ -758,7 +760,7 @@ module RBI
758
760
 
759
761
  sig { returns(T::Array[String]) }
760
762
  def comments_lines
761
- comments.flat_map { |comment| comment.text.lines.map(&:strip) }
763
+ comments.flat_map { |comment| comment.text.lines.map(&:rstrip) }
762
764
  end
763
765
  end
764
766
 
@@ -47,6 +47,8 @@ module RBI
47
47
  sig { returns(Group::Kind) }
48
48
  def group_kind
49
49
  case self
50
+ when Group
51
+ kind
50
52
  when Include, Extend
51
53
  Group::Kind::Mixins
52
54
  when RequiresAncestor
data/lib/rbi/version.rb CHANGED
@@ -1,6 +1,6 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RBI
5
- VERSION = "0.0.11"
5
+ VERSION = "0.0.14"
6
6
  end
data/lib/rbi.rb CHANGED
@@ -23,4 +23,5 @@ require "rbi/rewriters/remove_known_definitions"
23
23
  require "rbi/rewriters/sort_nodes"
24
24
  require "rbi/parser"
25
25
  require "rbi/printer"
26
+ require "rbi/formatter"
26
27
  require "rbi/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Terrasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-27 00:00:00.000000000 Z
11
+ date: 2022-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 2.6.4.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 2.6.4.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sorbet-runtime
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -77,6 +77,7 @@ files:
77
77
  - README.md
78
78
  - Rakefile
79
79
  - lib/rbi.rb
80
+ - lib/rbi/formatter.rb
80
81
  - lib/rbi/index.rb
81
82
  - lib/rbi/loc.rb
82
83
  - lib/rbi/model.rb