rubocop-sorbet 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 680e24ce313fe336f5417dd3220b8d48dd964c1446551f46dbd45c176296d4b6
4
- data.tar.gz: c1064bb7fb452b930a3af32ea4e4c2367aa3a334cb85a152155654f0517c781c
3
+ metadata.gz: e08992657c999244fdd37049530c10f4fcec8b8cb6e684524a4470731ce99ed2
4
+ data.tar.gz: b8e2d1e235cadaff0ea6c2a2ce5f9c467dac66614635c346f44f9e2b0da596d8
5
5
  SHA512:
6
- metadata.gz: b1f8f864ffde946476f7c3b5f8d3d6d8c88bd7b067d3434fb0b731b34036c54a30fc2e45fe02650926fd6b87b3e833db8691cdd6d36adb13cec5e973ebe1b63c
7
- data.tar.gz: 6f18aa463bcdeba81770daeb90440e9e36fa443812832e9e41c3b61261b53a8bed94963bc6b0a354e220d9b18db843d74de957545ee2d50e99cf4e1213f5e9c6
6
+ metadata.gz: e28879548b27db2b19cd0d0663c0e9ee7fae3fd66479fde696263ba4a44a2893f2ab9236400178ea06434e099ed7b9a0400cf96e64b36fffa7545d55d59e045e
7
+ data.tar.gz: ff1ef7390f88de492bf084936222fac544de5e18f4ac93aa7882d33ec3692fbd65ec78c28d9b66686c7b9d6126152d70f77d0db822a4fbd40074bce2ef0f2822
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-sorbet (0.17.0)
4
+ rubocop-sorbet (0.17.1)
5
5
  lint_roller
6
6
  rbi (~> 0.4, >= 0.4.3)
7
7
  rbs (>= 4.1.1)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.17.0
1
+ 0.17.1
data/config/default.yml CHANGED
@@ -12,6 +12,12 @@ Sorbet/ForbidTAssertType:
12
12
  Description: 'Forbid usage of T.assert_type!.'
13
13
  Enabled: false
14
14
  VersionAdded: '0.17.0'
15
+ Sorbet/ForbidTCollectionInstantiation:
16
+ Description: 'Forbids instantiating Sorbet collection types instead of Ruby collections.'
17
+ Enabled: false
18
+ AutocorrectToRBS: false
19
+ SafeAutoCorrect: false
20
+ VersionAdded: '0.17.1'
15
21
 
16
22
  Sorbet/ForbidTHelpers:
17
23
  Description: 'Forbid usage of T::Helpers via `extend` or `include`.'
@@ -283,7 +289,10 @@ Sorbet/ForbidTAnyWithNil:
283
289
  Sorbet/ForbidTTypeAlias:
284
290
  Description: 'Forbid usage of T.type_alias.'
285
291
  Enabled: false
292
+ AutocorrectToRBS: false
293
+ SafeAutoCorrect: false
286
294
  VersionAdded: 0.10.4
295
+ VersionChanged: '0.17.1'
287
296
 
288
297
  Sorbet/ForbidTUnsafe:
