Skip to content

Commit

Permalink
Merge pull request #68 from Shopify/at-rbi
Browse files Browse the repository at this point in the history
Ensure RBI files are always under `sorbet/rbi`
  • Loading branch information
Morriar committed Jun 2, 2021
2 parents 5aae04a + 834a33e commit 5d71ade
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 3 deletions.
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions lib/rubocop/cop/sorbet/rbi/forbid_rbi_outside_of_sorbet_dir.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# 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/forbid_rbi_outside_of_sorbet_dir'
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'
Expand Down
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 66 additions & 1 deletion manual/cops_sorbet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -298,6 +324,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
Expand All @@ -306,6 +339,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
Expand Down Expand Up @@ -427,6 +466,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
Expand All @@ -435,6 +480,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
Expand All @@ -443,6 +494,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
Expand All @@ -459,3 +516,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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5d71ade

Please sign in to comment.