From 33c13ff7623c66b42d281a0107bbf3b083a8378a Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 31 May 2021 18:41:58 -0400 Subject: [PATCH 1/3] Move RBi related cops to `rbi/` Signed-off-by: Alexandre Terrasa --- .../sorbet/{ => rbi}/forbid_extend_t_sig_helpers_in_shims.rb | 0 .../{ => rbi}/single_line_rbi_class_module_definitions.rb | 0 lib/rubocop/cop/sorbet_cops.rb | 5 +++-- .../{ => rbi}/forbid_extend_t_sig_helpers_in_shims_spec.rb | 0 .../single_line_rbi_class_module_definitions_spec.rb | 0 5 files changed, 3 insertions(+), 2 deletions(-) rename lib/rubocop/cop/sorbet/{ => rbi}/forbid_extend_t_sig_helpers_in_shims.rb (100%) rename lib/rubocop/cop/sorbet/{ => rbi}/single_line_rbi_class_module_definitions.rb (100%) rename spec/rubocop/cop/sorbet/{ => rbi}/forbid_extend_t_sig_helpers_in_shims_spec.rb (100%) rename spec/rubocop/cop/sorbet/{ => rbi}/single_line_rbi_class_module_definitions_spec.rb (100%) diff --git a/lib/rubocop/cop/sorbet/forbid_extend_t_sig_helpers_in_shims.rb b/lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb similarity index 100% rename from lib/rubocop/cop/sorbet/forbid_extend_t_sig_helpers_in_shims.rb rename to lib/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims.rb diff --git a/lib/rubocop/cop/sorbet/single_line_rbi_class_module_definitions.rb b/lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb similarity index 100% rename from lib/rubocop/cop/sorbet/single_line_rbi_class_module_definitions.rb rename to lib/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions.rb diff --git a/lib/rubocop/cop/sorbet_cops.rb b/lib/rubocop/cop/sorbet_cops.rb index 7c4be028..f49a60a8 100644 --- a/lib/rubocop/cop/sorbet_cops.rb +++ b/lib/rubocop/cop/sorbet_cops.rb @@ -1,14 +1,15 @@ # frozen_string_literal: true require_relative 'sorbet/binding_constants_without_type_alias' require_relative 'sorbet/constants_from_strings' -require_relative 'sorbet/forbid_extend_t_sig_helpers_in_shims' require_relative 'sorbet/forbid_superclass_const_literal' require_relative 'sorbet/forbid_include_const_literal' require_relative 'sorbet/forbid_untyped_struct_props' -require_relative 'sorbet/single_line_rbi_class_module_definitions' require_relative 'sorbet/one_ancestor_per_line' require_relative 'sorbet/callback_conditionals_binding' +require_relative 'sorbet/rbi/forbid_extend_t_sig_helpers_in_shims' +require_relative 'sorbet/rbi/single_line_rbi_class_module_definitions' + require_relative 'sorbet/signatures/allow_incompatible_override' require_relative 'sorbet/signatures/checked_true_in_signature' require_relative 'sorbet/signatures/keyword_argument_ordering' diff --git a/spec/rubocop/cop/sorbet/forbid_extend_t_sig_helpers_in_shims_spec.rb b/spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb similarity index 100% rename from spec/rubocop/cop/sorbet/forbid_extend_t_sig_helpers_in_shims_spec.rb rename to spec/rubocop/cop/sorbet/rbi/forbid_extend_t_sig_helpers_in_shims_spec.rb diff --git a/spec/rubocop/cop/sorbet/single_line_rbi_class_module_definitions_spec.rb b/spec/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions_spec.rb similarity index 100% rename from spec/rubocop/cop/sorbet/single_line_rbi_class_module_definitions_spec.rb rename to spec/rubocop/cop/sorbet/rbi/single_line_rbi_class_module_definitions_spec.rb From 6ee09e0abf7c09524c61c161c0fa8de24cbea355 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 1 Jun 2021 15:07:35 -0400 Subject: [PATCH 2/3] Update documentation Signed-off-by: Alexandre Terrasa --- manual/cops_sorbet.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/manual/cops_sorbet.md b/manual/cops_sorbet.md index 47153ba0..9923d215 100644 --- a/manual/cops_sorbet.md +++ b/manual/cops_sorbet.md @@ -198,7 +198,7 @@ This cop makes the Sorbet `false` sigil mandatory in all files. Name | Default value | Configurable values --- | --- | --- -SuggestedStrictness | `true` | Boolean +SuggestedStrictness | `false` | String Include | `**/*.rb`, `**/*.rbi`, `**/*.rake`, `**/*.ru` | Array Exclude | `bin/**/*`, `db/**/*.rb`, `script/**/*` | Array @@ -298,6 +298,13 @@ Options: If a `MinimumStrictness` level is specified, it will be used in offense messages and autocorrect. +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +SuggestedStrictness | `false` | String +MinimumStrictness | `false` | String + ## Sorbet/IgnoreSigil Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged @@ -306,6 +313,12 @@ Disabled | Yes | Yes | 0.3.3 | - This cop makes the Sorbet `ignore` sigil mandatory in all files. +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +SuggestedStrictness | `ignore` | String + ## Sorbet/KeywordArgumentOrdering Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged @@ -427,6 +440,12 @@ Disabled | Yes | Yes | 0.3.3 | - This cop makes the Sorbet `strict` sigil mandatory in all files. +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +SuggestedStrictness | `strict` | String + ## Sorbet/StrongSigil Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged @@ -435,6 +454,12 @@ Disabled | Yes | Yes | 0.3.3 | - This cop makes the Sorbet `strong` sigil mandatory in all files. +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +SuggestedStrictness | `strong` | String + ## Sorbet/TrueSigil Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged @@ -443,6 +468,12 @@ Disabled | Yes | Yes | 0.3.3 | - This cop makes the Sorbet `true` sigil mandatory in all files. +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +SuggestedStrictness | `true` | String + ## Sorbet/ValidSigil Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged @@ -459,3 +490,11 @@ Options: * `MinimumStrictness`: If set, make offense if the strictness level in the file is below this one If a `MinimumStrictness` level is specified, it will be used in offense messages and autocorrect. + +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +RequireSigilOnAllFiles | `false` | Boolean +SuggestedStrictness | `false` | String +MinimumStrictness | `false` | String From 834a33e111bd88373b840924fa06a471c2d546c4 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 31 May 2021 19:58:39 -0400 Subject: [PATCH 3/3] Add cop to ensure RBI files are always under sorbet/rbi Signed-off-by: Alexandre Terrasa --- config/default.yml | 7 ++++ .../rbi/forbid_rbi_outside_of_sorbet_dir.rb | 31 ++++++++++++++++++ lib/rubocop/cop/sorbet_cops.rb | 1 + manual/cops.md | 1 + manual/cops_sorbet.md | 26 +++++++++++++++ .../forbid_rbi_outside_of_sorbet_dir_spec.rb | 32 +++++++++++++++++++ 6 files changed, 98 insertions(+) create mode 100644 lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir.rb create mode 100644 spec/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir_spec.rb diff --git a/config/default.yml b/config/default.yml index d4dcc1a6..7a4ce382 100644 --- a/config/default.yml +++ b/config/default.yml @@ -65,6 +65,13 @@ Sorbet/ForbidExtendTSigHelpersInShims: Include: - "**/*.rbi" +Sorbet/ForbidRBIOutsideOfSorbetDir: + Description: 'Forbids RBI files outside of the sorbet/ directory.' + Enabled: true + VersionAdded: 0.6.1 + Include: + - "**/*.rbi" + Sorbet/ForbidIncludeConstLiteral: Description: 'Forbids include of non-literal constants.' Enabled: false diff --git a/lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir.rb b/lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir.rb new file mode 100644 index 00000000..6932a58e --- /dev/null +++ b/lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Sorbet + # This cop makes sure that RBI files are always located under sorbet/rbi/. + # + # @example + # # bad + # lib/some_file.rbi + # other_file.rbi + # + # # good + # sorbet/rbi/some_file.rbi + # sorbet/rbi/any/path/for/file.rbi + class ForbidRBIOutsideOfSorbetDir < RuboCop::Cop::Cop + include RangeHelp + + PATH_REGEXP = %r{sorbet/rbi} + + def investigate(processed_source) + add_offense( + nil, + location: source_range(processed_source.buffer, 1, 0), + message: "RBI files are only accepted in the sorbet/rbi/ directory." + ) unless processed_source.file_path =~ PATH_REGEXP + end + end + end + end +end diff --git a/lib/rubocop/cop/sorbet_cops.rb b/lib/rubocop/cop/sorbet_cops.rb index f49a60a8..059a398e 100644 --- a/lib/rubocop/cop/sorbet_cops.rb +++ b/lib/rubocop/cop/sorbet_cops.rb @@ -8,6 +8,7 @@ require_relative 'sorbet/callback_conditionals_binding' require_relative 'sorbet/rbi/forbid_extend_t_sig_helpers_in_shims' +require_relative 'sorbet/rbi/forbid_rbi_outside_of_sorbet_dir' require_relative 'sorbet/rbi/single_line_rbi_class_module_definitions' require_relative 'sorbet/signatures/allow_incompatible_override' diff --git a/manual/cops.md b/manual/cops.md index 48b8a6ca..d1ee2ff3 100644 --- a/manual/cops.md +++ b/manual/cops.md @@ -15,6 +15,7 @@ In the following section you find all available cops: * [Sorbet/FalseSigil](cops_sorbet.md#sorbetfalsesigil) * [Sorbet/ForbidExtendTSigHelpersInShims](cops_sorbet.md#sorbetforbidextendtsighelpersinshims) * [Sorbet/ForbidIncludeConstLiteral](cops_sorbet.md#sorbetforbidincludeconstliteral) +* [Sorbet/ForbidRBIOutsideOfSorbetDir](cops_sorbet.md#sorbetforbidrbioutsideofsorbetdir) * [Sorbet/ForbidSuperclassConstLiteral](cops_sorbet.md#sorbetforbidsuperclassconstliteral) * [Sorbet/ForbidUntypedStructProps](cops_sorbet.md#sorbetforbiduntypedstructprops) * [Sorbet/HasSigil](cops_sorbet.md#sorbethassigil) diff --git a/manual/cops_sorbet.md b/manual/cops_sorbet.md index 9923d215..fb187bba 100644 --- a/manual/cops_sorbet.md +++ b/manual/cops_sorbet.md @@ -244,6 +244,32 @@ Disabled | Yes | No | 0.2.0 | 0.5.0 No documentation +## Sorbet/ForbidRBIOutsideOfSorbetDir + +Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged +--- | --- | --- | --- | --- +Enabled | Yes | No | 0.6.1 | - + +This cop makes sure that RBI files are always located under sorbet/rbi/. + +### Examples + +```ruby +# bad +lib/some_file.rbi +other_file.rbi + +# good +sorbet/rbi/some_file.rbi +sorbet/rbi/any/path/for/file.rbi +``` + +### Configurable attributes + +Name | Default value | Configurable values +--- | --- | --- +Include | `**/*.rbi` | Array + ## Sorbet/ForbidSuperclassConstLiteral Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged diff --git a/spec/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir_spec.rb b/spec/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir_spec.rb new file mode 100644 index 00000000..bbe97197 --- /dev/null +++ b/spec/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe(RuboCop::Cop::Sorbet::ForbidRBIOutsideOfSorbetDir, :config) do + subject(:cop) { described_class.new(config) } + + let(:source) { 'print 1' } + let(:processed_source) { parse_source(source) } + + before do + allow(processed_source.buffer).to(receive(:name).and_return(filename)) + _investigate(cop, processed_source) + end + + context 'with an .rbi file outside of sorbet/' do + let(:filename) { 'some/dir/file.rbi' } + + it 'makes offense if an RBI file is outside of the sorbet/ directory' do + expect(cop.offenses.size).to(eq(1)) + expect(cop.messages).to(eq(['RBI files are only accepted in the sorbet/rbi/ directory.'])) + end + end + + context 'with an .rbi file inside of sorbet/' do + let(:filename) { 'sorbet/rbi/ome/dir/file.rbi' } + + it 'makes no offense if an RBI file is inside the sorbet/ directory' do + expect(cop.offenses.empty?).to(be(true)) + end + end +end