289
298
  Description: 'Forbid usage of T.unsafe.'
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbi"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # Disallows instantiating Sorbet collection types. Instantiate the Ruby
9
+ # collection directly and declare its type separately when needed.
10
+ #
11
+ # Checks `T::Array`, `T::Hash`, `T::Set`, `T::Range`,
12
+ # `T::Enumerator`, `T::Enumerator::Lazy`, and `T::Enumerator::Chain`,
13
+ # with or without type arguments.
14
+ #
15
+ # Set `AutocorrectToRBS: true` to replace Sorbet constructors with Ruby
16
+ # constructors. RBS inline type annotations are added only to assignments.
17
+ # Assignments with an existing RBS annotation or an untranslatable type
18
+ # are not autocorrected.
19
+ # Empty Array and Hash constructors use literals unless arguments, blocks,
20
+ # or comments inside the call need to be preserved.
21
+ #
22
+ # @safety
23
+ # Autocorrection removes runtime evaluation of type arguments and changes
24
+ # constant lookup. RBS annotations require an RBS-aware type checker.
25
+ #
26
+ # @example
27
+ # # bad
28
+ # T::Array[String].new
29
+ # T::Hash[Symbol, Integer].new
30
+ # T::Set[String].new
31
+ # T::Array.new
32
+ #
33
+ # # good
34
+ # Array.new
35
+ # Hash.new
36
+ # Set.new
37
+ # T.let(Array.new, T::Array[String])
38
+ # Set.new #: Set[String]
39
+ #
40
+ # @example AutocorrectToRBS: true
41
+ # # bad
42
+ # arr = T::Array[String].new
43
+ # items = T::Set[T.nilable(String)].new(values)
44
+ # consume(T::Array[String].new)
45
+ #
46
+ # # good
47
+ # arr = [] #: Array[String]
48
+ # items = Set.new(values) #: Set[String?]
49
+ # consume([])
50
+ class ForbidTCollectionInstantiation < Base
51
+ include RBSAssertionCorrection
52
+ extend AutoCorrector
53
+
54
+ MSG = "Instantiate Ruby collections directly instead of Sorbet collection types."
55
+ RESTRICT_ON_SEND = [:new].freeze
56
+
57
+ # @!method t_collection?(node)
58
+ def_node_matcher :t_collection?, <<~PATTERN
59
+ {
60
+ (const (const {nil? cbase} :T) {:Array :Hash :Set :Range :Enumerator})
61
+ (const (const (const {nil? cbase} :T) :Enumerator) {:Lazy :Chain})
62
+ }
63
+ PATTERN
64
+
65
+ def on_send(node)
66
+ receiver = node.receiver
67
+ return unless receiver
68
+
69
+ type = receiver.send_type? && receiver.method?(:[]) ? receiver.receiver : receiver
70
+ return unless t_collection?(type)
71
+
72
+ add_offense(receiver) { |corrector| autocorrect_to_rbs(corrector, node, type) }
73
+ end
74
+ alias_method :on_csend, :on_send
75
+
76
+ private
77
+
78
+ def autocorrect_to_rbs(corrector, node, type)
79
+ return unless cop_config["AutocorrectToRBS"]
80
+
81
+ expression = node.block_node || node
82
+ expression = expression.parent while expression.parent&.begin_type? && expression.parent.children.one?
83
+ assignment = expression.parent
84
+ annotate = node.receiver.send_type? && assignment&.assignment? && assignment.children.last.equal?(expression)
85
+ return if annotate && !rbs_assertion_autocorrectable?(expression, allow_assignment: true)
86
+ return if comments_within?(node.receiver)
87
+
88
+ ruby_class = type.const_name.delete_prefix("T::")
89
+ ruby_class = "::#{ruby_class}" if type.source.start_with?("::")
90
+ if annotate
91
+ arguments = node.receiver.arguments.map { |argument| ::RBI::Type.parse_string(argument.source).rbs_string }
92
+ annotation = "#{ruby_class}[#{arguments.join(", ")}]"
93
+ corrector.insert_after(expression, " #: #{annotation}")
94
+ end
95
+ literal = empty_collection_literal(node, type)
96
+ if literal
97
+ corrector.replace(node, literal)
98
+ else
99
+ corrector.replace(node.receiver, ruby_class)
100
+ end
101
+ rescue ::RBI::Type::Error
102
+ nil
103
+ end
104
+
105
+ def empty_collection_literal(node, type)
106
+ return unless node.arguments.empty? && !node.block_node
107
+ return if comments_within?(node)
108
+
109
+ case type.short_name
110
+ when :Array then "[]"
111
+ when :Hash
112
+ parent = node.parent
113
+ if parent&.call_type? && parent.arguments.include?(node) && !parent.loc.begin
114
+ "({})"
115
+ else
116
+ "{}"
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -96,6 +96,15 @@ module RuboCop
96
96
  (send nil? {:const :prop} ...)
97
97
  PATTERN
98
98
 
99
+ # Override AST::Traversal's callbacks to stop at nested scopes, so their properties and `extend T::Sig` stay
100
+ # out of this class.
101
+ # RuboCop's separate traversal still visits nested structs and creates a fresh walker for each.
102
+ def on_class(_node); end
103
+ alias_method :on_module, :on_class
104
+ alias_method :on_sclass, :on_class
105
+ alias_method :on_def, :on_class
106
+ alias_method :on_defs, :on_class
107
+
99
108
  def on_send(node)
100
109
  if extend_t_sig?(node)
101
110
  # So we know we won't need to generate again a `extend T::Sig` line in the new class body
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rubocop"
4
+ require "rbi"
4
5
 
5
6
  module RuboCop
6
7
  module Cop
7
8
  module Sorbet
8
9
  # Disallows using `T.type_alias` anywhere.
