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

Improve performance of deny_* methods by not raising in prod environments #1632

Merged
merged 7 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/gorgeous-spies-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Improve performance of the deny\_\* methods
4 changes: 2 additions & 2 deletions app/components/primer/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ def deny_tag_argument(**arguments)
end

def should_raise_error?
raise_on_invalid_options? && !ENV["PRIMER_WARNINGS_DISABLED"]
!Rails.env.production? && raise_on_invalid_options? && !ENV["PRIMER_WARNINGS_DISABLED"]
end

def should_raise_aria_error?
raise_on_invalid_aria? && !ENV["PRIMER_WARNINGS_DISABLED"]
!Rails.env.production? && raise_on_invalid_aria? && !ENV["PRIMER_WARNINGS_DISABLED"]
end
end
end
4 changes: 2 additions & 2 deletions demo/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ gem "lookbook", "~> 1.3.4" unless rails_version.to_f < 7

group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem "spring"
gem "spring-watcher-listen", "~> 2.0.0"
gem "spring", "~> 4.0"
gem "spring-watcher-listen", "~> 2.1"
gem "hotwire-livereload", "~> 1.1"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
Expand Down
11 changes: 6 additions & 5 deletions demo/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ GEM
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
sorbet-runtime-stub (0.2.0)
spring (2.1.1)
spring-watcher-listen (2.0.1)
spring (4.1.0)
spring-watcher-listen (2.1.0)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
spring (>= 4)
sprockets (4.1.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -378,6 +378,7 @@ GEM

PLATFORMS
x86_64-darwin-19
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
Expand All @@ -399,8 +400,8 @@ DEPENDENCIES
rack-cors
railties (= 7.0.3)
rake (~> 13.0)
spring
spring-watcher-listen (~> 2.0.0)
spring (~> 4.0)
spring-watcher-listen (~> 2.1)
sprockets
sprockets-rails
stimulus-rails
Expand Down
35 changes: 35 additions & 0 deletions test/components/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,39 @@ def test_deprecated_components_by_status_match_list

assert_empty(deprecated_by_status - deprecated_by_list, "Please make sure that components are officially deprecated by setting the `status :deprecated` within the component file.\nMake sure to provide an alternative component for each deprecated component in Primer::Deprecations::DEPRECATED_COMPONENTS (lib/primer/deprecations.rb). If there is no alternative to suggest, set the value to nil.")
end

class DenyComponent < Primer::Component
def initialize(**system_arguments)
deny_single_argument(:class, "Use `classes` instead.", **system_arguments)
deny_aria_key(:label, "Don't use labels?", **system_arguments)
end

def call
"<p>foo</p>".html_safe
end
end

def test_deny_single_argument_does_not_raise_in_production
with_raise_on_invalid_options(true) do
assert_raises(ArgumentError) { DenyComponent.new(class: "foo") }

# rubocop:disable Rails/Inquiry
Rails.stub(:env, "production".inquiry) do
DenyComponent.new(class: "foo")
end
# rubocop:enable Rails/Inquiry
end
end

def test_deny_aria_key_does_not_raise_in_production
with_raise_on_invalid_aria(true) do
assert_raises(ArgumentError) { DenyComponent.new(aria: { label: "foo" }) }

# rubocop:disable Rails/Inquiry
Rails.stub(:env, "production".inquiry) do
DenyComponent.new(aria: { label: "foo" })
end
# rubocop:enable Rails/Inquiry
end
end
end