Skip to content

Commit

Permalink
Merge pull request #101 from Shopify/uk-refine-mutable-constant
Browse files Browse the repository at this point in the history
Refine `Style/MutableConstant` to process `T.let` expressions.
  • Loading branch information
paracycle committed Mar 9, 2022
2 parents a9d0bfd + c0b4cb0 commit 6634f03
Show file tree
Hide file tree
Showing 4 changed files with 665 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/rubocop/cop/sorbet/mutable_constant_sorbet_aware_behaviour.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "rubocop/cop/style/mutable_constant"

module RuboCop
module Cop
module Sorbet
module MutableConstantSorbetAwareBehaviour
def self.prepended(base)
base.def_node_matcher(:t_let, <<~PATTERN)
(send (const nil? :T) :let $_constant _type)
PATTERN
end

def on_assignment(value)
t_let(value) do |constant|
value = constant
end

super(value)
end
end
end
end
end

RuboCop::Cop::Style::MutableConstant.prepend(
RuboCop::Cop::Sorbet::MutableConstantSorbetAwareBehaviour
)
2 changes: 2 additions & 0 deletions lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
require_relative "sorbet/sigils/strong_sigil"
require_relative "sorbet/sigils/enforce_sigil_order"
require_relative "sorbet/sigils/enforce_single_sigil"

require_relative "sorbet/mutable_constant_sorbet_aware_behaviour"
5 changes: 5 additions & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ In the following section you find all available cops:
* [Sorbet/ValidSigil](cops_sorbet.md#sorbetvalidsigil)

<!-- END_COP_LIST -->

In addition to the cops defined in this gem, it also modifies the behaviour of some other cops
defined in other RuboCop gems:

* [Style/MutableConstant](https://docs.rubocop.org/rubocop/cops_style.html#stylemutableconstant): In addition to the default behaviour, RuboCop Sorbet makes this cop `T.let` aware, so that `CONST = T.let([1, 2, 3], T::Array[Integer])` is also treated as a mutable literal constant value.

0 comments on commit 6634f03

Please sign in to comment.