sord 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b9a2d105021ccde04330fef38f0a2d3fb270fbd3d2e2c43a258c3bc3f6e0b9f1
4
+ data.tar.gz: 7d4ff5718684cfd2505595c77ef0bbc6e7f79c6981b0210f28912a66cb6725eb
5
+ SHA512:
6
+ metadata.gz: 8681c3b8c07c397f2f454f0e71546ba33ead6b66efdee7c98ecb57ccfea4908a9fe7f03c43d206d46409d4cf7bcdf3116dbb89b628feaf23bfdb52d9f2168c6a
7
+ data.tar.gz: 552beb2ce3b13800f16687967a3d6bc80b6ab7398f54c16e998612241a37b7bb4a6f490e25d17fa427960ccaecd3a4c0964ac224728dc3e13eb8abc0bb6de970
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at aaronc20000@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sord.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sord (0.1.0)
5
+ colorize
6
+ sorbet-runtime
7
+ yard
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ colorize (0.8.1)
13
+ diff-lcs (1.3)
14
+ rake (10.5.0)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.1)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.4)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.2)
28
+ sorbet (0.4.4254)
29
+ sorbet-static (= 0.4.4254)
30
+ sorbet-runtime (0.4.4254)
31
+ sorbet-static (0.4.4254-x86_64-linux)
32
+ yard (0.9.19)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 2.0)
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+ sorbet
42
+ sord!
43
+
44
+ BUNDLED WITH
45
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Aaron Christiansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Sord
2
+
3
+ ## Overview
4
+
5
+ Sord is a **So**rbet and YA**RD** crossover. It can automatically generate
6
+ Sorbet type signatures files by looking at the types specified in YARD
7
+ documentation comments.
8
+
9
+ If your project is already YARD documented, then this can generate most of the
10
+ Sorbet signatures you need!
11
+
12
+ Sord has the following features:
13
+ - Automatically generates signatures for modules, classes and methods
14
+ - Support for multiple parameter or return types (`T.any`)
15
+ - Gracefully handles missing YARD types (`T.untyped`)
16
+ - Can infer setter parameter type from the corresponding getter's return type
17
+ - Recognises mixins (`include` and `extend`)
18
+ - Support for single-argument generic types such as `Array` (no hashes yet)
19
+
20
+ ## Usage
21
+
22
+ Sord is a command line tool. To use it, open a terminal in the root directory
23
+ of your project, and run `yard` to generate a YARD registry if you haven't
24
+ already. Then, invoke `sord`, passing a path of where you'd like to save your
25
+ RBI to (this file will be overwritten):
26
+
27
+ ```
28
+ sord defs.rbi
29
+ ```
30
+
31
+ Sord will print information about what it's inferred as it runs. It is best to
32
+ fix any issues in the YARD documentation, as any edits made to the resulting
33
+ RBI file will be replaced if you re-run Sord.
34
+
35
+ ## Example
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sord. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the Sord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sord/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/exe/sord ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'sord'
3
+
4
+ Sord::RbiGenerator.new.run($*[0])
@@ -0,0 +1,47 @@
1
+ require 'colorize'
2
+
3
+ module Sord
4
+ module Logging
5
+ @@hooks = []
6
+
7
+ def self.generic(kind, header, msg, item)
8
+ if item
9
+ puts "#{header} (#{item.path.light_white}) #{msg}"
10
+ else
11
+ puts "#{header} #{msg}"
12
+ end
13
+
14
+ invoke_hooks(kind, msg, item)
15
+ end
16
+
17
+ def self.warn(msg, item=nil)
18
+ generic(:warn, '[WARN ]'.yellow, msg, item)
19
+ end
20
+
21
+ def self.error(msg, item=nil)
22
+ generic(:error, '[ERROR]'.red, msg, item)
23
+ end
24
+
25
+ def self.infer(msg, item=nil)
26
+ generic(:infer, '[INFER]'.light_blue, msg, item)
27
+ end
28
+
29
+ def self.omit(msg, item=nil)
30
+ generic(:omit, '[OMIT ]'.magenta, msg, item)
31
+ end
32
+
33
+ def self.done(msg, item=nil)
34
+ generic(:done, '[DONE ]'.green, msg, item)
35
+ end
36
+
37
+ def self.invoke_hooks(type, msg, item)
38
+ @@hooks.each do |hook|
39
+ hook.(type, msg, item) rescue nil
40
+ end
41
+ end
42
+
43
+ def self.add_hook(&blk)
44
+ @@hooks << blk
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,133 @@
1
+ # typed: true
2
+ require 'yard'
3
+ require 'sord/type_converter'
4
+ require 'colorize'
5
+ require 'sord/logging'
6
+
7
+ module Sord
8
+ class RbiGenerator
9
+ attr_reader :rbi_contents, :object_count
10
+
11
+ def initialize
12
+ @rbi_contents = ['# typed: true']
13
+ @object_count = 0
14
+
15
+ Logging.add_hook do |type, msg, item|
16
+ rbi_contents << " # sord #{type} - #{msg}"
17
+ end
18
+ end
19
+
20
+ def count_object
21
+ @object_count += 1
22
+ end
23
+
24
+ def add_mixins(item)
25
+ extends = item.instance_mixins
26
+ includes = item.class_mixins
27
+
28
+ extends.each do |this_extend|
29
+ rbi_contents << " extend #{this_extend.path}"
30
+ end
31
+ includes.each do |this_include|
32
+ rbi_contents << " include #{this_include.path}"
33
+ end
34
+ end
35
+
36
+ def add_methods(item)
37
+ # TODO: block documentation
38
+ item.meths.each do |meth|
39
+ count_object
40
+
41
+ parameter_list = meth.parameters.map do |name, default|
42
+ "#{name}#{default && " = #{default}"}"
43
+ end.join(", ")
44
+
45
+ parameter_names_to_tags = meth.parameters.map do |name, _|
46
+ [name, meth.tags('param').find { |p| p.name == name }]
47
+ end.to_h
48
+
49
+ # TODO: if it's a _= method, infer from the _ method
50
+ sig_params_list = parameter_names_to_tags.map do |name, tag|
51
+ if tag
52
+ "#{name}: #{TypeConverter.yard_to_sorbet(tag.types, meth)}"
53
+ elsif name.start_with? '*'
54
+ # TODO: is there a YARD definition for this?
55
+ "args: T::Array[T.any]"
56
+ elsif meth.path.end_with? '='
57
+ # Look for the matching getter method
58
+ getter_path = meth.path[0...-1]
59
+ getter = item.meths.find { |m| m.path == getter_path }
60
+
61
+ unless getter
62
+ Logging.omit("no YARD type given for #{name.inspect}, using T.untyped", meth)
63
+ next "#{name}: T.untyped"
64
+ end
65
+
66
+ inferred_type = TypeConverter.yard_to_sorbet(
67
+ getter.tags('return').flat_map(&:types), meth)
68
+
69
+ Logging.infer("inferred type of parameter #{name.inspect} as #{inferred_type} using getter's return type", meth)
70
+ "#{name}: #{inferred_type}"
71
+ else
72
+ Logging.omit("no YARD type given for #{name.inspect}, using T.untyped", meth)
73
+ "#{name}: T.untyped"
74
+ end
75
+ end.join(", ")
76
+
77
+ return_tags = meth.tags('return')
78
+ returns = if return_tags.length == 0
79
+ "void"
80
+ elsif return_tags.length == 1 && return_tags.first.types.first.downcase == "void"
81
+ "void"
82
+ else
83
+ "returns(#{TypeConverter.yard_to_sorbet(meth.tag('return').types, meth)})"
84
+ end
85
+
86
+ prefix = meth.scope == :class ? 'self.' : ''
87
+
88
+ rbi_contents << " sig { params(#{sig_params_list}).#{returns} }"
89
+
90
+ rbi_contents << " def #{prefix}#{meth.name}(#{parameter_list}) end"
91
+ end
92
+ end
93
+
94
+ def run(filename)
95
+ # Get YARD ready
96
+ YARD::Registry.load!
97
+
98
+ # TODO: constants?
99
+
100
+ # Populate the RBI with modules first
101
+ YARD::Registry.all(:module).each do |item|
102
+ count_object
103
+
104
+ rbi_contents << "module #{item.path}"
105
+ add_mixins(item)
106
+ add_methods(item)
107
+ rbi_contents << "end"
108
+ end
109
+
110
+ # Now populate with classes
111
+ YARD::Registry.all(:class).each do |item|
112
+ count_object
113
+
114
+ superclass = (item.superclass if item.superclass.to_s != "Object")
115
+ rbi_contents << "class #{item.path} #{"< #{superclass}" if superclass}"
116
+ add_mixins(item)
117
+ add_methods(item)
118
+ rbi_contents << "end"
119
+ end
120
+
121
+ # Write the file
122
+ raise "no filename specified" unless filename
123
+ File.write(filename, rbi_contents.join(?\n))
124
+
125
+ Logging.done("Processed #{object_count} objects")
126
+ rescue
127
+ Logging.error($!)
128
+ $@.each do |line|
129
+ puts " #{line}".light_white
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,52 @@
1
+ require 'sord/logging'
2
+
3
+ module Sord
4
+ module TypeConverter
5
+ SIMPLE_TYPE_REGEX =
6
+ /(?:\:\:)?[a-zA-Z_][a-zA-Z_0-9]*(?:\:\:[a-zA-Z_][a-zA-Z_0-9]*)*/
7
+
8
+ # TODO: does not support mulitple type arguments (e.g. Hash<A, B>)
9
+ GENERIC_TYPE_REGEX =
10
+ /(#{SIMPLE_TYPE_REGEX})<(#{SIMPLE_TYPE_REGEX})>/
11
+
12
+ # TODO: Hash
13
+ SORBET_SUPPORTED_GENERIC_TYPES = %w{Array Set Enumerable Enumerator Range}
14
+
15
+ def self.yard_to_sorbet(yard, item=nil, &blk)
16
+ case yard
17
+ when nil
18
+ "T.untyped"
19
+ when "bool", "Bool", "boolean", "Boolean", ["true", "false"], ["false", "true"]
20
+ "T::Boolean"
21
+ when Array
22
+ # If there's only one element, unwrap it, otherwise allow for a
23
+ # selection of any of the types
24
+ yard.length == 1 \
25
+ ? yard_to_sorbet(yard.first, item, &blk)
26
+ : "T.any(#{yard.map { |x| yard_to_sorbet(x, item, &blk) }.join(', ')})"
27
+ when /^#{SIMPLE_TYPE_REGEX}$/
28
+ if /^[_a-z]/ === yard
29
+ Logging.warn("#{yard} is probably not a type, but using anyway", item)
30
+ end
31
+ yard
32
+ when /^#{GENERIC_TYPE_REGEX}$/
33
+ generic_type = $1
34
+ type_parameter = $2
35
+
36
+ if SORBET_SUPPORTED_GENERIC_TYPES.include?(generic_type)
37
+ if /^[_a-z]/ === type_parameter
38
+ Logging.warn("#{type_parameter} is probably not a type, but using anyway", item)
39
+ end
40
+
41
+ "T::#{generic_type}[#{yard_to_sorbet(type_parameter, item, &blk)}]"
42
+ else
43
+ Logging.warn("unsupported generic type #{generic_type.inspect} in #{yard.inspect}", item)
44
+ "SORD_ERROR_#{generic_type.gsub(/[^0-9A-Za-z_]/i, '')}"
45
+ end
46
+ else
47
+ Logging.warn("#{yard.inspect} does not appear to be a type", item)
48
+ "SORD_ERROR_#{yard.gsub(/[^0-9A-Za-z_]/i, '')}"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ # typed: strong
2
+ module Sord
3
+ VERSION = "0.1.0"
4
+ end
data/lib/sord.rb ADDED
@@ -0,0 +1,5 @@
1
+ # typed: strong
2
+ require 'sord/version'
3
+ require 'sord/rbi_generator'
4
+ require 'yard'
5
+ require 'sorbet-runtime'
data/sorbet/config ADDED
@@ -0,0 +1,2 @@
1
+ --dir
2
+ .