Skip to content

Commit

Permalink
Merge pull request #7 from euglena1215/introduce-sorbet
Browse files Browse the repository at this point in the history
Introduce sorbet
  • Loading branch information
euglena1215 authored Jan 7, 2024
2 parents d89439a + 6582156 commit 8c387be
Show file tree
Hide file tree
Showing 68 changed files with 265,046 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/pkg/
/spec/reports/
/tmp/

sorbet/rbi/hidden-definitions/errors.txt
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]

- Add packwerk to dependencies https://github.com/euglena1215/packwerk-yard/pull/6
- Introduce sorbet https://github.com/euglena1215/packwerk-yard/pull/7

## [0.1.0] - 2024-01-07

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ gem "rubocop", require: false
gem "rubocop-performance", require: false
gem "rubocop-sorbet", require: false
gem "rubocop-shopify", require: false
gem "sorbet-static-and-runtime"
gem "tapioca", require: false
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ GEM
nokogiri (>= 1.12.0)
minitest (5.20.0)
mutex_m (0.2.0)
netrc (0.11.0)
nokogiri (1.16.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-linux)
Expand All @@ -76,6 +77,8 @@ GEM
parser (3.2.2.4)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
prism (0.19.0)
racc (1.7.3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
Expand All @@ -86,6 +89,9 @@ GEM
nokogiri (~> 1.14)
rainbow (3.1.1)
rake (13.1.0)
rbi (0.1.6)
prism (>= 0.18.0, < 0.20)
sorbet-runtime (>= 0.5.9204)
regexp_parser (2.8.3)
rexml (3.2.6)
rubocop (1.59.0)
Expand All @@ -111,11 +117,38 @@ GEM
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
smart_properties (1.17.0)
sorbet (0.5.11175)
sorbet-static (= 0.5.11175)
sorbet-runtime (0.5.11175)
sorbet-static (0.5.11175-universal-darwin)
sorbet-static (0.5.11175-x86_64-linux)
sorbet-static-and-runtime (0.5.11175)
sorbet (= 0.5.11175)
sorbet-runtime (= 0.5.11175)
spoom (1.2.4)
erubi (>= 1.10.0)
sorbet-static-and-runtime (>= 0.5.10187)
syntax_tree (>= 6.1.1)
thor (>= 0.19.2)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
tapioca (0.11.14)
bundler (>= 2.2.25)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
rbi (>= 0.1.4, < 0.2)
sorbet-static-and-runtime (>= 0.5.10187)
spoom (~> 1.2.0, >= 1.2.0)
thor (>= 1.2.0)
yard-sorbet
thor (1.3.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
yard (0.9.34)
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.12)

PLATFORMS
Expand All @@ -132,6 +165,8 @@ DEPENDENCIES
rubocop-performance
rubocop-shopify
rubocop-sorbet
sorbet-static-and-runtime
tapioca

BUNDLED WITH
2.4.18
27 changes: 27 additions & 0 deletions bin/tapioca
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'tapioca' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("tapioca", "tapioca")
2 changes: 1 addition & 1 deletion lib/packwerk_yard.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: strict
# frozen_string_literal: true

require "packwerk"
Expand Down
21 changes: 12 additions & 9 deletions lib/packwerk_yard/parser.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# typed: false
# typed: strict
# frozen_string_literal: true

module PackwerkYard
class Parser
extend T::Sig
include Packwerk::FileParser

# Array Syntax e.g. Array<String>
ARRAY_REGEXP = /Array<(.+)>/.freeze
ARRAY_REGEXP = T.let(/Array<(.+)>/.freeze, Regexp)
private_constant :ARRAY_REGEXP

sig { params(ruby_parser: T.nilable(Packwerk::Parsers::Ruby)).void }
def initialize(ruby_parser: Packwerk::Parsers::Ruby.new)
@ruby_parser = ruby_parser
end

sig { override.params(io: T.any(IO, StringIO), file_path: String).returns(T.untyped) }
def call(io:, file_path: "<unknown>")
source_code = io.read
return to_ruby_ast(nil.inspect, file_path) if source_code.nil?
Expand All @@ -27,12 +30,14 @@ def call(io:, file_path: "<unknown>")
)
end

sig { override.params(path: String).returns(T::Boolean) }
def match?(path:)
path.end_with?(".rb")
end

private

sig { params(source_code: String).returns(T::Array[String]) }
def extract_from_yard_to_types(source_code)
YARD::Registry.clear
YARD::Logger.instance.enter_level(YARD::Logger::ERROR) do
Expand All @@ -51,23 +56,21 @@ def extract_from_yard_to_types(source_code)
types.uniq
end

sig { params(type: String).returns(String) }
def to_evaluable_type(type)
# "Array<Integer>" => "Integer"
if type =~ ARRAY_REGEXP
Regexp.last_match(1)
else
type
end
ARRAY_REGEXP.match(type).to_a[1] || type
end

sig { params(name: T.any(Symbol, String)).returns(T.untyped) }
def to_constant(name)
Object.const_get(name) # rubocop:disable Sorbet/ConstantsFromStrings
rescue NameError
nil
end

sig { params(code: String, file_path: T.untyped).returns(T.untyped) }
def to_ruby_ast(code, file_path)
@ruby_parser.call(
T.must(@ruby_parser).call(
io: StringIO.new(code),
file_path: file_path,
)
Expand Down
2 changes: 1 addition & 1 deletion lib/packwerk_yard/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
# typed: strict
# frozen_string_literal: true

module PackwerkYard
Expand Down
4 changes: 4 additions & 0 deletions sorbet/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--dir
.
--ignore=tmp/
--ignore=vendor/
1 change: 1 addition & 0 deletions sorbet/rbi/annotations/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.rbi linguist-vendored=true
75 changes: 75 additions & 0 deletions sorbet/rbi/annotations/actionview.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c387be

Please sign in to comment.