Skip to content

Commit

Permalink
Fix stylelint violations when using Tailwind (#1153)
Browse files Browse the repository at this point in the history
When we introduced #1148 we did not test it against applications that
invoked `suspenders:styles --css=tailwind`.
  • Loading branch information
stevepolitodesign authored Dec 12, 2023
1 parent 551d60b commit 8628dcb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/generators/suspenders/lint_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def install_gems
end

def configure_stylelint
copy_file "stylelintrc.json", ".stylelintrc.json"
if using_tailwind?
copy_file "tailwind.stylelintrc.json", ".stylelintrc.json"
else
copy_file "stylelintrc.json", ".stylelintrc.json"
end
end

def configure_eslint
Expand Down
11 changes: 11 additions & 0 deletions lib/generators/templates/lint/tailwind.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@thoughtbot/stylelint-config",
"rules": {
"scss/at-rule-no-unknown": [
true,
{
"ignoreAtRules": ["tailwind"]
}
]
}
}
4 changes: 4 additions & 0 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def default_test_helper_present?
def rspec_test_helper_present?
File.exist? Rails.root.join("spec/rails_helper.rb")
end

def using_tailwind?
File.exist? Rails.root.join("tailwind.config.js")
end
end

module APIAppUnsupported
Expand Down
24 changes: 24 additions & 0 deletions test/generators/suspenders/lint_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ class LintGeneratorTest < Rails::Generators::TestCase
end
end

test "configures stylelint for tailwind" do
expected_content = <<~TEXT
{
"extends": "@thoughtbot/stylelint-config",
"rules": {
"scss/at-rule-no-unknown": [
true,
{
"ignoreAtRules": ["tailwind"]
}
]
}
}
TEXT

with_css_option :tailwind do
capture(:stderr) { run_generator }

assert_file app_root(".stylelintrc.json") do |file|
assert_equal expected_content, file
end
end
end

test "configures eslint" do
expected_content = <<~JSON
{
Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ def with_test_suite(test_suite, &block)
remove_dir_if_exists "spec"
end

def with_css_option(css, &block)
case css
when :postcss
touch "postcss.config.js"
when :tailwind
touch "tailwind.config.js"
else
raise ArgumentError, "unknown css option: #{css.inspect}"
end
yield
ensure
remove_file_if_exists "postcss.config.js"
remove_file_if_exists "tailwind.config.js"
end

def backup_file(file)
FileUtils.copy app_root(file), app_root("#{file}.bak")
end
Expand Down

0 comments on commit 8628dcb

Please sign in to comment.