diff --git a/config/default.yml b/config/default.yml index 07780afb..0a95f986 100644 --- a/config/default.yml +++ b/config/default.yml @@ -200,11 +200,6 @@ Sorbet/BuggyObsoleteStrictMemoization: Safe: true SafeAutoCorrect: false -Sorbet/OneAncestorPerLine: - Description: 'Enforces one ancestor per call to requires_ancestor' - Enabled: false - VersionAdded: '0.6.0' - Sorbet/RedundantExtendTSig: Description: >- Forbid the usage of redundant `extend T::Sig`. diff --git a/config/obsoletion.yml b/config/obsoletion.yml index f0189180..6c41ce9b 100644 --- a/config/obsoletion.yml +++ b/config/obsoletion.yml @@ -3,4 +3,6 @@ # # See: https://docs.rubocop.org/rubocop/extensions.html#config-obsoletions # -{} +removed: + Sorbet/OneAncestorPerLine: + reason: '`require_ancestor` now takes a block instead of arguments' diff --git a/lib/rubocop/cop/sorbet/one_ancestor_per_line.rb b/lib/rubocop/cop/sorbet/one_ancestor_per_line.rb deleted file mode 100644 index b01d04b9..00000000 --- a/lib/rubocop/cop/sorbet/one_ancestor_per_line.rb +++ /dev/null @@ -1,80 +0,0 @@ -# encoding: utf-8 -# frozen_string_literal: true - -require "rubocop" - -module RuboCop - module Cop - module Sorbet - # Ensures one ancestor per requires_ancestor line - # rather than chaining them as a comma-separated list. - # - # @example - # - # # bad - # module SomeModule - # requires_ancestor Kernel, Minitest::Assertions - # end - # - # # good - # module SomeModule - # requires_ancestor Kernel - # requires_ancestor Minitest::Assertions - # end - class OneAncestorPerLine < RuboCop::Cop::Cop # rubocop:todo InternalAffairs/InheritDeprecatedCopClass - MSG = "Cannot require more than one ancestor per line" - - # @!method requires_ancestors(node) - def_node_search :requires_ancestors, <<~PATTERN - (send nil? :requires_ancestor ...) - PATTERN - - # @!method more_than_one_ancestor(node) - def_node_matcher :more_than_one_ancestor, <<~PATTERN - (send nil? :requires_ancestor const const+) - PATTERN - - # @!method abstract?(node) - def_node_search :abstract?, <<~PATTERN - (send nil? :abstract!) - PATTERN - - def on_module(node) - return unless node.body - return unless requires_ancestors(node) - - process_node(node) - end - - def on_class(node) - return unless abstract?(node) - return unless requires_ancestors(node) - - process_node(node) - end - - def autocorrect(node) - ->(corrector) do - ra_call = node.parent - split_ra_calls = ra_call.source.gsub(/,\s+/, new_ra_line(ra_call.loc.column)) - corrector.replace(ra_call, split_ra_calls) - end - end - - private - - def process_node(node) - requires_ancestors(node).each do |ra| - add_offense(ra.child_nodes[1]) if more_than_one_ancestor(ra) - end - end - - def new_ra_line(indent_count) - indents = " " * indent_count - indented_ra_call = "#{indents}requires_ancestor " - "\n#{indented_ra_call}" - end - end - end - end -end diff --git a/lib/rubocop/cop/sorbet_cops.rb b/lib/rubocop/cop/sorbet_cops.rb index c952ea42..097f915a 100644 --- a/lib/rubocop/cop/sorbet_cops.rb +++ b/lib/rubocop/cop/sorbet_cops.rb @@ -10,7 +10,6 @@ require_relative "sorbet/forbid_type_aliased_shapes" require_relative "sorbet/forbid_untyped_struct_props" require_relative "sorbet/implicit_conversion_method" -require_relative "sorbet/one_ancestor_per_line" require_relative "sorbet/callback_conditionals_binding" require_relative "sorbet/forbid_t_struct" require_relative "sorbet/forbid_t_unsafe" diff --git a/manual/cops.md b/manual/cops.md index f83034d4..cad412b8 100644 --- a/manual/cops.md +++ b/manual/cops.md @@ -31,7 +31,6 @@ In the following section you find all available cops: * [Sorbet/KeywordArgumentOrdering](cops_sorbet.md#sorbetkeywordargumentordering) * [Sorbet/MultipleTEnumValues](cops_sorbet.md#sorbetmultipletenumvalues) * [Sorbet/ObsoleteStrictMemoization](cops_sorbet.md#sorbetobsoletestrictmemoization) -* [Sorbet/OneAncestorPerLine](cops_sorbet.md#sorbetoneancestorperline) * [Sorbet/RedundantExtendTSig](cops_sorbet.md#sorbetredundantextendtsig) * [Sorbet/SignatureBuildOrder](cops_sorbet.md#sorbetsignaturebuildorder) * [Sorbet/SingleLineRbiClassModuleDefinitions](cops_sorbet.md#sorbetsinglelinerbiclassmoduledefinitions) diff --git a/manual/cops_sorbet.md b/manual/cops_sorbet.md index de6b8ebd..f11ca8fa 100644 --- a/manual/cops_sorbet.md +++ b/manual/cops_sorbet.md @@ -703,30 +703,6 @@ def foo end ``` -## Sorbet/OneAncestorPerLine - -Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged ---- | --- | --- | --- | --- -Disabled | Yes | Yes | 0.6.0 | - - -Ensures one ancestor per requires_ancestor line -rather than chaining them as a comma-separated list. - -### Examples - -```ruby -# bad -module SomeModule - requires_ancestor Kernel, Minitest::Assertions -end - -# good -module SomeModule - requires_ancestor Kernel - requires_ancestor Minitest::Assertions -end -``` - ## Sorbet/RedundantExtendTSig Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged diff --git a/spec/rubocop/cop/sorbet/one_ancestor_per_line_spec.rb b/spec/rubocop/cop/sorbet/one_ancestor_per_line_spec.rb deleted file mode 100644 index 1ed59a36..00000000 --- a/spec/rubocop/cop/sorbet/one_ancestor_per_line_spec.rb +++ /dev/null @@ -1,212 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe(RuboCop::Cop::Sorbet::OneAncestorPerLine, :config) do - describe("no offences") do - it "adds no offences when there are no requires_ancestor calls" do - expect_no_offenses(<<~RUBY) - module MyModule - def hello_world; end - end - RUBY - end - - it "adds no offences when just one ancestor is required per line" do - expect_no_offenses(<<~RUBY) - module MyModule - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor Foo::Bar - end - RUBY - end - - it "adds no offences when an abstract class does not require any ancestors" do - expect_no_offenses(<<~RUBY) - class MyClass - extend T::Sig - extend T::Helpers - abstract! - - sig {abstract.void} - def self.foo; end - end - RUBY - end - - it "adds no offences when an abstract class has just one required ancestor per line" do - expect_no_offenses(<<~RUBY) - class MyClass - extend T::Sig - extend T::Helpers - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor Foo::Bar - - abstract! - - sig {abstract.void} - def self.foo; end - end - RUBY - end - end - - describe("offenses") do - it "adds offences when more than one ancestor is required on a line" do - expect_offense(<<~RUBY) - module MyModule - requires_ancestor Kernel, Minitest::Assertions, SomeOtherModule, Foo::Bar - ^^^^^^^^^^^^^^^^^^^^ Cannot require more than one ancestor per line - end - RUBY - end - - it "adds offences when a number of ancestors are formatted across multiple lines" do - expect_offense(<<~RUBY) - module MyModule - requires_ancestor Kernel, Minitest::Assertions, - ^^^^^^^^^^^^^^^^^^^^ Cannot require more than one ancestor per line - SomeOtherModule, Foo::Bar - end - RUBY - end - - it "adds offences to abstract classes that use more than one ancestor per line" do - expect_offense(<<~RUBY) - class MyClass - extend T::Sig - extend T::Helpers - requires_ancestor Kernel, Minitest::Assertions, Foo::Bar - ^^^^^^^^^^^^^^^^^^^^ Cannot require more than one ancestor per line - abstract! - - sig {abstract.void} - def self.foo; end - end - RUBY - end - - it "adds an offence to a module inside a not-abstract class" do - expect_offense(<<~RUBY) - class Foo - # not abstract - - module Bar - requires_ancestor Kernel, Minitest::Assertions - ^^^^^^^^^^^^^^^^^^^^ Cannot require more than one ancestor per line - end - end - RUBY - end - - it "adds an offence to a module inside a not-abstract class" do - expect_offense(<<~RUBY) - class Foo - extend T::Helpers - - abstract! - - module Bar - requires_ancestor Kernel, Minitest::Assertions - ^^^^^^^^^^^^^^^^^^^^ Cannot require more than one ancestor per line - end - end - RUBY - end - end - - describe("Autocorrect") do - it "autocorrects the source to have requires_ancestor only call one ancestor per line" do - source = <<~RUBY - module MyModule - requires_ancestor Kernel, Minitest::Assertions, SomeOtherModule, Foo::Bar - end - RUBY - expect(autocorrect_source(source)) - .to(eq(<<~RUBY)) - module MyModule - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor SomeOtherModule - requires_ancestor Foo::Bar - end - RUBY - end - - it "autocorrects when a large number of calls are formatted across multiple lines" do - source = <<~RUBY - module MyModule - requires_ancestor Kernel, Minitest::Assertions, - SomeOtherModule, Foo::Bar - end - RUBY - expect(autocorrect_source(source)) - .to(eq(<<~RUBY)) - module MyModule - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor SomeOtherModule - requires_ancestor Foo::Bar - end - RUBY - end - - it "does not try to autocorrect otherwise valid code" do - source = <<~RUBY - module MyModule - requires_ancestor Kernel, Minitest::Assertions, - SomeOtherModule, Foo::Bar - - def foo(one, two) - # Method body - end - end - RUBY - expect(autocorrect_source(source)) - .to(eq(<<~RUBY)) - module MyModule - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor SomeOtherModule - requires_ancestor Foo::Bar - - def foo(one, two) - # Method body - end - end - RUBY - end - - it "autocorrects abstract classes correctly" do - source = <<~RUBY - class MyClass - extend T::Sig - extend T::Helpers - requires_ancestor Kernel, Minitest::Assertions, Foo::Bar - - abstract! - - sig {abstract.void} - def self.foo; end - end - RUBY - expect(autocorrect_source(source)) - .to(eq(<<~RUBY)) - class MyClass - extend T::Sig - extend T::Helpers - requires_ancestor Kernel - requires_ancestor Minitest::Assertions - requires_ancestor Foo::Bar - - abstract! - - sig {abstract.void} - def self.foo; end - end - RUBY - end - end -end