Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ForbidRBIOutsideOfAllowedPaths & rename to RBIFilePath #208

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Sorbet/ForbidExtendTSigHelpersInShims:
Include:
- "**/*.rbi"

Sorbet/ForbidRBIOutsideOfAllowedPaths:
Sorbet/RBIFilePath:
Description: 'Forbids RBI files outside of the allowed paths'
Enabled: true
VersionAdded: 0.6.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need a VersionChanged: entry here.

Expand Down
3 changes: 2 additions & 1 deletion config/obsoletion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#
# See: https://docs.rubocop.org/rubocop/extensions.html#config-obsoletions
#
{}
renamed:
Sorbet/ForbidRBIOutsideOfAllowedPaths: Sorbet/RBIFilePath

This file was deleted.

39 changes: 39 additions & 0 deletions lib/rubocop/cop/sorbet/rbi/rbi_file_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "pathname"

module RuboCop
module Cop
module Sorbet
# Makes sure that RBI files are always located under the defined allowed paths.
#
# Options:
#
# * `AllowedPaths`: A list of the paths where RBI files are allowed (default: ["rbi/**", "sorbet/rbi/**"])
#
# @example
# # bad
# # lib/some_file.rbi
# # other_file.rbi
#
# # good
# # rbi/external_interface.rbi
# # sorbet/rbi/some_file.rbi
# # sorbet/rbi/any/path/for/file.rbi
class RBIFilePath < RuboCop::Cop::Base
MSG = "RBI file path should match one of: %<allowed_paths>s"

def on_new_investigation
allowed_paths = cop_config.fetch("AllowedPaths")

# When executed the path to the source file is absolute.
# We need to remove the exec path directory prefix before matching with the filename regular expressions.
rel_path = processed_source.file_path.sub("#{Dir.pwd}/", "")
return if allowed_paths.any? { |pattern| File.fnmatch(pattern, rel_path) }

add_global_offense(format(MSG, allowed_paths: allowed_paths.join(", ")))
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require_relative "sorbet/buggy_obsolete_strict_memoization"

require_relative "sorbet/rbi/forbid_extend_t_sig_helpers_in_shims"
require_relative "sorbet/rbi/forbid_rbi_outside_of_allowed_paths"
require_relative "sorbet/rbi/rbi_file_path"
require_relative "sorbet/rbi/single_line_rbi_class_module_definitions"

require_relative "sorbet/signatures/allow_incompatible_override"
Expand Down
2 changes: 1 addition & 1 deletion manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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/ForbidRBIOutsideOfAllowedPaths](cops_sorbet.md#sorbetforbidrbioutsideofallowedpaths)
* [Sorbet/ForbidSuperclassConstLiteral](cops_sorbet.md#sorbetforbidsuperclassconstliteral)
* [Sorbet/ForbidTStruct](cops_sorbet.md#sorbetforbidtstruct)
* [Sorbet/ForbidTUnsafe](cops_sorbet.md#sorbetforbidtunsafe)
Expand All @@ -31,6 +30,7 @@ In the following section you find all available cops:
* [Sorbet/KeywordArgumentOrdering](cops_sorbet.md#sorbetkeywordargumentordering)
* [Sorbet/ObsoleteStrictMemoization](cops_sorbet.md#sorbetobsoletestrictmemoization)
* [Sorbet/OneAncestorPerLine](cops_sorbet.md#sorbetoneancestorperline)
* [Sorbet/RBIFilePath](cops_sorbet.md#sorbetrbifilepath)
* [Sorbet/RedundantExtendTSig](cops_sorbet.md#sorbetredundantextendtsig)
* [Sorbet/SignatureBuildOrder](cops_sorbet.md#sorbetsignaturebuildorder)
* [Sorbet/SingleLineRbiClassModuleDefinitions](cops_sorbet.md#sorbetsinglelinerbiclassmoduledefinitions)
Expand Down
64 changes: 32 additions & 32 deletions manual/cops_sorbet.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,38 +356,6 @@ or
include Polaris::Engine.helpers
```

## Sorbet/ForbidRBIOutsideOfAllowedPaths

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.6.1 | -

Makes sure that RBI files are always located under the defined allowed paths.

Options:

* `AllowedPaths`: A list of the paths where RBI files are allowed (default: ["rbi/**", "sorbet/rbi/**"])

### Examples

```ruby
# bad
# lib/some_file.rbi
# other_file.rbi

# good
# rbi/external_interface.rbi
# sorbet/rbi/some_file.rbi
# sorbet/rbi/any/path/for/file.rbi
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
AllowedPaths | `rbi/**`, `sorbet/rbi/**` | Array
Include | `**/*.rbi` | Array

## Sorbet/ForbidSuperclassConstLiteral

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down Expand Up @@ -700,6 +668,38 @@ module SomeModule
end
```

## Sorbet/RBIFilePath

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.6.1 | -

Makes sure that RBI files are always located under the defined allowed paths.

Options:

* `AllowedPaths`: A list of the paths where RBI files are allowed (default: ["rbi/**", "sorbet/rbi/**"])

### Examples

```ruby
# bad
# lib/some_file.rbi
# other_file.rbi

# good
# rbi/external_interface.rbi
# sorbet/rbi/some_file.rbi
# sorbet/rbi/any/path/for/file.rbi
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
AllowedPaths | `rbi/**`, `sorbet/rbi/**` | Array
Include | `**/*.rbi` | Array

## Sorbet/RedundantExtendTSig

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down
Loading
Loading