-
Notifications
You must be signed in to change notification settings - Fork 457
Codemod v3 slots improvements #1746
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
Merged
Spone
merged 16 commits into
ViewComponent:codemods/v3-slots
from
kirillplatonov:codemod-v3-slots
Jun 29, 2023
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0df0236
Add rake task for legacy slots detection
kirillplatonov 3f74fc8
Add view component previews to codemod view paths
kirillplatonov d20dd2c
Autodetect rails view paths
kirillplatonov 1b16e1c
Improve uncertain matches detection
kirillplatonov 462327a
Update lib/view_component/codemods/v3_slot_setters.rb
kirillplatonov 1b1934f
Update usage instruction
kirillplatonov 7427963
Add rake task to migrate to new slots
kirillplatonov 9dae9d2
Update changelog
kirillplatonov 5c2857a
Standardrb
kirillplatonov 6683c2e
Add tests
kirillplatonov 5b82e1f
Call eager loading via Rails
kirillplatonov a421280
Update docs/CHANGELOG.md
kirillplatonov e312e3d
Add support for passing view paths via CLI
kirillplatonov 94f8f70
Fix helper in rake tasks
kirillplatonov 4abff5f
Fix tests
kirillplatonov a75c3e7
Add note about codemod to changelog
kirillplatonov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "view_component/codemods/v3_slot_setters" | ||
|
||
namespace :view_component do | ||
task detect_legacy_slots: :environment do | ||
ARGV.each { |a| task a.to_sym {} } | ||
custom_paths = ARGV.compact.map { |path| Rails.root.join(path) } | ||
ViewComponent::Codemods::V3SlotSetters.new(view_path: custom_paths).call | ||
end | ||
|
||
task migrate_legacy_slots: :environment do | ||
ARGV.each { |a| task a.to_sym {} } | ||
custom_paths = ARGV.compact.map { |path| Rails.root.join(path) } | ||
ViewComponent::Codemods::V3SlotSetters.new(view_path: custom_paths, migrate: true).call | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module AliasesHelper | ||
def sandbox_slots(*args, **kwargs, &block) | ||
render SlotsComponent.new(*args, **kwargs), &block | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
test/sandbox/app/views/codemods/_v2_slots_setters_alias.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= sandbox_slots do |component| %> | ||
<% component.subtitle do %> | ||
<small>This is my subtitle!</small> | ||
<% end %> | ||
<% end %> |
14 changes: 14 additions & 0 deletions
14
test/sandbox/app/views/codemods/_v2_slots_setters_exact.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<%= render SlotsComponent.new do |component| %> | ||
<% component.title do %> | ||
This is my title! | ||
<% end %> | ||
<% end %> | ||
|
||
<%= render SlotsComponent.new do |component| %> | ||
<% component.tab do %> | ||
<h1>Tab A</h1> | ||
<% end %> | ||
<% component.tab do %> | ||
<h1>Tab B</h1> | ||
<% end %> | ||
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
= render(EmptySlotComponent.new) do |component| | ||
- component.title | ||
- component.with_title | ||
- nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "view_component/codemods/v3_slot_setters" | ||
|
||
class V3SlotSettersTest < Minitest::Test | ||
def teardown | ||
restore_legacy_slots | ||
end | ||
|
||
def test_detects_legacy_slots | ||
output = capture_output do | ||
ViewComponent::Codemods::V3SlotSetters.new.call | ||
end | ||
|
||
assert_match "_v2_slots_setters_exact.html.erb\n=> line 2: probably replace `component.title` with `component.with_title`", output | ||
assert_match "line 8: probably replace `component.tab` with `component.with_tab`", output | ||
assert_match "line 11: probably replace `component.tab` with `component.with_tab`", output | ||
assert_match "_v2_slots_setters_alias.html.erb\n=> line 2: maybe replace `subtitle` with `with_subtitle`", output | ||
end | ||
|
||
def test_migrate_legacy_slots | ||
ViewComponent::Codemods::V3SlotSetters.new(migrate: true).call | ||
|
||
output = capture_output do | ||
ViewComponent::Codemods::V3SlotSetters.new.call | ||
end | ||
|
||
refute_match "_v2_slots_setters_exact.html.erb\n=> line 2: probably replace `component.title` with `component.with_title`", output | ||
refute_match "line 6: probably replace `component.tab` with `component.with_tab`", output | ||
refute_match "line 9: probably replace `component.tab` with `component.with_tab`", output | ||
refute_match "_v2_slots_setters_alias.html.erb\n=> line 2: maybe replace `subtitle` with `with_subtitle`", output | ||
end | ||
|
||
private | ||
|
||
def capture_output | ||
original_stdout = $stdout | ||
$stdout = StringIO.new | ||
yield | ||
$stdout.string | ||
ensure | ||
$stdout = original_stdout | ||
end | ||
|
||
def restore_legacy_slots | ||
test_views = [ | ||
Rails.root.join("app/views/codemods/_v2_slots_setters_alias.html.erb"), | ||
Rails.root.join("app/views/codemods/_v2_slots_setters_exact.html.erb") | ||
] | ||
test_views.each do |file| | ||
content = File.read(file) | ||
content.gsub!("with_", "") | ||
File.write(file, content) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.