sorbet_erb 0.2.0 → 0.3.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: 066514027114bf88c8a5fe55bf534b6fde19c739faa0ee8a95878a6a337e735c
4
- data.tar.gz: b709e8f1e30f9280ce1143fd8348e1099b31b3f2040716557fd177caf6eb8b39
3
+ metadata.gz: 1243e1d235eda3fa678fb4cc05edf42af42548b453d679058cf3a84ab1bfe405
4
+ data.tar.gz: cd5c593cb18eef72cbe4a8bf3ba9209b3090b462714187ed1d36fedb7a5e1a59
5
5
  SHA512:
6
- metadata.gz: 47a69221db20d63363de18294e9132acf72bcef2a5a7fe25070f404a5461dab44f06ab2ada93bb6f6fbe082f80443ea3744404e259ffb2b42057c9a521702597
7
- data.tar.gz: 71df4f5bf5bdc9e1c36c04862fad4911e164bb5904d5a449c8669bb9bd76cedc45e876102adcbd121ef6dc79d5b6e4494a70ee47de86c754727d47b885d297d8
6
+ metadata.gz: 6d4a0d9521a33594a74488a6ae3e1ee62d8f970209f75190cdb67a61a90ddb4010702071c3a81b27e97b4c5f3374c518b3374196d88b65601316477498c97413
7
+ data.tar.gz: d01ca6fbcbdb67752099e2d9602ee41bedb6fc4ef5322be0d6c66c939bdf27608edb4041f2d0db788c64062d84d2f3a0e23da78aca84b633333f3cad3ca3e8a8
data/.rubocop.yml CHANGED
@@ -1,9 +1,12 @@
1
1
  AllCops:
2
- TargetRubyVersion: 3.0.0
2
+ TargetRubyVersion: 3.1.0
3
3
 
4
4
  Style/Documentation:
5
5
  Enabled: false
6
6
 
7
+ Style/HashSyntax:
8
+ Enabled: false
9
+
7
10
  Style/StringLiterals:
8
11
  Enabled: true
9
12
  EnforcedStyle: single_quotes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.6
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SorbetErb
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/sorbet_erb.rb CHANGED
@@ -28,7 +28,7 @@ module SorbetErb
28
28
 
29
29
  ERB_TEMPLATE = <<~ERB_TEMPLATE
30
30
  # typed: true
31
- class SorbetErb<%= class_suffix %> < ApplicationController
31
+ class <%= class_name %><%= extend_app_controller ? " < ApplicationController" : "" %>
32
32
  extend T::Sig
33
33
  include ActionView::Helpers
34
34
  include ApplicationController::HelperMethods
@@ -87,6 +87,8 @@ module SorbetErb
87
87
  locals ||= '()'
88
88
  locals_sig ||= ''
89
89
 
90
+ class_name, extend_app_controller = class_name_from_path(pathname)
91
+
90
92
  rel_output_dir = File.join(
91
93
  output_dir,
92
94
  pathname.dirname.relative_path_from(d)
@@ -100,7 +102,8 @@ module SorbetErb
100
102
  erb = ERB.new(ERB_TEMPLATE)
101
103
  File.open(output_path, 'w') do |f|
102
104
  result = erb.result_with_hash(
103
- class_suffix: SecureRandom.hex(6),
105
+ class_name: class_name,
106
+ extend_app_controller: extend_app_controller,
104
107
  locals: locals,
105
108
  locals_sig: locals_sig,
106
109
  extra_includes: config.fetch('extra_includes'),
@@ -127,6 +130,40 @@ module SorbetErb
127
130
  file_name.start_with?('_') || file_name.end_with?('.turbo_stream.erb')
128
131
  end
129
132
 
133
+ def self.class_name_from_path(pathname)
134
+ # ViewComponents are stored under app/components, and the partials need access to the instance
135
+ # methods and variables available on the component class, so set the class name to the component
136
+ # class name.
137
+ # TODO: support namespacing
138
+ if pathname.to_s.start_with?('app/components')
139
+ return [
140
+ extract_class_name(pathname).map do |part|
141
+ ActiveSupport::Inflector.camelize(part)
142
+ end.join('::'),
143
+ false
144
+ ]
145
+ end
146
+
147
+ # Otherwise make a random class so this doesn't collide with any existing code
148
+ ["SorbetErb#{SecureRandom.hex(6)}", true]
149
+ end
150
+
151
+ def self.extract_class_name(pathname)
152
+ return [] if pathname.to_s == 'app/components' || pathname.to_s == '.'
153
+
154
+ # Strip template suffix
155
+ basename = File.basename(pathname.basename, '.html.erb')
156
+
157
+ # We need to handle the cases where the dirname matches the component name, or if there's
158
+ # a namespace
159
+ # `app/components/my_component/my_component.html.erb`
160
+ # `app/components/namespace/my_component/my_component.html.erb`
161
+ dirname = pathname.dirname
162
+ dirname = dirname.dirname if basename.to_s == dirname.basename.to_s
163
+
164
+ extract_class_name(dirname) + [basename]
165
+ end
166
+
130
167
  def self.start(argv)
131
168
  input = argv[0]
132
169
  output = argv[1]
@@ -98,7 +98,7 @@ module Tapioca
98
98
  def generate_instance_methods(klass, name, return_type, is_many)
99
99
  return_type_maybe_plural =
100
100
  if is_many
101
- "T::Enumerable[#{return_type}]"
101
+ "T::Array[#{return_type}]"
102
102
  else
103
103
  return_type
104
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet_erb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-04 00:00:00.000000000 Z
11
+ date: 2025-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: better_html
@@ -61,6 +61,7 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
63
  - ".rubocop.yml"
64
+ - ".ruby-version"
64
65
  - README.md
65
66
  - Rakefile
66
67
  - exe/sorbet_erb
@@ -76,7 +77,7 @@ metadata:
76
77
  homepage_uri: https://github.com/franklinhu/sorbet_erb
77
78
  source_code_uri: https://github.com/franklinhu/sorbet_erb
78
79
  changelog_uri: https://github.com/franklinhu/sorbet_erb
79
- post_install_message:
80
+ post_install_message:
80
81
  rdoc_options: []
81
82
  require_paths:
82
83
  - lib
@@ -84,15 +85,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
85
  requirements:
85
86
  - - ">="
86
87
  - !ruby/object:Gem::Version
87
- version: 3.0.0
88
+ version: 3.1.0
88
89
  required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  requirements:
90
91
  - - ">="
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubygems_version: 3.5.16
95
- signing_key:
95
+ rubygems_version: 3.5.22
96
+ signing_key:
96
97
  specification_version: 4
97
98
  summary: Extracts Ruby code from ERB files for Sorbet
98
99
  test_files: []