diff --git a/fixtures/view_helper.rb b/fixtures/view_helper.rb index d0782aa5..b649806e 100644 --- a/fixtures/view_helper.rb +++ b/fixtures/view_helper.rb @@ -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 } @@ -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 + end end diff --git a/lib/phlex/elements.rb b/lib/phlex/elements.rb index 50e3c3c4..156527cd 100644 --- a/lib/phlex/elements.rb +++ b/lib/phlex/elements.rb @@ -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}.") + end + target = @_context.target if attributes.length > 0 # with attributes @@ -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 diff --git a/test/phlex/view/tags.rb b/test/phlex/view/tags.rb index 36364e7d..d8fd3616 100644 --- a/test/phlex/view/tags.rb +++ b/test/phlex/view/tags.rb @@ -122,4 +122,40 @@ end end end + + describe "Elements deprecation" do + deprecated_methods = %i[param] + + deprecated_methods.each do |method_name| + 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