From 6a5442b72e4560a452467a332841e1c90cd835d6 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Tue, 15 Feb 2022 15:51:48 +0100 Subject: [PATCH] Add ViewComponent::Deprecation (#1277) * Add ViewComponent::Deprecation This should allow consumers to see deprecation warnings that correctly mention the next version of VC. Co-Authored-By: Elia Schito Co-Authored-By: Hans Lemuet * Avoid warning about polymorphic slots getter being redefined ``` lib/view_component/polymorphic_slots.rb:42: warning: method redefined; discarding old header lib/view_component/polymorphic_slots.rb:42: warning: previous definition of header was here lib/view_component/polymorphic_slots.rb:42: warning: method redefined; discarding old items lib/view_component/polymorphic_slots.rb:42: warning: previous definition of items was here ``` * Remove the unsupported frozen_string_literal comment from jbuilder template Was generating this warning: ``` test/sandbox/app/components/jbuilder_component.json.jbuilder:1: warning: `frozen_string_literal' is ignored after any tokens ``` See also: https://bugs.ruby-lang.org/issues/16602 * Add deprecation horizon management to the release script * Update deprecation copy for manually loading the engine Co-authored-by: Cameron Dutro --- Co-authored-by: Max Beizer Co-authored-by: Hans Lemuet Co-authored-by: Cameron Dutro --- .rubocop.yml | 4 ++++ docs/CHANGELOG.md | 4 ++++ lib/view_component.rb | 5 +++-- lib/view_component/base.rb | 5 +---- lib/view_component/compiler.rb | 2 +- lib/view_component/content_areas.rb | 2 +- lib/view_component/deprecation.rb | 8 ++++++++ lib/view_component/engine.rb | 6 ++++-- lib/view_component/polymorphic_slots.rb | 13 ++++++++----- lib/view_component/slotable.rb | 2 +- script/release | 6 ++++++ .../app/components/jbuilder_component.json.jbuilder | 2 -- test/test_helper.rb | 3 +++ 13 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 lib/view_component/deprecation.rb diff --git a/.rubocop.yml b/.rubocop.yml index add8c0264f..1e6d1e02f0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,3 +25,7 @@ Layout/LineLength: Layout/SpaceBeforeBrackets: Enabled: true + +Style/FrozenStringLiteralComment: + Exclude: + - "**/*.jbuilder" # not yet supported inside jbuilder templates diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 08fb2492ae..c7db707b24 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,10 @@ title: Changelog ## main +* Use a dedicated deprecation instance, silence it while testing + + *Max Beizer, Hans Lemuet, Elia Schito* + * Fix Ruby warnings. *Hans Lemuet* diff --git a/lib/view_component.rb b/lib/view_component.rb index c5ff2a95e1..2b5ddfceaf 100644 --- a/lib/view_component.rb +++ b/lib/view_component.rb @@ -11,6 +11,7 @@ module ViewComponent autoload :CompileCache autoload :ComponentError autoload :GlobalBuffer + autoload :Deprecation autoload :Instrumentation autoload :Preview autoload :PreviewTemplateError @@ -22,8 +23,8 @@ module ViewComponent # :nocov: if defined?(ViewComponent::Engine) - ActiveSupport::Deprecation.warn( - "This manually engine loading is deprecated and will be removed in v3.0.0. " \ + ViewComponent::Deprecation.warn( + "Manually loading the engine is deprecated and will be removed in v3.0.0. " \ "Remove `require \"view_component/engine\"`." ) elsif defined?(Rails::Engine) diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 073a98dd5b..8f4451fd69 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -249,14 +249,11 @@ def format # @param variant [Symbol] The variant to be used by the component. # @return [self] def with_variant(variant) - ActiveSupport::Deprecation.warn( - "`with_variant` is deprecated and will be removed in ViewComponent v3.0.0." - ) - @__vc_variant = variant self end + deprecate :with_variant, deprecator: ViewComponent::Deprecation # The current request. Use sparingly as doing so introduces coupling that # inhibits encapsulation & reuse, often making testing difficult. diff --git a/lib/view_component/compiler.rb b/lib/view_component/compiler.rb index 486a1cc075..a899bfd03a 100644 --- a/lib/view_component/compiler.rb +++ b/lib/view_component/compiler.rb @@ -49,7 +49,7 @@ def compile(raise_errors: false) end if subclass_instance_methods.include?(:before_render_check) - ActiveSupport::Deprecation.warn( + ViewComponent::Deprecation.warn( "`#before_render_check` will be removed in v3.0.0.\n\n" \ "To fix this issue, use `#before_render` instead." ) diff --git a/lib/view_component/content_areas.rb b/lib/view_component/content_areas.rb index 3419fd167e..232a531bc0 100644 --- a/lib/view_component/content_areas.rb +++ b/lib/view_component/content_areas.rb @@ -31,7 +31,7 @@ def with(area, content = nil, &block) class_methods do def with_content_areas(*areas) - ActiveSupport::Deprecation.warn( + ViewComponent::Deprecation.warn( "`with_content_areas` is deprecated and will be removed in ViewComponent v3.0.0.\n\n" \ "Use slots (https://viewcomponent.org/guide/slots.html) instead." ) diff --git a/lib/view_component/deprecation.rb b/lib/view_component/deprecation.rb new file mode 100644 index 0000000000..5a33df65d9 --- /dev/null +++ b/lib/view_component/deprecation.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "active_support/deprecation" + +module ViewComponent + DEPRECATION_HORIZON = 3 + Deprecation = ActiveSupport::Deprecation.new(DEPRECATION_HORIZON.to_s, "ViewComponent") +end diff --git a/lib/view_component/engine.rb b/lib/view_component/engine.rb index 9e9d2fbe92..50ec1f702c 100644 --- a/lib/view_component/engine.rb +++ b/lib/view_component/engine.rb @@ -28,7 +28,7 @@ class Engine < Rails::Engine # :nodoc: ) if options.preview_path.present? - ActiveSupport::Deprecation.warn( + ViewComponent::Deprecation.warn( "`preview_path` will be removed in v3.0.0. Use `preview_paths` instead." ) options.preview_paths << options.preview_path @@ -160,7 +160,9 @@ class Engine < Rails::Engine # :nodoc: # :nocov: unless defined?(ViewComponent::Base) - ActiveSupport::Deprecation.warn( + require "view_component/deprecation" + + ViewComponent::Deprecation.warn( "This manually engine loading is deprecated and will be removed in v3.0.0. " \ "Remove `require \"view_component/engine\"`." ) diff --git a/lib/view_component/polymorphic_slots.rb b/lib/view_component/polymorphic_slots.rb index 60f69c6b92..f19e8b4a2a 100644 --- a/lib/view_component/polymorphic_slots.rb +++ b/lib/view_component/polymorphic_slots.rb @@ -25,12 +25,19 @@ def renders_many(slot_name, callable = nil) end def register_polymorphic_slot(slot_name, types, collection:) + unless types.empty? + getter_name = slot_name + + define_method(getter_name) do + get_slot(slot_name) + end + end + renderable_hash = types.each_with_object({}) do |(poly_type, poly_callable), memo| memo[poly_type] = define_slot( "#{slot_name}_#{poly_type}", collection: collection, callable: poly_callable ) - getter_name = slot_name setter_name = if collection "#{ActiveSupport::Inflector.singularize(slot_name)}_#{poly_type}" @@ -38,10 +45,6 @@ def register_polymorphic_slot(slot_name, types, collection:) "#{slot_name}_#{poly_type}" end - define_method(getter_name) do - get_slot(slot_name) - end - define_method(setter_name) do |*args, &block| set_polymorphic_slot(slot_name, poly_type, *args, &block) end diff --git a/lib/view_component/slotable.rb b/lib/view_component/slotable.rb index be75f8dfb8..a52d18b283 100644 --- a/lib/view_component/slotable.rb +++ b/lib/view_component/slotable.rb @@ -23,7 +23,7 @@ module Slotable # class_name: "Header" # class name string, used to instantiate Slot # ) def with_slot(*slot_names, collection: false, class_name: nil) - ActiveSupport::Deprecation.warn( + ViewComponent::Deprecation.warn( "`with_slot` is deprecated and will be removed in ViewComponent v3.0.0.\n" \ "Use the new slots API (https://viewcomponent.org/guide/slots.html) instead." ) diff --git a/script/release b/script/release index a854d53fb1..9ae555a7fc 100755 --- a/script/release +++ b/script/release @@ -46,6 +46,12 @@ update_ruby_version() { -e "s/MINOR = [0-9]+/MINOR = $2/g" \ -e "s/PATCH = [0-9]+/PATCH = $3/g" \ lib/view_component/version.rb + + # Update deprecation horizon version + major=$1 + sed -E -i '' \ + -e "s/DEPRECATION_HORIZON = [0-9]+/DEPRECATION_HORIZON = $((major + 1))/g" \ + lib/view_component/deprecation.rb } update_gemfiles() { diff --git a/test/sandbox/app/components/jbuilder_component.json.jbuilder b/test/sandbox/app/components/jbuilder_component.json.jbuilder index 4f6b1bee08..38e6b8c550 100644 --- a/test/sandbox/app/components/jbuilder_component.json.jbuilder +++ b/test/sandbox/app/components/jbuilder_component.json.jbuilder @@ -1,4 +1,2 @@ -# frozen_string_literal: true - json.message @message json.conent content diff --git a/test/test_helper.rb b/test/test_helper.rb index e66aed4741..7e11030e08 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -19,6 +19,9 @@ # Configure Rails Environment ENV["RAILS_ENV"] = "test" +require "view_component/deprecation" +ViewComponent::Deprecation.behavior = :silence + require File.expand_path("../sandbox/config/environment.rb", __FILE__) require "rails/test_help"