Skip to content

Commit 8c2af97

Browse files
authored
Merge pull request #1045 from Shopify/uk-fix-method-name-validation
Fix method name validation edge-case
2 parents d2ef1e0 + 75f6857 commit 8c2af97

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

lib/tapioca/helpers/rbi_helper.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def self.serialize_type_variable(type, variance, fixed, upper, lower)
9393

9494
sig { params(name: String).returns(T::Boolean) }
9595
def valid_method_name?(name)
96-
!name.to_sym.inspect.start_with?(':"', ":@", ":$")
96+
name == "==" || !(
97+
name.to_sym.inspect.start_with?(':"', ":@", ":$") ||
98+
name.delete_suffix("=").to_sym.inspect.start_with?(':"', ":@", ":$")
99+
)
97100
end
98101

99102
sig { params(name: String).returns(T::Boolean) }

spec/tapioca/dsl/compilers/active_record_columns_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,35 @@ def title?; end
211211
assert_includes(output, expected)
212212
end
213213

214+
it "skips columns with names that can't be Ruby method names" do
215+
add_ruby_file("schema.rb", <<~RUBY)
216+
ActiveRecord::Migration.suppress_messages do
217+
ActiveRecord::Schema.define do
218+
create_table :posts do |t|
219+
t.string :"4_to_5"
220+
t.string :"foo-bar"
221+
t.string :"@foo"
222+
end
223+
end
224+
end
225+
RUBY
226+
227+
add_ruby_file("post.rb", <<~RUBY)
228+
class Post < ActiveRecord::Base
229+
end
230+
RUBY
231+
232+
output = rbi_for(:Post)
233+
234+
# "4_to_5" should never be the start of a method name,
235+
# but method names like "saved_change_to_4_to_5" are fine.
236+
refute_includes(output, "def 4_to_5")
237+
# no method name should include "foo-bar"
238+
refute_includes(output, "foo-bar")
239+
# no method name should include "@foo"
240+
refute_includes(output, "@foo")
241+
end
242+
214243
it "generates a proper type for every ActiveRecord column type" do
215244
add_ruby_file("schema.rb", <<~RUBY)
216245
ActiveRecord::Migration.suppress_messages do

spec/tapioca/helpers/rbi_helper_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class Tapioca::RBIHelperSpec < Minitest::Spec
1313
"!", "~", "+@", "**", "-@", "*", "/", "%", "+", "-", "<<", ">>", "&", "|", "^",
1414
"<", "<=", ">", ">=", "==", "===", "!=", "=~", "!~", "<=>", "[]", "[]=", "`",
1515
].each do |name|
16-
assert(valid_method_name?(name))
16+
assert(valid_method_name?(name), "Expected `#{name}` to be a valid method name")
1717
end
1818
end
1919

2020
it "rejects invalid method names" do
21-
["", "42", "42foo", "!foo", "-@foo", "foo-", "foo-bar", "foo.bar", "=>"].each do |name|
22-
refute(valid_method_name?(name))
21+
["", "42", "42foo", "42foo=", "!foo", "-@foo", "foo-", "foo-bar", "foo.bar", "=>"].each do |name|
22+
refute(valid_method_name?(name), "Expected `#{name}` to be an invalid method name")
2323
end
2424
end
2525

2626
it "accepts valid parameter names" do
2727
["f", "foo", "_foo", "foo_", "fOO", "f00", "éxéctôr", "❨╯°□°❩╯︵┻━┻"].each do |name|
28-
assert(valid_parameter_name?(name))
28+
assert(valid_parameter_name?(name), "Expected `#{name}` to be a valid parameter name")
2929
end
3030
end
3131

0 commit comments

Comments
 (0)