class RuboCop::Cop::Sorbet::BindingConstantWithoutTypeAlias
This cop disallows binding the return value of `T.any`, `T.all`, `T.enum` to a constant directly. To bind the value, one must use `T.type_alias`.
@example
# bad FooOrBar = T.any(Foo, Bar) # good FooOrBar = T.type_alias { T.any(Foo, Bar) }
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 110 def autocorrect(node) lambda do |corrector| corrector.replace( node.source_range, "T.type_alias { #{node.source} }" ) end end
not_dynamic_type_creation_with_block?(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 79 def not_dynamic_type_creation_with_block?(node) !dynamic_type_creation_with_block?(node) end
not_generic_parameter_decl?(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 83 def not_generic_parameter_decl?(node) !generic_parameter_decl?(node) end
not_nil?(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 87 def not_nil?(node) !node.nil? end
not_t_let?(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 75 def not_t_let?(node) !t_let?(node) end
on_casgn(node)
click to toggle source
# File lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb, line 91 def on_casgn(node) return unless binding_unaliased_type?(node) && !using_type_alias?(node.children[2]) if using_deprecated_type_alias_syntax?(node.children[2]) add_offense( node.children[2], message: "It looks like you're using the old `T.type_alias` syntax. " \ '`T.type_alias` now expects a block.' \ 'Run Sorbet with the options "--autocorrect --error-white-list=5043" ' \ 'to automatically upgrade to the new syntax.' ) return end add_offense( node.children[2], message: "It looks like you're trying to bind a type to a constant. " \ 'To do this, you must alias the type using `T.type_alias`.' ) end