From 8ef2cfdc69dc24eeba15de38e2bd7f79c6af456e Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 9 Oct 2024 10:53:39 -0500 Subject: [PATCH] Add Prevent erb_lint From Removing Opening Tags as a Ruby TIL --- README.md | 3 +- ...ent-erb-lint-from-removing-opening-tags.md | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 ruby/prevent-erb-lint-from-removing-opening-tags.md diff --git a/README.md b/README.md index c877ae7..adeac5e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1455 TILs and counting..._ +_1456 TILs and counting..._ --- @@ -1212,6 +1212,7 @@ _1455 TILs and counting..._ - [Pattern Match Values From A Hash](ruby/pattern-match-values-from-a-hash.md) - [Percent Notation](ruby/percent-notation.md) - [Precedence Of Logical Operators](ruby/precedence-of-logical-operators.md) +- [Prevent erb_lint From Removing Opening Tags](ruby/prevent-erb-lint-from-removing-opening-tags.md) - [Print Data To Formatted Table](ruby/print-data-to-formatted-table.md) - [Question Mark Operator](ruby/question-mark-operator.md) - [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md) diff --git a/ruby/prevent-erb-lint-from-removing-opening-tags.md b/ruby/prevent-erb-lint-from-removing-opening-tags.md new file mode 100644 index 0000000..fa6b932 --- /dev/null +++ b/ruby/prevent-erb-lint-from-removing-opening-tags.md @@ -0,0 +1,45 @@ +# Prevent erb_lint From Removing Opening Tags + +The [`erb_lint` gem](https://github.com/Shopify/erb_lint) is a tool from +shopify for linting and auto-formatting ERB files. When I first set it up in a +Rails codebase with the base `.erb-lint.yml` recommended in the README, I ran +into a pernicious issue. The linter wanted to remove opening tags (i.e. `<%` +and `<%=`) from my ERB files. + +So, for a file that looked like this: + +```erb +
+ <%= form_with(url: login_path, scope: :session) do |f| %> +
+ <%= f.label :email %> + <%= f.email_field :email %> +
+
+``` + +It would get formatted to this: + +```erb +
+form_with(url: login_path, scope: :session) do |f| %> +
+f.label :email %> +f.email_field :email %> +
+
+``` + +Yikes! + +I had to disable a couple rules (under `rubocop_config:`) in the `.erb-lint.yml` file to get it to stop +doing this. + +```yaml +Layout/InitialIndentation: + Enabled: false +Layout/TrailingEmptyLines: + Enabled: false +``` + +[source](https://github.com/Shopify/erb_lint/issues/222)