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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf3418f5f5f96e17c76ee48515382ac008d975ed200eae3f837f9e136077a7f3
4
- data.tar.gz: 2592ad90c6b6c17ec9a9c550d348a3989b4511a51b2b115643dafa05b41e9191
3
+ metadata.gz: 9a20a16fb8b21d103c934ed6f4846db387ab40174ebe9468b33ec70b82c8c883
4
+ data.tar.gz: 235f786ea4950554c1c78a9887c40218efdb1cafdf2b281ca5d923af2130eaf5
5
5
  SHA512:
6
- metadata.gz: 4db0db07817d59efdceb9455cbf3613d4d6031d635fc40ad6dd310ad0828a6b8a6be35c6cd7e9cfa11d3cb7ab23496c851686027a82f5e6f55c034420a9c98a2
7
- data.tar.gz: b00f3f123af693111782b734ce72d00930613408f113197bcddd182c6b36308b8f6b9e7d3cc2e0bc7a7ed15bb666aec93a9da7d2e7f9595e601ff51b7e16866a
6
+ metadata.gz: e4c45eef8d708353b0a1817b273c7a548799fed3380e5ad76a0744e6c51b02d9b5d139e31a9aac49bfafc9936a4cecf8f0098a9911b2cda556b9c8456bb3cde9
7
+ data.tar.gz: 47f885d8222de2169f987e603cc46b949475046439a7d7e7e312ecb09e9d1510459a59e1621622e967d185b5855cdd5face4a984cf825a1a17256e7ed0876186
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in sord.gemspec
4
4
  gemspec
5
-
6
- gem "commander", "~> 4.4"
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sord (0.3.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.4293)
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.
@@ -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').find { |p| p.name == name }]
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"
@@ -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*<\s*(.*)\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
- current_bracketing_level -= 1 if params[character_pointer] == ?>
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
- if params[character_pointer] == ?,
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
- "T::#{generic_type}[#{
89
- split_type_parameters(type_parameters).map { |x| yard_to_sorbet(x, item) }.join(', ')}]"
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
@@ -1,4 +1,4 @@
1
1
  # typed: strong
2
2
  module Sord
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
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.1
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