Skip to content

Commit

Permalink
Merge pull request #2471 from alphagov/bk-eslint
Browse files Browse the repository at this point in the history
Add ESLint config for JavaScript Standard Style
  • Loading branch information
domoscargin committed Dec 5, 2022
2 parents d514bb0 + e7525d1 commit 8ecd932
Show file tree
Hide file tree
Showing 31 changed files with 1,779 additions and 1,697 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,11 @@
module.exports = {
extends: 'standard',
overrides: [
{
files: ['**/*.test.{cjs,js,mjs}'],
env: {
jest: true
}
}
]
}
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,5 +1,6 @@
deploy/public
/node_modules
node_modules/
npm-debug.log
.DS_Store
.sass-cache
.cache
3 changes: 1 addition & 2 deletions __tests__/accessiblity_audit.test.js
@@ -1,4 +1,3 @@
/* eslint-env jest */

const { AxePuppeteer } = require('axe-puppeteer')

Expand All @@ -7,7 +6,7 @@ const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

async function audit (page) {
const axe = new AxePuppeteer(page)
Expand Down
3 changes: 1 addition & 2 deletions __tests__/back-to-top.test.js
@@ -1,11 +1,10 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

beforeAll(async () => {
page = await setupPage()
Expand Down
3 changes: 1 addition & 2 deletions __tests__/component-options.test.js
@@ -1,11 +1,10 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

beforeEach(async () => {
page = await setupPage()
Expand Down
7 changes: 3 additions & 4 deletions __tests__/cookie-banner.test.js
@@ -1,11 +1,10 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

const COOKIE_BANNER_SELECTOR = '[data-module="govuk-cookie-banner"]'

Expand All @@ -17,9 +16,9 @@ describe('Cookie banner', () => {
afterEach(async () => {
await page.evaluate(() => {
// Delete test cookies
var cookies = document.cookie.split(';')
const cookies = document.cookie.split(';')
cookies.forEach(function (cookie) {
var name = cookie.split('=')[0]
const name = cookie.split('=')[0]
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/'
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=' + window.location.hostname + ';path=/'
})
Expand Down
14 changes: 6 additions & 8 deletions __tests__/cookie-functions.test.js
Expand Up @@ -2,8 +2,6 @@
* @jest-environment jsdom
*/

/* eslint-env jest */

import * as CookieHelpers from '../src/javascripts/components/cookie-functions'
import * as Analytics from '../src/javascripts/components/analytics'
jest.mock('../src/javascripts/components/analytics')
Expand All @@ -15,9 +13,9 @@ describe('Cookie settings', () => {

afterEach(() => {
// Delete test cookies
var cookies = document.cookie.split(';')
const cookies = document.cookie.split(';')
cookies.forEach(function (cookie) {
var name = cookie.split('=')[0]
const name = cookie.split('=')[0]
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/'
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=' + window.location.hostname + ';path=/'
})
Expand Down Expand Up @@ -231,25 +229,25 @@ describe('Cookie settings', () => {

describe('isValidConsentCookie', () => {
it('isValidConsentCookie returns true if consent cookie is current version', async () => {
var cookieOptions = { analytics: true, version: 1 }
const cookieOptions = { analytics: true, version: 1 }

expect(CookieHelpers.isValidConsentCookie(cookieOptions)).toEqual(true)
})

it('isValidConsentCookie returns true if consent cookie is newer than current version', async () => {
var cookieOptions = { analytics: true, version: 2 }
const cookieOptions = { analytics: true, version: 2 }

expect(CookieHelpers.isValidConsentCookie(cookieOptions)).toEqual(true)
})

it('isValidConsentCookie returns false if consent cookie is older than current version', async () => {
var cookieOptions = { analytics: true, version: 0 }
const cookieOptions = { analytics: true, version: 0 }

expect(CookieHelpers.isValidConsentCookie(cookieOptions)).toEqual(false)
})

it('isValidConsentCookie returns false if consent cookie version is not a number', async () => {
var cookieOptions = { analytics: true, version: 'foobar' }
const cookieOptions = { analytics: true, version: 'foobar' }

expect(CookieHelpers.isValidConsentCookie(cookieOptions)).toEqual(false)
})
Expand Down
5 changes: 2 additions & 3 deletions __tests__/cookies-page.test.js
@@ -1,4 +1,3 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
Expand All @@ -19,9 +18,9 @@ describe('Cookies page', () => {
afterEach(async () => {
await page.evaluate(() => {
// Delete test cookies
var cookies = document.cookie.split(';')
const cookies = document.cookie.split(';')
cookies.forEach(function (cookie) {
var name = cookie.split('=')[0]
const name = cookie.split('=')[0]
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/'
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=' + window.location.hostname + ';path=/'
})
Expand Down
5 changes: 2 additions & 3 deletions __tests__/example.test.js
@@ -1,11 +1,10 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

beforeAll(async () => {
page = await setupPage()
Expand All @@ -22,7 +21,7 @@ describe('Example page', () => {
await page.goto(defaultExampleUrl, { waitUntil: 'load' })
await page.waitForSelector('form[action="/form-handler"]')
await page.click('.govuk-button')
let url = await page.url()
const url = await page.url()
// url should stay the same as the form shouldn't submit
expect(url).toBe(defaultExampleUrl)
})
Expand Down
3 changes: 1 addition & 2 deletions __tests__/navigation.test.js
@@ -1,4 +1,3 @@
/* eslint-env jest */
const devices = require('puppeteer/DeviceDescriptors')
const iPhone = devices['iPhone 6']

Expand All @@ -7,7 +6,7 @@ const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

const mobileNav = '.app-navigation'
const mobileNavToggler = '.js-app-navigation__toggler'
Expand Down
5 changes: 2 additions & 3 deletions __tests__/search.test.js
@@ -1,4 +1,3 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
Expand All @@ -8,7 +7,7 @@ const PORT = configPaths.testPort
const isSearchIndex = /.*\/search-index-[0-9a-f]{32}.json$/

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

beforeEach(async () => {
page = await setupPage()
Expand Down Expand Up @@ -79,7 +78,7 @@ describe('Site search', () => {
page.waitForNavigation(),
page.keyboard.press('Enter')
])
let url = await page.url()
const url = await page.url()

expect(url).toBe(baseUrl + '/components/details/')
})
Expand Down
3 changes: 1 addition & 2 deletions __tests__/tabs.test.js
@@ -1,11 +1,10 @@
/* eslint-env jest */

const { setupPage } = require('../lib/jest-utilities.js')
const configPaths = require('../lib/paths.js')
const PORT = configPaths.testPort

let page
let baseUrl = 'http://localhost:' + PORT
const baseUrl = 'http://localhost:' + PORT

beforeAll(async () => {
page = await setupPage()
Expand Down
2 changes: 1 addition & 1 deletion jest-puppeteer.config.js
Expand Up @@ -3,7 +3,7 @@ const PORT = configPaths.testPort

module.exports = {
server: {
command: `node tasks/test-serve.js`,
command: 'node tasks/test-serve.js',
launchTimeout: 30000,
port: PORT
}
Expand Down
4 changes: 2 additions & 2 deletions lib/colours.js
Expand Up @@ -53,10 +53,10 @@ const palette = function () {
*/

const applied = function () {
let data = require('../data/colours.json')
const data = require('../data/colours.json')
const sass = parseSCSS('_colours-applied.scss')

for (let group in data) {
for (const group in data) {
data[group] = data[group].map(colour => {
colour.colour = getColourFromSass(sass, colour.name)

Expand Down
2 changes: 1 addition & 1 deletion lib/debug.js
Expand Up @@ -10,7 +10,7 @@ module.exports = function () {
console.log('\nMETADATA:')
console.log(metalsmith.metadata())

for (var f in files) {
for (const f in files) {
console.log('\nFILE:')
console.log(files[f])
}
Expand Down
10 changes: 5 additions & 5 deletions lib/extract-page-headings/index.js
Expand Up @@ -6,11 +6,11 @@ const plugin = () => {
if (!file.endsWith('.njk')) {
return
}
var data = files[file]
var contents = data.contents.toString()
const data = files[file]
const contents = data.contents.toString()
const lexer = new marked.Lexer()
let tokens = lexer.lex(contents)
let headingsArray = []
const tokens = lexer.lex(contents)
const headingsArray = []
tokens.forEach(token => {
if (token.type !== 'heading') {
return
Expand All @@ -25,7 +25,7 @@ const plugin = () => {
.join()
}

let heading = {
const heading = {
depth: token.depth,
text: token.text,
url: token.text.toLowerCase().replace(/[^\w]+/g, '-'),
Expand Down
1 change: 0 additions & 1 deletion lib/extract-page-headings/index.test.js
@@ -1,4 +1,3 @@
/* eslint-env jest */

const metalsmith = require('metalsmith')
const plugin = require('./index.js')
Expand Down
44 changes: 20 additions & 24 deletions lib/file-helper.js
Expand Up @@ -30,14 +30,14 @@ exports.getFileContents = path => {
// This helper function takes a path of a *.md.njk file and
// returns the Nunjucks syntax inside that file without markdown data and imports
exports.getNunjucksCode = path => {
let fileContents = this.getFileContents(path)
const fileContents = this.getFileContents(path)

let parsedFile = matter(fileContents)
const parsedFile = matter(fileContents)

// Omit any `{% extends "foo.njk" %}` nunjucks code, because we extend
// templates that only exist within the Design System – it's not useful to
// include this in the code we expect others to copy.
let content = parsedFile.content
const content = parsedFile.content
.replace(
/{%\s*extends\s*\S*\s*%}\s+/,
''
Expand All @@ -54,9 +54,9 @@ exports.getNunjucksCode = path => {
// This helper function takes a path of a *.md.njk file and
// returns the frontmatter as an object
exports.getFrontmatter = path => {
let fileContents = this.getFileContents(path)
const fileContents = this.getFileContents(path)

let parsedFile = matter(fileContents)
const parsedFile = matter(fileContents)
return parsedFile.data
}

Expand All @@ -72,35 +72,31 @@ exports.getFingerprint = function (file) {
// We only know the path of the current file when we're compiling the layout –
// calls to this function with a relative path will fail if made from the
// source files themselves.
if (filePath) {
// Use path.join to correctly join, but metalsmith-fingerprint-ignore
// always expects forward slashes, so replace any backslashes (Windows)
// with a forward slashes.
const relativeFile = path.join(filePath, file).replace(/\\/g, '/')

if (fingerprints.hasOwnProperty(relativeFile)) {
return '/' + fingerprints[relativeFile]
}
}

// Look for a fingerprinted asset at this path relative to the site root
if (fingerprints.hasOwnProperty(file)) {
return '/' + fingerprints[file]
if (!fingerprints[file] && filePath) {
// Use path.join to correctly join, but metalsmith-fingerprint-ignore
// always expects forward slashes, so replace any backslashes (Windows)
// with a forward slashes.
file = path.join(filePath, file).replace(/\\/g, '/')
}

// The thrown error will stop the build, but not provide any useful output,
// so we have to console.log as well.
console.log(`Could not find fingerprint for file ${file}`)
throw new Error(`Could not find fingerprint for file ${file}`)
if (!fingerprints[file]) {
console.log(`Could not find fingerprint for file ${file}`)
throw new Error(`Could not find fingerprint for file ${file}`)
}

// Look for a fingerprinted asset at this path relative to the site root
return '/' + fingerprints[file]
}

// This helper function takes a path of a *.md.njk file and
// returns the HTML rendered by Nunjucks without markdown data
exports.getHTMLCode = path => {
let fileContents = this.getFileContents(path)
const fileContents = this.getFileContents(path)

let parsedFile = matter(fileContents)
let content = parsedFile.content
const parsedFile = matter(fileContents)
const content = parsedFile.content

let html
try {
Expand Down
10 changes: 5 additions & 5 deletions lib/generate-sitemap.js
Expand Up @@ -6,7 +6,7 @@ const match = require('multimatch')

// Only include keys that have values
const removeEmptyKeys = (obj) => {
let newObj = {}
const newObj = {}
Object.keys(obj).forEach((prop) => {
if (obj[prop] !== '') {
newObj[prop] = obj[prop]
Expand Down Expand Up @@ -49,7 +49,7 @@ const plugin = (opts) => {

return function (files, metalsmith, done) {
const sitemap = sm.createSitemap({
hostname: hostname
hostname
})

// Checks whether files should be processed
Expand Down Expand Up @@ -82,10 +82,10 @@ const plugin = (opts) => {

// Use canonical in frontmatter instead of the file's canonical data
// set by metalsmith-canonical plugin
let url = frontmatter.canonical ? frontmatter.canonical : files[file].canonical
const url = frontmatter.canonical ? frontmatter.canonical : files[file].canonical

let sitemapEntry = {
url: url,
const sitemapEntry = {
url,
changefreq: frontmatter.changefreq || changefreq,
lastmod: frontmatter.lastmod || lastmod,
priority: frontmatter.priority || priority
Expand Down

0 comments on commit 8ecd932

Please sign in to comment.