Skip to content

Commit

Permalink
Add erblint to lint ERB and run Rubocop on Ruby embedded in ERB (#492)
Browse files Browse the repository at this point in the history
Closes #489
  • Loading branch information
eoinkelly authored Oct 5, 2023
1 parent b52b374 commit 0233d95
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Where possible we stick to Rails defaults.
- Install [Overcommit](https://github.com/sds/overcommit) for managing custom
git hooks. Configure it with our default settings:
[overcommit.yml](./overcommit.yml)
- Use [erb_lint](https://github.com/Shopify/erb-lint) to lint HTML in ERB and
run Rubocop on Ruby snippets within ERB
- General testing
- RSpec for tests
- Install [webdrivers](https://github.com/titusfortner/webdrivers)
Expand Down
7 changes: 7 additions & 0 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
# generate routes and models
apply "variants/code-annotation/template.rb"

# erblint does not like some of Rails auto-generated ERB code e.g.
# `app/views/layouts/mailer.html.erb` so we auto-correct it.
copy_file "variants/backend-base/.erb-lint.yml", ".erb-lint.yml"
run_with_clean_bundler_env "bundle exec erblint --autocorrect ."
git add: "-A ."
git commit: "-n -m 'Set up erblint'"

# Run the README template at the end because it introspects the app to
# discover rake tasks etc.
apply_readme_template
Expand Down
42 changes: 42 additions & 0 deletions variants/backend-base/.erb-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
exclude:
- '**/vendor/**/*'
- '**/node_modules/**/*'
EnableDefaultLinters: true
linters:
RequireScriptNonce:
enabled: true
PartialInstanceVariable:
enabled: true
HardCodedString:
enabled: false
ErbSafety:
enabled: true
SpaceInHtmlTag:
enabled: false
NoJavascriptTagHelper:
enabled: false
Rubocop:
enabled: true
rubocop_config:
inherit_from:
- .rubocop.yml
Layout/InitialIndentation:
Enabled: false
Layout/LineLength:
Enabled: false
Layout/TrailingEmptyLines:
Enabled: false
Layout/TrailingWhitespace:
Enabled: false
Naming/FileName:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Lint/UselessAssignment:
Enabled: false
Layout/LeadingEmptyLines:
Enabled: false
Rails/OutputSafety:
# Disable this because erb-lint already checks this
Enabled: false
3 changes: 3 additions & 0 deletions variants/backend-base/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ group :development, :test do
gem "factory_bot_rails"
gem "pry-rails"
gem "pry-byebug"

# ERB linting. Run via `bundle exec erblint .`
gem "erb_lint", require: false
end

group :test do
Expand Down
2 changes: 2 additions & 0 deletions variants/github_actions_ci/workflows/ci.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ jobs:
- name: Check Ruby controller annotations
run: bundle exec chusaku --exit-with-error-on-annotation
- run: bundle exec rubocop
- name: Run erb-lint to check HTML formatting and run Rubocop on Ruby within ERB
run: bundle exec erblint .
- run: bundle exec brakeman --run-all-checks --exit-on-warn --format plain .
- run: bundle exec rails db:setup
- uses: actions/setup-node@v3
Expand Down

0 comments on commit 0233d95

Please sign in to comment.