10
+ # Set `AutocorrectToRBS: true` to replace standalone constant assignments with RBS type aliases.
11
+ # Autocorrection is unsafe because it removes the Ruby constant. References to the
12
+ # constant must be migrated separately to the lowercase RBS alias name.
13
+ # Declarations containing RuboCop directives are not autocorrected.
9
14
  #
10
15
  # @example
11
16
  #
@@ -15,16 +20,61 @@ module RuboCop
15
20
  # # good
16
21
  # #: type string_or_integer = Integer | String
17
22
  class ForbidTTypeAlias < RuboCop::Cop::Base
23
+ include RBSAssertionCorrection
24
+ extend AutoCorrector
25
+
18
26
  MSG = "Do not use `T.type_alias`."
19
27
 
20
28
  # @!method t_type_alias?(node)
21
29
  def_node_matcher(:t_type_alias?, "(block (call (const nil? :T) :type_alias) _ _)")
22
30
 
23
31
  def on_block(node)
24
- add_offense(node) if t_type_alias?(node)
32
+ return unless t_type_alias?(node)
33
+
34
+ add_offense(node) { |corrector| autocorrect_type_alias(corrector, node) }
25
35
  end
26
36
  alias_method :on_numblock, :on_block
27
37
  alias_method :on_itblock, :on_block
