Shopify / erb-lint

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

lib/erb_lint/linters: Add a new CommentSyntax linter #279

Closed issyl0 closed 1 year ago

issyl0 commented 1 year ago
<%# This is the correct comment syntax. %>
<%# Recommending the "proper" ERB comment syntax seems like the safest way to go; even with semicolons it will parse. %>
<% # This is technically incorrect comment syntax but it generally parses. %>
<% # Until someone puts a ; in. %>
app/views/proof_of_concept.html.erb:7:2: E: Lint/Syntax: unexpected token kIN
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
 in.
 ^^

1 file inspected, 1 offense detected

[1] - https://github.com/ruby/erb/tree/a3492c4bd1061071814cca085544ce259a9d8d56#recognized-tags


Example ERB file:

<%# erb comment here %>
<%= # bad erb comment here %>
<%
  # apparently this comment syntax is valid?
%>
<% # very; bad erb comment here %>

Example behaviour of this new linter:

❯ exe/erblint --enable-linters=comment_syntax test_comment_syntax.html.erb
.erb-lint.yml not found: using default config
Linting 1 files with 1 linters...

Bad ERB comment syntax. Should be <%#= without a space between.
Leaving a space between ERB tags and the Ruby comment character can cause parser errors.
In file: test_comment_syntax.html.erb:2

Bad ERB comment syntax. Should be <%# without a space between.
Leaving a space between ERB tags and the Ruby comment character can cause parser errors.
In file: test_comment_syntax.html.erb:6

2 error(s) were found in ERB files

Thanks for building ERBLint, and thanks in advance for the review here! ✨

issyl0 commented 1 year ago

Thanks for the review @etiennebarrie. 🙇🏻 I made the changes.