Skip to content

Commit

Permalink
Stop raising error if CSS classes are used instead of system arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Dec 4, 2024
1 parent a574b0e commit 874af8c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 57 deletions.
27 changes: 1 addition & 26 deletions lib/primer/classify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def call(args = {})
case key
when :classes
# insert :classes first to avoid huge doc diffs
if (class_names = validated_class_names(val))
result.unshift(class_names)
end
result.unshift(val)
next
when :style
style = val
Expand Down Expand Up @@ -105,29 +103,6 @@ def validate(key, val, brk)
brk_str = Primer::Classify::Utilities::BREAKPOINTS[brk]
Primer::Classify::Utilities.validate(key, val, brk_str)
end

def validated_class_names(classes)
return if classes.blank?

if raise_on_invalid_options? && !ENV["PRIMER_WARNINGS_DISABLED"]
invalid_class_names =
classes.split.each_with_object([]) do |class_name, memo|
memo << class_name if Primer::Classify::Validation.invalid?(class_name)
end

if invalid_class_names.any?
raise ArgumentError, "Use System Arguments (https://primer.style/view-components/system-arguments) "\
"instead of Primer CSS class #{'name'.pluralize(invalid_class_names.length)} #{invalid_class_names.to_sentence}. "\
"This warning will not be raised in production. Set PRIMER_WARNINGS_DISABLED=1 to disable this warning."
end
end

classes
end

def raise_on_invalid_options?
Rails.application.config.primer_view_components.raise_on_invalid_options
end
end
end
end
31 changes: 0 additions & 31 deletions test/lib/classify_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,43 +411,12 @@ def test_animation
assert_generated_class("anim-hover-grow", { animation: :hover_grow })
end

def test_does_not_raise_error_when_passing_in_a_primer_css_class_name_in_development_and_flag_is_set
ENV["PRIMER_WARNINGS_DISABLED"] = "1"
with_raise_on_invalid_options(true) do
assert_generated_class("color-bg-primary text-center float-left ml-1", { classes: "color-bg-primary text-center float-left ml-1" })
end
ensure
ENV["PRIMER_WARNINGS_DISABLED"] = nil
end

def test_does_not_raise_error_when_passing_in_a_primer_css_class_otherwise
with_raise_on_invalid_options(false) do
assert_generated_class("color-bg-primary text-center float-left ml-1", { classes: "color-bg-primary text-center float-left ml-1" })
end
end

def test_does_include_leading_trailing_whitespace_in_class
generated_class = Primer::Classify.call(classes: "foo-class")[:class]
refute(generated_class.start_with?(" "))
refute(generated_class.end_with?(" "))
end

def test_raises_if_not_using_system_arguments_when_raise_on_invalid_options_is_true
with_raise_on_invalid_options(true) do
exception = assert_raises ArgumentError do
Primer::Classify.call(classes: "d-block")
end

assert_includes exception.message, "Use System Arguments (https://primer.style/view-components/system-arguments) instead of Primer CSS class"
end
end

def test_does_not_raise_if_not_using_system_arguments_when_raise_on_invalid_options_is_false
with_raise_on_invalid_options(false) do
assert_generated_class("d-block", { classes: "d-block" })
end
end

private

def assert_generated_class(generated_class_name, input)
Expand Down

0 comments on commit 874af8c

Please sign in to comment.