Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better name validation for methods and parameters #987

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ def as_nilable_type(type)
"T.nilable(#{type})"
end
end

sig { params(name: String).returns(T::Boolean) }
def valid_parameter_name?(name)
name.match?(/^[[[:alnum:]]_]+$/)
end
end
end
end
18 changes: 1 addition & 17 deletions lib/tapioca/gem/listeners/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ module Listeners
class Methods < Base
extend T::Sig

include RBIHelper
include Runtime::Reflection

SPECIAL_METHOD_NAMES = T.let([
"!", "~", "+@", "**", "-@", "*", "/", "%", "+", "-", "<<", ">>", "&", "|", "^",
"<", "<=", "=>", ">", ">=", "==", "===", "!=", "=~", "!~", "<=>", "[]", "[]=", "`",
], T::Array[String])

private

sig { override.params(event: ScopeNodeAdded).void }
Expand Down Expand Up @@ -184,18 +180,6 @@ def struct_method?(constant, method_name)
.include?(method_name.gsub(/=$/, "").to_sym)
end

sig { params(name: String).returns(T::Boolean) }
def valid_method_name?(name)
return true if SPECIAL_METHOD_NAMES.include?(name)

!!name.match(/^[[:word:]]+[?!=]?$/)
end

sig { params(name: String).returns(T::Boolean) }
def valid_parameter_name?(name)
name.match?(/^[[[:alnum:]]_]+$/)
end

sig { params(constant: Module).returns(T.nilable(UnboundMethod)) }
def initialize_method_for(constant)
constant.instance_method(:initialize)
Expand Down
11 changes: 11 additions & 0 deletions lib/tapioca/helpers/rbi_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module RBIHelper
extend T::Sig
include SorbetHelper
extend SorbetHelper
extend self

sig { params(name: String, type: String).returns(RBI::TypedParam) }
def create_param(name, type:)
Expand Down Expand Up @@ -89,5 +90,15 @@ def self.serialize_type_variable(type, variance, fixed, upper, lower)
serialized << " { { #{block.join(", ")} } }" unless block.empty?
serialized
end

sig { params(name: String).returns(T::Boolean) }
def valid_method_name?(name)
!name.to_sym.inspect.start_with?(':"', ":@", ":$")
end

sig { params(name: String).returns(T::Boolean) }
def valid_parameter_name?(name)
/^([[:lower:]]|_|[^[[:ascii:]]])([[:alnum:]]|_|[^[[:ascii:]]])*$/.match?(name)
end
end
end
15 changes: 1 addition & 14 deletions lib/tapioca/rbi_ext/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def create_type_variable(name, type:, variance: :invariant, fixed: nil, upper: n
).void
end
def create_method(name, parameters: [], return_type: "T.untyped", class_method: false, visibility: RBI::Public.new)
return unless valid_method_name?(name)
return unless Tapioca::RBIHelper.valid_method_name?(name)

sig = RBI::Sig.new(return_type: return_type)
method = RBI::Method.new(name, sigs: [sig], is_singleton: class_method, visibility: visibility)
Expand All @@ -99,19 +99,6 @@ def create_method(name, parameters: [], return_type: "T.untyped", class_method:

private

SPECIAL_METHOD_NAMES = T.let(
["!", "~", "+@", "**", "-@", "*", "/", "%", "+", "-", "<<", ">>", "&", "|", "^", "<", "<=", "=>", ">", ">=",
"==", "===", "!=", "=~", "!~", "<=>", "[]", "[]=", "`",].freeze,
T::Array[String]
)

sig { params(name: String).returns(T::Boolean) }
def valid_method_name?(name)
return true if SPECIAL_METHOD_NAMES.include?(name)

!!name.match(/^[a-zA-Z_][[:word:]]*[?!=]?$/)
end

sig { returns(T::Hash[String, RBI::Node]) }
def nodes_cache
T.must(@nodes_cache ||= T.let({}, T.nilable(T::Hash[String, Node])))
Expand Down
5 changes: 0 additions & 5 deletions sorbet/rbi/gems/[email protected]

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

42 changes: 42 additions & 0 deletions spec/tapioca/helpers/rbi_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# typed: strict
# frozen_string_literal: true

require "spec_helper"

class Tapioca::RBIHelperSpec < Minitest::Spec
include Tapioca::RBIHelper

describe Tapioca::RBIHelper do
it "accepts valid method names" do
[
"f", "foo", "_foo", "foo_", "fOO", "f00", "foo!", "foo=", "foo?", "Foo", "éxéctôr", "❨╯°□°❩╯︵┻━┻",
"!", "~", "+@", "**", "-@", "*", "/", "%", "+", "-", "<<", ">>", "&", "|", "^",
"<", "<=", ">", ">=", "==", "===", "!=", "=~", "!~", "<=>", "[]", "[]=", "`",
].each do |name|
assert(valid_method_name?(name))
end
end

it "rejects invalid method names" do
["", "42", "42foo", "!foo", "-@foo", "foo-", "foo-bar", "foo.bar", "=>"].each do |name|
refute(valid_method_name?(name))
end
end

it "accepts valid parameter names" do
["f", "foo", "_foo", "foo_", "fOO", "f00", "éxéctôr", "❨╯°□°❩╯︵┻━┻"].each do |name|
assert(valid_parameter_name?(name))
end
end

it "rejects invalid parameter names" do
[
"", "42", "42foo", "foo!", "foo=", "foo?", "Foo", "!foo", "-@foo", "foo-", "foo-bar", "foo.bar",
"!", "~", "+@", "**", "-@", "*", "/", "%", "+", "-", "<<", ">>", "&", "|", "^",
"<", "<=", "=>", ">", ">=", "==", "===", "!=", "=~", "!~", "<=>", "[]", "[]=", "`",
].each do |name|
refute(valid_parameter_name?(name))
end
end
end
end