Skip to content

Commit

Permalink
Make sure COMPONENT_TO_USE_INSTEAD keys are aligned with officially…
Browse files Browse the repository at this point in the history
… deprecated components (#1242)

* add check to ensure statuses.json matches

* Remove check from linter and test

* Add CI check

* Add print statement

* Update lib/tasks/deprecated.rake

Co-authored-by: Cameron Dutro <camertron@gmail.com>

* Add static:dump task

@camertron suggestion

* Fix rubocop

Co-authored-by: Cameron Dutro <camertron@gmail.com>
Co-authored-by: Jon Rohan <yes@jonrohan.codes>
  • Loading branch information
3 people authored Aug 1, 2022
1 parent 918cfe3 commit 3b2bec4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ jobs:
with:
name: simplecov-resultset-rails${{matrix.rails_version}}-ruby${{matrix.ruby_version}}
path: coverage
deprecated:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Setup Ruby
uses: actions/setup-ruby@v1
with:
ruby-version: 3.0.x
- name: Make sure deprecated components can be linted
run: |
gem install bundler:2.2.9
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
bundle exec rake static:dump
bundle exec rake deprecated:check
coverage:
needs: test
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ def statuses_json
end

def deprecated_components
@deprecated_components ||= statuses_json.select { |_, value| value == "deprecated" }.keys.tap do |deprecated_components|
deprecated_components.each do |deprecated|
unless COMPONENT_TO_USE_INSTEAD.key?(deprecated)
raise "Please provide a component that should be used in place of #{deprecated} in COMPONENT_TO_USE_INSTEAD. "\
"If there is no alternative, set the value to nil."
end
end
end
@deprecated_components ||= statuses_json.select { |_, value| value == "deprecated" }.keys
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/deprecated.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

namespace :deprecated do
task :check do
require_relative "./../primer/view_components/linters/helpers/deprecated_components_helpers"
require_relative "../primer/view_components/statuses"

puts "Checking that officially deprecated components are linted by `DeprecatedComponents` linter...."

if Primer::ViewComponents::STATUSES.select { |_, value| value == "deprecated" }.keys.sort != ERBLint::Linters::Helpers::DeprecatedComponentsHelpers::COMPONENT_TO_USE_INSTEAD.keys.sort
puts "\n**************************************************************************************************************************"
raise "Please make sure that components are officially deprecated by setting the `status :deprecated` within the component file.\n"\
"Run `bundle exec rake static:dump` so the deprecated status is reflected in `statuses.json`.\n"\
"Make sure to provide an alternative component for each deprecated component in `ERBLint::Linters::Helpers::DeprecatedComponentsHelpers::COMPONENT_TO_USE_INSTEAD`.\n"\
"If there is no alternative to suggest, set the value to nil."
end

puts "\n============================================================================="
puts "All good!"
puts "============================================================================="
end
end
9 changes: 0 additions & 9 deletions test/rubocop/deprecated_components_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,4 @@ def test_suggests_alternative_component_if_available
assert_equal 1, cop.offenses.count
assert_equal "Primer::Tooltip has been deprecated and should not be used. Try Primer::Alpha::Tooltip instead.", cop.offenses.first.message
end

def test_raises_if_legacy_component_missing_in_alternative_components
RuboCop::Cop::Primer::DeprecatedComponents.any_instance.stubs(:statuses_json).returns({ "Primer::FakeComponent": "deprecated" })
assert_raises(RuntimeError) do
investigate(cop, <<-RUBY)
render(Primer::FakeComponent.new(foo: "bar"))
RUBY
end
end
end

0 comments on commit 3b2bec4

Please sign in to comment.