Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Jun 8, 2023
1 parent 132c0d7 commit cb802c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gem "rake", ">= 12.3.3"
gem "rspec"
gem "rubocop-shopify", require: false
gem "yard", "~> 0.9"
gem "awesome_print"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
awesome_print (1.9.2)
byebug (11.1.3)
diff-lcs (1.3)
parallel (1.21.0)
Expand Down Expand Up @@ -54,6 +55,7 @@ PLATFORMS
ruby

DEPENDENCIES
awesome_print
byebug
rake (>= 12.3.3)
rspec
Expand Down
17 changes: 11 additions & 6 deletions lib/rubocop/cop/sorbet/obsolete_strict_memoization.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "rubocop"
require "awesome_print"

module RuboCop
module Cop
Expand Down Expand Up @@ -40,13 +41,15 @@ class ObsoleteStrictMemoization < RuboCop::Cop::Base
# @!method legacy_memoization_pattern?(node)
def_node_matcher :legacy_memoization_pattern?, <<~PATTERN
(begin
(ivasgn $_ivar # @_ivar = ...
... # Match and ignore any other lines that come first.
$(ivasgn $_ivar # First line: @_ivar = ...
(send # T.let(_ivar, T.nilable(_ivar_type))
(const nil? :T) :let
(ivar _ivar)
(send # T.nilable(_ivar_type)
(const nil? :T) :nilable $_ivar_type)))
(or-asgn # @_ivar ||= _initialization_expr
...
$(or-asgn # Second line: @_ivar ||= _initialization_expr
(ivasgn _ivar)
$_initialization_expr))
PATTERN
Expand All @@ -55,9 +58,9 @@ def on_begin(node)
expression = legacy_memoization_pattern?(node)
return unless expression

add_offense(node, message: MESSAGE) do |corrector|
ivar, ivar_type, initialization_expr = expression

first_assignment_node, ivar, ivar_type, second_conditional_assignment_node, initialization_expr = expression
# ap expression
add_offense(first_assignment_node, message: MESSAGE) do |corrector|
base_indent = infer_base_indentation(node)

is_multiline_init_expr = initialization_expr.line_count != 1
Expand All @@ -75,7 +78,8 @@ def on_begin(node)
end
end

corrector.replace(node, correction)
corrector.replace(first_assignment_node, correction)
corrector.remove(second_conditional_assignment_node)
end
end

Expand Down Expand Up @@ -111,6 +115,7 @@ def max_line_length
def infer_base_indentation(node)
# The first line of the node has the indentation already stripped,
# so we infer the base indent from next non-blank line.
# ap(node)
second_line = node.source.lines.drop(1).find { |line| !line.strip.empty? }
/^\s*/.match(second_line)[0]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def foo
end
end

context "when its not the first line in a method", pending: "Not implemented yet" do
context "when its not the first line in a method" do
it "registers an offence and autocorrects" do
expect_offense(<<~RUBY)
sig { returns(Foo) }
Expand Down

0 comments on commit cb802c9

Please sign in to comment.