Skip to content

Commit

Permalink
Merge pull request #80 from rubocop/rubocop-plugin
Browse files Browse the repository at this point in the history
Make RuboCop ThreadSafe work as a RuboCop plugin
  • Loading branch information
viralpraxis authored Feb 17, 2025
2 parents 50e1d80 + dac3947 commit 109ace5
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 76 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ jobs:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4", ruby-head, jruby-9.4]
rubocop_version: ["1.48", "1.69"]
exclude:
- ruby: ruby-head
rubocop_version: "1.48"
rubocop_version: ["1.72"]
env:
BUNDLE_GEMFILE: "gemfiles/rubocop_${{ matrix.rubocop_version }}.gemfile"
steps:
Expand All @@ -47,7 +44,7 @@ jobs:
test-prism:
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: "gemfiles/rubocop_1.69.gemfile"
BUNDLE_GEMFILE: "gemfiles/rubocop_1.72.gemfile"
PARSER_ENGINE: parser_prism
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require:
- rubocop/cop/internal_affairs
plugins:
- rubocop-internal_affairs
- rubocop-rake
- rubocop-rspec

Expand Down
9 changes: 2 additions & 7 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ customize_gemfiles do
}
end

appraise 'rubocop-1.48' do
gem 'base64', '~> 0.1.1'
gem 'rubocop', '~> 1.48.0'
end

appraise 'rubocop-1.69' do
gem 'rubocop', '~> 1.69.0'
appraise 'rubocop-1.72' do
gem 'rubocop', '~> 1.72.1'
end
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [#76](https://github.com/rubocop/rubocop-thread_safety/pull/76): Detect offenses when using safe navigation for `ThreadSafety/DirChdir`, `ThreadSafety/NewThread` and `ThreadSafety/RackMiddlewareInstanceVariable` cops. ([@viralpraxis](https://github.com/viralpraxis))
- [#73](https://github.com/rubocop/rubocop-thread_safety/pull/73): Add `AllowCallWithBlock` option to `ThreadSafety/DirChdir` cop. ([@viralpraxis](https://github.com/viralpraxis))
- [#80] Make RuboCop ThreadSafety work as a RuboCop plugin. ([@bquorning])

## 0.6.0

Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gem 'pry' unless ENV['CI']
gem 'rake', '>= 10.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', github: 'rubocop/rubocop'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec'
gem 'rubocop-rake', '~> 0.7'
gem 'rubocop-rspec', '~> 3.5'
gem 'simplecov'
gem 'yard'
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ Install it with Bundler by invoking:

Add this line to your application's `.rubocop.yml`:

require: rubocop-thread_safety
plugins: rubocop-thread_safety

Now you can run `rubocop` and it will automatically load the RuboCop
Thread-Safety cops together with the standard cops.

> [!NOTE]
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
### Scanning an application without adding it to the Gemfile

Install the gem:
Expand Down
20 changes: 0 additions & 20 deletions gemfiles/rubocop_1.48.gemfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ gem 'prism', '~> 1.2.0'
gem 'pry'
gem 'rake', '>= 10.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.69.0'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec'
gem 'rubocop', '~> 1.72.1'
gem 'rubocop-rake', '~> 0.7'
gem 'rubocop-rspec', '~> 3.5'
gem 'simplecov'
gem 'yard'

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop-thread_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

require 'rubocop/thread_safety'
require 'rubocop/thread_safety/version'
require 'rubocop/thread_safety/inject'

RuboCop::ThreadSafety::Inject.defaults!
require 'rubocop/thread_safety/plugin'

require 'rubocop/cop/mixin/operation_with_threadsafe_result'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def requires_parentheses?(node)
end

def range_type?(node)
node.erange_type? || node.irange_type?
node.type?(:erange, :irange)
end

def correct_splat_expansion(corrector, expr, splat_value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def on_class(node)
def on_send(node)
argument = node.first_argument

return unless argument&.sym_type? || argument&.str_type?
return unless argument&.type?(:sym, :str)
return if allowed_identifier?(argument.value)

add_offense node
Expand Down
7 changes: 0 additions & 7 deletions lib/rubocop/thread_safety.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,5 @@
module RuboCop
# RuboCop::ThreadSafety detects some potential thread safety issues.
module ThreadSafety
PROJECT_ROOT = Pathname.new(File.expand_path('../../', __dir__))
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze

private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)

::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
end
end
20 changes: 0 additions & 20 deletions lib/rubocop/thread_safety/inject.rb

This file was deleted.

38 changes: 38 additions & 0 deletions lib/rubocop/thread_safety/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'lint_roller'

module RuboCop
module ThreadSafety
# A plugin that integrates RuboCop ThreadSafety with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
# :nocov:
def about
LintRoller::About.new(
name: 'rubocop-thread_safety',
version: Version::STRING,
homepage: 'https://github.com/rubocop/rubocop-thread_safety',
description: 'Thread-safety checks via static analysis.'
)
end
# :nocov:

def supported?(context)
context.engine == :rubocop
end

def rules(_context)
project_root = Pathname.new(__dir__).join('../../..')

obsoletion = project_root.join('config', 'obsoletion.yml')
ConfigObsoletion.files << obsoletion

LintRoller::Rules.new(
type: :path,
config_format: :rubocop,
value: project_root.join('config/default.yml')
)
end
end
end
end
6 changes: 4 additions & 2 deletions rubocop-thread_safety.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Gem::Specification.new do |spec|
'changelog_uri' => 'https://github.com/rubocop/rubocop-thread_safety/blob/master/CHANGELOG.md',
'source_code_uri' => 'https://github.com/rubocop/rubocop-thread_safety',
'bug_tracker_uri' => 'https://github.com/rubocop/rubocop-thread_safety/issues',
'rubygems_mfa_required' => 'true'
'rubygems_mfa_required' => 'true',
'default_lint_roller_plugin' => 'RuboCop::ThreadSafety::Plugin'
}

spec.bindir = 'exe'
Expand All @@ -35,5 +36,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.7.0'

spec.add_dependency 'rubocop', '>= 1.48.1'
spec.add_dependency 'lint_roller', '~> 1.1'
spec.add_dependency 'rubocop', '~> 1.72', '>= 1.72.1'
end
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
require_relative 'shared_contexts'

RSpec.configure do |config|
config.include RuboCop::RSpec::ExpectOffense

config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
Expand Down
2 changes: 2 additions & 0 deletions tasks/cops_documentation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

desc 'Generate docs of all cops departments'
task generate_cops_documentation: :yard_for_generate_documentation do
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/../config/default.yml")

deps = ['ThreadSafety']
CopsDocumentationGenerator.new(departments: deps).call
end
Expand Down

0 comments on commit 109ace5

Please sign in to comment.