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

Warn about deprecated element methods #649

Closed
wants to merge 1 commit 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
16 changes: 16 additions & 0 deletions fixtures/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module ViewHelper
def self.extended(parent)
parent.include ViewHelper::Utils

parent.class_exec do
let(:output) { example.call }
let(:example) { view.new }
Expand All @@ -19,4 +21,18 @@ def svg_view(&block)
Class.new(Phlex::SVG, &block)
end
end

module Utils
def capture_stderr(&block)
captured_stream = StringIO.new
original_stream = $stderr
$stderr = captured_stream

block.call

captured_stream.string.strip
ensure
$stderr = original_stream
end
Comment on lines +26 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this as a module because I think it can be useful for other deprecation tests in the future.

end
end
8 changes: 8 additions & 0 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def register_element(method_name, tag: nil, deprecated: false)
# frozen_string_literal: true

def #{method_name}(**attributes, &block)
if #{deprecated}
Kernel.warn("`#{method_name}` and `_#{method_name}` are deprecated and will be unsupported in Phlex 2.0. This HTML element is no longer recommended. Check out MDN web docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/#{tag}.")
Copy link
Contributor Author

@stephannv stephannv Feb 2, 2024

Choose a reason for hiding this comment

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

  1. I couldn't get called method name here (eg. param or _param), using __method__ or __callee__ returns register_element here or register_void_element on line 82.

  2. I'm not sure if it makes sense to add link to MDN docs, but it explains why the method is being deprecated and redirects the user to more detailed context.

end

target = @_context.target

if attributes.length > 0 # with attributes
Expand Down Expand Up @@ -74,6 +78,10 @@ def register_void_element(method_name, tag: method_name.name.tr("_", "-"), depre
# frozen_string_literal: true

def #{method_name}(**attributes)
if #{deprecated}
Kernel.warn("`#{method_name}` and `_#{method_name}` are deprecated and will be unsupported in Phlex 2.0. This HTML element is no longer recommended. Check out MDN web docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/#{tag}.")
end

target = @_context.target

if attributes.length > 0 # with attributes
Expand Down
36 changes: 36 additions & 0 deletions test/phlex/view/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,40 @@
end
end
end

describe "Elements deprecation" do
deprecated_methods = %i[param]

deprecated_methods.each do |method_name|
Copy link
Contributor Author

@stephannv stephannv Feb 2, 2024

Choose a reason for hiding this comment

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

I'm not sure if it is worth testing non deprecated elements. It would be something like:

[
  Phlex::HTML::StandardElements.registered_elements,
  Phlex::HTML::VoidElements.registered_elements
].each do |element_group|
  element_group.each do |method_name, tag|
    if deprecated_methods.include?(method_name)
      # tests for method_name and _method_name
      ... warns about deprecation
    else
      # tests for method_name and _method_name
      ... doesn't warn about deprecation
    end
  end
end

describe "##{method_name}" do
view do
define_method :view_template do
send(method_name.to_sym)
end
end

it "warns about `#{method_name}` deprecation" do
message = capture_stderr { view.call }
deprecation_message = "`#{method_name}` and `_#{method_name}` are deprecated and will be unsupported in Phlex 2.0. This HTML element is no longer recommended. Check out MDN web docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/#{method_name}."

expect(message).to be == deprecation_message
end
end

describe "#_#{method_name}" do
view do
define_method :view_template do
send(:"_#{method_name}")
end
end

it "warns about `_#{method_name}` deprecation" do
message = capture_stderr { view.call }
deprecation_message = "`#{method_name}` and `_#{method_name}` are deprecated and will be unsupported in Phlex 2.0. This HTML element is no longer recommended. Check out MDN web docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/#{method_name}."

expect(message).to be == deprecation_message
end
end
end
end
end