sord 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +3 -3
- data/lib/sord/logging.rb +11 -0
- data/lib/sord/rbi_generator.rb +5 -4
- data/lib/sord/type_converter.rb +34 -8
- data/lib/sord/version.rb +1 -1
- data/sord.gemspec +1 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a20a16fb8b21d103c934ed6f4846db387ab40174ebe9468b33ec70b82c8c883
|
4
|
+
data.tar.gz: 235f786ea4950554c1c78a9887c40218efdb1cafdf2b281ca5d923af2130eaf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4c45eef8d708353b0a1817b273c7a548799fed3380e5ad76a0744e6c51b02d9b5d139e31a9aac49bfafc9936a4cecf8f0098a9911b2cda556b9c8456bb3cde9
|
7
|
+
data.tar.gz: 47f885d8222de2169f987e603cc46b949475046439a7d7e7e312ecb09e9d1510459a59e1621622e967d185b5855cdd5face4a984cf825a1a17256e7ed0876186
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sord (0.
|
4
|
+
sord (0.4.1)
|
5
5
|
colorize
|
6
|
+
commander (~> 4.4)
|
6
7
|
sorbet-runtime
|
7
8
|
yard
|
8
9
|
|
@@ -37,7 +38,7 @@ GEM
|
|
37
38
|
simplecov-html (0.10.2)
|
38
39
|
sorbet (0.4.4254)
|
39
40
|
sorbet-static (= 0.4.4254)
|
40
|
-
sorbet-runtime (0.4.
|
41
|
+
sorbet-runtime (0.4.4294)
|
41
42
|
sorbet-static (0.4.4254-x86_64-linux)
|
42
43
|
yard (0.9.19)
|
43
44
|
|
@@ -46,7 +47,6 @@ PLATFORMS
|
|
46
47
|
|
47
48
|
DEPENDENCIES
|
48
49
|
bundler (~> 2.0)
|
49
|
-
commander (~> 4.4)
|
50
50
|
rake (~> 10.0)
|
51
51
|
rspec (~> 3.0)
|
52
52
|
simplecov
|
data/lib/sord/logging.rb
CHANGED
@@ -53,6 +53,17 @@ module Sord
|
|
53
53
|
generic(:warn, '[WARN ]'.yellow, msg, item)
|
54
54
|
end
|
55
55
|
|
56
|
+
# Print a duck-typing message. This should be used when the YARD
|
57
|
+
# documentation contains duck typing, which isn't supported by Sorbet, so
|
58
|
+
# it is substituted for something different.
|
59
|
+
# @param [String] msg The log message to write.
|
60
|
+
# @param [YARD::CodeObjects::Base] item The CodeObject which this log
|
61
|
+
# is associated with, if any. This is shown before the log message if it is
|
62
|
+
# specified.
|
63
|
+
def self.duck(msg, item=nil)
|
64
|
+
generic(:ducl, '[DUCK ]'.cyan, msg, item)
|
65
|
+
end
|
66
|
+
|
56
67
|
# Print an error message. This should be used for things which require the
|
57
68
|
# current process to stop.
|
58
69
|
# @param [String] msg The log message to write.
|
data/lib/sord/rbi_generator.rb
CHANGED
@@ -75,16 +75,17 @@ module Sord
|
|
75
75
|
|
76
76
|
# This is better than iterating over YARD's "@param" tags directly
|
77
77
|
# because it includes parameters without documentation
|
78
|
+
# (The gsubs allow for better splat-argument compatibility)
|
78
79
|
parameter_names_to_tags = meth.parameters.map do |name, _|
|
79
|
-
[name, meth.tags('param')
|
80
|
+
[name, meth.tags('param')
|
81
|
+
.find { |p| p.name.gsub('*', '') == name.gsub('*', '') }]
|
80
82
|
end.to_h
|
81
83
|
|
82
84
|
sig_params_list = parameter_names_to_tags.map do |name, tag|
|
85
|
+
name = name.gsub('*', '')
|
86
|
+
|
83
87
|
if tag
|
84
88
|
"#{name}: #{TypeConverter.yard_to_sorbet(tag.types, meth)}"
|
85
|
-
elsif name.start_with? '*'
|
86
|
-
# TODO: is there a YARD definition for this?
|
87
|
-
"args: T::Array[T.any]"
|
88
89
|
elsif name.start_with? '&'
|
89
90
|
# Cut the ampersand from the block parameter name.
|
90
91
|
"#{name[1..-1]}: T.untyped"
|
data/lib/sord/type_converter.rb
CHANGED
@@ -11,12 +11,14 @@ module Sord
|
|
11
11
|
|
12
12
|
# A regular expression which matches a Ruby namespace immediately followed
|
13
13
|
# by another Ruby namespace in angle brackets. This is the format usually
|
14
|
-
# used in YARD to model generic types, such as "Array<String>"
|
14
|
+
# used in YARD to model generic types, such as "Array<String>",
|
15
|
+
# "Hash<String, Symbol>", "Hash{String => Symbol}", etc.
|
15
16
|
GENERIC_TYPE_REGEX =
|
16
|
-
/(#{SIMPLE_TYPE_REGEX})\s
|
17
|
+
/(#{SIMPLE_TYPE_REGEX})\s*[<{]\s*(.*)\s*[>}]/
|
17
18
|
|
18
19
|
# An array of built-in generic types supported by Sorbet.
|
19
20
|
SORBET_SUPPORTED_GENERIC_TYPES = %w{Array Set Enumerable Enumerator Range Hash}
|
21
|
+
SORBET_SINGLE_ARG_GENERIC_TYPES = %w{Array Set Enumerable Enumerator Range}
|
20
22
|
|
21
23
|
# Given a string of YARD type parameters (without angle brackets), splits
|
22
24
|
# the string into an array of each type parameter.
|
@@ -31,10 +33,15 @@ module Sord
|
|
31
33
|
while character_pointer < params.length
|
32
34
|
should_buffer = true
|
33
35
|
|
34
|
-
current_bracketing_level += 1 if params[character_pointer]
|
35
|
-
|
36
|
+
current_bracketing_level += 1 if ['<', '{'].include?(params[character_pointer])
|
37
|
+
# Decrease bracketing level by 1 when encountering `>` or `}`, unless
|
38
|
+
# the previous character is `=` (to prevent hash rockets from causing
|
39
|
+
# nesting problems).
|
40
|
+
current_bracketing_level -= 1 if ['>', '}'].include?(params[character_pointer]) && params[character_pointer - 1] != '='
|
36
41
|
|
37
|
-
|
42
|
+
# Handle commas as separators.
|
43
|
+
# e.g. Hash<Symbol, String>
|
44
|
+
if params[character_pointer] == ','
|
38
45
|
if current_bracketing_level == 0
|
39
46
|
result << buffer.strip
|
40
47
|
buffer = ""
|
@@ -42,6 +49,17 @@ module Sord
|
|
42
49
|
end
|
43
50
|
end
|
44
51
|
|
52
|
+
# Handle hash rockets as separators.
|
53
|
+
# e.g. Hash<Symbol => String>
|
54
|
+
if params[character_pointer] == '=' && params[character_pointer + 1] == '>'
|
55
|
+
if current_bracketing_level == 0
|
56
|
+
character_pointer += 1
|
57
|
+
result << buffer.strip
|
58
|
+
buffer = ""
|
59
|
+
should_buffer = false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
45
63
|
buffer += params[character_pointer] if should_buffer
|
46
64
|
character_pointer += 1
|
47
65
|
end
|
@@ -80,13 +98,21 @@ module Sord
|
|
80
98
|
Logging.warn("#{yard} is probably not a type, but using anyway", item)
|
81
99
|
end
|
82
100
|
yard
|
101
|
+
when /^\##{SIMPLE_TYPE_REGEX}$/
|
102
|
+
Logging.duck("#{yard} looks like a duck type, replacing with T.untyped", item)
|
103
|
+
'T.untyped'
|
83
104
|
when /^#{GENERIC_TYPE_REGEX}$/
|
84
105
|
generic_type = $1
|
85
106
|
type_parameters = $2
|
86
107
|
|
87
108
|
if SORBET_SUPPORTED_GENERIC_TYPES.include?(generic_type)
|
88
|
-
|
89
|
-
|
109
|
+
parameters = split_type_parameters(type_parameters)
|
110
|
+
.map { |x| yard_to_sorbet(x, item) }
|
111
|
+
if SORBET_SINGLE_ARG_GENERIC_TYPES.include?(generic_type) && parameters.length > 1
|
112
|
+
"T::#{generic_type}[T.any(#{parameters.join(', ')})]"
|
113
|
+
else
|
114
|
+
"T::#{generic_type}[#{parameters.join(', ')}]"
|
115
|
+
end
|
90
116
|
else
|
91
117
|
Logging.warn("unsupported generic type #{generic_type.inspect} in #{yard.inspect}", item)
|
92
118
|
"SORD_ERROR_#{generic_type.gsub(/[^0-9A-Za-z_]/i, '')}"
|
@@ -97,4 +123,4 @@ module Sord
|
|
97
123
|
end
|
98
124
|
end
|
99
125
|
end
|
100
|
-
end
|
126
|
+
end
|
data/lib/sord/version.rb
CHANGED
data/sord.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_dependency 'yard'
|
26
26
|
spec.add_dependency 'sorbet-runtime'
|
27
27
|
spec.add_dependency 'colorize'
|
28
|
+
spec.add_dependency 'commander', '~> 4.4'
|
28
29
|
|
29
30
|
spec.add_development_dependency "bundler", "~> 2.0"
|
30
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Christiansen
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: commander
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.4'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.4'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: bundler
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|