Skip to main content
Table of contents

Applications

Last updated: 10 Nov 2020

Configure linting

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.

Linting Ruby

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

After running bundle install you can test the linting by running bundle exec rubocop.

Linting JavaScript and SCSS

We use StandardJS for JavaScript linting and Stylelint for SCSS linting, using the stylelint-config-gds configuration.

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:

{
  "name": "My application",
  "description": "A brief description of the application's purpose",
  "private": true,
  "author": "Government Digital Service",
  "license": "MIT",
  "scripts": {
    "lint": "yarn run lint:js && yarn run lint:scss",
    "lint:js": "standard 'app/assets/javascripts/**/*.js' 'spec/javascripts/**/*.js'",
    "lint:scss": "stylelint app/assets/stylesheets/"
  },
  "stylelint": {
    "extends": "stylelint-config-gds/scss"
  }
}

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 node_modules and yarn-error.log to your .gitignore file.

Configuring Rails

To configure this linting in Rails you should create a rake task for this in lib/tasks/lint.rake:

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 Rakefile:

# 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.