38
+
39
+ private
40
+
41
+ def autocorrect_type_alias(corrector, node)
42
+ assignment = node.parent
43
+ return unless assignment&.casgn_type? && assignment.children.first.nil?
44
+ return unless standalone_alias?(assignment)
45
+ return unless node.arguments.empty? && node.body
46
+ return unless rbs_assertion_autocorrectable?(node, allow_assignment: true)
47
+
48
+ name = assignment.children[1].to_s
49
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
50
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
51
+ .downcase
52
+ type = ::RBI::Type.parse_string(node.body.source).rbs_string
53
+ declaration = "type #{name} = #{type}"
54
+
55
+ range = assignment.source_range
56
+ comments = processed_source.each_comment_in_lines(range.first_line..range.last_line).select do |comment|
57
+ comment.source_range.begin_pos >= range.begin_pos
58
+ end
59
+ return if comments.any? { |comment| comment.text.match?(/\A#[:|]|\brubocop\s*:/) }
60
+
61
+ indentation = range.source_line[/\A[ \t]*/]
62
+ replacement = (comments.map(&:text) << "#: #{declaration}").join("\n#{indentation}")
63
+ range = range.join(comments.last.source_range) if comments.last&.source_range&.end_pos.to_i > range.end_pos
64
+ corrector.replace(range, replacement)
65
+ rescue ::RBI::Type::Error
66
+ nil
67
+ end
68
+
69
+ def standalone_alias?(assignment)
70
+ parent = assignment.parent
71
+ if parent&.begin_type?
72
+ return false if parent.loc.begin
73
+
74
+ parent = parent.parent
75
+ end
76
+ parent.nil? || parent.type?(:class, :module)
77
+ end
28
78
  end
29
79
  end
30
80
  end
@@ -15,6 +15,7 @@ require_relative "sorbet/forbid_include_const_literal"
15
15
  require_relative "sorbet/forbid_mixes_in_class_methods"
16
16
  require_relative "sorbet/forbid_t_any_with_nil"
17
17
  require_relative "sorbet/forbid_t_assert_type"
18
+ require_relative "sorbet/forbid_t_collection_instantiation"
18
19
  require_relative "sorbet/forbid_type_aliased_shapes"
19
20
  require_relative "sorbet/forbid_untyped_struct_props"
20
21
  require_relative "sorbet/implicit_conversion_method"
data/manual/cops.md CHANGED
@@ -33,6 +33,7 @@ In the following section you find all available cops:
33
33
  * [Sorbet/ForbidTBind](cops_sorbet.md#sorbetforbidtbind)
34
34
  * [Sorbet/ForbidTBindInAssignment](cops_sorbet.md#sorbetforbidtbindinassignment)
35
35
  * [Sorbet/ForbidTCast](cops_sorbet.md#sorbetforbidtcast)
36
+ * [Sorbet/ForbidTCollectionInstantiation](cops_sorbet.md#sorbetforbidtcollectioninstantiation)
36
37
  * [Sorbet/ForbidTEnum](cops_sorbet.md#sorbetforbidtenum)
37
38
  * [Sorbet/ForbidTHelpers](cops_sorbet.md#sorbetforbidthelpers)
38
39
  * [Sorbet/ForbidTLet](cops_sorbet.md#sorbetforbidtlet)
@@ -788,6 +788,62 @@ Name | Default value | Configurable values
788
788
  --- | --- | ---
789
789
  AutocorrectToRBS | `false` | Boolean
790
790
 
791
+ ## Sorbet/ForbidTCollectionInstantiation
792
+
793
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
794
+ --- | --- | --- | --- | ---
795
+ Disabled | Yes | Yes (Unsafe) | 0.17.1 | -
796
+
797
+ Disallows instantiating Sorbet collection types. Instantiate the Ruby
798
+ collection directly and declare its type separately when needed.
799
+
800
+ Checks `T::Array`, `T::Hash`, `T::Set`, `T::Range`,
801
+ `T::Enumerator`, `T::Enumerator::Lazy`, and `T::Enumerator::Chain`,
802
+ with or without type arguments.
803
+
804
+ Set `AutocorrectToRBS: true` to replace Sorbet constructors with Ruby
805
+ constructors. RBS inline type annotations are added only to assignments.
806
+ Assignments with an existing RBS annotation or an untranslatable type
807
+ are not autocorrected.
808
+ Empty Array and Hash constructors use literals unless arguments, blocks,
809
+ or comments inside the call need to be preserved.
810
+
811
+ ### Examples
812
+
813
+ ```ruby
814
+ # bad
815
+ T::Array[String].new
816
+ T::Hash[Symbol, Integer].new
817
+ T::Set[String].new
818
+ T::Array.new
819
+
820
+ # good
821
+ Array.new
822
+ Hash.new
823
+ Set.new
824
+ T.let(Array.new, T::Array[String])
825
+ Set.new #: Set[String]
826
+ ```
827
+ #### AutocorrectToRBS: true
828
+
829
+ ```ruby
830
+ # bad
831
+ arr = T::Array[String].new
832
+ items = T::Set[T.nilable(String)].new(values)
833
+ consume(T::Array[String].new)
834
+
835
+ # good
836
+ arr = [] #: Array[String]
837
+ items = Set.new(values) #: Set[String?]
838
+ consume([])
839
+ ```
840
+
841
+ ### Configurable attributes
842
+
843
+ Name | Default value | Configurable values
844
+ --- | --- | ---
845
+ AutocorrectToRBS | `false` | Boolean
846
+
791
847
  ## Sorbet/ForbidTEnum
792
848
 
793
849
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
@@ -996,9 +1052,13 @@ AutocorrectStyle | `sig` | String
996
1052
 
997
1053
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
998
1054
  --- | --- | --- | --- | ---
999
- Disabled | Yes | No | 0.10.4 | -
1055
+ Disabled | Yes | Yes (Unsafe) | 0.10.4 | 0.17.1
1000
1056
 
1001
1057
  Disallows using `T.type_alias` anywhere.
1058
+ Set `AutocorrectToRBS: true` to replace standalone constant assignments with RBS type aliases.
1059
+ Autocorrection is unsafe because it removes the Ruby constant. References to the
1060
+ constant must be migrated separately to the lowercase RBS alias name.
1061
+ Declarations containing RuboCop directives are not autocorrected.
1002
1062
 
1003
1063
  ### Examples
1004
1064
 
@@ -1010,6 +1070,12 @@ STRING_OR_INTEGER = T.type_alias { T.any(Integer, String) }
1010
1070
  #: type string_or_integer = Integer | String
1011
1071
  ```
1012
1072
 
1073
+ ### Configurable attributes
1074
+
1075
+ Name | Default value | Configurable values
1076
+ --- | --- | ---
1077
+ AutocorrectToRBS | `false` | Boolean
1078
+
1013
1079
  ## Sorbet/ForbidTUnsafe
1014
1080
 
1015
1081
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-sorbet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ufuk Kayserilioglu
@@ -140,6 +140,7 @@ files:
140
140
  - lib/rubocop/cop/sorbet/forbid_t_bind.rb
141
141
  - lib/rubocop/cop/sorbet/forbid_t_bind_in_assignment.rb
142
142
  - lib/rubocop/cop/sorbet/forbid_t_cast.rb
143
+ - lib/rubocop/cop/sorbet/forbid_t_collection_instantiation.rb
143
144
  - lib/rubocop/cop/sorbet/forbid_t_enum.rb
144
145
  - lib/rubocop/cop/sorbet/forbid_t_helpers.rb
145
146
  - lib/rubocop/cop/sorbet/forbid_t_let.rb