This explains how to configure linting for a GOV.UK application. It is written with the expectation that you are configuring a conventional GOV.UK Rails application although the approaches can be applied to non-Rails applications by minor adjustments to the steps.
We use rubocop-govuk to lint Ruby projects.
This is installed by adding
gem "rubocop" to your Gemfile and then creating a
.rubocop.yml file in the root of your project:
inherit_gem: rubocop-govuk: - config/default.yml - config/rails.yml - config/rake.yml - config/rspec.yml inherit_mode: merge: - Exclude
bundle install you can test the linting by running
bundle exec rubocop.
To enable these in a Rails application you will first need to
install Yarn. Then you should create a
package.json file in
your project root. You can use the following template:
The dependencies can then be installed:
yarn add --dev standard stylelint stylelint-config-gds
You can now test the linting by running
yarn run lint.
To finish up you should add
To configure this linting in Rails you should create a rake task for this
desc "Lint files" task "lint" do sh "bundle exec rubocop" sh "yarn run lint" # lint JS and SCSS end
You should then configure the default rake task for the application to include
linting. For example to run linting and RSpec as the default task add the
following code to your
# undo any existing default tasks added by depenencies so we have control Rake::Task[:default].clear if Rake::Task.task_defined?(:default) task default: %i[lint spec]
You can confirm this works by running
bundle exec rake and seeing your
linting run followed by specs.