-
Notifications
You must be signed in to change notification settings - Fork 440
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
V3SlotSetters linter/codemod draft #1669
Open
Spone
wants to merge
11
commits into
main
Choose a base branch
from
codemods/v3-slots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de176b5
V3SlotSetters codemod draft
Spone c413492
Merge branch 'main' into codemods/v3-slots
Spone 21a8a26
Update lib/view_component/codemods/v3_slot_setters.rb
Spone fb748a6
Update lib/view_component/codemods/v3_slot_setters.rb
Spone d075cde
Merge branch 'main' into codemods/v3-slots
Spone e376b29
standardrb
Spone 547c202
Add Zeitwerk eager loading to initializer
Spone 2542ba5
Merge branch 'main' into codemods/v3-slots
Spone b805fd7
Merge branch 'main' into codemods/v3-slots
Spone 3240607
Codemod v3 slots improvements (#1746)
kirillplatonov cf8a038
Merge branch 'main' into codemods/v3-slots
Spone 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 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 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 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,220 @@ | ||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
# Usage: | ||
# | ||
# Run via rake task: | ||
# | ||
# bin/rails view_component:detect_legacy_slots | ||
# bin/rails view_component:migrate_legacy_slots | ||
# bin/rails view_component:migrate_legacy_slots app/views | ||
# | ||
# Or run via rails console if you need to pass custom paths: | ||
# | ||
# ViewComponent::Codemods::V3SlotSetters.new( | ||
# view_path: Rails.root.join("app/views"), | ||
# ).call | ||
module Codemods | ||
class V3SlotSetters | ||
TEMPLATE_LANGUAGES = %w[erb slim haml].join(",").freeze | ||
RENDER_REGEX = /render[( ](?<component>\w+(?:::\w+)*)\.new[) ]+(do|\{) \|(?<arg>\w+)\b/ # standard:disable Lint/MixedRegexpCaptureTypes | ||
|
||
Suggestion = Struct.new(:file, :line, :message) | ||
|
||
def initialize(view_component_path: [], view_path: [], migrate: false) | ||
Rails.application.eager_load! | ||
|
||
@view_component_path = view_component_path | ||
@view_path = view_path | ||
@migrate = migrate | ||
end | ||
|
||
def call | ||
puts "Using ViewComponent path: #{view_component_paths.join(", ")}" | ||
puts "Using Views path: #{view_paths.join(", ")}" | ||
puts "#{view_components.size} ViewComponents found" | ||
puts "#{slottable_components.size} ViewComponents using Slots found" | ||
puts "#{view_component_files.size} ViewComponent templates found" | ||
puts "#{view_files.size} view files found" | ||
process_all_files | ||
end | ||
|
||
def process_all_files | ||
all_files.each do |file| | ||
process_file(file) | ||
end | ||
end | ||
|
||
def process_file(file) | ||
@suggestions = [] | ||
@suggestions += scan_exact_matches(file) | ||
@suggestions += scan_uncertain_matches(file) | ||
|
||
if @suggestions.any? | ||
puts | ||
puts "File: #{file}" | ||
@suggestions.each do |s| | ||
puts "=> line #{s.line}: #{s.message}" | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def scan_exact_matches(file) | ||
[].tap do |suggestions| | ||
rendered_components = [] | ||
content = File.read(file) | ||
|
||
if (render_match = content.match(RENDER_REGEX)) | ||
component = render_match[:component] | ||
arg = render_match[:arg] | ||
|
||
if registered_slots.key?(component.constantize) | ||
used_slots_names = registered_slots[component.constantize] | ||
rendered_components << {component: component, arg: arg, slots: used_slots_names} | ||
end | ||
end | ||
|
||
File.open(file, "r+") do |f| | ||
lines = [] | ||
f.each_line do |line| | ||
rendered_components.each do |rendered_component| | ||
arg = rendered_component[:arg] | ||
slots = rendered_component[:slots] | ||
|
||
if (matches = line.scan(/#{arg}\.#{Regexp.union(slots)}/)) | ||
matches.each do |match| | ||
new_value = match.gsub("#{arg}.", "#{arg}.with_") | ||
message = if @migrate | ||
"replaced `#{match}` with `#{new_value}`" | ||
else | ||
"probably replace `#{match}` with `#{new_value}`" | ||
end | ||
suggestions << Suggestion.new(file, f.lineno, message) | ||
if @migrate | ||
line.gsub!("#{arg}.", "#{arg}.with_") | ||
end | ||
end | ||
end | ||
end | ||
lines << line | ||
end | ||
|
||
if @migrate | ||
f.rewind | ||
f.write(lines.join) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def scan_uncertain_matches(file) | ||
[].tap do |suggestions| | ||
File.open(file, "r+") do |f| | ||
lines = [] | ||
f.each_line do |line| | ||
if (matches = line.scan(/(?<!\s)\.(?<slot>#{Regexp.union(all_registered_slot_names)})\b/)) | ||
matches.flatten.each do |match| | ||
next if @suggestions.find { |s| s.file == file && s.line == f.lineno } | ||
|
||
message = if @migrate | ||
"replaced `#{match}` with `with_#{match}`" | ||
else | ||
"maybe replace `#{match}` with `with_#{match}`" | ||
end | ||
suggestions << Suggestion.new(file, f.lineno, message) | ||
if @migrate | ||
line.gsub!(/(?<!\s)\.(#{match})\b/, ".with_\\1") | ||
end | ||
end | ||
end | ||
lines << line | ||
end | ||
|
||
if @migrate | ||
f.rewind | ||
f.write(lines.join) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def view_components | ||
ViewComponent::Base.descendants | ||
end | ||
|
||
def slottable_components | ||
view_components.select do |comp| | ||
comp.registered_slots.any? | ||
end | ||
end | ||
|
||
def registered_slots | ||
@registered_slots ||= {}.tap do |slots| | ||
puts | ||
puts "Detected slots:" | ||
slottable_components.each do |comp| | ||
puts "- `#{comp}` has slots: #{comp.registered_slots.keys.join(", ")}" | ||
slots[comp] = comp.registered_slots.map do |slot_name, slot| | ||
normalized_slot_name(slot_name, slot) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def all_registered_slot_names | ||
@all_registered_slot_names ||= registered_slots.values.flatten.uniq | ||
end | ||
|
||
def view_component_files | ||
Dir.glob(Pathname.new(File.join(view_component_path_glob, "**", "*.{rb,#{TEMPLATE_LANGUAGES}}"))) | ||
end | ||
|
||
def view_files | ||
Dir.glob(Pathname.new(File.join(view_path_glob, "**", "*.{#{TEMPLATE_LANGUAGES}}"))) | ||
end | ||
|
||
def all_files | ||
view_component_files + view_files | ||
end | ||
|
||
def view_component_paths | ||
@view_component_paths ||= [ | ||
Rails.application.config.view_component.view_component_path, | ||
@view_component_path | ||
].flatten.compact.uniq | ||
end | ||
|
||
def view_component_path_glob | ||
return view_component_paths.first if view_component_paths.size == 1 | ||
|
||
"{#{view_component_paths.join(",")}}" | ||
end | ||
|
||
def rails_view_paths | ||
ActionController::Base.view_paths.select do |path| | ||
path.to_s.include?(Rails.root.to_s) | ||
end.map(&:to_s) | ||
end | ||
|
||
def view_paths | ||
@view_paths ||= [ | ||
rails_view_paths, | ||
Rails.application.config.view_component.preview_paths, | ||
@view_path | ||
].flatten.compact.uniq | ||
end | ||
|
||
def view_path_glob | ||
return view_paths.first if view_paths.size == 1 | ||
|
||
"{#{view_paths.join(",")}}" | ||
end | ||
|
||
def normalized_slot_name(slot_name, slot) | ||
slot[:collection] ? ActiveSupport::Inflector.singularize(slot_name) : slot_name.to_s | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this assuming only a single render_match per file?
If I render multiple different components in the same file, would this catch it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After re-reading the code, I guess you're right. I'm a bit surprised I didn't catch this earlier 😅
This part needs to be reworked.