Shopify / erb-lint

Lint your ERB or HTML files
MIT License
593 stars 114 forks source link

Destructive Autocorrect: Commas in a div cause code deletion #256

Open softwaregravy opened 2 years ago

softwaregravy commented 2 years ago

I'm a new user to erblint. I can't tell which linter is doing this. But in an erb file, we found an error that caused the linter to break our code during auto-correct.

Our input was

      <div class="col-start-1 col-span-8 pl-8",
         data-controller= "nested-form",
         data-nested-form-wrapper-selector-value= '.nested-form-wrapper'>
         <div class="w-full grid grid-cols-8">
           <div class="col-start-1 col-span-4">
             <div class="grid grid-cols-8 gap-y-4">
               <%= form.fields_for :account_users do |form_au|%>
                 <%= render "account_user_fields", form: form_au %>
               <%end%>
             </div>
           </div>
         </div>

This was autocorrected to

      <div class="col-start-1 col-span-8 pl-8"
         <% form.fields_for :account_users do |form_au| %>
           <%= render "account_user_fields", form: form_au %>
         <% end %>

Notice the incorrect use of commas at the end of the first and 2nd line of our input? This apparently caused the linter to destroy the following 5 lines of divs and matching div close tags. This is highly undesirable behavior. Removing those commas in the div class helped the tool correctly autocorrect the section.

When running without the autocorrect, I was getting the following messages on the lines in question. The first div in my input above was line 36.

expected whitespace, '>', attribute name or value (at ,)
In file: app/views/accounts/_form.html.erb:36
Non-whitespace character(s) detected: ",".
In file: app/views/accounts/_form.html.erb:36

PS obviously this code needs further improvement, but it was working going into this.

softwaregravy commented 2 years ago

I've dropped this example into a stand-alone demo app. Available here

To reproduce you just need to run erblint --lint-all -a and the file app/views/welcome/index.html.erb will be modified incorrectly.