cookpad / global-style-guides

Official style guides for Cookpad Global
67 stars 16 forks source link

Follow consistent ERB indent style #168

Closed davidstosik closed 4 years ago

davidstosik commented 4 years ago

In addition to setting up ERB-lint, these rules will need to be looked after manually, for now.

jesseclark commented 4 years ago

I much prefer this style:

      <!-- ERB tag opener & closer on their own line -->
      <%=
        render "accounts/header",
          left_navigation: link_to(...),
          dismissable: true
      %>

over

      <%= render "accounts/header",
            left_navigation: link_to(...),
            dismissable: true %>

It seems to me that the former is more along the lines of how indentation of other multi-line blocks are handled in our Ruby style, e.g:

# method parameter lists like this

   expect(Sms::TestClient.deliveries.last).to eq(
     phone_number: "441134960000",
     template: "phone_number_verification_code"
   )

# instead of

    expect(Sms::TestClient.deliveries.last).to eq(
      phone_number: "441134960000",
      template: "phone_number_verification_code")

# hashes/arrays like this
{
  foo: bar,
  baz: bash
}

# instead of 

{ foo: bar,
   baz: bash }

Also, it looks like ERBLint::Linters::ClosingErbTagIndent allows both single line tags or ERB tags that open and close on separate lines:

    context 'when tag is correct' do
      let(:file) { "<% foo %>" }
      it { expect(subject).to(eq([])) }
    end

    context 'when tag starts and ends with a newline' do
      let(:file) { <<~ERB }
        <%
          foo
        %>
      ERB
      it { expect(subject).to(eq([])) }
    end

I would prefer that we either go with the style enforced by the linter or allow both.

davidstosik commented 4 years ago

@jesseclark Thank you for the detailed opinion, and sorry for the delay in acknowledging it! 🙇‍♂️

Starting from the end, ERBLint::Linters::ClosingErbTagIndent allows all the following syntaxes:

<%= "1. opening and closing do not use their own new line" %>

<%= "2. opening and closing do not use their own new line,
     this time with a multiline content" %>

<%=
  "3. opening and closing are each on their own new line"
%>

<%=
  "4. opening and closing are each on their own new line,
    this time with a multiline content"
%>

It prevents these:

<%= "only closing is on a new line"
%>

<%=
"only opening is on a new line" %>

<%=
  "opening and closing are each on their own new line, but have a different indentation"
  %>

This entry I propose to add to our guidelines is not an offense to ERBlint, but rather a restriction to the options 1 and 2 above. There are a few reasons why we opted to propose this one after deliberating with @balvig:

I'll admit that I myself prefer the option 4 over option 2 when its content is not taking a block (no do/end), but looking for uniformity and simplicity, the proposed rule made more sense.

balvig commented 4 years ago

@davidstosik following up on https://github.com/cookpad/global-web/pull/20353, some suggestions: https://github.com/cookpad/global-style-guides/compare/ds%2Ffollow-consistent-erb-indent-style...suggestion-9df29f87e5c36130 🙇

davidstosik commented 4 years ago

♻️

balvig commented 4 years ago

Well merge this on @davidstosik's behalf