diff --git a/README.md b/README.md index 1dbb4450..e48384bf 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/template.rb b/template.rb index 233b2e60..b0e031ad 100644 --- a/template.rb +++ b/template.rb @@ -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 diff --git a/variants/backend-base/.erb-lint.yml b/variants/backend-base/.erb-lint.yml new file mode 100644 index 00000000..c32962d2 --- /dev/null +++ b/variants/backend-base/.erb-lint.yml @@ -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 diff --git a/variants/backend-base/Gemfile.tt b/variants/backend-base/Gemfile.tt index 3ebf9d0c..808e12e1 100644 --- a/variants/backend-base/Gemfile.tt +++ b/variants/backend-base/Gemfile.tt @@ -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 diff --git a/variants/github_actions_ci/workflows/ci.yml.tt b/variants/github_actions_ci/workflows/ci.yml.tt index 4bbfabda..86481c22 100644 --- a/variants/github_actions_ci/workflows/ci.yml.tt +++ b/variants/github_actions_ci/workflows/ci.yml.tt @@ -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